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.
#include <bits/stdc++.h>
using namespace std;
namespace INPUT {
const int L = 1 << 15;
char _buf[L], *S, *T, c;
char _gc() {
  if (S == T) {
    T = (S = _buf) + fread(_buf, 1, L, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
void readi(int &X) {
  bool flag;
  for (c = _gc(); (c < '0' || c > '9') && c != '-'; c = _gc())
    ;
  if (c == '-')
    X = 0, flag = true;
  else
    X = c & 15, flag = false;
  for (c = _gc(); c >= '0' && c <= '9'; X = X * 10 + (c & 15), c = _gc())
    ;
}
}  // namespace INPUT
using INPUT::readi;
const int Maxn = 205;
int N, M;
int num[15];
char s[Maxn];
int S[Maxn];
int S1[Maxn][Maxn];
bool mrk[Maxn][15][10000];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%s", s + 1);
    S1[i][0] = strlen(s + 1);
    for (int j = 1; j <= S1[i][0]; ++j) S1[i][j] = s[j] - '0';
  }
  memset(mrk, false, sizeof(mrk));
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= min(S1[i][0], 12); ++j) {
      int cur = 0;
      for (int k = 1; k < j; ++k) cur = cur << 1 | S1[i][k];
      for (int k = j; k <= S1[i][0]; ++k) {
        cur = cur << 1 | S1[i][k];
        mrk[i][j][cur] = true;
        cur -= S1[i][k - j + 1] << j - 1;
      }
    }
  scanf("%d", &M);
  for (int q = N + 1; q <= N + M; ++q) {
    int x, y, n = 0, Ans = 0;
    scanf("%d %d", &x, &y);
    memset(S, 0, sizeof(S));
    memset(num, 0, sizeof(num));
    for (int l = 1; l <= 12; ++l)
      for (int j = 0; j < (1 << l); ++j) {
        mrk[q][l][j] = (mrk[x][l][j] | mrk[y][l][j]);
        num[l] += mrk[q][l][j];
      }
    for (int i = max(1, S1[x][0] - 11); i <= S1[x][0]; ++i) S[++n] = S1[x][i];
    for (int i = 1; i <= min(12, S1[y][0]); ++i) S[++n] = S1[y][i];
    for (int l = 1; l <= min(n, 12); ++l) {
      int cur = 0;
      for (int i = 1; i < l; ++i) cur = cur << 1 | S[i];
      for (int i = l; i <= n; ++i) {
        cur = cur << 1 | S[i];
        num[l] += (mrk[q][l][cur] == false);
        mrk[q][l][cur] = true;
        cur -= S[i - l + 1] << l - 1;
      }
    }
    while (num[Ans + 1] == (1 << Ans + 1)) ++Ans;
    printf("%d\n", Ans);
    n = 0;
    for (int i = 1; i <= S1[x][0]; ++i) S[++n] = S1[x][i];
    for (int i = 1; i <= S1[y][0]; ++i) S[++n] = S1[y][i];
    for (int i = 1; i <= min(n, 12); ++i) S1[q][++S1[q][0]] = S[i];
    for (int i = max(n - 11, min(n, 12) + 1); i <= n; ++i)
      S1[q][++S1[q][0]] = S[i];
  }
  return 0;
}
