#include #define rep(i,n) for(int i=0;i<(n);++i) using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ int m; cin >> m; vector d(9); rep(i,9) cin >> d[i]; ll a=0; int c=17; rep(i,9){ rep(j,d[i]){ a += (i+1)*(pow(10,c)); c--; } } a += m-a%m; cout << a << endl; } }