#include <bits/stdc++.h>
using namespace std;
const int N = int(6e5) + 5;
const int inf = (int)1e9 + 7;
const int maxk = 13;
int n;
int wow[(1 << maxk)], ans[201];
string s[201];
int find_num(string x) {
  for (int l = 1; l <= 9; ++l) {
    for (int j = 0; j < (1 << l); ++j) {
      string now;
      for (int k = 0; k < l; ++k) {
        now += (j >> k) & 1 ? '1' : '0';
      }
      if (x.find(now) == string ::npos) {
        return l - 1;
      }
    }
  }
}
void connect(int a, int b, int t) {
  ans[t] = max(ans[a], ans[b]);
  s[t] = s[a] + s[b];
  if ((int)s[t].size() > 1000) {
    s[t] = s[t].substr(0, 500) + s[t].substr((int)s[t].size() - 500, 500);
  }
  ans[t] = max(ans[t], find_num(s[t]));
}
int main() {
  cin >> n;
  int sz = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    ans[i] = find_num(s[i]);
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    connect(a, b, i + n);
    cout << ans[i + n] << "\n";
  }
  return 0;
}
