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

typedef long long ll;
int T, K, C;

ll * gcde(ll a, ll b){
	ll x0=1, x1=0, y0=0, y1=1;

	while (b != 0) {
		ll q = a/b;
		ll tmp = a;
		a = b;
		b = (tmp % b + b) % b;

		tmp = x1;
		x1 = x0 - q * x1;
		x0 = tmp;

		tmp = y1;
		y1 = y0 - q * y1;
		y0 = tmp;
	}

	ll * ret = new ll[3];
	ret[0] = a; ret[1] = x0; ret[2] = y0;
	return ret;
}

int main(void){
	cin >> T;
	int tc;
	for (tc=0; tc<T; ++tc){
		cin >> K >> C;
		if (C == 1){
			cout << K + 1 << "\n";
			continue;
		}
		if (K == 1 && C > 0){
			cout << 1 << "\n";
			continue;
		}

		ll * a = gcde(K, C);

		if (a[0] != 1){
			cout << "IMPOSSIBLE\n";
		}
		else{
			while (a[2] < 0) a[2] += K;
			a[2] %= K;
			if (a[2] == 0){
				cout << "IMPOSSIBLE\n";
			}
			else {
				cout << a[2] << "\n";
			}
		}
	}
	return 0;
}