#include <fstream>
#include <vector>

using namespace std;

const int maxCnt = 50;

unsigned int rs = 51444532U;

int rnd() {
  return ((rs = rs * 239017U + 301703U) >> 8);
}

int main() {
  ifstream fin("mosaic.in");
  ofstream fout("mosaic.out");

  int n;
  fin >> n;
  vector <pair <int, int> > a(n);
  for (int i = 0; i < n; ++i) {
    fin >> a[i].first >> a[i].second;
  }
  int k;
  fin >> k;
  for (int i = 0; i < k; ++i) {
    int l, r;
    fin >> l >> r;
    l--;
    r--;
    int x = -1, y = -1, cnt = 0;
    do {
      x = l + rnd() % (r - l + 1);
      y = l + rnd() % (r - l + 1);
      cnt++;
    } while (cnt < maxCnt && (a[x].first == a[y].first || a[x].second == a[y].second));
    if (cnt == maxCnt) {
      fout << "0 0" << endl;
    } else {
      fout << (x + 1) << " " << (y + 1) << endl;
    }
  }

  return 0;
}
