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.lang.*;
import java.math.*;
import java.util.*;
import java.io.*;

public class Main {
    void solve() {
        int n=ni();

        char s[][]=new char[n][];

        for(int i=0;i<n;i++) s[i]=ns().toCharArray();
        int m=ni();
        int d=15;
        int MX=1<<d;
        String left[]=new String[n+m];
        String right[]=new String[n+m];
        for(int i=0;i<n;i++){
            left[i]=new String(s[i]);
            right[i]=new String(s[i]);
        }

        boolean a[][][]=new boolean[n+m][d+1][MX+1];
        for(int i=0;i<n;i++){
            for(int j=0;j<s[i].length;j++){
                int num=0;
                int id=j,l=0;
                while(l+1<=d && id<s[i].length && num*2+s[i][id]-'0'<=MX){
                    num*=2;
                    num+=s[i][id]-'0';
                    l++;
                    a[i][l][num]=true;
                    id++;
                }
            }
        }

        int idx[][]=new int[n+m][2];
        for(int i=0;i<n;i++) idx[i][0]=idx[i][1]=i;

        int p[][]=new int[m+n][2];

        for(int i=0;i<m;i++){
            int i1=ni()-1,i2=ni()-1;
            for(int l=1;l<=d;l++)
            for(int j=0;j<=MX;j++) {
                a[i+n][l][j]=a[i1][l][j]||a[i2][l][j];
            }

            idx[i+n][0]=idx[i1][0]; idx[i+n][1]=idx[i2][1];
            p[i][0]=i1; p[i][1]=i2;
            left[i+n]=new String(left[i1]);
            if(left[i+n].length()<d)left[i+n]+=new String(left[i2]);

            if(right[i2].length()<d) right[i+n]=new String(right[i1])+new String(right[i2]);
            else right[i+n]=new String(right[i2]);



            for(int l=2;l<=d;l++){
                for(int l2=1;l2<l && l2<=left[i2].length();l2++){
                    int l1=l-l2;
                    if(l1>right[i1].length()) continue;
                    int num1=0,num2=0;
                    for(int j=0;j<l2;j++){
                        num2*=2;
                        num2+=left[i2].charAt(j)-'0';
                    }
                    for(int j=right[i1].length()-l1;j<right[i1].length();j++){
                        num1*=2;
                        num1+=right[i1].charAt(j)-'0';
                    }
                    int num=num2+(1<<l2)*num1;
                    a[i+n][l][num]=true;
                    //if(i==1) pw.println(num+" "+i1+" "+i2);

                }
            }
            int ans=0;
            for(int l=1;l<=d;l++) {
                int id = 0;

                while (id <= MX && a[i + n][l][id]) id++;
                if((1<<l)<=id) ans=l;
            }

            pw.println(ans);

        }


    }




    long M=(long)1e9+7;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";
    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");

    }

    public static void main(String[] args) throws Exception { new Main().run(); }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
}