#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int lim = 1e9;
vector<ll> tmp;
int _=[]{
for(ll t=1;t<=lim;t*=2) {
for(ll tt=t;tt<=lim;tt*=5) {
tmp.push_back(tt);
}
}
return 0;
}();
void sol() {
ll a,b; cin>>a>>b;
a%=b;
pair<int,int> ans{lim,lim};
for(ll y:tmp) {
ll x=(a*y+b-1)/b;
ll c=x*b-a*y;
ll d=b*y;
ll g=gcd(c,d);
c/=g,d/=g;
if(d<=lim) ans=min(ans,pair<int,int>{c,d});
}
cout<<ans.first<<' '<<ans.second<<endl;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T; cin>>T;
while(T--)
sol();
return 0;
}