#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MP make_pair
#define F first
#define S second
#define pi pair<int,int>
#define sz(s) ((int)s.size())
#define rep(i,a,b) for (int i = a; i <= (int)b; ++i)
int dp[19][100][1<<11][2][2], n;
string a, b;
int mod;
int go(int id, int m, int mask, bool f, bool f3) {
if (id == a.size()) {
// cout << mask << '\n';
return !m && !(mask & (mask - 1));
}
if (~dp[id][m][mask][f][f3]) return dp[id][m][mask][f][f3];
int l = 0, r = 9;
if (f3 == 0 && f == 0) {
l = a[id] - '0', r = b[id] - '0';
}
if (f3 && !f) {
l = a[id] - '0';
}
if (!f3 && f) {
r = b[id] - '0';
}
int dig = a[id] - '0', ans = 0;
int c = m * 10;
while (c >= mod) c-=mod;
if (mask) l = max(l, 1LL);
for (int i = l; i <= r; ++i) {
int mask2 = mask;
if (i) {
for (int j = i; j < 10; j +=i) {
if (mask2 >> j & 1) mask2 ^= 1 << j;
}
}
if (i) mask2 |= 1 << i;
int pro = max(1LL, i);
int md = c + i;
while (md >= mod) md -= mod;
ans = max(ans, pro*go(id + 1, md, mask2, f | (a[id] - '0' < i), f3 | (i < b[id]-'0')));
}
return dp[id][m][mask][f][f3] = ans;
}
string fin;
void construct(int id, int m, int mask, bool f, bool f3) {
if (id == a.size()) {
return;
}
int ans = dp[id][m][mask][f][f3];
int l = 0, r = 9;
if (f3 == 0 && f == 0) {
l = a[id] - '0', r = b[id] - '0';
}
if (f3 && !f) {
l = a[id] - '0';
}
if (!f3 && f) {
r = b[id] - '0';
}
int c = m * 10;
while (c >= mod) c-=mod;
if (mask) l = max(l, 1LL);
for (int i = l; i <= r; ++i) {
int mask2 = mask;
if (i) {
for (int j = i; j < 10; j +=i) {
if (mask2 >> j & 1) mask2 ^= 1 << j;
}
}
if (i) mask2 |= 1 << i;
int pro = max(1LL, i);
int md = c + i;
while (md >= mod) md -= mod;
if (ans == pro*go(id + 1, md, mask2, f | (a[id] - '0' < i), f3 | (i < b[id]-'0'))) {
fin += i + '0';
construct(id + 1, md, mask2, f | (a[id] - '0' < i), f3 | (i < b[id]-'0'));
break;
}
}
return;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t = 1, k, num; cin >> t;
while (t--) {
cin >> num >> b >> k;
mod = k;
fin.clear();
a = to_string(num-1);
while (a.size() != b.size())
a = '0' + a;
memset(dp, -1, sizeof dp);
int ans = go(0,0,0,0,0);
construct(0,0,0,0,0);
bool f = false;
if (ans) {
cout << ans << ' ';
for (int i = 0; i < fin.size(); ++i) {
f |= fin[i] > '0';
if (f) cout << fin[i];
}
cout << '\n';
}
else cout << -1 << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgRiBmaXJzdAojZGVmaW5lIFMgc2Vjb25kCiNkZWZpbmUgcGkgcGFpcjxpbnQsaW50PiAKI2RlZmluZSBzeihzKSAoKGludClzLnNpemUoKSkKI2RlZmluZSByZXAoaSxhLGIpIGZvciAoaW50IGkgPSBhOyBpIDw9IChpbnQpYjsgKytpKQoKaW50IGRwWzE5XVsxMDBdWzE8PDExXVsyXVsyXSwgbjsKc3RyaW5nIGEsIGI7CmludCBtb2Q7CgppbnQgZ28oaW50IGlkLCBpbnQgbSwgaW50IG1hc2ssIGJvb2wgZiwgYm9vbCBmMykgewogIGlmIChpZCA9PSBhLnNpemUoKSkgewogICAgLy8gY291dCA8PCBtYXNrIDw8ICdcbic7CiAgICByZXR1cm4gIW0gJiYgIShtYXNrICYgKG1hc2sgLSAxKSk7CiAgfQoKICBpZiAofmRwW2lkXVttXVttYXNrXVtmXVtmM10pIHJldHVybiBkcFtpZF1bbV1bbWFza11bZl1bZjNdOwoKICBpbnQgbCA9IDAsIHIgPSA5OwogIAogIGlmIChmMyA9PSAwICYmIGYgPT0gMCkgewogICAgbCA9IGFba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