#include <bits/stdc++.h>
using namespace std;
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = numeric_limits<double>::infinity();
const int MOD = 1000000007;
const double EPS = 1e-9;
const int DX[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int DY[] = {0, 1, 0, -1, 1, -1, -1, 1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long sqr(long long x) { return x * x; }
long long sqr(int x) { return (long long)x * x; }
double sqr(double x) { return x * x; }
long double sqr(long double x) { return x * x; }
mt19937 mmtw(960172);
long long rnd(long long x, long long y) {
  static uniform_int_distribution<long long> d;
  return d(mmtw) % (y - x + 1) + x;
}
const int T = 25;
int n, m;
vector<string> si;
vector<string> ss1, ss2;
vector<pair<int, int> > qs;
vector<int> ans;
int s2i(string const& a) {
  int x = 0;
  for (int i = 0; i < ((int)((a).size())); ++i)
    if (a[i] == '1') x |= 1 << i;
  return x;
}
string pref(string const& s, int t) {
  t = min(t, ((int)((s).size())));
  return s.substr(0, t);
}
string suf(string const& s, int t) {
  t = min(t, ((int)((s).size())));
  return s.substr(((int)((s).size())) - t, t);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  si.resize(n);
  ans.resize(n + m);
  ss1.resize(n + m);
  ss2.resize(n + m);
  for (int i = 0; i < n; ++i) {
    cin >> si[i];
    ss1[i] = pref(si[i], T);
    ss2[i] = suf(si[i], T);
  }
  cin >> m;
  qs.resize(m);
  ans.resize(n + m);
  ss1.resize(n + m);
  ss2.resize(n + m);
  for (int i = 0; i < m; ++i) {
    cin >> qs[i].first >> qs[i].second;
    --qs[i].first;
    --qs[i].second;
    int x = qs[i].first, y = qs[i].second;
    ss1[n + i] = ss1[x];
    if (((int)((ss1[n + i]).size())) < T)
      ss1[n + i] += pref(ss1[y], T - ((int)((ss1[n + i]).size())));
    ss2[n + i] = ss2[y];
    if (((int)((ss2[n + i]).size())) < T)
      ss2[n + i] = suf(ss2[x], T - ((int)((ss2[n + i]).size()))) + ss2[n + i];
  }
  ans.resize(m);
  for (int len = 1; true; ++len) {
    bool was = false;
    vector<unordered_set<int> > ss;
    ss.resize(n + m);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j + len <= ((int)((si[i]).size())); ++j)
        ss[i].insert(s2i(si[i].substr(j, len)));
    }
    for (int i = 0; i < m; ++i) {
      string s = ss2[qs[i].first] + ss1[qs[i].second];
      ss[n + i] = ss[qs[i].first];
      for (int y : ss[qs[i].second]) ss[n + i].insert(y);
      for (int j = max(0, ((int)((ss2[qs[i].first]).size())) + 1 - len);
           j < ((int)((ss2[qs[i].first]).size())) &&
           j + len <= ((int)((s).size()));
           ++j)
        ss[n + i].insert(s2i(s.substr(j, len)));
      if (((int)((ss[n + i]).size())) == (1 << len)) {
        was = 1;
        ans[i] = len;
      }
    }
    if (!was) break;
  }
  for (int i = 0; i < m; ++i) cout << ans[i] << "\n";
  return 0;
}
