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 INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 2e5 + 7;
const double eps = 0.00000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
  T ret = 1;
  for (; b; b >>= 1ll, a = a * a % M)
    if (b & 1) ret = 1ll * ret * a % M;
  return ret;
}
int i, j, k;
int u, v;
int n, m;
string pre[207], sub[207];
string s;
int ans[207];
int mark[207][1 << 14 | 7];
string tostring(int x) {
  string ret;
  while (x != 1) {
    char c = '0' + (x & 1);
    ret = c + ret;
    x /= 2;
  }
  return ret;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> s;
    pre[i] = sub[i] = s;
    ans[i] = 2;
    while (s.find(tostring(ans[i])) != string::npos)
      mark[i][ans[i]] = 1, ans[i]++;
  }
  cin >> m;
  for (i = n + 1; i <= n + m; i++) {
    cin >> u >> v;
    pre[i] = pre[u] + pre[v];
    sub[i] = sub[u] + sub[v];
    if (pre[i].length() > 30) pre[i] = pre[i].substr(0, 30);
    reverse(sub[i].begin(), sub[i].end());
    if (sub[i].length() > 30) sub[i] = sub[i].substr(0, 30);
    reverse(sub[i].begin(), sub[i].end());
    ans[i] = max(ans[u], ans[v]);
    s = sub[u] + pre[v];
    for (j = 2; j <= 1 << 14; j++)
      if (mark[u][j] || mark[v][j] || s.find(tostring(j)) != string::npos)
        mark[i][j] = 1;
    while (mark[i][ans[i]]) ans[i]++;
    int T = ans[i], A = 0;
    while (T) A++, T >>= 1;
    printf("%d\n", A - 2);
  }
}