#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <stdio.h>
#define ll long long
#define mem(a,n) memset((a),n,sizeof (a))
void fast();
ll gcd(ll x, ll y) { return(!y ? x : gcd(y, x % y)); }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
int dx[]{ 1, -1, 0, 0, 1, 1, -1, -1 };
int dy[]{ 0, 0, 1, -1, 1, -1, 1, -1 };
using namespace std;
int main()
{
//freopen("input.txt" ,"r" ,stdin);
//freopen("output.txt" ,"w" ,stdout);
fast();
int t; cin >> t;
while (t--) {
ll n, a;
cin >> n >> a;
ll temp1 = lcm(n, a) / gcd(n, a);
ll temp2 = lcm(n - 1, a) / gcd(n - 1, a);
if (temp1 > temp2)cout << n;
else cout << n - 1;
cout << "\n";
}
return 0;
}
void fast() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
Copy