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>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const double eps = 1e-8;
const int INF = 1e9 + 7;
const long long inf = (1LL << 62);
const int MOD = 1e9 + 7;
const long long mod = (1LL << 32);
const int N = 1e6 + 6;
const int M = 100010;
const int maxn = 1001;
const int lowbit(int x) { return x & -x; }
int read() {
  int v = 0, f = 1;
  char c = getchar();
  while (c < 48 || 57 < c) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (48 <= c && c <= 57) v = v * 10 + c - 48, c = getchar();
  return v * f;
}
struct timer {
  double t;
  timer() { t = clock(); }
  ~timer() { printf("\nruntime %.3f s\n", get_time()); }
  double get_time() { return (clock() - t) / CLOCKS_PER_SEC; }
};
int dp[234][12][4567];
void solve(int x, char tmp[]) {
  int len = strlen(tmp);
  for (int i = 0; i < 12; i++) {
    for (int j = 0; j < (1 << (i + 1)); j++) {
      for (int k = 0; k + i < len; k++) {
        int flag = 0;
        for (int h = 0; h <= i; h++) {
          if (((j >> h) & 1) != tmp[k + h] - '0') {
            flag = 1;
            break;
          }
        }
        if (!flag) {
          dp[x][i][j] = 1;
          break;
        }
      }
    }
  }
}
int len[234];
char tmp1[234][15], tmp2[234][20], tmp3[45];
int n, q;
char s[123][123];
int main() {
  n = read();
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
    solve(i, s[i]);
    int length = strlen(s[i]);
    len[i] = length;
    if (len[i] >= 13) {
      len[i] = 14;
    }
    for (int j = 0; j < len[i]; j++) {
      tmp1[i][j] = s[i][j];
      tmp2[i][j] = s[i][length - len[i] + j];
    }
  }
  q = read();
  int a, b;
  for (int i = n; i < n + q; i++) {
    a = read(), b = read();
    a--;
    b--;
    for (int j = 0; j <= 11; j++) {
      for (int k = 0; k < (1 << (j + 1)); k++) {
        dp[i][j][k] = dp[a][j][k] | dp[b][j][k];
      }
    }
    for (int j = 0; j < len[a]; j++) {
      tmp3[j] = tmp2[a][j];
    }
    for (int j = 0; j < len[b]; j++) {
      tmp3[j + len[a]] = tmp1[b][j];
    }
    tmp3[len[a] + len[b]] = 0;
    solve(i, tmp3);
    len[i] = len[a] + len[b];
    if (len[i] >= 13) len[i] = 14;
    for (int j = 0; j < len[a]; j++) {
      tmp1[i][j] = tmp1[a][j];
    }
    for (int j = 0; j + len[a] < len[i]; j++) {
      tmp1[i][j + len[a]] = tmp1[b][j];
    }
    for (int j = 0; j < len[b]; j++) {
      tmp2[i][j + len[i] - len[b]] = tmp2[b][j];
    }
    for (int j = 0; j + len[b] < len[i]; j++) {
      tmp2[i][j] = tmp2[a][j + len[a] + len[b] - len[i]];
    }
    int ans = 12;
    for (int j = 0; j <= 11; j++) {
      int flag = 0;
      for (int k = 0; k < (1 << (j + 1)); k++) {
        if (dp[i][j][k] == 0) {
          flag = 1;
          break;
        }
      }
      if (flag) {
        ans = j;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}