# import pypyjit;pypyjit.set_param("max_unroll_recursion=-1") import os import sys # from bisect import * # from collections import * # from datetime import * # from decimal import * # PyPyだと遅い # from heapq import * from itertools import * # from math import gcd, lcm # from random import * # from string import * # import numpy as np # from atcoder.dsu import * # from atcoder.segtree import * # from more_itertools import * # from sortedcontainers import * # sys.setrecursionlimit(10**7) # PyPyは呪文を付ける # sys.set_int_max_str_digits(0) INF = 1 << 61 MOD = 998244353 # MOD = 10**9 + 7 IS_ATCODER = os.getenv("ATCODER", 0) File = sys.stdin def input(): return File.readline().replace("\n", "") # /////////////////////////////////////////////////////////////////////////// for _ in range(int(input())): M = int(input()) d = list(map(int, input().split())) l = [] for i, j in enumerate(d, 1): if j: l.append(str(i) * j) n = int("".join(l) + "0" * 9) n += n % M print(n)