#include <bits/stdc++.h>
using namespace std;
void amn(long long &x, long long y) { x = min(x, y); }
void amx(long long &x, long long y) { x = max(x, y); }
const long long INF = 1e18;
const long long N = 1e5 + 5;
const long long M = 9;
long long n, m, cnt[1 << M];
vector<pair<long long, long long> > pz[1 << M];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    long long f;
    cin >> f;
    long long msk = 0;
    for (long long j = 0; j < f; j++) {
      long long b;
      cin >> b;
      b--;
      msk |= (1 << b);
    }
    ++cnt[msk];
  }
  for (long long i = 0; i < m; i++) {
    long long cost, f;
    cin >> cost >> f;
    long long msk = 0;
    for (long long j = 0; j < f; j++) {
      long long b;
      cin >> b;
      b--;
      msk |= (1 << b);
    }
    pz[msk].push_back({cost, i + 1});
    sort(pz[msk].begin(), pz[msk].end());
    while ((long long)(pz[msk].size()) > 2) {
      pz[msk].pop_back();
    }
  }
  long long ans = 0, cost = 2 * INF + 1;
  pair<long long, long long> res = {-1, -1};
  for (long long p1 = 0; p1 < (1 << M); p1++) {
    for (long long p2 = 0; p2 < (1 << M); p2++) {
      long long lcost = 0;
      pair<long long, long long> lres;
      if (p1 == p2) {
        if ((long long)(pz[p1].size()) < 2) continue;
        lcost = pz[p1][0].first + pz[p1][1].first;
        lres = {pz[p1][0].second, pz[p1][1].second};
      } else {
        if ((long long)(pz[p1].size()) == 0 || (long long)(pz[p2].size()) == 0)
          continue;
        lcost = pz[p1][0].first + pz[p2][0].first;
        lres = {pz[p1][0].second, pz[p2][0].second};
      }
      long long lans = 0, mask = p1 | p2;
      for (long long pp = 0; pp < (1 << M); pp++) {
        if ((mask & pp) == pp) {
          lans += cnt[pp];
        }
      }
      if (lans > ans || (lans == ans && lcost < cost)) {
        ans = lans;
        cost = lcost;
        res = lres;
      }
    }
  }
  cout << res.first << " " << res.second << endl;
  return 0;
}
