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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.BitSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            DHugeStrings solver = new DHugeStrings();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class DHugeStrings {
        int limit = 12;
        int limitLength = 1 << limit;
        long inf = (long) 1e18;

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            List<StringHolder> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(build(in.readString()));
            }

            int m = in.readInt();
            for (int i = 0; i < m; i++) {
                int a = in.readInt() - 1;
                int b = in.readInt() - 1;
                StringHolder ans = concatenate(list.get(a), list.get(b));
                out.println(ans.report());
                list.add(ans);
            }
        }

        public void analyse(BitSet[] bs, String s) {
            for (int i = 1; i <= limit; i++) {
                int mask = (1 << i) - 1;
                int cur = 0;
                for (int j = 0; j < s.length(); j++) {
                    int c = s.charAt(j) - '0';
                    cur = (cur << 1) | c;
                    if (j >= i - 1) {
                        bs[i].set(cur & mask);
                    }
                }
            }
        }

        public void trim(StringHolder holder) {
            if (holder.prefix.length() > limitLength) {
                holder.prefix = holder.prefix.substring(0, limitLength);
            }
            if (holder.suffix.length() > limitLength) {
                holder.suffix = holder.suffix.substring(holder.suffix.length() - limitLength);
            }
        }

        public StringHolder build(String s) {
            StringHolder holder = new StringHolder(limit);
            holder.prefix = s;
            holder.suffix = s;
            holder.length = s.length();
            analyse(holder.bits, s);
            trim(holder);
            return holder;
        }

        public StringHolder concatenate(StringHolder a, StringHolder b) {
            StringHolder holder = new StringHolder(limit);
            holder.prefix = a.prefix;
            holder.suffix = b.suffix;
            holder.length = Math.min(inf, a.length + b.length);
            for (int i = 1; i <= limit; i++) {
                holder.bits[i].or(a.bits[i]);
                holder.bits[i].or(b.bits[i]);
            }
            String middle = a.suffix + b.prefix;
            analyse(holder.bits, middle);
            if (a.length < limitLength) {
                holder.prefix = a.prefix + b.prefix;
            }
            if (b.length < limitLength) {
                holder.suffix = middle;
            }
            trim(holder);
            return holder;
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(int c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(int c) {
            return append(c).println();
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class StringHolder {
        long length;
        String prefix;
        String suffix;
        BitSet[] bits;

        public StringHolder(int n) {
            bits = new BitSet[n + 1];
            for (int i = 1; i <= n; i++) {
                bits[i] = new BitSet(1 << i);
            }
        }

        public int report() {
            int ans = 0;
            while (ans + 1 < bits.length && bits[ans + 1].cardinality() == (1 << (ans + 1))) {
                ans++;
            }
            return ans;
        }

        public String toString() {
            return "" + report();
        }

    }

    static class FastInput {
        private final InputStream is;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

    }
}

