#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N];
int b[N];
int last[N];
int ne[N];
int was[N];
vector<pair<int, int> > z[N];
int ans[N];
int main() {
  int n;
  scanf("%d", &n);
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", a + i, b + i);
  }
  for (int i = 0; i <= n; ++i) {
    last[i] = n + 1;
  }
  vector<int> w;
  for (int i = n; i >= 0; --i) {
    if (last[a[i]] == n + 1) {
      w.push_back(i);
    }
    ne[i] = last[a[i]];
    last[a[i]] = i;
  }
  for (int i = 0; i <= n; ++i) {
    z[i].clear();
  }
  memset(was, 0, sizeof was);
  int st = 0;
  int qq;
  scanf("%d", &qq);
  for (int i = 0; i < qq; ++i) {
    int k;
    scanf("%d", &k);
    ++st;
    while (k--) {
      int foo;
      scanf("%d", &foo);
      was[foo] = st;
    }
    int j = 0;
    while (was[a[w[j]]] == st) {
      ++j;
    }
    if (j == (int)w.size() - 1) {
      ans[i] = 0;
      continue;
    }
    int v = a[w[j]];
    ++j;
    while (was[a[w[j]]] == st) {
      ++j;
    }
    z[v].push_back({w[j] + 1, i});
  }
  for (int i = 1; i <= n; ++i) {
    sort(z[i].begin(), z[i].end());
    int k = 0;
    int j = last[i];
    while (j <= n) {
      while (k < (int)z[i].size() && z[i][k].first <= j) {
        ans[z[i][k].second] = j;
        ++k;
      }
      j = ne[j];
    }
  }
  for (int i = 0; i < qq; ++i) {
    printf("%d %d\n", a[ans[i]], b[ans[i]]);
  }
}
