#include <iostream>
#include <cstdio>

using namespace std;

long long x;
long long y;

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

    long long gcd = extended_gcd(b, a % b);

    long long x1 = y;
    long long y1 = x - (a/b)*y;
    x = x1;
    y = y1;

    return gcd;
}

int main() {
    int t;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        long long K,C;
        scanf("%lld %lld", &K, &C);

        long long gcd = extended_gcd(K,C);

        if (gcd != 1) {
            printf("IMPOSSIBLE\n");
            continue;
        }

        while (x > 0 || y < 0) {
            x -= C;
            y += K;
        }

        if (y > 1000000000) {
            printf("IMPOSSIBLE\n");
        }
        else printf("%lld\n", y);
    }
}