#include <bits/stdc++.h>
using namespace std;
unsigned int n, m, minn[1 << 9][2], pissa[1 << 9][2], v[1 << 9], ans_p1, ans_p2,
    ans_sum, ans_v = 2e9;
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (unsigned int i = 1; i <= n; ++i) {
    unsigned int f, o = 0;
    cin >> f;
    for (unsigned int j = 0, a; j < f; ++j) cin >> a, o |= (1 << a - 1);
    v[o]++;
  }
  memset(minn, 0x7f, sizeof minn);
  for (unsigned int i = 1; i <= m; ++i) {
    unsigned int r, v, o = 0;
    cin >> v >> r;
    for (unsigned int j = 0, a; j < r; ++j) cin >> a, o |= (1 << a - 1);
    if (minn[o][0] >= v)
      swap(minn[o][0], minn[o][1]),
          minn[o][0] = v, swap(pissa[o][0], pissa[o][1]), pissa[o][0] = i;
    else if (minn[o][1] >= v)
      minn[o][1] = v, pissa[o][1] = i;
  }
  for (unsigned int p1 = 0; p1 < (1 << 9); ++p1)
    for (unsigned int p2 = 0; p2 < (1 << 9); ++p2) {
      unsigned int set = p1 | p2, sum = 0,
                   vv = (p1 == p2 ? minn[p1][0] + minn[p2][1]
                                  : minn[p1][0] + minn[p2][0]);
      if (vv > 2e9) continue;
      for (unsigned int sub = set; sub; sub = (sub - 1) & set) sum += v[sub];
      if (sum > ans_sum) {
        ans_sum = sum, ans_v = vv;
        if (p1 == p2)
          ans_p1 = pissa[p1][0], ans_p2 = pissa[p2][1];
        else
          ans_p1 = pissa[p1][0], ans_p2 = pissa[p2][0];
      } else if (sum == ans_sum) {
        if (ans_v >= vv) {
          ans_v = vv;
          if (p1 == p2)
            ans_p1 = pissa[p1][0], ans_p2 = pissa[p2][1];
          else
            ans_p1 = pissa[p1][0], ans_p2 = pissa[p2][0];
        }
      }
    }
  cout << ans_p1 << " " << ans_p2 << endl;
  return 0;
}
