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;
char string_in_buffer[(int)260];
void fast_scan(int &first) { scanf("%d", &first); }
void fast_scan(long long &first) { scanf("%lld", &first); }
void fast_scan(unsigned long long &first) { scanf("%llu", &first); }
void fast_scan(double &first) { scanf("%lf", &first); }
void fast_scan(long double &first) { scanf("%Lf", &first); }
void fast_scan(char &first) {
  scanf("%c", &first);
  if (first == '\n') {
    fast_scan(first);
  }
}
void fast_scan(string &first) {
  scanf("%s", string_in_buffer);
  first = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
  fast_scan(p.first);
  fast_scan(p.second);
}
template <class T>
void fast_scan(vector<T> &v) {
  for (auto &first : v) fast_scan(first);
}
void fast_print(const int &first) { printf("%d", first); }
void fast_print(const long long &first) { printf("%lld", first); }
void fast_print(const unsigned long long &first) { printf("%llu", first); }
void fast_print(const double &first) { printf("%.15lf", first); }
void fast_print(const long double &first) { printf("%.15Lf", first); }
void fast_print(const char &first) { printf("%c", first); };
void fast_print(const string &first) { printf("%s", first.c_str()); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
  fast_print(p.first);
  fast_print(' ');
  fast_print(p.second);
}
template <class T>
void fast_print(const vector<T> &v) {
  if (v.empty()) return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print(" ");
    fast_print(v[i]);
  }
}
template <class T>
void fast_print(const vector<vector<T>> &v) {
  if (v.empty()) return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print("\n");
    fast_print(v[i]);
  }
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
  fast_print(print_start);
  print_start = "\n";
  first_print = true;
}
}  // namespace smart_io
template <class T>
ostream &operator,(ostream &os, const T &object) {
  if (!smart_io::first_print) {
    fast_print(smart_io::sep);
  } else {
    smart_io::first_print = false;
  }
  fast_print(object);
  return os;
}
template <class T>
istream &operator,(istream &is, T &object) {
  fast_scan(object);
  return is;
}
namespace typedefs {}
namespace numbers_operation {
template <class T>
T floor_mod(T a, T b) {
  if (a % b == 0) return 0;
  if (a >= 0 && b >= 0) return a % b;
  if (a <= 0 && b <= 0) return a % b;
  return abs(b) - (abs(a) % abs(b));
}
}  // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
const int T = 11;
int to_mask(const string &s, int l, int r) {
  int mask = 0;
  for (int i = l; i < r; i++) {
    mask = (mask | ((s[i] == '1') << (i - l)));
  }
  return mask;
}
string cut(string s) {
  return string(s.begin(), s.begin() + min(((int)s.size()), 20));
}
string cut_rev(string s) {
  return string(s.end() - min(((int)s.size()), 20), s.end());
}
struct ustring {
  vector<set<int>> kek;
  string st, end;
  ustring(string s) {
    kek.resize(T);
    for (int i = 1; i < T; i++) {
      for (int mask = 0; mask < (1 << i); mask++) {
        for (int j = 0; j < ((int)s.size()) - i + 1; j++) {
          kek[i].insert(to_mask(s, j, j + i));
        }
      }
    }
    st = s;
    end = s;
  }
  ustring() {}
  friend ustring operator+(ustring a, ustring b) {
    ustring rez;
    rez.st = cut(a.st + b.st);
    rez.end = cut_rev(a.end + b.end);
    vector<set<int>> top(T);
    {
      string inter = a.end + b.st;
      for (int i = 0; i < T; i++) {
        for (int j = 0; j < ((int)inter.size()) - i + 1; j++) {
          top[i].insert(to_mask(inter, j, j + i));
        }
      }
    }
    {
      for (int i = 0; i < T; i++) {
        for (int j : a.kek[i]) {
          top[i].insert(j);
        }
        for (int j : b.kek[i]) {
          top[i].insert(j);
        }
      }
    }
    { rez.kek = top; }
    return rez;
  }
  int get_laugh() {
    for (int i = T - 1; i > -1; i--) {
      if (((int)kek[i].size()) == (1 << i)) {
        return i;
      }
    }
    assert(false);
  }
};
signed main(signed argc, char *argv[]) {
  vector<ustring> v;
  int n;
  cin, n;
  for (int i = 0; i < n; i++) {
    string s;
    cin, s;
    v.emplace_back(s);
  }
  int m;
  cin, m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin, a, b;
    a--;
    b--;
    v.emplace_back(v[a] + v[b]);
    smart_io::precall_print();
    cout, v.back().get_laugh();
  }
}