#include <bits/stdc++.h>
using namespace std;
constexpr int sz = 11;
constexpr int ssz = (1 << sz);
unordered_set<string> mp[sz];
constexpr int nn = 205;
string a[nn];
int dp[nn];
void prepare() {
  for (int i = 1; i < sz; ++i) {
    for (int j = 0; j < (1 << i); ++j) {
      string cur = "";
      for (int k = 0; k < i; ++k) {
        if ((1 << k) & j)
          cur += "1";
        else
          cur += "0";
      }
      mp[i].insert(cur);
    }
  }
}
int solve(const string& s) {
  for (int i = 0; i < sz; ++i) {
    for (auto t : mp[i + 1]) {
      if (s.find(t) == string::npos) return i;
    }
  }
}
int main() {
  prepare();
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    dp[i] = solve(a[i]);
  }
  cin >> m;
  for (int i = n + 1, x, y; i <= n + m; ++i) {
    cin >> x >> y;
    a[i] = a[x] + a[y];
    if (a[i].length() > ssz)
      a[i] = a[i].substr(0, ssz / 2) +
             a[i].substr(a[i].length() - ssz / 2, ssz / 2);
    dp[i] = max(dp[x], max(dp[y], solve(a[i])));
    cout << dp[i] << endl;
  }
}
