#include using namespace std; #define rep(i, n) for( ll i = 0; i < n; i++ ) using ll = long long; int main() { int T; cin >> T; rep(i, T) { ll M; vector vd(9); cin >> M; rep(j, 9) cin >> vd[j]; ll x = 0; rep(j, 9) { if(vd[j] == 0) continue; rep(k, vd[j]) x = x * 10 + j + 1; } x *= 1e9; ll r = x % M; if(r) x += M - r; cout << x << endl; } }