T=int(input()) for _ in range(T): M=int(input()) d=list(map(int, input().split())) ans=0 p=1 for (i,di) in enumerate(d): for _ in range(di): ans+=p*(i+1) p*=10 ans*=int(1e9) r=ans%M if r!=0: ans+=M-r print(ans)