A/B

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
int extend_gcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    int t = extend_gcd(b, a % b, y, x);
    y -= a / b * x;
    return t;
}
int main() {
    int T, n, b, x, y;
    cin >> T;
    while (T--) {
        cin >> n >> b;
        extend_gcd(9973, b, x, y);
        y *= n;
        y = (9973 + y % 9973) % 9973;
        cout << y << endl;
    }
}