#define _USE_MATH_DEFINES #include //cin, cout #include //vector #include //sort,min,max,count #include //string,getline, to_string #include //abs(int) #include //swap, pair #include //tuple #include //deque #include //INT_MAX #include //bitset #include //sqrt, ceil. M_PI, pow, sin #include //fixed #include //setprecision #include //stringstream #include //gcd, assumlate #include //randam_device #include //numeric_limits using namespace std; constexpr long long int D_MOD = 1000000007; int main() { int T; cin >> T; int64_t M, X; int d; string S, A, B; for (int i = 0; i < T; i++) { cin >> M; X = M; S.clear(); for (int j = 1; j <= 9; j++) { cin >> d; if (d != 0) { for (int k = 0; k < d; k++) { S.push_back(j + '0'); } } } if (M == 1) { cout << S << endl; continue; } B = S; sort(B.begin(), B.end()); while (1) { A = to_string(X); sort(A.begin(), A.end()); if (A == B) { cout << X << endl; break; } X += M; } } return 0; }