#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); cout << (K+M-1)/M*M << endl; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); int T; cin >> T; while(T){ T--; solve(); } return 0; }