import std; void main () { int T = readln.chomp.to!int; foreach (_; 0..T) { int M = readln.chomp.to!int; int[] d = readln.split.to!(int[]); solve(M, d); } } void solve (int M, int[] d) { /* 条件がゆるいので、先に必要条件だけ揃えて後は端の方で調整する */ long X = 0; int radix = 0; foreach (i; 0..d.length) { foreach (j; 0..d[i]) { X += (i+1)*10L^^radix; radix++; } } X *= 10L^^9; long cur = X % M; X += M - cur; writeln(X); } void read (T...) (string S, ref T args) { auto buf = S.split; foreach (i, ref arg; args) { arg = buf[i].to!(typeof(arg)); } }