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>
using namespace std;
inline void read(int &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void read(long long &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(int x) {
  static const int maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
const int MAXN = 300;
const int MAXLEN = 22;
char s[MAXN][MAXN];
int n;
int m;
set<long long> S[MAXN][23];
void _merge(int x, int y, int z) {
  int lenx = strlen(s[x]);
  int leny = strlen(s[y]);
  for (int i = 1; i <= min(lenx + leny, MAXLEN); i++) {
    for (auto t : S[x][i]) S[z][i].insert(t);
    for (auto t : S[y][i]) S[z][i].insert(t);
    int cnt = 0;
    for (int st = max(0, lenx - i); st < lenx; st++) s[z][cnt++] = s[x][st];
    for (int st = 0; st < min(leny, i); st++) s[z][cnt++] = s[y][st];
    int tmp = 0;
    for (int j = 0; j < i; j++) tmp = (s[z][j] - '0') + tmp * 2;
    S[z][i].insert(tmp);
    for (int j = i; j < cnt; j++) {
      tmp = (tmp * 2 + s[z][j] - '0') % (1 << i);
      S[z][i].insert(tmp);
    }
  }
  for (int i = 0; i < lenx; i++) s[z][i] = s[x][i];
  for (int i = 0; i < leny; i++) s[z][i + lenx] = s[y][i];
  s[z][leny + lenx] = 0;
  if (leny + lenx > MAXLEN * 2) {
    for (int i = 0; i < MAXLEN; i++)
      s[z][i + MAXLEN] = s[z][lenx + leny - MAXLEN + i];
    s[z][MAXLEN * 2] = 0;
  }
}
int get_ans(int x) {
  for (int i = 1;; i++) {
    if (S[x][i].size() != (1 << i)) return i - 1;
  }
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i]);
    int s_len = strlen(s[i]);
    for (int len = 1; len <= min(s_len, MAXLEN); len++) {
      int tmp = 0;
      for (int j = 0; j < len; j++) tmp = tmp * 2 + (s[i][j] - '0');
      S[i][len].insert(tmp);
      for (int j = len; j < s_len; j++) {
        tmp = (tmp * 2 + (s[i][j] - '0')) % (1 << len);
        S[i][len].insert(tmp);
      }
    }
    if (s_len > MAXLEN * 2) {
      for (int j = 0; j < MAXLEN; j++)
        s[i][j + MAXLEN] = s[i][j + s_len - MAXLEN];
    }
  }
  read(m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    read(x);
    read(y);
    int lenx = strlen(s[x]);
    int leny = strlen(s[y]);
    _merge(x, y, i + n);
    printf("%d\n", get_ans(i + n));
  }
  return 0;
  for (int i = 1; i <= 7; i++) {
    printf("NO.%d: ", i);
    for (auto t : S[i][2]) printf("%d ", t);
    puts("");
    for (int j = 0; s[i][j]; j++) printf("%c", s[i][j]);
    puts("");
  }
  return 0;
}