#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const int M = 1e9 + 7;
struct el {
  string l, r;
  vector<unordered_set<bitset<10>>> mp;
  el(const string &s) {
    l = "";
    r = "";
    mp.resize(10);
    fill_mp(s);
    fill_l(s);
    fill_r(s);
  }
  void fill_mp(const string &s) {
    int n = s.size();
    for (int len_ = 1; len_ <= 9; ++len_) {
      bitset<10> b;
      if (n < len_) {
        break;
      }
      for (int i = 0; i < len_; ++i) {
        b[i] = (s[i] == '1');
      }
      mp[len_].insert(b);
      for (int i = len_; i < n; ++i) {
        b >>= 1;
        b[len_ - 1] = (s[i] == '1');
        mp[len_].insert(b);
      }
    }
  }
  void fill_l(const string &s) {
    int n = s.size();
    string tl;
    int tlen = min(9, n);
    for (int i = 0; i < tlen; ++i) {
      tl.push_back(s[i]);
    }
    l = tl;
  }
  void fill_r(const string &s) {
    int n = s.size();
    string tr;
    int tlen = min(9, n);
    for (int i = n - 1; i >= n - tlen; --i) {
      tr.push_back(s[i]);
    }
    reverse(tr.begin(), tr.end());
    r = tr;
  }
  el(const el &L, const el &R) {
    l = "";
    r = "";
    mp.resize(10);
    for (int len = 1; len <= 9; ++len) {
      for (auto b : L.mp[len]) {
        mp[len].insert(b);
      }
      for (auto b : R.mp[len]) {
        mp[len].insert(b);
      }
    }
    string s = L.r + R.l;
    string sl = L.l + R.l;
    string sr = L.r + R.r;
    fill_mp(s);
    fill_l(sl);
    fill_r(sr);
  }
  int getk() {
    for (int i = 9; i >= 1; --i) {
      if (mp[i].size() == (1 << i)) {
        return i;
      }
    }
    return 0;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<el> A;
  string s;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    el nel(s);
    A.push_back(nel);
  }
  int m;
  cin >> m;
  int l, r;
  for (int i = 0; i < m; ++i) {
    cin >> l >> r;
    --l;
    --r;
    el nel(A[l], A[r]);
    A.push_back(nel);
    cout << nel.getk() << "\n";
  }
  return 0;
}
