#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int test; cin >> test; while(test--){ long long M; cin >> M; string s = ""; for(int i=1; i<=9; i++){ int d; cin >> d; while(d--) s += '0'+i; } if(s == ""){cout << M << endl; continue;} long long D = stoll(s); D *= 1e9; D += (M-D%M)%M; cout << D << endl; } }