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;
int n, m, x, y;
char a[2000];
struct node {
  bitset<1030> vis;
  int h, t, len;
  node() {
    len = h = t = 0;
    vis.reset();
  }
  void record(char a[]) {
    len = strlen(a + 1);
    int x = 0;
    for (int j = 1; j <= len; j++) {
      x = ((x << 1) + (a[j] == '1')) % (1 << 9);
      if (j == 9) h = x;
      for (int k = 1; k <= min(j, 9); k++) vis[x % (1 << k) + (1 << k)] = 1;
    }
    if (len < 9)
      h = x;
    else
      len = 9;
    t = x;
  }
} w[2001];
node merge(node a, node b) {
  node tem;
  tem.len = a.len + b.len;
  if (tem.len > 9) tem.len = 9;
  if (a.len < 9) {
    tem.h = a.h;
    for (int i = 1; i <= min(9 - a.len, b.len); i++)
      tem.h = (tem.h << 1) + ((b.h & (1 << b.len - i)) > 0);
  } else
    tem.h = a.h;
  if (b.len < 9)
    tem.t = ((a.t % (1 << 9 - b.len)) << b.len) + b.t;
  else
    tem.t = b.t;
  int x = 0;
  tem.vis = a.vis | b.vis;
  for (int i = 1; i <= a.len; i++) {
    x = (x << 1) + ((a.t & (1 << (a.len - i))) > 0);
    for (int k = 1; k <= min(i, 9); k++) tem.vis[x % (1 << k) + (1 << k)] = 1;
  }
  for (int i = 1; i <= b.len; i++) {
    x = (x << 1) + ((b.h & (1 << (b.len - i))) > 0);
    for (int k = 1; k <= min(i + a.len, 9); k++)
      tem.vis[x % (1 << k) + (1 << k)] = 1;
  }
  return tem;
}
bool ok(int x, int in) {
  for (int i = 0; i < 1 << x; i++)
    if (!w[in].vis[i + (1 << x)]) return 0;
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", a + 1);
    w[i].record(a);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    w[n + i] = merge(w[x], w[y]);
    for (int k = 1; k <= 9; k++)
      if (!ok(k, n + i)) {
        printf("%d\n", k - 1);
        break;
      }
  }
  return 0;
}
