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.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;

public class d {
	static int MAX_ANS = 10;
	public static void main(String[] args) throws IOException {
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt();
		ArrayList<BinStr> strs = new ArrayList<>();
		for(int i=0;i<n;i++) {
			strs.add(new BinStr(in.next()));
		}
		int m = in.nextInt();
		for(int q=0;q<m;q++) {
			int a = in.nextInt()-1, b = in.nextInt()-1;
			BinStr str1 = strs.get(a), str2 = strs.get(b);
			BinStr res = new BinStr(str1, str2);
			strs.add(res);
			int lo = 0, hi = MAX_ANS;
			while(lo < hi) {
				int mid = (lo+hi+1)/2;
				if(res.sets[mid].size() == 1 << mid) {
					lo = mid;
				} else {
					hi = mid-1;
				}
			}
			System.out.println(lo);
		}
	}
	static String suffix(String s) {
		return s.substring(Math.max(s.length()-MAX_ANS, 0), s.length());
	}
	static String prefix(String s) {
		return s.substring(0, Math.min(MAX_ANS, s.length()));
	}
	static class BinStr {
		String suffix;
		String prefix;
		HashSet<String>[] sets;
		public BinStr(String s) {
			sets = new HashSet[MAX_ANS+1];
			for(int i=0;i<=MAX_ANS;i++) {
				sets[i] = new HashSet<>();
			}
			prefix = prefix(s);
			suffix = suffix(s);
			for(int i=0;i<s.length();i++) {
				for(int j=i+1;j<=s.length() && j-i <= MAX_ANS;j++) {
					sets[j-i].add(s.substring(i, j));
				}
			}
		}
		public BinStr(BinStr a, BinStr b) {
			sets = new HashSet[MAX_ANS+1];
			for(int i=0;i<=MAX_ANS;i++) {
				sets[i] = new HashSet<>();
				sets[i].addAll(a.sets[i]);
				sets[i].addAll(b.sets[i]);
			}
			
			prefix = prefix(a.prefix + b.prefix);
			suffix = suffix(a.suffix + b.suffix);
			
			String as = a.suffix;
			String bp = b.prefix;
			for(int i=1;i<=as.length();i++) {
				String av = as.substring(as.length()-i, as.length());
				for(int j=1;j<=bp.length() && i+j <= MAX_ANS;j++) {
					sets[i+j].add(av+bp.substring(0, j));
				}
			}
		}
		
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream i) {
			br = new BufferedReader(new InputStreamReader(i));
			st = null;
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++)
				arr[i] = nextInt();
			return arr;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++)
				arr[i] = nextInt();
			return arr;
		}

		public String[] nextStringArray(int n) throws IOException {
			String[] arr = new String[n];
			for (int i = 0; i < n; i++)
				arr[i] = next();
			return arr;
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
	}
}
