#include #include #define chmin(x,y) (x) = min((x),(y)) #define chmax(x,y) (x) = max((x),(y)) #define ld long double using namespace std; using namespace atcoder; using ll = long long; const ll mod = 998244353; using mint = modint998244353; using Graph = vector>; const vector dx = {1,0,-1,0}, dy = {0,1,0,-1}; int main(){ // input int t; cin >> t; const int N = 1e9; while(t--){ ll M; cin >> M; ll ans = 0; for(int i = 1; i <= 9; i++){ int a; cin >> a; while(a--){ ans *= 10; ans += i; } } ans *= N; ans += M - ans % M; //cout << (ans % M == 0 ? "OK" : "NG") << endl; cout << ans<< endl; } }