#include #include #include using namespace std; void solve() { string x; int m; cin >> x >> m; int siz = x.size(); for (int i = siz; i <= 18; ++i) { string ans(i, '#'); for (int j = 0; j < siz; ++j) { ans[j] = x[j]; ans[i - siz + j] = x[j]; } if (ans.substr(0, siz) != x || ans.substr(i - siz, siz) != x) continue; long long d = 0; for (auto c : ans) d = (d * 10LL + (c == '#' ? 0 : c - '0')) % m; d = (m - d) % m; if (d == 0) { for (auto &c : ans) if (c == '#') c = '0'; cout << ans << endl; return; } int flag = 0; for (int j = 0; j < 1000; ++j) { long long tmp = j * m + d; if (tmp % 1000 == 0) { tmp /= 1000; for (int k = i - 4; k >= 0 && tmp > 0; --k) { ans[k] = tmp % 10; tmp /= 10; } break; } } for (auto c : ans) if (c == '#') flag = 1; if (ans.substr(0, siz) != x || ans.substr(i - siz, siz) != x) flag = 1; if (flag) continue; cout << ans << endl; return; } cout << -1 << endl; } int main() { int t; cin >> t; while (t--) solve(); }