#include <bits/stdc++.h>
using namespace std;
string s[205];
int ans[205];
void cal(int x, int low) {
  int n = s[x].size();
  for (int i = low + 1; i <= 10; ++i) {
    for (int j = 0; j < (1 << i); ++j) {
      string tmp = "";
      for (int k = 0; k < i; ++k) {
        if ((j >> k) & 1) {
          tmp += "1";
        } else {
          tmp += "0";
        }
      }
      if (s[x].find(tmp) == -1) {
        ans[x] = i - 1;
        return;
      }
    }
  }
}
int main() {
  int n = 0, m = 0, a = 0, b = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> a >> b;
    s[n + i] = s[a] + s[b];
    if (s[n + i].size() > 1000) {
      s[n + i] = s[n + i].substr(0, 500) +
                 s[n + i].substr((int)s[n + i].size() - 500, 500);
    }
    cal(n + i, max(ans[a], ans[b]));
    cout << ans[n + i] << endl;
  }
  return 0;
}
