#include <bits/stdc++.h>
using namespace std;
const int N = -1;
const int INF = 1.01e9;
const int first = 10;
struct Str {
  bitset<(1ll << (first + 1))> b;
  string pref;
  string suf;
};
int makeMask(int mask, int len) {
  int x = ((1ll << (first - len)) - 1) << (len + 1);
  return x | mask;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<Str> data(n);
  for (int h = 0; h < (n); h++) {
    string t;
    cin >> t;
    for (int i = 0; i < (min(first, (int)t.size())); i++) {
      data[h].pref += t[i];
      data[h].suf += t[t.size() - 1 - i];
    }
    for (int len = 1; len <= first; len++) {
      for (int i = 0; i + len <= (int)t.size(); i++) {
        int mask = 0;
        for (int j = 0; j < len; j++) {
          if (t[i + j] == '1') {
            mask |= (1ll << (j));
          }
        }
        data[h].b[makeMask(mask, len)] = 1;
      }
    }
  }
  int k;
  scanf("%d", &k);
  for (int t = 0; t < (k); t++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    data.push_back(Str());
    auto& last = data.back();
    last.pref = data[x].pref;
    for (int i = 0;
         (int)last.pref.size() < first && i < (int)data[y].pref.size(); i++) {
      last.pref += data[y].pref[i];
    }
    last.suf = data[y].suf;
    for (int i = 0; (int)last.suf.size() < first && i < (int)data[x].suf.size();
         i++) {
      last.suf += data[x].suf[i];
    }
    last.b = data[x].b | data[y].b;
    for (int len = 1; len <= first; len++) {
      for (int right = 1; right <= len - 1; right++) {
        int left = len - right;
        if (left <= (int)data[x].suf.size() &&
            right <= (int)data[y].pref.size()) {
          int mask = 0;
          for (int j = 0; j < left; j++) {
            mask |= (1ll << (j)) * (data[x].suf[left - j - 1] - '0');
          }
          for (int j = 0; j < right; j++) {
            mask |= (1ll << (left + j)) * (data[y].pref[j] - '0');
          }
          last.b[makeMask(mask, len)] = 1;
        }
      }
    }
    int answerCand = 1;
    for (; answerCand <= first; answerCand++) {
      int ok = 1;
      for (int mask = 0; mask < ((1ll << (answerCand))); mask++) {
        ok &= last.b[makeMask(mask, answerCand)];
      }
      if (!ok) {
        break;
      }
    }
    cout << answerCand - 1 << endl;
  }
  return 0;
}
