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>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int N = 205, M0 = 12, M = 4096;
struct Str {
  int k, l0, l1;
  bool v[M0][M];
  char s0[M0 + 5], s1[M0 + 5];
  int calk() {
    int i, j;
    for (j = 0; j < M0; j++) {
      for (i = 0; i < (1 << (j + 1)); i++)
        if (!v[j][i]) break;
      if (i < (1 << (j + 1))) return j;
    }
    return j;
  }
  void init(char *s) {
    memset(v, 0, sizeof(v));
    int i, j, n = strlen(s + 1);
    for (i = 1; i <= n; i++) {
      int x = 0;
      for (j = 0; j < M0 && i + j <= n; j++) {
        x = (x << 1) + (s[i + j] - '0');
        v[j][x] = 1;
      }
    }
    k = this->calk();
    for (i = 1; i <= M0; i++) {
      if (i > n) break;
      s0[i] = s[i], s1[i] = s[n - i + 1];
    }
    l0 = l1 = i - 1;
  }
  int work(const Str &a, const Str &b) {
    memset(v, 0, sizeof(v));
    int i, j, x, y;
    for (j = 0; j < M0; j++)
      for (i = 0; i < M; i++) v[j][i] = (a.v[j][i] || b.v[j][i]);
    for (i = 1, y = 0; i <= a.l1; i++) {
      y = y + (a.s1[i] - '0') * (1 << (i - 1));
      for (j = 1, x = y; i + j <= M0 && j <= b.l0; j++) {
        x = (x << 1) + (b.s0[j] - '0');
        v[i + j - 1][x] = 1;
      }
    }
    k = this->calk();
    l0 = min(a.l0 + b.l0, M0);
    for (i = 1; i <= a.l0; i++) s0[i] = a.s0[i];
    if (a.l0 < M0)
      for (j = 1; i <= l0; j++, i++) s0[i] = b.s0[j];
    l1 = min(a.l1 + b.l1, M0);
    for (i = 1; i <= b.l1; i++) s1[i] = b.s1[i];
    if (b.l1 < M0)
      for (j = 1; i <= l1; j++, i++) s1[i] = a.s1[j];
    return k;
  }
  void PRT() {
    int i, j;
    puts("");
    puts("");
    for (i = 0; i < (1 << (k + 1)); i++) printf("%d", v[k][i]);
    puts("");
    printf("%d\n", k);
    for (i = 1; i <= l0; i++) printf("%c", s0[i]);
    puts("");
    for (i = l1; i >= 1; i--) printf("%c", s1[i]);
    puts("");
    puts("");
  }
};
Str a[N];
int main() {
  int i, n, q;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    char s[105];
    scanf("%s", s + 1);
    a[i].init(s);
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%d\n", a[n + i].work(a[x], a[y]));
  }
  return 0;
}
