/* -*- coding: utf-8 -*- * * 3365.cc: No.3365 Prefix and Suffix X - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 3; const int MAX_L = 18; const int MAX_P = 32000; /* typedef */ using ll = long long; using vb = vector; using vi = vector; using pii = pair; using vpii = vector; struct MI { static int MOD; int v; MI(): v() {} MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; } MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator-() const { return MI(MOD - v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } bool operator==(const MI m) const { return v == m.v; } bool operator!=(const MI m) const { return v != m.v; } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; int MI::MOD; using mi = MI; /* global variables */ vb primes; vi pnums; char sx[MAX_N + 4], sy[MAX_L + 4], sz[MAX_L + 4]; mi ts[MAX_L + 1]; /* subroutines */ int gen_primes(int maxp) { primes.assign(maxp + 1, true); primes[0] = primes[1] = false; int p; for (p = 2; p * p <= maxp; p++) if (primes[p]) { pnums.push_back(p); for (int q = p * p; q <= maxp; q += p) primes[q] = false; } for (; p <= maxp; p++) if (primes[p]) pnums.push_back(p); return (int)pnums.size(); } vpii prime_decomp(int n) { vpii pds; for (auto pi: pnums) { if (pi * pi > n) { if (n > 1) pds.push_back(pii(n, 1)); break; } if (n % pi == 0) { int fi = 0; while (n % pi == 0) n /= pi, fi++; pds.push_back(pii(pi, fi)); } } return pds; } int powi(int a, int b) { int p = 1; while (b > 0) { if (b & 1) p *= a; a *= a; b >>= 1; } return p; } int phi(int m) { auto pds = prime_decomp(m); int ph = 1; for (auto [p, d]: pds) ph *= (p - 1) * powi(p, d - 1); return ph; } mi s2i(int n, char s[]) { mi sum = 0; for (int i = 0; i < n; i++) sum = sum * 10 + (s[i] - '0'); return sum; } int i2s(int n, char s[]) { int l = 0; while (n > 0) s[l++] = '0' + (n % 10), n /= 10; s[l] = '\0'; reverse(s, s + l); return l; } /* main */ int main() { gen_primes(MAX_P); int tn; scanf("%d", &tn); while (tn--) { int m; scanf("%s%d", sx, &m); int n = strlen(sx); MI::MOD = m; bool f = false; for (int i = n; i >= 0; i--) { strcpy(sy, sx); strcpy(sy + n - i, sx); if (! strncmp(sy, sx, n)) { if (s2i(n * 2 - i, sy) == 0) { f = true; break; } } } if (f) { puts(sy); continue; } if (m % 2 == 0 || m % 5 == 0) { puts("-1"); continue; } ts[0] = 1; for (int i = 0; i < MAX_L; i++) ts[i + 1] = ts[i] * 10; // y = x*t[n+zl]+z*t[n]+x = x*(t[n+zl]+1)+z*t[n] == 0 // -> z*t[n] == -x*(t[n+zl]+1) // -> z == -x*(t[n+zl]+1)*t[n]^(-1) int phm = phi(m); mi x = s2i(n, sx), invtn = ts[n].pow(phm - 1); for (int zl = 1; zl + n * 2 <= MAX_L; zl++) { mi z = -x * (ts[n + zl] + 1) * invtn; int l = i2s((int)z, sz); //printf(" zl=%d, l=%d, sz=%s\n", zl, l, sz); if (l <= zl) { fill(sy, sy + (zl + n * 2), '0'); copy(sx, sx + n, sy); copy(sx, sx + n, sy + (n + zl)); copy(sz, sz + l, sy + (n + zl - l)); sy[zl + n * 2] = '\0'; int r = (int)s2i(zl + n * 2, sy); //printf(" %s == %d\n", sy, r); if (r == 0) { f = true; break; } } } if (f) puts(sy); else puts("-1"); } return 0; }