#include using namespace std; using ll = long long; void solve(){ ll M, d, K; const ll inf = 2e18; string S=""; cin >> M; for (int i=1; i<=9; i++){ cin >> d; S += string(d, char(i+'0')); } S += string(9, '0'); K = stoll(S); ll l=0, r=inf/M, c; while(r-l>1){ c = (l+r)/2; if (c * M >= K) r=c; else l=c; } cout << r*M << endl; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); int T; cin >> T; while(T){ T--; solve(); } return 0; }