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;
void pass() { return; }
const double EPS = 1e-6;
mt19937 rnd((rand() << 15 + rand()));
const int MOD = 1e9 + 7;
const long long BIG_MOD = 1e18 + 7;
const int MAXN = 207;
const int MX = 12;
struct str {
  set<int> s[MX + 1];
  string left, right;
  int len;
};
int to_int(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[s.size() - i - 1] == '1') {
      res += (1 << i);
    }
  }
  return res;
}
str merge(str l, str r) {
  str res;
  res.len = min(1000000, l.len + r.len);
  if (l.len < MX) {
    res.left = l.left + r.left;
  } else {
    res.left = l.left;
  }
  if (r.len < MX) {
    res.right = l.right + r.right;
  } else {
    res.right = r.right;
  }
  for (int len = 1; len < MX; ++len) {
    for (auto elem : l.s[len]) {
      res.s[len].insert(elem);
    }
    for (auto elem : r.s[len]) {
      res.s[len].insert(elem);
    }
    for (int lp = 1; lp < len; ++lp) {
      int rp = len - lp;
      if (lp > l.right.size() || rp > r.left.size()) continue;
      string new_el = "";
      for (int i = l.right.size() - lp; i < l.right.size(); ++i) {
        new_el += l.right[i];
      }
      for (int i = 0; i < rp; ++i) {
        new_el += r.left[i];
      }
      res.s[len].insert(to_int(new_el));
    }
  }
  return res;
}
signed main() {
  cout.tie(NULL);
  cout.precision(20);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<str> a(n);
  string s;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    string left = "";
    for (int p = 0; p < min(MX, (int)s.size()); ++p) {
      left += s[p];
    }
    string right = "";
    for (int p = max((long long)0, (long long)s.size() - MX); p < s.size();
         ++p) {
      right += s[p];
    }
    a[i].left = left;
    a[i].right = right;
    a[i].len = s.size();
    for (int l = 0; l < s.size(); ++l) {
      string new_el = "";
      for (int r = l; r < min((int)s.size(), l + MX - 1); ++r) {
        new_el += s[r];
        a[i].s[r - l + 1].insert(to_int(new_el));
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int p1, p2;
    cin >> p1 >> p2;
    a.push_back(merge(a[p1 - 1], a[p2 - 1]));
    for (int len = 1; len <= MX; ++len) {
      if (a[a.size() - 1].s[len].size() < (1 << len)) {
        cout << len - 1 << '\n';
        break;
      }
    }
  }
  return 0;
}
