#include using namespace std; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } void solve() { long long m; cin >> m; vector d(10); long long num = 0; for (int i = 1; i <= 9; i++) { cin >> d[i]; for (int j = 0; j < d[i]; j++) { num = num * 10 + i; } } num *= (long long)1e9; num += (m - num % m); assert(num % m == 0); cout << num << endl; } int main() { fast_io(); int t; cin >> t; while (t--) { solve(); } }