#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long re = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) re = (re << 1) + (re << 3) + ch - '0', ch = getchar();
  return re * flag;
}
long long n, m, a[1000010], b[1000010], c[1000010];
struct elee {
  long long minn, num;
  elee() {
    minn = 1e18;
    num = 0;
  }
  elee(long long aa, long long bb) {
    minn = aa;
    num = bb;
  }
  inline void up(long long x, long long nm) {
    if (x < minn) minn = x, num = nm;
  }
  inline void merge(const elee &b) {
    if (b.minn < minn) minn = b.minn, num = b.num;
  }
} x[10010];
struct ele {
  elee fir, sec;
  ele() { fir.minn = sec.minn = fir.num = sec.num = 1e18; }
  inline void up(elee x) {
    if (x.minn <= fir.minn)
      sec = fir, fir = x;
    else if (x.minn < sec.minn)
      sec = x;
  }
} minc;
long long s[10010];
vector<long long> lis[1000010];
long long li[1000010], cntl;
inline long long check(long long mid) {
  long long re = 1e18, i, le;
  for (auto sum : lis[mid]) {
    for (i = 1; i <= m; i++) {
      if ((sum & b[i]) == sum) {
        re = min(re,
                 c[i] + ((i == minc.fir.num) ? minc.sec.minn : minc.fir.minn));
        continue;
      }
      le = sum ^ (sum & b[i]);
      re = min(re, c[i] + x[le].minn);
    }
  }
  return re;
}
inline void check2(long long mid) {
  long long re = 1e18, i, le;
  pair<long long, long long> rep;
  rep = make_pair(mid, cntl);
  for (auto sum : lis[mid]) {
    for (i = 1; i <= m; i++) {
      if ((sum & b[i]) == sum) {
        if (re > c[i] + ((i == minc.fir.num) ? minc.sec.minn : minc.fir.minn)) {
          re = c[i] + ((i == minc.fir.num) ? minc.sec.minn : minc.fir.minn);
          rep =
              make_pair(i, ((i == minc.fir.num) ? minc.sec.num : minc.fir.num));
        }
        continue;
      }
      le = sum ^ (sum & b[i]);
      if (re > c[i] + x[le].minn) {
        re = c[i] + x[le].minn;
        rep = make_pair(i, x[le].num);
      }
    }
  }
  assert(re != 1e18);
  cout << rep.first << ' ' << rep.second << '\n';
}
int main() {
  n = read();
  m = read();
  long long i, j, tmp, l, r, mid;
  for (i = 1; i <= n; i++) {
    tmp = read();
    for (j = 1; j <= tmp; j++) a[i] |= (1 << (read() - 1));
    s[a[i]]++;
  }
  for (i = 511; i >= 1; i--) {
    for (j = (i - 1) & i; j; j = (j - 1) & i) s[i] += s[j];
    lis[s[i]].push_back(i);
  }
  for (i = 1; i <= n; i++)
    if (lis[i].size()) li[++cntl] = i;
  for (i = 1; i <= m; i++) {
    c[i] = read();
    tmp = read();
    minc.up(elee(c[i], i));
    for (j = 1; j <= tmp; j++) b[i] |= (1 << (read() - 1));
    x[b[i]].up(c[i], i);
  }
  for (i = 511; i >= 1; i--) {
    for (j = (i - 1) & i; j; j = (j - 1) & i) x[j].merge(x[i]);
  }
  l = 1;
  r = cntl;
  while (l < r) {
    mid = (l + r) >> 1;
    mid++;
    if (check(li[mid]) != 1e18)
      l = mid;
    else
      r = mid - 1;
  }
  if (l == 1 && check(li[1]) == 1e18) {
    cout << minc.fir.num << ' ' << minc.sec.num << '\n';
    return 0;
  }
  check2(li[l]);
}
