#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 ans = -1;
for (ll i = n;; i--) {
if (gcd(i, a) == 1) { ans = i; break; }
}
cout << ans << "\n";
}
return 0;
}
void fast() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
Copy