from collections import defaultdict, deque, Counter import copy from itertools import combinations, permutations, product, accumulate, groupby, chain from heapq import heapify, heappop, heappush import math import bisect from pprint import pprint from random import randint import sys # sys.setrecursionlimit(700000) input = lambda: sys.stdin.readline().rstrip('\n') inf = float('inf') mod1 = 10**9+7 mod2 = 998244353 def ceil_div(x, y): return -(-x//y) ################################################ T = int(input()) for _ in range(T): M = int(input()) D = list(map(int, input().split())) s = 0 ans = 0 cnt = 0 for i, d in enumerate(D, 1): for j in range(d): s = (s*10 + i) % M ans = ans*10 + i cnt += 1 s = s * 10**(18-cnt) % M ans *= 10**(18-cnt) ans += (-s)%M print(ans)