#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include <string.h>

#include <time.h>
#include <random>
#include <iomanip>
using namespace std;

#define ll long long

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; }

ll gcd(ll a, ll b) {
    if (b==0)
        return a;
    else
        return gcd(b, a%b);
}

void solve() {

    ll a, b, x, y;
    cin >> b >> a;
    if (egcd(a, b, x, y)!=1) {
        cout << "IMPOSSIBLE" << endl;
        return;
    }
    while (x < 0 || y > 0) {
        x += b;
        y -= a;
    }
    if (x > 1000000000) {
        cout << "IMPOSSIBLE" << endl;
        return;
    }
    cout << x << endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++) {
        solve();
    }
}