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 IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = numeric_limits<double>::infinity();
const int MOD = 1000000007;
const double EPS = 1e-9;
const int DX[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int DY[] = {0, 1, 0, -1, 1, -1, -1, 1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long sqr(long long x) { return x * x; }
long long sqr(int x) { return (long long)x * x; }
double sqr(double x) { return x * x; }
long double sqr(long double x) { return x * x; }
mt19937 mmtw(960172);
long long rnd(long long x, long long y) {
  static uniform_int_distribution<long long> d;
  return d(mmtw) % (y - x + 1) + x;
}
const int T = 25;
int n, m;
vector<string> si;
vector<string> ss1, ss2;
vector<pair<int, int> > qs;
vector<int> ans;
int s2i(string const& a) {
  int x = 0;
  for (int i = 0; i < ((int)((a).size())); ++i)
    if (a[i] == '1') x |= 1 << i;
  return x;
}
string pref(string const& s, int t) {
  t = min(t, ((int)((s).size())));
  return s.substr(0, t);
}
string suf(string const& s, int t) {
  t = min(t, ((int)((s).size())));
  return s.substr(((int)((s).size())) - t, t);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  si.resize(n);
  ans.resize(n + m);
  ss1.resize(n + m);
  ss2.resize(n + m);
  for (int i = 0; i < n; ++i) {
    cin >> si[i];
    ss1[i] = pref(si[i], T);
    ss2[i] = suf(si[i], T);
  }
  cin >> m;
  qs.resize(m);
  ans.resize(n + m);
  ss1.resize(n + m);
  ss2.resize(n + m);
  for (int i = 0; i < m; ++i) {
    cin >> qs[i].first >> qs[i].second;
    --qs[i].first;
    --qs[i].second;
    int x = qs[i].first, y = qs[i].second;
    ss1[n + i] = ss1[x];
    if (((int)((ss1[n + i]).size())) < T)
      ss1[n + i] += pref(ss1[y], T - ((int)((ss1[n + i]).size())));
    ss2[n + i] = ss2[y];
    if (((int)((ss2[n + i]).size())) < T)
      ss2[n + i] = suf(ss2[x], T - ((int)((ss2[n + i]).size()))) + ss2[n + i];
  }
  ans.resize(m);
  for (int len = 1; true; ++len) {
    bool was = false;
    vector<unordered_set<int> > ss;
    ss.resize(n + m);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j + len <= ((int)((si[i]).size())); ++j)
        ss[i].insert(s2i(si[i].substr(j, len)));
    }
    for (int i = 0; i < m; ++i) {
      string s = ss2[qs[i].first] + ss1[qs[i].second];
      ss[n + i] = ss[qs[i].first];
      for (int y : ss[qs[i].second]) ss[n + i].insert(y);
      for (int j = max(0, ((int)((ss2[qs[i].first]).size())) + 1 - len);
           j < ((int)((ss2[qs[i].first]).size())) &&
           j + len <= ((int)((s).size()));
           ++j)
        ss[n + i].insert(s2i(s.substr(j, len)));
      if (((int)((ss[n + i]).size())) == (1 << len)) {
        was = 1;
        ans[i] = len;
      }
    }
    if (!was) break;
  }
  for (int i = 0; i < m; ++i) cout << ans[i] << "\n";
  return 0;
}