#include <bits/stdc++.h>
using namespace std;
string s[222], str;
int res[1000000] = {0};
bool hh(int n, int x) {
  for (int i = 0; i < (1 << x); i++) {
    str = "";
    for (int k = 0; k < x; k++) (i & (1 << k)) ? str += "1" : str += "0";
    if (s[n].find(str) == -1) return false;
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    int ii = 0, jj = 10;
    while (ii < jj && hh(i, ii + 1)) ii++;
    res[i] = ii;
  }
  int t, m;
  cin >> t;
  while (t--) {
    int i, j, mid, ii, jj;
    cin >> i >> j;
    ii = i;
    jj = j;
    n++;
    s[n] = s[i] + s[j];
    if (s[n].size() > 1000)
      s[n] = s[n].substr(0, 500) + s[n].substr(s[n].size() - 500, 500);
    i = 0;
    j = 10;
    while (i < j && hh(n, i + 1)) i++;
    res[n] = max(i, max(res[ii], res[jj]));
    cout << res[n] << "\n";
  }
  return 0;
}
