#include #include #include #include #include #include #include #include #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using namespace atcoder; using ll = long long; using p = pair; using mod = modint998244353; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } vector> RunLength(string s){ vector> R; int n = s.size(); int pre = 0; rep(i,n - 1){ if(s[i + 1] != s[i]){ R.push_back({s[i],i - pre + 1}); pre = i + 1; } } if(pre != n)R.push_back({s[n - 1],n - pre}); return R; } int op(int a,int b){return min(a,b);} int e(){return (int)1e9;} segtree seg(1); int n,m; map> mp; int bfs(int start,int goal){ queue Q; vector seen(n,-1); Q.push(start); seen[start] = 0; while(Q.size()){ int now = Q.front();Q.pop(); for(int next:mp[now]){ if(seen[next] != -1)continue; seen[next] = seen[now] + 1; Q.push(next); if(next == goal)Q = {}; } } return seen[goal]; } int main(){ int t;cin >> t; while(t--){ ll m;cin >> m; string s = ""; rep(i,9){ int d;cin >> d; rep(_,d)s += (i + 1) + '0'; } rep(i,9)s += '0'; ll sl = stoll(s); ll r = sl%m; sl += m - r; cout << sl << endl; } }