import std, core.bitop; string[] _R; void readM(T)(ref T x) { while (_R.empty) { _R = readln.chomp.split; } x = _R.front.to!T; _R.popFront; } bool chmin(T)(ref T A, T B) { if (A > B) { A = B; return true; } else { return false; } } bool chmax(T)(ref T A, T B) { if (A < B) { A = B; return true; } else { return false; } } int lowerBound(T)(T[] A, T x) { int lo = -1, hi = cast(int)(A.length); while (hi - lo > 1) { int mid = lo + hi >> 1; (A[mid] < x ? lo : hi) = mid; } return hi; } int upperBound(T)(T[] A, T x) { int lo = -1, hi = cast(int)(A.length); while (hi - lo > 1) { int mid = lo + hi >> 1; (A[mid] > x ? hi : lo) = mid; } return hi; } void main() { int TE; TE.readM; foreach (_; 0 .. TE) { int M; M.readM; auto D = new int[9]; foreach (i; 0 .. 9) { D[i].readM; } long x; foreach (i; 0 .. 9) { foreach (j; 0 .. D[i]) { x *= 10; x += i + 1; } } x *= 10 ^^ 9; if (x % M) { x += M - x % M; } writeln(x); } }