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;
const int N = -1;
const int INF = 1.01e9;
const int first = 10;
struct Str {
  bitset<(1ll << (first + 1))> b;
  string pref;
  string suf;
};
int makeMask(int mask, int len) {
  int x = ((1ll << (first - len)) - 1) << (len + 1);
  return x | mask;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<Str> data(n);
  for (int h = 0; h < (n); h++) {
    string t;
    cin >> t;
    for (int i = 0; i < (min(first, (int)t.size())); i++) {
      data[h].pref += t[i];
      data[h].suf += t[t.size() - 1 - i];
    }
    for (int len = 1; len <= first; len++) {
      for (int i = 0; i + len <= (int)t.size(); i++) {
        int mask = 0;
        for (int j = 0; j < len; j++) {
          if (t[i + j] == '1') {
            mask |= (1ll << (j));
          }
        }
        data[h].b[makeMask(mask, len)] = 1;
      }
    }
  }
  int k;
  scanf("%d", &k);
  for (int t = 0; t < (k); t++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    data.push_back(Str());
    auto& last = data.back();
    last.pref = data[x].pref;
    for (int i = 0;
         (int)last.pref.size() < first && i < (int)data[y].pref.size(); i++) {
      last.pref += data[y].pref[i];
    }
    last.suf = data[y].suf;
    for (int i = 0; (int)last.suf.size() < first && i < (int)data[x].suf.size();
         i++) {
      last.suf += data[x].suf[i];
    }
    last.b = data[x].b | data[y].b;
    for (int len = 1; len <= first; len++) {
      for (int right = 1; right <= len - 1; right++) {
        int left = len - right;
        if (left <= (int)data[x].suf.size() &&
            right <= (int)data[y].pref.size()) {
          int mask = 0;
          for (int j = 0; j < left; j++) {
            mask |= (1ll << (j)) * (data[x].suf[left - j - 1] - '0');
          }
          for (int j = 0; j < right; j++) {
            mask |= (1ll << (left + j)) * (data[y].pref[j] - '0');
          }
          last.b[makeMask(mask, len)] = 1;
        }
      }
    }
    int answerCand = 1;
    for (; answerCand <= first; answerCand++) {
      int ok = 1;
      for (int mask = 0; mask < ((1ll << (answerCand))); mask++) {
        ok &= last.b[makeMask(mask, answerCand)];
      }
      if (!ok) {
        break;
      }
    }
    cout << answerCand - 1 << endl;
  }
  return 0;
}