#include #include #include #include #include #include #include using namespace std; string A, B; int n, m, L; int a[1111111], b[31111], c[31111], ans[31111]; int pcalc[7][7][7]; int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int t; cin >> t; for (int ca = 0; ca < 7; ca++) { for (int cb = 0; cb < 7; cb++) { for (int cc = 0; cc < 7; cc++) { for (int x = 0; x < 7; x++) { if ((ca + x * cb) % 7 == cc) { pcalc[ca][cb][cc] = x; break; } } } } } while (t) { t--; cin >> A >> B >> L; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); memset(ans, 0, sizeof(ans)); n = A.size(); m = B.size(); for (int i = 0; i < n; i++) { a[i] = A[n - i - 1] - '0'; } for (int i = 0; i < m; i++) { b[i] = B[m - i - 1] - '0'; } int zeroes = 0; while (b[zeroes] == 0) { zeroes++; } n -= zeroes; m -= zeroes; for (int i = 0; i < n; i++) { a[i] = a[i + zeroes]; } for (int i = n; i < n + zeroes; i++) { a[i] = 0; } for (int i = 0; i < m; i++) { b[i] = b[i + zeroes]; } for (int i = m; i < m + zeroes; i++) { b[i] = 0; } for (int i = 0; i < L; i++) { c[i + 1] += c[i] / 7; c[i] %= 7; int already = c[i]; int goal = a[i]; int have = b[0]; int what = pcalc[already][have][goal]; ans[i] = what; for (int j = 0; (i + j) < L; j++) { c[i + j] += b[j] * what; } c[i + 1] += c[i] / 7; c[i] %= 7; } int p = L - 1; while (!ans[p] && p) { p--; } for (; p + 1; p--) { cout << ans[p]; } cout << endl; } return 0; }