#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
const int mod = 1e9 + 7;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int t;
cin >> t;
while(t--){
long long a ,n;
cin >> n >> a;
long long b = n;
for(long long i = n; i >= a; i--){
if(__gcd(i , a) == 1){
b = i;
break;
}
}
cout << b << "\n";
}
return 0;
}