#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int a[21], b[21], p[N], l[N], s[N];
vector<int> v[N];
int main() {
  int n, q, i, j, k, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", p + i);
  scanf("%d", &q);
  for (i = 1; i <= q; ++i) scanf("%d%d", l + i, &j), v[j].push_back(i);
  for (i = 1; i <= n; ++i) {
    for (j = 19, x = p[i], y = i; ~j; --j)
      if (x & (1 << j)) {
        if (!a[j]) {
          a[j] = x, b[j] = y;
          break;
        }
        if (b[j] < y) swap(a[j], x), swap(b[j], y);
        x ^= a[j];
      }
    for (auto k : v[i]) {
      for (j = 19, x = l[k], y = 0; ~j; --j)
        if (b[j] >= x) y = max(y, y ^ a[j]);
      s[k] = y;
    }
  }
  for (i = 1; i <= q; ++i) printf("%d\n", s[i]);
  return 0;
}
