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

long long egcd(long long a, long long b, long long& x, long long& y) {

        if (b == 0) {
                x = 1;
                y = 0;
                return a;
        }

        long long gcd = egcd(b, a % b, y, x);
        y -= x * (a / b);
        return gcd;
}

long long t, k, c, x, y;
int main() {
        cin >> t;
        while (t--) {
                cin >> k >> c;
                long long r = egcd(k, c, x, y);

                if (r != 1) 
                        cout << "IMPOSSIBLE" << endl;
                else {
                        int z = abs(k / r);
                        while (x > 0 || y < 0) {
                                x -= c;
                                y += k;
                        }
                        if (y > 1000000000) {
                                cout << "IMPOSSIBLE" << endl;
                        } else {
                                cout << y << endl;
                        }
                }
        }

        return 0;
}