#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int n, m;
  cin >> n >> m;
  vector<int> ap(1 << 9);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    int msk = 0;
    for (int j = 0; j < x; ++j) {
      int nr;
      cin >> nr;
      msk += 1 << (nr - 1);
    }
    ap[msk]++;
  }
  const int INF = 1e9 + 1;
  vector<int> mn(1 << 9, INF);
  vector<int> repres(1 << 9);
  vector<vector<pair<int, int> > > IN(1 << 9);
  for (int i = 0; i < m; ++i) {
    int cost, x;
    cin >> cost >> x;
    int msk = 0;
    for (int j = 0; j < x; ++j) {
      int nr;
      cin >> nr;
      msk += 1 << (nr - 1);
    }
    if (cost < mn[msk]) mn[msk] = cost, repres[msk] = i;
    IN[msk].emplace_back(cost, i);
  }
  for (int i = 0; i < 1 << 9; ++i) sort(IN[i].begin(), IN[i].end());
  vector<int> are(1 << 9);
  for (int mas = 0; mas < 1 << 9; ++mas) {
    for (int o = 0; o < (1 << 9); ++o)
      if ((mas & o) == o) are[mas] += ap[o];
  }
  int BEST = -1, i1 = 0, i2 = 0, CMIN = 2e9 + 1;
  for (int a = 0; a < 1 << 9; ++a)
    for (int b = 0; b < 1 << 9; ++b) {
      if (mn[a] == INF or mn[b] == INF) continue;
      if (a == b && IN[a].size() < 2) continue;
      long long CurC = mn[a] + mn[b];
      int ca = repres[a], cb = repres[b];
      if (a == b) {
        CurC = IN[a][0].first + IN[a][1].first;
        ca = IN[a][0].second;
        cb = IN[a][1].second;
      }
      int now = are[a | b];
      if (now > BEST)
        BEST = now, i1 = ca, i2 = cb, CMIN = CurC;
      else if (now == BEST && CurC < CMIN)
        CMIN = CurC, i1 = ca, i2 = cb;
    }
  cout << i1 + 1 << ' ' << i2 + 1 << ' ';
}
