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;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
int MOD = 1e9 + 7;
void ADD(long long &x, long long v) {
  x = (x + v) % MOD;
  if (x < 0) x += MOD;
}
const int SIZE = 1 << 20;
string s[201];
int len[201];
int n, m;
int a[202], b[202];
bitset<4096> d[201][13];
void solve() {
  for (int i = (1); i <= (n); ++i) {
    len[i] = ((int)(s[i]).size());
    for (int l = (1); l <= (12); ++l) {
      for (int k = 0; k + l <= len[i]; k++) {
        int me = 0;
        for (int r = 0; r < (l); ++r) {
          me = me * 2 + (s[i][k + r] - '0');
        }
        d[i][l][me] = 1;
      }
    }
  }
  for (int i = (n + 1); i <= (n + m); ++i) {
    for (int l = (1); l <= (12); ++l) {
      d[i][l] = d[a[i]][l] | d[b[i]][l];
    }
    string tmp = s[a[i]].substr(max(0, ((int)(s[a[i]]).size()) - 12)) +
                 s[b[i]].substr(0, min(12, ((int)(s[b[i]]).size())));
    for (int l = (1); l <= (12); ++l) {
      for (int k = 0; k + l <= ((int)(tmp).size()); k++) {
        int me = 0;
        for (int r = 0; r < (l); ++r) {
          me = me * 2 + (tmp[k + r] - '0');
        }
        d[i][l][me] = 1;
      }
    }
    s[i] = s[a[i]] + s[b[i]];
    if (((int)(s[i]).size()) > 24) {
      s[i] = s[i].substr(0, 12) + s[i].substr(((int)(s[i]).size()) - 12);
    }
    int ans = 0;
    while (ans < 12 && d[i][ans + 1].count() == (1 << (ans + 1))) ans++;
    W(ans);
  }
}
void input() {
  R(n);
  for (int i = (1); i <= (n); ++i) {
    R(s[i]);
  }
  R(m);
  for (int i = (1); i <= (m); ++i) {
    R(a[i + n], b[i + n]);
  }
}
int main() {
  input();
  solve();
  return 0;
}