#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
const ll N = 100005;
ll b[N];
ll p[N];
ll mval[1000];
ll id[1000];
ll n, m;
ll dp[1000];
ll solve(int mask) {
  if (dp[mask] != -1) return dp[mask];
  ll ret = 0;
  for (ll i = 0; i < n; i++) {
    ret += (b[i] & mask) == b[i];
  }
  return dp[mask] = ret;
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%lld %lld", &n, &m);
  for (ll i = 0; i < n; i++) {
    ll f;
    scanf("%lld", &f);
    while (f--) {
      ll x;
      scanf("%lld", &x);
      x--;
      b[i] |= 1 << x;
    }
  }
  ll val[N];
  ii ans = {0, -1e16};
  ll p1 = 1, p2 = 2;
  for (ll i = 0; i < m; i++) {
    scanf("%lld", val + i);
    ll f;
    scanf("%lld", &f);
    while (f--) {
      ll x;
      scanf("%lld", &x);
      x--;
      p[i] |= 1 << x;
    }
    if (mval[p[i]]) {
      ll cnt = solve(p[i]);
      if (ii(cnt, -(mval[p[i]] + val[i])) > ans) {
        ans = {cnt, -(mval[p[i]] + val[i])};
        p1 = id[p[i]];
        p2 = i;
      }
    }
    if (mval[p[i]] == 0 || val[i] < mval[p[i]]) {
      mval[p[i]] = val[i];
      id[p[i]] = i;
    }
  }
  for (ll i = 1; i < (1 << 9); i++) {
    if (mval[i]) {
      for (ll j = 1; j < (1 << 9); j++) {
        if (j != i && mval[j]) {
          ll cnt = solve(j | i);
          if (ii(cnt, -(mval[i] + mval[j])) > ans) {
            ans = {cnt, -(mval[i] + mval[j])};
            p1 = id[i];
            p2 = id[j];
          }
        }
      }
    }
  }
  printf("%lld %lld\n", p1 + 1, p2 + 1);
  return 0;
}
