//(a/b)%c ==> a%c = (b*k) %c;
// k = (a*(b_1))%c ,b_1为b的逆元
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int mod = 9973 ;
typedef __int64 ll;
int exgcd(int a ,int b , ll &x ,ll &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a ;
}
int r = exgcd(b , a%b , x , y) ;
int temp = x ;
x = y ;
y = temp - a/b*y ;
}
int main()
{
int T ;
scanf("%d" ,&T) ;
while(T--)
{
ll a , b;
scanf("%I64d%I64d" ,&a , &b) ;
ll x , y ;
int r = exgcd(b , mod , x ,y) ;
while(x<0){x+=mod;}
printf("%I64d\n" ,(a*x)%mod);
}
}