#include using namespace std; #define rep(i, s, e) for (int i = s; i < e; i++) #define M1 = 998244353 #define M2 = 1000000007 #define ll long long ll power(ll x, int y) { ll res = 1; rep(i, 0, y) { res *= x; } return res; } int main() { int t; cin >> t; vector v(t); rep(c, 0, t) { ll m; cin >> m; vector d(9); rep(i, 0, 9) { cin >> d[i]; } ll ans = 0; rep(i, 0, 9) { rep(j, 0, d[i]) { ans *= 10; ans += i + 1; } } if (ans * power(10, 9) % m == 0) { v[c] = ans * power(10, 9); } else { v[c] = ans * power(10, 9) + (m - (ans * power(10, 9)) % m); } } rep(i, 0, t) { cout << v[i] << endl; } }