#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200001;
int t[NMAX], last[NMAX], nex[21][NMAX];
void inc(int i, int val) {
  for (; i > 0; i &= (i + 1), i--) {
    t[i] += val;
  }
}
int sum(int l) {
  int res = 0;
  for (; l < NMAX; l |= (l + 1)) {
    res += t[l];
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<pair<int, int> > v(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i].first >> v[i].second;
    if (last[v[i].first]) {
      nex[0][i] = last[v[i].first];
      for (int j = 1; j < 20; ++j) {
        nex[j][i] = nex[j - 1][nex[j - 1][i]];
      }
    }
    last[v[i].first] = i;
  }
  for (int i = 1; i <= n; ++i) {
    if (last[i]) inc(last[i], 1);
  }
  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    vector<int> vec(k);
    for (int i = 0; i < k; ++i) cin >> vec[i];
    for (int i : vec)
      if (last[i]) inc(last[i], -1);
    if (sum(0) == 0) {
      cout << "0 0\n";
      for (int i : vec)
        if (last[i]) inc(last[i], 1);
      continue;
    }
    int L = 0, R = n + 1;
    while (R - L > 1) {
      int M = (R + L) / 2;
      if (sum(M))
        L = M;
      else
        R = M;
    }
    int pos = L;
    for (int i = 19; i >= 0; --i) {
      if (nex[i][pos] && sum(nex[i][pos]) == 1) {
        pos = nex[i][pos];
      }
    }
    cout << v[pos].first << ' ' << v[pos].second << '\n';
    for (int i : vec)
      if (last[i]) inc(last[i], 1);
  }
}
