#include using namespace std; using ll = long long; #ifdef LOCAL #include #else #define debug(...) #endif void solve() { int M; vector d(9); cin >> M; for (int i = 0; i < 9; i++) cin >> d[i]; // corner case: d[i] = 0 if (reduce(d.begin(), d.end()) == 0) { cout << M << '\n'; return; } string tmp = ""; for (int i = 0; i < 9; i++) tmp += string(d[i], i + '1'); tmp += string(9, '0'); ll ans = stoll(tmp); ans += (ans % M ? M - (ans % M) : 0); cout << ans << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int T; cin >> T; while (T--) solve(); }