#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; } } if (num == 0) { cout << m << "\n"; return; } num *= (long long)1e9; num += (m - num % m) % m; assert(num % m == 0); cout << num << "\n"; } int main() { fast_io(); int t; cin >> t; while (t--) { solve(); } }