#if __INCLUDE_LEVEL__ == 0 #include __BASE_FILE__ using Mint = atcoder::modint; void Solve() { string X; int M; IN(X, M); Mint::set_mod(M); for (int o : Rep1(0, Sz(X))) { string s = X; for (int i : Rep(0, Sz(X))) { if (o + i < Sz(X)) { if (s[o + i] != X[i]) { s.clear(); break; } } else { s += X[i]; } } if (!s.empty()) { Mint t = 0; for (char c : s) { t *= 10; t += c - '0'; } if (t == 0) { OUT(s); return; } } } for (int w : Rep1(1, 18 - 2 * Sz(X))) { Mint::set_mod(M); Mint A = Mint(10).pow(Sz(X)); Mint B = 0; for (char c : X) { B *= 10; B += c - '0'; } B *= Mint(10).pow(w + Sz(X)) + 1; B = -B; int g = gcd(A.val(), M); if (B.val() % g) { continue; } A = A.val() / g; B = B.val() / g; Mint::set_mod(M / g); if (A == 0) { if (B == 0) { OUT(X + string(w, '0') + X); return; } } else { if ((B / A).val() < Pow(INT64_C(10), w)) { OUT(X + to_string(Pow(INT64_C(10), w) + (B / A).val()).substr(1) + X); return; } } } OUT(-1); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; IN(t); while (t--) { Solve(); } } #elif __INCLUDE_LEVEL__ == 1 #include #include template > constexpr T Pow1(T x, N n, Op op = {}) { assert(n >= 1); while (n % 2 == 0) { x = op(x, x); n >>= 1; } T ret = x; while (n >>= 1) { x = op(x, x); if (n % 2) { ret = op(ret, x); } } return ret; } template constexpr T Pow(T x, N n) { if (n < 0) { if constexpr (requires { T(1) / x; }) { x = T(1) / x; n = -n; } else { assert(n >= 0); } } if (n == 0) { return T(1); } return Pow1(x, n); } template concept MyRange = std::ranges::range && !std::convertible_to; template concept MyTuple = std::__is_tuple_like::value && !MyRange; namespace std { istream& operator>>(istream& is, MyRange auto&& r) { for (auto&& e : r) is >> e; return is; } istream& operator>>(istream& is, MyTuple auto&& t) { apply([&](auto&... xs) { (is >> ... >> xs); }, t); return is; } ostream& operator<<(ostream& os, MyRange auto&& r) { auto sep = ""; for (auto&& e : r) os << exchange(sep, " ") << e; return os; } ostream& operator<<(ostream& os, MyTuple auto&& t) { auto sep = ""; apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t); return os; } template * = nullptr> istream& operator>>(istream& is, T& x) { int v; is >> v; x = T::raw(v); return is; } template * = nullptr> ostream& operator<<(ostream& os, const T& x) { return os << x.val(); } } // namespace std using namespace std; #define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__) #define Rep1(...) [](int l, int r) { return Rep(l, r + 1); }(__VA_ARGS__) #define Sz(r) int(size(r)) #define IN(...) (cin >> forward_as_tuple(__VA_ARGS__)) #define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n') #endif // __INCLUDE_LEVEL__ == 1