#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
long long n, a;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> n >> a;
long long res = n;
while (__gcd(res, a) != 1)
res -= 1;
cout << res;
if (i != t)
cout << "\n";
}
return 0;
}