#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 2e5 + 7;
const double eps = 0.00000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
  T ret = 1;
  for (; b; b >>= 1ll, a = a * a % M)
    if (b & 1) ret = 1ll * ret * a % M;
  return ret;
}
int i, j, k;
int u, v;
int n, m;
string pre[207], sub[207];
string s;
int ans[207];
int mark[207][1 << 14 | 7];
string tostring(int x) {
  string ret;
  while (x != 1) {
    char c = '0' + (x & 1);
    ret = c + ret;
    x /= 2;
  }
  return ret;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> s;
    pre[i] = sub[i] = s;
    ans[i] = 2;
    while (s.find(tostring(ans[i])) != string::npos)
      mark[i][ans[i]] = 1, ans[i]++;
  }
  cin >> m;
  for (i = n + 1; i <= n + m; i++) {
    cin >> u >> v;
    pre[i] = pre[u] + pre[v];
    sub[i] = sub[u] + sub[v];
    if (pre[i].length() > 30) pre[i] = pre[i].substr(0, 30);
    reverse(sub[i].begin(), sub[i].end());
    if (sub[i].length() > 30) sub[i] = sub[i].substr(0, 30);
    reverse(sub[i].begin(), sub[i].end());
    ans[i] = max(ans[u], ans[v]);
    s = sub[u] + pre[v];
    for (j = 2; j <= 1 << 14; j++)
      if (mark[u][j] || mark[v][j] || s.find(tostring(j)) != string::npos)
        mark[i][j] = 1;
    while (mark[i][ans[i]]) ans[i]++;
    int T = ans[i], A = 0;
    while (T) A++, T >>= 1;
    printf("%d\n", A - 2);
  }
}
