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;
bitset<1 << 8> pd[109 << 1][8 + 1];
int n, m, l[109 << 1][8 + 1], r[109 << 1][8 + 1], len[109 << 1], b[109], ret;
char str[109];
int read() {
  int x = 1;
  char ch;
  while (ch = getchar(), ch < '0' || ch > '9')
    if (ch == '-') x = -1;
  int s = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
  return x * s;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    len[i] = strlen(str + 1);
    for (int j = 1; j <= 8; j++) {
      if (len[i] < j) break;
      int now = 0;
      for (int k = 1; k < j; k++) now = now << 1 | (str[k] - '0');
      for (int k = j; k <= len[i]; k++) {
        now = (now << 1 | (str[k] - '0')) & ((1 << j) - 1);
        pd[i][j][now] = 1;
      }
    }
    for (int j = 1; j <= min(len[i], 8); j++)
      l[i][j] = (str[j] - '0'), r[i][j] = (str[len[i] - j + 1] - '0');
  }
  m = read();
  while (m--) {
    int x = read(), y = read(), L = 0;
    for (int j = min(len[x], 8); j; j--) b[++L] = r[x][j];
    for (int j = 1; j <= min(len[y], 8); j++) b[++L] = l[y][j];
    assert(L <= 2 * 8);
    len[++n] = len[x] + len[y], ret = 0;
    if (len[x] > 8 || len[y] > 8 || len[n] > 8) len[n] = 8 + 1;
    if (len[n] <= 8)
      for (int i = 1; i <= L; i++) l[n][i] = b[i], r[n][i] = b[L - i + 1];
    else {
      for (int i = 1; i <= min(len[x], 8); i++) l[n][i] = l[x][i];
      if (len[x] < 8)
        for (int i = len[x] + 1; i <= 8; i++) l[n][i] = l[y][i - len[x]];
      for (int i = 1; i <= min(len[y], 8); i++) r[n][i] = r[y][i];
      if (len[y] < 8)
        for (int i = len[y] + 1; i <= 8; i++) r[n][i] = r[x][i - len[y]];
    }
    for (int i = 1; i <= 8; i++) {
      pd[n][i] = pd[x][i] | pd[y][i];
      if (L < i) continue;
      int now = 0;
      for (int k = 1; k < i; k++) now = now << 1 | b[k];
      for (int k = i; k <= L; k++) {
        now = (now << 1 | b[k]) & ((1 << i) - 1);
        assert(now < (1 << i));
        pd[n][i][now] = 1;
      }
    }
    for (int i = 1; i <= 8; i++) {
      bool flag = ((int)pd[n][i].count() == (1 << i));
      if (flag)
        ret = i;
      else
        break;
    }
    printf("%d\n", ret);
  }
  return 0;
}
