#include <bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
#define endl '\n'
const lll inf= 1e17;
const lll mod = 998244353;
const vector<lll> denoms = {
1, 2, 4, 5, 8,
10, 16, 20, 25, 32,
40, 50, 64, 80, 100,
125, 128, 160, 200, 250,
256, 320, 400, 500, 512,
625, 640, 800, 1000, 1024,
1250, 1280, 1600, 2000, 2048,
2500, 2560, 3125, 3200, 4000,
4096, 5000, 5120, 6250, 6400,
8000, 8192, 10000, 10240, 12500,
12800, 15625, 16000, 16384, 20000,
20480, 25000, 25600, 31250, 32000,
32768, 40000, 40960, 50000, 51200,
62500, 64000, 65536, 78125, 80000,
81920, 100000, 102400, 125000, 128000,
131072, 156250, 160000, 163840, 200000,
204800, 250000, 256000, 262144, 312500,
320000, 327680, 390625, 400000, 409600,
500000, 512000, 524288, 625000, 640000,
655360, 781250, 800000, 819200, 1000000,
1024000, 1048576, 1250000, 1280000, 1310720,
1562500, 1600000, 1638400, 1953125, 2000000,
2048000, 2097152, 2500000, 2560000, 2621440,
3125000, 3200000, 3276800, 3906250, 4000000,
4096000, 4194304, 5000000, 5120000, 5242880,
6250000, 6400000, 6553600, 7812500, 8000000,
8192000, 8388608, 9765625, 10000000, 10240000,
10485760, 12500000, 12800000, 13107200, 15625000,
16000000, 16384000, 16777216, 19531250, 20000000,
20480000, 20971520, 25000000, 25600000, 26214400,
31250000, 32000000, 32768000, 33554432, 39062500,
40000000, 40960000, 41943040, 48828125, 50000000,
51200000, 52428800, 62500000, 64000000, 65536000,
67108864, 78125000, 80000000, 81920000, 83886080,
97656250, 100000000, 102400000, 104857600, 125000000,
128000000, 131072000, 134217728, 156250000, 160000000,
163840000, 167772160, 195312500, 200000000, 204800000,
209715200, 244140625, 250000000, 256000000, 262144000,
268435456, 312500000, 320000000, 327680000, 335544320,
390625000, 400000000, 409600000, 419430400, 488281250,
500000000, 512000000, 524288000, 536870912, 625000000,
640000000, 655360000, 671088640, 781250000, 800000000,
819200000, 838860800, 976562500, 1000000000
};
void oper(lll testcase) {
int aa = 0, bb = 0;
lll a = 0, b = 0, e = 0, f = 0, c = 0, d = 0, divisor = 0, best_c = 0, best_d = 0;
cin >> aa >> bb;
a = aa;
b = bb;
best_c = inf;
for (lll denom : denoms) {
f = denom;
e = (a * f + b - 1) / b;
c = b * e - a * f;
d = b * f;
divisor = gcd(c, d);
c /= divisor;
d /= divisor;
if (d <= 1'000'000'000 && c < best_c) {
best_c = c;
best_d = d;
}
}
cout << (ll)best_c << ' ' << (ll)best_d << '\n';
}
int main() {
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int ttt = 1;
cin >> ttt;
for (lll i = 1; i <= ttt; i++) {
oper(i);
}
}