#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
if(!b)
{
return a;
}
return gcd(b,a%b);
}
ll t,x,l,r,ans=0;
int main()
{
scanf("%lld",&t);
for(int i=1;i<=t;i++)
{
cin>>l>>r>>x;
for(int i=l;i<=r;i++)
{
ans=gcd(ans,i/x);
}
cout<<ans<<endl;
ans=0;
}
return 0;
}