#include <bits/stdtr1c++.h>

using namespace std;

typedef pair<int, pair<int, int>> pipii;

pipii ExtendedGCD(int a, int b) {
  if (a == 0) {
    return {b, {0, 1}};
  }
  auto tmp = ExtendedGCD(b%a, a);
  return {tmp.first, {tmp.second.second- (b/a)*tmp.second.first,
  tmp.second.first}};
}

int main() {
  ios_base::sync_with_stdio(0);
  int t; cin >> t;
  while (t--) {
    int k, c;
    cin >> k >> c;
    if (__gcd(k,c) != 1) {
      cout << "IMPOSSIBLE" << endl;
      continue;
    }
    auto res = ExtendedGCD(c, k);
    while (res.second.second > 0 ||res.second.first < 0) {
      res.second.second -= c;
      res.second.first += k;
    }
    if (res.second.first > 1e9)
      cout << "IMPOSSIBLE" << endl;
    cout << res.second.first << endl;
  }
}