#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long INF64 = 1e18;
const int N = 100 * 1000 + 11;
const int MSK = 1 << 9;
int n, m;
int cnt[MSK];
vector<pair<int, int> > pz[N];
void solve() {
  for (int i = 0; i < int(m); ++i) {
    int c;
    scanf("%d", &c);
    int msk = 0;
    int k;
    scanf("%d", &k);
    for (int j = 0; j < int(k); ++j) {
      int pos;
      scanf("%d", &pos);
      --pos;
      msk |= (1 << pos);
    }
    pz[msk].push_back(make_pair(c, i));
    sort(pz[msk].begin(), pz[msk].end());
    while (pz[msk].size() > 2) pz[msk].pop_back();
  }
  int ans = 0, cost = 2 * INF + 1;
  pair<int, int> res = make_pair(-1, -1);
  for (int p1 = 0; p1 < int(1024); ++p1)
    for (int p2 = 0; p2 < int(1024); ++p2) {
      int curcost = 0;
      int pos1, pos2;
      if (p1 == p2) {
        if ((pz[p1].size()) < 2) continue;
        curcost = pz[p1][0].first + pz[p1][1].first;
        pos1 = pz[p1][0].second;
        pos2 = pz[p1][1].second;
      } else {
        if ((pz[p1].size()) == 0 || (pz[p2].size()) == 0) continue;
        curcost = pz[p1][0].first + pz[p2][0].first;
        pos1 = pz[p1][0].second;
        pos2 = pz[p2][0].second;
      }
      int curans = 0;
      int curmask = p1 | p2;
      for (int pp = 0; pp < int(1024); ++pp) {
        if ((curmask & pp) == pp) {
          curans += cnt[pp];
        }
      }
      if (curans > ans || (curans == ans && curcost < cost)) {
        ans = curans;
        cost = curcost;
        res = make_pair(pos1, pos2);
      }
    }
  cout << res.first + 1 << " " << res.second + 1 << endl;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n); ++i) {
    int k;
    scanf("%d", &k);
    int msk = 0;
    for (int j = 0; j < int(k); ++j) {
      int pos;
      scanf("%d", &pos);
      --pos;
      msk |= (1 << pos);
    }
    ++cnt[msk];
  }
  solve();
  return 0;
}
