#include <bits/stdc++.h>
using namespace std;
const int N = 205;
vector<bool> g[N][11];
string s[N], L[N], R[N];
char arr[N];
void pre(int in) {
  for (int i = 1; i < 10; i++) {
    vector<bool> temp(1 << i);
    for (int j = 0, x = 0; j + i <= s[in].size(); j++) {
      string d = s[in].substr(j, i);
      x = 0;
      for (int k = 0; k < i; k++) {
        x *= 2;
        x += d[k] - '0';
      }
      temp[x] = 1;
    }
    g[in][i] = temp;
  }
  if (s[in].size() > 20) {
    L[in] = s[in].substr(0, 10);
    R[in] = s[in].substr((int)s[in].size() - 10, 10);
  } else
    L[in] = R[in] = s[in];
}
int add(int in, int a, int b) {
  int ret = 0;
  for (int i = 1; i < 10; i++) {
    int am = (1 << i);
    vector<bool> temp(1 << i);
    for (int j = 0; j < (1 << i); j++) {
      temp[j] = g[a][i][j] | g[b][i][j];
      if (temp[j]) am--;
    }
    string S = R[a] + L[b];
    for (int j = 0, x = 0; j + i <= S.size(); j++) {
      string d = S.substr(j, i);
      x = 0;
      for (int k = 0; k < i; k++) {
        x *= 2;
        x += d[k] - '0';
      }
      if (!temp[x]) am--;
      temp[x] = 1;
    }
    if (L[a].size() + L[b].size() < 10)
      L[in] = L[a] + L[b];
    else
      L[in] = L[a] + L[b].substr(0, 10 - min(10, (int)L[a].size()));
    if (R[b].size() + R[a].size() < 10)
      R[in] = R[a] + R[b];
    else {
      int len = 10 - min(10, (int)R[b].size());
      R[in] = R[a].substr(R[a].size() - len, len) + R[b];
    }
    if (!am) ret = i;
    g[in][i] = temp;
  }
  return ret;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", arr);
    s[i] = arr;
    pre(i);
  }
  int q;
  scanf("%d", &q);
  for (int in = n, a, b; in < n + q; in++) {
    scanf("%d%d", &a, &b);
    a--, b--;
    printf("%d\n", add(in, a, b));
  }
  return 0;
}
