#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
int t;
cin>>t;
while(t--) {
ll n, a;
cin>>n>>a;
while(__gcd(n, a) > 1) n--;
cout<<n<<endl;
}
}