#include using namespace std; using lint = long long; int main() { int t; cin >> t; while (t--) { lint m; cin >> m; vector d(10); for (int i = 1; i <= 9; i++) { cin >> d[i]; } if (accumulate(d.begin(), d.end(), 0LL) == 0LL) { cout << m << endl; continue; } lint ans = 0; lint ten = 1; for (lint i = 1; i <= 9; i++) { while (d[i]--) { ans += ten * i; ten *= 10LL; } } lint digit = to_string(m - 1).size(); ans *= powl(10LL, digit); lint r = ans % m; if (r > 0) { ans += m - r; } cout << ans << endl; } }