#include #include using namespace std; using ll = long long; using pii = pair; using pll = pair; using vi = vector; using vl = vector; #define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c)) #define rep2(i, a, b) rep3(i, a, b, 1) #define rep1(i, n) rep2(i, 0, n) #define rep0(n) rep1(aaaaa, n) #define ov4(a, b, c, d, name, ...) name #define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__) #define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--) #define fore(e, v) for (auto &&e : v) #define all(a) begin(a), end(a) #define sz(a) (int)(size(a)) #define lb(v, x) (lower_bound(all(v), x) - begin(v)) #define eb emplace_back template bool chmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template bool chmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } const int INF = 1e9 + 100; const ll INFL = 3e18 + 100; #define i128 __int128_t struct _ { _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); } } __; string solve(ll, ll); int main() { int T; cin >> T; rep(T) { ll X, M; cin >> X >> M; cout << solve(X, M) << '\n'; } } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll d = extGCD(b, a % b, y, x); y = y - a / b * x; return d; } string solve(ll X, ll M) { int xlen=1; if(X>=10)xlen++; if(X>=100)xlen++; ll d2 = 1,c2=0; while (M % (d2 * 2) == 0) { d2 *= 2; c2++; } ll d5 = 1,c5=0; while (M % (d5 * 5) == 0) { d5 *= 5; c5++; } while(c2>xlen)c2--,d2/=2; while(c5>xlen)c5--,d5/=5; if(X%(d2*d5)!=0){ return "-1"; } ll K = X * (1'000'000'000'000'000ll + 1) % M; ll x = 0, y = 0; if(xlen==3){ extGCD(1000, M, x, y); ll c = (-x * K / gcd(1000, M) % M + M) % M; return format("{}{:012}{:03}", X, c, X); }else if(xlen==2){ extGCD(100, M, x, y); ll c = (-x * K / gcd(100, M) % M + M) % M; return format("{}{:013}{:02}", X, c, X); }else{ extGCD(10, M, x, y); ll c = (-x * K / gcd(10, M) % M + M) % M; return format("{}{:014}{:01}", X, c, X); } }