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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200 + 5;
const int MAX_K = 9;
set<string> substrings[MAX][MAX_K + 1];
string pref[MAX], suf[MAX];
int n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = int(1); i < int(n + 1); i++) {
    string cur;
    cin >> cur;
    for (int j = int(0); j < int(cur.size()); j++) {
      string sub = "";
      for (int k = j; k < (int)cur.size() && k - j + 1 <= MAX_K; k++) {
        sub += cur[k];
        substrings[i][sub.size()].insert(sub);
      }
    }
    pref[i] = cur.substr(0, min<int>(cur.size(), MAX_K));
    suf[i] = cur.substr(cur.size() - min<int>(cur.size(), MAX_K),
                        min<int>(cur.size(), MAX_K));
  }
  cin >> m;
  for (int t = int(1); t < int(m + 1); t++) {
    int a, b;
    cin >> a >> b;
    int id = n + t;
    int ans = 0;
    for (int i = int(1); i < int(MAX_K + 1); i++) {
      set_union(substrings[a][i].begin(), substrings[a][i].end(),
                substrings[b][i].begin(), substrings[b][i].end(),
                inserter(substrings[id][i], substrings[id][i].end()));
    }
    string tmp = suf[a] + pref[b];
    for (int i = int(0); i < int(tmp.size()); i++) {
      string sub = "";
      for (int j = i; j < (int)tmp.size() && j - i + 1 <= MAX_K; j++) {
        sub += tmp[j];
        substrings[id][sub.size()].insert(sub);
      }
    }
    string new_pref = pref[a] + pref[b];
    pref[id] = new_pref.substr(0, min<int>(MAX_K, new_pref.size()));
    string new_suf = suf[a] + suf[b];
    suf[id] = new_suf.substr(new_suf.size() - min<int>(MAX_K, new_suf.size()),
                             min<int>(MAX_K, new_suf.size()));
    for (int i = int(1); i < int(MAX_K + 1); i++) {
      if ((int)substrings[id][i].size() == (1 << i)) {
        ans = i;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}