#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void read(long long &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(int x) {
  static const int maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
const int MAXN = 300;
const int MAXLEN = 22;
char s[MAXN][MAXN];
int n;
int m;
set<long long> S[MAXN][23];
void _merge(int x, int y, int z) {
  int lenx = strlen(s[x]);
  int leny = strlen(s[y]);
  for (int i = 1; i <= min(lenx + leny, MAXLEN); i++) {
    for (auto t : S[x][i]) S[z][i].insert(t);
    for (auto t : S[y][i]) S[z][i].insert(t);
    int cnt = 0;
    for (int st = max(0, lenx - i); st < lenx; st++) s[z][cnt++] = s[x][st];
    for (int st = 0; st < min(leny, i); st++) s[z][cnt++] = s[y][st];
    int tmp = 0;
    for (int j = 0; j < i; j++) tmp = (s[z][j] - '0') + tmp * 2;
    S[z][i].insert(tmp);
    for (int j = i; j < cnt; j++) {
      tmp = (tmp * 2 + s[z][j] - '0') % (1 << i);
      S[z][i].insert(tmp);
    }
  }
  for (int i = 0; i < lenx; i++) s[z][i] = s[x][i];
  for (int i = 0; i < leny; i++) s[z][i + lenx] = s[y][i];
  s[z][leny + lenx] = 0;
  if (leny + lenx > MAXLEN * 2) {
    for (int i = 0; i < MAXLEN; i++)
      s[z][i + MAXLEN] = s[z][lenx + leny - MAXLEN + i];
    s[z][MAXLEN * 2] = 0;
  }
}
int get_ans(int x) {
  for (int i = 1;; i++) {
    if (S[x][i].size() != (1 << i)) return i - 1;
  }
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i]);
    int s_len = strlen(s[i]);
    for (int len = 1; len <= min(s_len, MAXLEN); len++) {
      int tmp = 0;
      for (int j = 0; j < len; j++) tmp = tmp * 2 + (s[i][j] - '0');
      S[i][len].insert(tmp);
      for (int j = len; j < s_len; j++) {
        tmp = (tmp * 2 + (s[i][j] - '0')) % (1 << len);
        S[i][len].insert(tmp);
      }
    }
    if (s_len > MAXLEN * 2) {
      for (int j = 0; j < MAXLEN; j++)
        s[i][j + MAXLEN] = s[i][j + s_len - MAXLEN];
    }
  }
  read(m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    read(x);
    read(y);
    int lenx = strlen(s[x]);
    int leny = strlen(s[y]);
    _merge(x, y, i + n);
    printf("%d\n", get_ans(i + n));
  }
  return 0;
  for (int i = 1; i <= 7; i++) {
    printf("NO.%d: ", i);
    for (auto t : S[i][2]) printf("%d ", t);
    puts("");
    for (int j = 0; s[i][j]; j++) printf("%c", s[i][j]);
    puts("");
  }
  return 0;
}
