#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const long long maxn = (long long)1e5 + 5;
const long long maxm = (1 << 9) + 5;
const long long mod = 1000000007;
const long long inf = 0x3f3f3f3f;
long long a[maxn], c[maxn], s[maxn];
long long num[maxm], minn[maxm], minn2[maxm], ans1[maxm], ans2[maxm], ans[maxm],
    k[maxm], k2[maxm];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  memset(minn, inf, sizeof(minn));
  memset(minn2, inf, sizeof(minn2));
  memset(ans, inf, sizeof(ans));
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    long long x;
    cin >> x;
    while (x--) {
      long long y;
      cin >> y;
      a[i] |= (1 << (y - 1));
    }
    ++num[a[i]];
  }
  for (long long i = 1; i <= m; ++i) {
    cin >> c[i];
    long long x;
    cin >> x;
    while (x--) {
      long long y;
      cin >> y;
      s[i] |= 1 << (y - 1);
    }
    if (c[i] < minn[s[i]]) {
      minn2[s[i]] = minn[s[i]];
      minn[s[i]] = c[i];
      k2[s[i]] = k[s[i]];
      k[s[i]] = i;
    } else if (c[i] < minn2[s[i]]) {
      minn2[s[i]] = c[i];
      k2[s[i]] = i;
    }
  }
  for (long long i = 0; i < (1 << 9); ++i) {
    if (k[i] && k2[i]) {
      ans[i] = minn[i] + minn2[i];
      ans1[i] = k[i], ans2[i] = k2[i];
    }
  }
  for (long long i = 0; i < (1 << 9); ++i)
    for (long long j = i + 1; j < (1 << 9); ++j) {
      if (minn[i] == 4557430888798830399 || minn[j] == 4557430888798830399)
        continue;
      if (ans[i | j] > minn[i] + minn[j]) {
        ans1[i | j] = k[i];
        ans2[i | j] = k[j];
        ans[i | j] = minn[i] + minn[j];
      }
    }
  long long id = 0, maxx = -1, val = 4557430888798830399;
  for (long long i = 0; i < (1 << 9); ++i) {
    if (ans[i] == 4557430888798830399) continue;
    long long tmp = 0;
    for (long long j = 0; j < (1 << 9); ++j)
      if ((j & i) == j) tmp += num[j];
    if (tmp > maxx) {
      id = i;
      maxx = tmp;
      val = ans[i];
    } else if (tmp == maxx && ans[i] < val) {
      id = i;
      val = ans[i];
    }
  }
  cout << ans1[id] << ' ' << ans2[id];
  return 0;
}
