#include <bits/stdc++.h>
const int cHalfLim = 1000;
int GetAns(std::string str) {
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < (1 << i); j++) {
      std::string tmp = "";
      for (int l = 0; l < i; l++) {
        if ((1 << l) & j)
          tmp += "1";
        else
          tmp += "0";
      }
      if (str.find(tmp) == -1) return i - 1;
    }
  }
  return 10;
}
int main() {
  int n;
  scanf("%d", &n);
  std::vector<std::string> strs;
  std::vector<int> outs;
  for (int i = 0; i < n; i++) {
    std::string tmp;
    std::cin >> tmp;
    strs.push_back(tmp);
    outs.push_back(GetAns(tmp));
  }
  int m;
  scanf("%d", &m);
  while (m--) {
    int l, r;
    std::cin >> l >> r;
    std::string tmp;
    l--, r--;
    tmp = strs[l] + strs[r];
    if (tmp.size() > 2 * cHalfLim) {
      std::string ntmp =
          tmp.substr(0, cHalfLim) + tmp.substr(tmp.size() - cHalfLim, cHalfLim);
      tmp = ntmp;
    }
    strs.push_back(tmp);
    outs.push_back(std::max(std::max(outs[l], outs[r]), GetAns(tmp)));
    std::cout << outs[outs.size() - 1] << std::endl;
  }
}
