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 <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const int M = 1e9 + 7;
struct el {
  string l, r;
  vector<unordered_set<bitset<10>>> mp;
  el(const string &s) {
    l = "";
    r = "";
    mp.resize(10);
    fill_mp(s);
    fill_l(s);
    fill_r(s);
  }
  void fill_mp(const string &s) {
    int n = s.size();
    for (int len_ = 1; len_ <= 9; ++len_) {
      bitset<10> b;
      if (n < len_) {
        break;
      }
      for (int i = 0; i < len_; ++i) {
        b[i] = (s[i] == '1');
      }
      mp[len_].insert(b);
      for (int i = len_; i < n; ++i) {
        b >>= 1;
        b[len_ - 1] = (s[i] == '1');
        mp[len_].insert(b);
      }
    }
  }
  void fill_l(const string &s) {
    int n = s.size();
    string tl;
    int tlen = min(9, n);
    for (int i = 0; i < tlen; ++i) {
      tl.push_back(s[i]);
    }
    l = tl;
  }
  void fill_r(const string &s) {
    int n = s.size();
    string tr;
    int tlen = min(9, n);
    for (int i = n - 1; i >= n - tlen; --i) {
      tr.push_back(s[i]);
    }
    reverse(tr.begin(), tr.end());
    r = tr;
  }
  el(const el &L, const el &R) {
    l = "";
    r = "";
    mp.resize(10);
    for (int len = 1; len <= 9; ++len) {
      for (auto b : L.mp[len]) {
        mp[len].insert(b);
      }
      for (auto b : R.mp[len]) {
        mp[len].insert(b);
      }
    }
    string s = L.r + R.l;
    string sl = L.l + R.l;
    string sr = L.r + R.r;
    fill_mp(s);
    fill_l(sl);
    fill_r(sr);
  }
  int getk() {
    for (int i = 9; i >= 1; --i) {
      if (mp[i].size() == (1 << i)) {
        return i;
      }
    }
    return 0;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<el> A;
  string s;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    el nel(s);
    A.push_back(nel);
  }
  int m;
  cin >> m;
  int l, r;
  for (int i = 0; i < m; ++i) {
    cin >> l >> r;
    --l;
    --r;
    el nel(A[l], A[r]);
    A.push_back(nel);
    cout << nel.getk() << "\n";
  }
  return 0;
}