#include #include #define chmin(x,y) (x) = min((x),(y)) #define chmax(x,y) (x) = max((x),(y)) #define ld long double using namespace std; using namespace atcoder; using ll = long long; const ll mod = 998244353; using mint = modint998244353; using Graph = vector>; const vector dx = {1,0,-1,0}, dy = {0,1,0,-1}; int main(){ // input int t; cin >> t; const ll N = 1e9; while(t--){ ll M; cin >> M; ll ans = 0; for(int i = 1; i <= 9; i++){ int d; cin >> d; for(int j = 0; j < d; j++){ ans *= 10; ans += i; } } ans *= N; ans += M - (ans % M); // cout << (ans % M == 0 && ans <= 1e18 ? "OK" : "NG") << endl; cout << ans << endl; } }