Problem: You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed, on each of them you concatenate two existing strings into a new one. On the i-th operation the concatenation saisbi is saved into a new string sn + i (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer k such that all possible strings consisting of 0 and 1 of length k (there are 2k such strings) are substrings of the new string. If there is no such k, print 0.

Input

The first line contains single integer n (1 ≤ n ≤ 100) — the number of strings. The next n lines contain strings s1, s2, ..., sn (1 ≤ |si| ≤ 100), one per line. The total length of strings is not greater than 100.

The next line contains single integer m (1 ≤ m ≤ 100) — the number of operations. m lines follow, each of them contains two integers ai abd bi (1 ≤ ai, bi ≤ n + i - 1) — the number of strings that are concatenated to form sn + i.

Output

Print m lines, each should contain one integer — the answer to the question after the corresponding operation.

Example

Input

5
01
10
101
11111
0
3
1 2
6 5
4 4


Output

1
2
0

Note

On the first operation, a new string "0110" is created. For k = 1 the two possible binary strings of length k are "0" and "1", they are substrings of the new string. For k = 2 and greater there exist strings of length k that do not appear in this string (for k = 2 such string is "00"). So the answer is 1.

On the second operation the string "01100" is created. Now all strings of length k = 2 are present.

On the third operation the string "1111111111" is created. There is no zero, so the answer is 0.

Solution (in PYTHON3):

Solution:
from sys import stdin, stdout
k = 20
def findAllStrings(s):
    n = len(s)
    sdict = {}
    for i in range(1,k+1):
        sdict[i]=set()
        for x in range(n-i+1):
            sdict[i].add(s[x:x+i])
    return sdict
n = int(stdin.readline().rstrip())
strdict = []
stringend = []
stringbegin = []
for i in range(n):
    s = stdin.readline().rstrip()
    strdict.append(findAllStrings(s))
    if len(s)<k:
        stringend.append(s)
        stringbegin.append(s)
    else:
        stringend.append(s[-20:])
        stringbegin.append(s[:20])
m = int(stdin.readline().rstrip())
for _ in range(m):
    a,b = map(int,stdin.readline().rstrip().split())
    a-=1
    b-=1
    sdict1 = findAllStrings(stringend[a]+stringbegin[b])
    sdict2 = strdict[a]
    sdict3 = strdict[b]
    sdict={}
    for i in range(1,k+1):
        sdict[i] = sdict1[i]|sdict2[i]|sdict3[i]
    strdict.append(sdict)
    for i in range(1,k+1):
        if len(sdict[i])!=2**i:
            print(i-1)
            break
    if len(stringbegin[a])<k and len(stringbegin[a])+len(stringbegin[b])<k:
        stringbegin.append(stringbegin[a]+stringbegin[b])
    elif len(stringbegin[a])<k:
        s = stringbegin[a]+stringbegin[b]
        stringbegin.append(s[:k])
    else:
        stringbegin.append(stringbegin[a])
        
    if len(stringend[b])<k and len(stringend[a])+len(stringend[b])<k:
        stringend.append(stringend[a]+stringend[b])
    elif len(stringend[b])<k:
        s = stringend[a]+stringend[b]
        stringend.append(s[-k:])
    else:
        stringend.append(stringend[b])