#include using namespace std; using lint = long long; int main() { int t; cin >> t; while (t--) { lint m; cin >> m; lint ans = 0; lint ten = 1; for (lint i = 1; i <= 9; i++) { lint d; cin >> d; while (d--) { ans += ten * i; ten *= 10LL; } } lint digit = to_string(m - 1).size(); ans *= powl(10LL, digit); lint r = ans % m; if (r > 0) { ans += m - r; } cout << ans << endl; } }