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