#include #include #include using namespace std; using ll = long long; int main() { int T; cin >> T; while (T--) { ll M; cin >> M; vector d(9); for (ll i = 0; i < 9; i++) { cin >> d[i]; } bool all_zero = true; for (ll i = 0; i < 9; i++){ if (d[i] != 0){ all_zero = false; } } if (all_zero){ cout << M << endl; continue; } string Y = ""; for (ll i = 0; i < 9; i++) { for (ll j = 0; j < d[i]; j++) { Y += (char)(i + 1 + '0'); } } ll X = (stoll(Y)) * 1000000000LL; ll rem = X % M; if (rem == 0) { rem = M; } X += (M - rem); cout << X << endl; } }