Chloe is fascinated by prime numbers. She came across the number $283002$ on a sign and, though the number is not prime, found some primes hiding in it by using the following rules:  

Every three consecutive digits sum to a prime:
$\underbrace{283}002\quad\underbrace{2830}02\quad28\underbrace{300}2\quad283\underbrace{002}$
Every four consecutive digits sum to a prime:
$\underbrace{28300}2\quad\underbrace{28300}2\quad28\underbrace{3002}$
Every five consecutive digits sum to a prime:
$\underbrace{283002}\quad2\underbrace{83002}$

You must answer $\textit{q}$ queries, where each query consists of an integer, $n$. For each $n$, find and print the number of positive $n$-digit numbers, modulo $10^9+7$, that satisfy all three of Chloe's rules (i.e., every three, four, and five consecutive digits sum to a prime).

Input Format

The first line contains an integer, $\textit{q}$, denoting the number of queries. 

Each of the $\textit{q}$ subsequent lines contains an integer denoting the value of $n$ for a query.  

Constraints

$1\leq q\leq2\times10^4$  
$1\leq n\leq4\times10^5$  

Output Format

For each query, print the number of $n$-digit numbers satisfying Chloe's rules, modulo $10^9+7$, on a new line.   

Sample Input 0
1
6

Sample Output 0
95

Explanation 0

There are $95$ six-digit numbers satisfying the property above, where the respective first and last ones are $\textbf{101101}$ and $\boldsymbol{902005}$.

Difficulty: MEDIUM

Solutions:
from math import sqrt
from collections import Counter
import sys
import datetime
import gc
from collections import defaultdict

def getSive(n):
	size = int(sqrt(n))
	result = [1] * (n + 1)
	for i in range(2, size + 1):
		limit = n // i
		for j in range(2, limit + 1):
			result[i * j] = 0
	result[0] = 0
	result[1] = 0
	return result

def isPrime(i, primes):
	sum = 0
	while i > 0:
		sum += i % 10
		i = i // 10
	return primes[sum]

def getSum(i):
	sum = 0
	while i > 0:
		sum += i % 10
		i = i // 10
	return sum

def getTop1000(primes):
	result = [i for i in range(10 ** 3, 10 ** 4) if isPrime(i, primes) and isPrime(i // 10, primes) and isPrime(i % 1000, primes)]
	return result
q = int(input().strip())
primes = getSive(45)
top1K = getTop1000(primes)
current = {n: 1 for n in top1K}
preComputedSums = {n: getSum(n) for n in range(10 ** 4)}
modulo = 10 ** 9 + 7
news_keys = None
ns = list()
for a0 in range(q):
	ns.append(int(input().strip()))
cache_size = max(ns) + 4
cache = [0] * cache_size
cache[0] = 0
cache[1] = 9
cache[2] = 90
cache[3] = len([i for i in range(100, 1000) if isPrime(i, primes)])
cache[4] = len(current)
reach57 = False
for _t in range(5, cache_size):
	if reach57 and news_keys:
		next = dict.fromkeys(keys, 0)
		for k_v in news_keys:
			next[k_v[1]] += current[k_v[0]]
	else:
		next = defaultdict(int)
		if reach57 and news_keys == None:
			news_keys = list()
		for (k, v) in current.items():
			s4 = preComputedSums[k]
			s3 = preComputedSums[k % 1000]
			s2 = preComputedSums[k % 100]
			for new in range(10):
				if primes[s4 + new] and primes[s3 + new] and primes[s2 + new]:
					next_key = k % 1000 * 10 + new
					next[next_key] += v
					if news_keys != None:
						news_keys.append([k, next_key])
		if news_keys != None:
			keys = next.keys()
	if _t % 20 == 0:
		for (k, v) in next.items():
			next[k] = v % modulo
	if not reach57 and len(next.keys()) == 57:
		reach57 = True
	current = next
	cache[_t] = sum(current.values()) % modulo
for _t in ns:
	print(cache[_t])
gc.enable()


import sys
from collections import defaultdict
MOD = 1000000007
primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
is_prime = [i in primes or i >= 64 for i in range(128)]
mult = 1 + 128 * (1 + 128 * (1 + 128 * (1 + 128)))
mat_col_memo = {}

def mat_col(t, start_digit=0):
	retv = mat_col_memo.get((t, start_digit))
	if not retv is None:
		return retv
	retv = {}
	for d in range(start_digit, 10):
		newt = t // 128 + d * mult
		if is_prime[newt % 128] and is_prime[newt // 128 % 128] and is_prime[newt // (128 * 128) % 128]:
			retv[newt] = 1
	mat_col_memo[t, start_digit] = retv
	return retv

def matf_to_vec(mat, vec):
	retv = defaultdict(lambda : 0, {})
	for (j, v) in vec.items():
		for (i, v2) in mat(j).items():
			retv[i] = (retv[i] + v * v2) % MOD
	return retv

def mat_to_vec(mat, vec):
	retv = defaultdict(lambda : 0, {})
	for (j, v) in vec.items():
		for (i, v2) in mat[j].items():
			retv[i] = (retv[i] + v * v2) % MOD
	return retv

def mat_to_straight_vec(mat, vec, out):
	for i in range(len(out)):
		out[i] = 0
	for (j, mj) in mat.items():
		v = vec[j]
		for (i, v2) in mj.items():
			out[i] += v * v2
	for i in range(len(out)):
		out[i] %= MOD

def mat_prod(mat1, mat2):
	retv = defaultdict(lambda : defaultdict(lambda : 0, {}))
	for (k, m2_k) in mat2.items():
		for (j, m1_j) in mat1.items():
			for (i, v) in m1_j.items():
				v2 = m2_k.get(j)
				if not v2 is None:
					retv[k][i] = (retv[k][i] + v * m2_k[j]) % MOD
	return retv
nums = [{64 * mult: 1}]
i = 0
while True:
	start_digit = 1 if i == 0 else 0
	i += 1
	nums.append(matf_to_vec(lambda d: mat_col(d, start_digit), nums[-1]))
	if i >= 32:
		break
mat = {k: mat_col(k, 0) for k in nums[-1].keys()}
assert all((all((k2 in mat for k2 in mat[k].keys())) for k in mat.keys()))
last_vec = nums[-1]
keys = list(last_vec.keys())
size = len(keys)
k2i = {keys[i]: i for i in range(size)}
straight_vec = [0 for _ in range(size)]
for (k, v) in last_vec.items():
	straight_vec[k2i[k]] = v
straight_mat = defaultdict(lambda : defaultdict(lambda : 0, {}))
for (i, mi) in mat.items():
	for (j, v) in mi.items():
		if v > 0:
			straight_mat[k2i[i]][k2i[j]] = v
mat_powers = {1: straight_mat}

def mpow(n):
	retv = mat_powers.get(n)
	if not retv is None:
		return retv
	if n & n - 1 == 0:
		mh = mpow(n // 2)
		retv = mat_powers[n] = mat_prod(mh, mh)
	else:
		a = n & n - 1
		b = n - a
		retv = mat_powers[n] = mat_prod(mpow(a), mpow(b))
	return retv
q = int(input())
(v1, v2) = (straight_vec.copy(), straight_vec.copy())
pows = sorted([(int(input()), i) for i in range(q)])
results = []
lastp = len(nums) - 1
(v1, v2) = (straight_vec.copy(), straight_vec.copy())
for (p, i) in pows:
	if p < len(nums):
		results.append((sum(nums[p].values()) % MOD, i))
		continue
	pw = p - lastp
	lastp = p
	pw2 = 15
	while pw > 0:
		if pw & pw2:
			mat_to_straight_vec(mpow(pw & pw2), v1, v2)
			(v1, v2) = (v2, v1)
		pw -= pw & pw2
		pw2 *= 16
	results.append((sum(v1) % MOD, i))
for (p, i) in sorted(results, key=lambda kw: kw[1]):
	print(p)


A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
C = [1, 9, 90, 303, 280, 218, 95, 101, 295]
M = 10 ** 9 + 7
q = int(input())
for Q in range(q):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % M)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % M)
	print(C[n] if n < len(C) else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % M)


def solve(hi):
	mod = 7 + 10 ** 9
	ans = [0, 9, 90, 303, 280, 218, 95, 101, 295, 513]
	u = [2, 2, 2, 2, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 2, 17, 17, 15, 15, 15, 15, 15, 15, 15, 15, 15, 6, 6, 6, 6, 4, 2, 13, 13, 13, 13, 13, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0]
	t = [(26, 27), (28, 29), (30, 31, 32), (33, 34), (35, 36), (37,), (38,), None, None, (39,), None, None, None, None, None, None, None, (40,), (41,), None, None, None, None, None, None, None, (43, 44, 45), (46, 47), (48, 49, 50), (51, 52), (53, 54, 55), (56, 57), (58, 59), (60, 61), (62, 63), (64, 65), (66,), (67,), (68,), None, (69,), (70,), None, (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16), (17, 18, 19, 20), (21, 22, 23, 24), (25,), None, None, None, None, (37,), (38,), None, (39,), None, None, None, (40,), None, None, None, (42,), None, None, (0, 1, 2, 3, 4), (26, 27), (0, 1, 2, 3, 4), (26, 27)]
	v = [0] * 71
	for _ in range(10, 1 + hi):
		for i in range(71):
			v[i] = 0
		for i in range(71):
			if not t[i]:
				continue
			c = u[i]
			for j in t[i]:
				v[j] = (v[j] + c) % mod
		(u, v) = (v, u)
		ans.append(sum(u) % mod)
	return ans
q = int(input())
ns = [int(input()) for _ in range(q)]
ans = solve(max(ns))
print(*(ans[n] for n in ns), sep='\n')


def solve(hi):
	mod = 7 + 10 ** 9
	ans = [0, 9, 90, 303, 280, 218, 95, 101, 295, 513]
	u = [2, 2, 2, 2, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 2, 17, 17, 15, 15, 15, 15, 15, 15, 15, 15, 15, 6, 6, 6, 6, 4, 2, 13, 13, 13, 13, 13, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0]
	t = [(26, 27), (28, 29), (30, 31, 32), (33, 34), (35, 36), (37,), (38,), None, None, (39,), None, None, None, None, None, None, None, (40,), (41,), None, None, None, None, None, None, None, (43, 44, 45), (46, 47), (48, 49, 50), (51, 52), (53, 54, 55), (56, 57), (58, 59), (60, 61), (62, 63), (64, 65), (66,), (67,), (68,), None, (69,), (70,), None, (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16), (17, 18, 19, 20), (21, 22, 23, 24), (25,), None, None, None, None, (37,), (38,), None, (39,), None, None, None, (40,), None, None, None, (42,), None, None, (0, 1, 2, 3, 4), (26, 27), (0, 1, 2, 3, 4), (26, 27)]
	v = [0] * 71
	for _ in range(10, 1 + hi):
		for i in range(71):
			v[i] = 0
		for i in range(71):
			if not t[i]:
				continue
			c = u[i]
			for j in t[i]:
				v[j] = (v[j] + c) % mod
		(u, v) = (v, u)
		ans.append(sum(u) % mod)
	return ans
q = int(input())
ns = [int(input()) for _ in range(q)]
ans = solve(max(ns))
print(*(ans[n] for n in ns), sep='\n')


import os
import sys
from collections import defaultdict
MOD = 1000000007
primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
is_prime = [i in primes or i >= 64 for i in range(128)]
mult = 1 + 128 * (1 + 128 * (1 + 128 * (1 + 128)))
mat_col_memo = {}

def mat_col(t, start_digit=0):
	retv = mat_col_memo.get((t, start_digit))
	if not retv is None:
		return retv
	retv = {}
	for d in range(start_digit, 10):
		newt = t // 128 + d * mult
		if is_prime[newt % 128] and is_prime[newt // 128 % 128] and is_prime[newt // (128 * 128) % 128]:
			retv[newt] = 1
	mat_col_memo[t, start_digit] = retv
	return retv

def matf_to_vec(mat, vec):
	retv = defaultdict(lambda : 0, {})
	for (j, v) in vec.items():
		for (i, v2) in mat(j).items():
			retv[i] = (retv[i] + v * v2) % MOD
	return retv

def mat_to_vec(mat, vec):
	retv = defaultdict(lambda : 0, {})
	for (j, v) in vec.items():
		for (i, v2) in mat[j].items():
			retv[i] = (retv[i] + v * v2) % MOD
	return retv

def mat_to_straight_vec(mat, vec, out):
	for i in range(len(out)):
		out[i] = 0
	for (j, mj) in mat.items():
		v = vec[j]
		for (i, v2) in mj.items():
			out[i] += v * v2
	for i in range(len(out)):
		out[i] %= MOD

def mat_prod(mat1, mat2):
	retv = defaultdict(lambda : defaultdict(lambda : 0, {}))
	for (k, m2_k) in mat2.items():
		for (j, m1_j) in mat1.items():
			for (i, v) in m1_j.items():
				v2 = m2_k.get(j)
				if not v2 is None:
					retv[k][i] = (retv[k][i] + v * m2_k[j]) % MOD
	return retv
nums = [{64 * mult: 1}]
i = 0
while True:
	start_digit = 1 if i == 0 else 0
	i += 1
	nums.append(matf_to_vec(lambda d: mat_col(d, start_digit), nums[-1]))
	if i >= 32:
		break
mat = {k: mat_col(k, 0) for k in nums[-1].keys()}
assert all((all((k2 in mat for k2 in mat[k].keys())) for k in mat.keys()))
last_vec = nums[-1]
keys = list(last_vec.keys())
size = len(keys)
k2i = {keys[i]: i for i in range(size)}
straight_vec = [0 for _ in range(size)]
for (k, v) in last_vec.items():
	straight_vec[k2i[k]] = v
straight_mat = defaultdict(lambda : defaultdict(lambda : 0, {}))
for (i, mi) in mat.items():
	for (j, v) in mi.items():
		if v > 0:
			straight_mat[k2i[i]][k2i[j]] = v
mat_powers = {1: straight_mat}

def mpow(n):
	retv = mat_powers.get(n)
	if not retv is None:
		return retv
	if n & n - 1 == 0:
		mh = mpow(n // 2)
		retv = mat_powers[n] = mat_prod(mh, mh)
	else:
		a = n & n - 1
		b = n - a
		retv = mat_powers[n] = mat_prod(mpow(a), mpow(b))
	return retv
q = int(input())
(v1, v2) = (straight_vec.copy(), straight_vec.copy())
pows = sorted([(int(input()), i) for i in range(q)])
results = []
lastp = len(nums) - 1
(v1, v2) = (straight_vec.copy(), straight_vec.copy())
for (p, i) in pows:
	if p < len(nums):
		results.append((sum(nums[p].values()) % MOD, i))
		continue
	pw = p - lastp
	lastp = p
	pw2 = 15
	while pw > 0:
		if pw & pw2:
			mat_to_straight_vec(mpow(pw & pw2), v1, v2)
			(v1, v2) = (v2, v1)
		pw -= pw & pw2
		pw2 *= 16
	results.append((sum(v1) % MOD, i))
for (p, i) in sorted(results, key=lambda kw: kw[1]):
	print(p)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


def solve(hi):
	mod = 7 + 10 ** 9
	ans = [0, 9, 90, 303, 280, 218, 95, 101, 295, 513]
	u = [2, 2, 2, 2, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 2, 17, 17, 15, 15, 15, 15, 15, 15, 15, 15, 15, 6, 6, 6, 6, 4, 2, 13, 13, 13, 13, 13, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0]
	t = [(26, 27), (28, 29), (30, 31, 32), (33, 34), (35, 36), (37,), (38,), None, None, (39,), None, None, None, None, None, None, None, (40,), (41,), None, None, None, None, None, None, None, (43, 44, 45), (46, 47), (48, 49, 50), (51, 52), (53, 54, 55), (56, 57), (58, 59), (60, 61), (62, 63), (64, 65), (66,), (67,), (68,), None, (69,), (70,), None, (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16), (17, 18, 19, 20), (21, 22, 23, 24), (25,), None, None, None, None, (37,), (38,), None, (39,), None, None, None, (40,), None, None, None, (42,), None, None, (0, 1, 2, 3, 4), (26, 27), (0, 1, 2, 3, 4), (26, 27)]
	v = [0] * 71
	for _ in range(10, 1 + hi):
		for i in range(71):
			v[i] = 0
		for i in range(71):
			if not t[i]:
				continue
			c = u[i]
			for j in t[i]:
				v[j] = (v[j] + c) % mod
		(u, v) = (v, u)
		ans.append(sum(u) % mod)
	return ans
q = int(input())
ns = [int(input()) for _ in range(q)]
ans = solve(max(ns))
print(*(ans[n] for n in ns), sep='\n')


import sys
primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}

def canextend(l, s):
	(s, l) = (s + l % 10, l // 10)
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	return True
last4digitmap = []
for i in range(10000):
	followups = []
	for j in range(10):
		if canextend(i, j):
			followups.append(i % 1000 * 10 + j)
	last4digitmap.append(followups)

def extend1(resultsofar):
	result = {}
	for (k, v) in resultsofar.items():
		for n in last4digitmap[k]:
			result[n] = result.setdefault(n, 0) + v
	return result

def good4(x):
	(a, x) = (x % 10, x // 10)
	(b, x) = (x % 10, x // 10)
	(c, x) = (x % 10, x // 10)
	d = x
	return a + b + c + d in primes and a + b + c in primes and (b + c + d in primes)
l = {x: 1 for x in range(1000, 10000) if good4(x)}
q = int(input().strip())
queries = []
for a0 in range(q):
	n = int(input().strip())
	queries.append(n)
todo = sorted(queries)
while todo[0] < 5:
	todo.pop(0)
cache = [0] * 400001
cache[1] = len(range(1, 10))
cache[2] = len(range(10, 100))
cache[3] = len([i for i in range(100, 1000) if sum([int(x) for x in str(i)]) in primes])
cache[4] = len([i for i in range(1000, 10000) if sum([int(x) for x in str(i)]) in primes and sum([int(x) for x in str(i)[:3]]) in primes and (sum([int(x) for x in str(i)[-3:]]) in primes)])
lastlength = 4
l = {x: 1 for x in range(1000, 10000) if good4(x)}
for stringlength in todo:
	stepstodo = stringlength - lastlength
	while stepstodo >= 1:
		l = extend1(l)
		stepstodo -= 1
	s = 0
	for (k, v) in l.items():
		l[k] = v % 1000000007
		s += v
	cache[stringlength] = s % 1000000007
	lastlength = stringlength
for n in queries:
	print(cache[n])


import sys
primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}

def canextend(l, s):
	(s, l) = (s + l % 10, l // 10)
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	return True
last4digitmap = []
for i in range(10000):
	followups = []
	for j in range(10):
		if canextend(i, j):
			followups.append(i % 1000 * 10 + j)
	last4digitmap.append(followups)

def extend1(resultsofar):
	result = {}
	for (k, v) in resultsofar.items():
		for n in last4digitmap[k]:
			result[n] = result.setdefault(n, 0) + v
	return result

def good4(x):
	(a, x) = (x % 10, x // 10)
	(b, x) = (x % 10, x // 10)
	(c, x) = (x % 10, x // 10)
	d = x
	return a + b + c + d in primes and a + b + c in primes and (b + c + d in primes)
l = {x: 1 for x in range(1000, 10000) if good4(x)}
q = int(input().strip())
queries = []
for a0 in range(q):
	n = int(input().strip())
	queries.append(n)
todo = sorted(queries)
while todo[0] < 5:
	todo.pop(0)
cache = [0] * 400001
cache[1] = len(range(1, 10))
cache[2] = len(range(10, 100))
cache[3] = len([i for i in range(100, 1000) if sum([int(x) for x in str(i)]) in primes])
cache[4] = len([i for i in range(1000, 10000) if sum([int(x) for x in str(i)]) in primes and sum([int(x) for x in str(i)[:3]]) in primes and (sum([int(x) for x in str(i)[-3:]]) in primes)])
lastlength = 4
l = {x: 1 for x in range(1000, 10000) if good4(x)}
for stringlength in todo:
	stepstodo = stringlength - lastlength
	while stepstodo >= 1:
		l = extend1(l)
		stepstodo -= 1
	s = 0
	for (k, v) in l.items():
		l[k] = v % 1000000007
		s += v
	cache[stringlength] = s % 1000000007
	lastlength = stringlength
for n in queries:
	print(cache[n])


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


import math
import os
import random
import re
import sys
mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


import math
import os
import random
import re
import sys
mod = 10 ** 9 + 7
suf_gen = [[], [], [12, 19], [21, 38], [], [9], [], [], [5], [21, 38], [], [24, 27], [29], [], [], [57, 69], [5], [], [48, 6, 59, 41], [], [50, 37], [55, 18, 45], [], [61, 8, 56], [47, 66], [63], [], [10, 33], [22], [], [], [], [], [], [46], [30, 26], [49, 25, 60, 13], [], [36, 62], [], [], [], [52], [], [34], [17, 14, 7, 53], [3, 64, 65, 11, 15], [42], [29], [42], [], [70, 4, 0], [3, 64, 65, 11, 15], [], [], [44, 16, 32, 39], [], [28, 67], [21, 38], [], [], [34], [31, 43, 68, 1], [58], [51, 20], [23, 2, 35], [], [], [], [40], [54]]

def primeDigitSums(T):
	ans = [0] * max(T + 1, 9)
	ans[:9] = [0, 9, 90, 303, 280, 218, 95, 101, 295]
	suf_v = [2, 4, 3, 15, 2, 0, 4, 4, 2, 1, 2, 15, 2, 4, 4, 15, 4, 4, 9, 2, 3, 13, 0, 3, 3, 4, 2, 3, 2, 0, 2, 4, 4, 2, 0, 3, 9, 2, 13, 4, 2, 4, 0, 4, 4, 9, 1, 2, 4, 4, 2, 3, 1, 4, 0, 9, 2, 3, 1, 4, 4, 2, 9, 0, 15, 15, 2, 2, 4, 3, 2]
	for i in range(9, T + 1):
		new_suf_v = [0] * 71
		for j in range(71):
			for k in suf_gen[j]:
				new_suf_v[k] = (new_suf_v[k] + suf_v[j]) % mod
		suf_v = new_suf_v
		ans[i] = sum(suf_v) % mod
	return ans
q = int(input().strip())
n = [0] * q
for q_itr in range(q):
	n[q_itr] = int(input().strip())
result = primeDigitSums(max(n))
ans = ''
for i in n:
	ans += str(result[i]) + '\n'
print(ans)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
output = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(output[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


import math
import os
import random
import re
import sys
mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


import math
import os
import random
import re
import sys
mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


import sys
primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}

def canextend(l, s):
	(s, l) = (s + l % 10, l // 10)
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	(s, l) = (s + l % 10, l // 10)
	if not s in primes:
		return False
	return True
last4digitmap = []
for i in range(10000):
	followups = []
	for j in range(10):
		if canextend(i, j):
			followups.append(i % 1000 * 10 + j)
	last4digitmap.append(followups)

def extend1(resultsofar):
	result = {}
	for (k, v) in resultsofar.items():
		for n in last4digitmap[k]:
			result[n] = result.setdefault(n, 0) + v
	return result

def good4(x):
	(a, x) = (x % 10, x // 10)
	(b, x) = (x % 10, x // 10)
	(c, x) = (x % 10, x // 10)
	d = x
	return a + b + c + d in primes and a + b + c in primes and (b + c + d in primes)
l = {x: 1 for x in range(1000, 10000) if good4(x)}
q = int(input().strip())
queries = []
for a0 in range(q):
	n = int(input().strip())
	queries.append(n)
todo = sorted(queries)
while todo[0] < 5:
	todo.pop(0)
cache = [0] * 400001
cache[1] = len(range(1, 10))
cache[2] = len(range(10, 100))
cache[3] = len([i for i in range(100, 1000) if sum([int(x) for x in str(i)]) in primes])
cache[4] = len([i for i in range(1000, 10000) if sum([int(x) for x in str(i)]) in primes and sum([int(x) for x in str(i)[:3]]) in primes and (sum([int(x) for x in str(i)[-3:]]) in primes)])
lastlength = 4
l = {x: 1 for x in range(1000, 10000) if good4(x)}
for stringlength in todo:
	stepstodo = stringlength - lastlength
	while stepstodo >= 1:
		l = extend1(l)
		stepstodo -= 1
	s = 0
	for (k, v) in l.items():
		l[k] = v % 1000000007
		s += v
	cache[stringlength] = s % 1000000007
	lastlength = stringlength
for n in queries:
	print(cache[n])


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)


mod = 10 ** 9 + 7
A = [0, 0, 0, 0, 0, 2, 3, 15, 2, 0]
B = [0, 0, 0, 0, 0, 4, 9, 13, 17, 2]
results = [1, 9, 90, 303, 280, 218, 95, 101, 295]
for _ in range(int(input())):
	n = int(input())
	for i in range(len(A), n):
		A.append(2 * (A[i - 5] + B[i - 5]) % mod)
		B.append((A[i - 1] + A[i - 5] + 2 * B[i - 5]) % mod)
	print(results[n] if n < 9 else (5 * A[n - 1] + 9 * A[n - 2] + 19 * A[n - 3] + 6 * A[n - 4] + 3 * A[n - 5] + 2 * B[n - 1] + 5 * B[n - 2] + 20 * B[n - 3] + 5 * B[n - 4] + 4 * B[n - 5]) % mod)