#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll x,y; 

ll egcd(ll a, ll b, ll& x, ll &y) {
  if (!b) {x=1;y=0;return abs(a);}
  ll d = egcd(b, a%b, y, x); y -= x * (a/b); return d; }
  
int main(){
    ios::sync_with_stdio(0);
    int t; cin>>t;
    while (t--){
        ll k,c; cin>>k>>c;
        if (egcd(c,k,x,y)!=1) cout<<"IMPOSSIBLE"<<endl;
        else if (k!=1) cout<<(x+k)%k<<endl;
        else{
            if (c==1) cout<<2<<endl;
            else cout<<1<<endl;
        }
    }
    return 0;
}