#include <bits/stdc++.h>
using namespace std;
int n, q;
int r[int(2e5 + 100)], m[int(2e5 + 100)], s[int(2e5 + 100)], id[int(2e5 + 100)],
    pos[int(2e5 + 100)];
vector<pair<int, int> > a[int(2e5 + 100)];
pair<int, int> b[int(2e5 + 100)];
bool cmp(int a, int b) { return r[a] < r[b]; }
int main() {
  scanf("%d", &n);
  for (int i = (1), _b = (n); i <= _b; i++) {
    scanf("%d%d", &b[i].first, &b[i].second);
    a[b[i].first].push_back(make_pair(i, b[i].second));
  }
  int cnt = 0;
  memset(r, 63, sizeof(r));
  int def = r[0];
  for (int i = (n), _a = (1); i >= _a; i--) {
    if (r[b[i].first] == def) {
      cnt++;
      r[b[i].first] = cnt;
      pos[b[i].first] = i;
      id[cnt] = b[i].first;
    }
  }
  int l = 0;
  scanf("%d", &q);
  for (int t = (1), _b = (q); t <= _b; t++) {
    int k;
    scanf("%d", &k);
    for (int i = (1), _b = (k); i <= _b; i++) scanf("%d", &s[i]);
    sort(s + 1, s + 1 + k, cmp);
    int x = 0, y = 0;
    for (int i = (1), _b = (k); i <= _b; i++) {
      if (r[s[i]] != i) {
        x = i;
        break;
      }
    }
    if (!x) {
      x = k + 1;
      if (!id[x]) {
        printf("0 0\n");
        continue;
      } else
        y = x + 1;
    } else {
      for (int i = (x), _b = (k); i <= _b; i++)
        if (r[s[i]] != i + 1) {
          y = i + 1;
          break;
        }
      if (!y) y = k + 2;
    }
    if (!id[x]) {
      printf("0 0\n");
      continue;
    }
    int l = pos[id[y]] + 1;
    auto it = lower_bound(a[id[x]].begin(), a[id[x]].end(), make_pair(l, 0));
    printf("%d %d\n", id[x], it->second);
  }
}
