John was learning mathematics and was very bored. Jane his best friend gave him a problem to solve. 
The description of the problem was as follows:- 
You are given a decimal number $N$(1<=$N$<=$10^9$) and three integers $A$, $B$, $C$. 
Steps to perform:
1) You have to create a $LIST$. 
2) You have to initialize the $LIST$ by adding N to the $LIST$ as its first element. 
3) Divide$N$ by $A$ and if the first digit of the fractional part is Non-Zero then add this digit to the  $LIST$ otherwise add the first digit of the integral part(Leftmost digit).
(The integer part or integral part of a decimal is the integer written to the left of the decimal separator.  The part from the decimal separator i.e to the right is the fractional part. )
4) Update $N$  by Last element of the $LIST$. 
N = Last element of $LIST$
5) You have to perform the same process from step 3 on $N$ for $B$ and $C$ respectively 
6) Repeat from step 3 
You have to answer$Q$(1 <= $Q$<= 100 ) queries
For each query you are given an integer $i$ (0 <= $i$ <= $10^9$ ). You have to print the element present at the ith position of the $LIST$. 
Help John solve this problem.

-----Input:-----
- The First Line of input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The First Line of each test case contains the  integer $N$.
- The second line of each test case contains three integers $A$, $B$, and $C$ separated by a space
- The third line of each test case contains an integer $Q$.
- Then the next $Q$ line follows.  
- An integer $i$ (0 <= $i$ <= 10^9 )

-----Output:-----
You have to answer the $Q$ queries in the next $Q$ lines. 

-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N \leq 10^9$
- $2 \leq A \leq 10$
- $2 \leq B \leq 10$
- $2 \leq C \leq 10$
- $2 \leq Q \leq 100$
- $0 \leq i \leq 10^9$

-----Sample Input:-----
1
56
3 5 7
4
0
1
2
3

-----Sample Output:-----
56
6
2
2

-----EXPLANATION:-----
This list is :
$N$ = 56 and $A$ = 3, $B$ = 5, $C$ = 7. 
Initially $LIST$ = $[ 56 ]$
$N$$/$$A$ =  56/3 = 18.666666666666668 
Add 6 to the $LIST$ 
$LIST$ = $[ 56, 6 ]$
$N$ = 6 
$N$$/$$B$ = 6/5 = 1.2 
Add 2 to the$LIST$ 
$LIST$ = $[ 56, 6, 2 ]$
N = 2 
$N$$/$$C$ = 2/7 =0. 2857142857142857
Add 2 to the $LIST$.  
$LIST$ = $[ 56, 6, 2, 2 ]$
$N$  = 2
We have to keep repeating this process. If any of the numbers got by $N$ dividing by either $A$, $B$, $C$ have 0 after the decimal point then we have to take the first digit of the number. 
for example: if we got 12.005 then here we take 1 and add it to the list and then assign N = 1 
Now the queries ask for the elements at index 0, 1, 2, 3 of the $LIST$ which is 56,6, 2, 2

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
t = int(input())
for _ in range(0, t):
	n = int(input())
	abc = [int(i) for i in input().split()]
	i = 0
	lst = [n]
	for _ in range(0, 100):
		k = str(lst[-1] / abc[i % 3]).split('.')
		if int(k[1][0]) > 0:
			lst.append(int(k[1][0]))
		else:
			lst.append(int(k[0][0]))
		i += 1
	pattern = []
	ind = 0
	while len(pattern) == 0:
		for i in range(ind, len(lst) - 1):
			check = lst[ind:i + 1] * 50
			check = check[:len(lst) - ind]
			if lst[ind:] == check:
				pattern = check
				break
		if len(pattern):
			break
		ind += 1
	final_pattern = []
	for i in range(0, len(pattern)):
		couldbe = pattern[:i + 1]
		check = pattern[:i + 1] * 100
		check = check[:len(pattern)]
		if check == pattern:
			final_pattern = couldbe
			break
	lp = len(final_pattern)
	q = int(input())
	for _ in range(0, q):
		qq = int(input())
		if qq < ind:
			print(lst[qq])
		else:
			qq -= ind
			kk = qq % lp
			print(final_pattern[kk])


t = int(input())
for i in range(t):
	n = int(input())
	q0 = n
	(a, b, c) = list(map(int, input().split()))
	list1 = [n]
	k = 1
	while len(list1) - list1.index(n) - 1 < 3 or (len(list1) - 1 - list1.index(n)) % 3 != 0:
		if k == 1:
			n /= a
			k = 2
		elif k == 2:
			n /= b
			k = 3
		elif k == 3:
			n /= c
			k = 1
		fd = str(n)[0]
		if int(n * 10) % 10 != 0:
			list1.append(int(n * 10) % 10)
		else:
			list1.append(int(fd))
		n = list1[-1]
	del list1[-1]
	list2 = list1[list1.index(n):]
	for j in range(int(input())):
		q = int(input())
		if q < len(list1):
			print(list1[q])
		else:
			print(list2[(q - list1.index(n)) % len(list2)])


for i in range(int(input())):
	n = int(input())
	abc = list(map(int, input().split()))
	query = int(input())
	q = []
	for j in range(query):
		q.append(int(input()))
	z = 0
	p = 0
	s = 0
	wlist = [n]
	while p < 30:
		x = n / abc[z]
		x = str(x)
		if x[x.index('.') + 1] == '0':
			wlist.append(int(x[0]))
			n = int(x[0])
		else:
			wlist.append(int(x[x.index('.') + 1]))
			n = int(x[x.index('.') + 1])
		if n == 0:
			break
		z += 1
		p += 1
		if z > 2:
			z = 0
	n = 0
	while True:
		if wlist[n:n + 3] == wlist[n + 3:n + 6]:
			s = 0
			break
		n += 1
	mlist = wlist[n:n + 3]
	if n >= 31:
		n = 0
		while True:
			if wlist[n:n + 6] == wlist[n + 6:n + 12]:
				s = 3
				break
			n += 1
		mlist = wlist[n:n + 6]
	if n >= 31:
		n = 0
		while True:
			if wlist[n:n + 9] == wlist[n + 9:n + 18]:
				s = 6
				break
			n += 1
		mlist = wlist[n:n + 9]
	for j in range(len(q)):
		if q[j] > n - 1:
			x = q[j] - n
			y = x % 3
			if s == 3:
				y = x % 6
			if s == 6:
				y = x % 9
			print(mlist[y])
		else:
			print(wlist[q[j]])


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7
T = int(stdin.readline())

def func(n, a):
	q = n // a
	r = n % a
	if r != 0:
		return 10 * r // a
	else:
		return int(str(q)[0])
for _ in range(T):
	n = int(stdin.readline())
	(a, b, c) = list(map(int, stdin.readline().split()))
	g = []
	for i in range(11):
		g.append([0, 0, 0])
		g[i][0] = func(i, a)
		g[i][1] = func(i, b)
		g[i][2] = func(i, c)
	l = [n]
	l.append(func(l[-1], a))
	l.append(func(l[-1], b))
	l.append(func(l[-1], c))
	cur = l[-1]
	ind = 0
	d = {}
	st = 4
	while (cur, ind) not in d:
		d[cur, ind] = st
		cur = g[cur][ind]
		ind = (ind + 1) % 3
		l.append(cur)
		st += 1
	st = d[cur, ind]
	en = len(l)
	sec = en - st
	q = int(stdin.readline())
	for i in range(q):
		qi = int(stdin.readline())
		if qi <= st:
			print(l[qi])
			continue
		ans = st + (qi - st) % sec
		print(l[ans])


T = int(input())
for t in range(T):
	n = int(input())
	y = list(map(int, input().split()))
	x = [n]
	i = 0
	prev = x[0]
	visited = [{}, {}, {}]
	element = 1
	cycle_length = -1
	cycle_start = -1
	while True:
		a = y[i]
		mod = prev % a
		div = mod / a
		digit = int(str(div)[2])
		if digit == 0:
			digit = int(str(prev // a)[0])
		if digit in visited[i]:
			cycle_length = element - visited[i][digit]
			cycle_start = visited[i][digit]
			break
		else:
			visited[i][digit] = element
		x.append(digit)
		prev = int(digit)
		element += 1
		i = (i + 1) % 3
	Q = int(input())
	for q in range(Q):
		idx = int(input())
		if idx < len(x):
			print(x[idx])
		else:
			idx -= cycle_start
			idx = idx % cycle_length
			print(x[cycle_start + idx])