#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int main()
{
int t;
cin >> t;
while (t > 0)
{
int a, n;
cin >> n >> a;
int b = n;
while (gcd(a, b) != 1)
b--;
cout << b << endl;
t--;
}
}