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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;

#define endl '\n'

// Find x,y such that ax + by = d = gcd(a,b)
// * a^-1 (mod m): if (egcd(a,m,x,y) == 1) return (x+m)%m; else ERROR;
ll egcd(ll a, ll b, ll& x, ll &y) {
  if (!b) {x = 1; y = 0; return a;}//to ensure d>=0: x=sgn(a);y=0;return abs(a);
  ll d = egcd(b, a%b, y, x); y -= x * (a/b); return d; }

////////////////////////////////////////////////////////////////////////////////
// Extended GCD in canonical form
////////////////////////////////////////////////////////////////////////////////
// Assuming a != 0, find smallest y >= 0 such that ax + by = c (if possible)
bool canon_egcd(ll a, ll b, ll c, ll& x, ll& y) {
  ll d = egcd(a, b, x, y), z = abs(a/d); if (c%d) return false;
  y = (y*(c/d)%z + z)%z, x = (c - b*y)/a; return true; }


int main() {
	ios::sync_with_stdio(0);
	
	int T; cin >> T;
	while(T--) {
		ll k, c; cin >> k >> c;
		if (c == 1) {
			cout << k+1 << endl;
		} else if (k == 1) {
			cout << 1 << endl;
		} else {
			ll x, y;
			if (canon_egcd(k,c,1,x,y) && y <= 1000000000) {
				cout << y << endl;
			} else {
				cout << "IMPOSSIBLE" << endl;
			}
		}
	}

	return 0;
}