#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;
int dp[N];
struct node {
  int v, id;
} a[N][2];
void up1(int sta, int cost, int i) {
  if (a[sta][0].v > cost) {
    a[sta][1] = a[sta][0];
    a[sta][0] = {cost, i};
  } else if (a[sta][1].v > cost) {
    a[sta][1] = {cost, i};
  }
}
int num = -1, now, cs;
pair<int, int> ans;
void up2(node x, node y, int sta) {
  if (x.v == INF || y.v == INF) return;
  now = x.v + y.v;
  if (dp[sta] > num || (dp[sta] == num && cs > now)) {
    num = dp[sta];
    cs = now;
    ans = {x.id, y.id};
  }
}
int main() {
  int k, n, m, sta, v, cost;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k);
    sta = 0;
    for (int j = 1; j <= k; j++) {
      scanf("%d", &v);
      sta |= 1 << (v - 1);
    }
    dp[sta]++;
  }
  int mx = 0;
  for (int i = 0; i < 9; i++) {
    for (int j = (1 << 9) - 1; j >= 0; j--) {
      if ((1 << i) & j) dp[j] = dp[j] + dp[j ^ (1 << i)];
      mx = max(mx, dp[j]);
    }
  }
  for (int j = 0; j < (1 << 9); j++) a[j][0].v = a[j][1].v = INF;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &cost, &k);
    sta = 0;
    for (int j = 1; j <= k; j++) {
      scanf("%d", &v);
      sta |= 1 << (v - 1);
    }
    up1(sta, cost, i);
  }
  for (int i = 0; i < (1 << 9); i++) {
    for (int j = 0; j < (1 << 9); j++) {
      if (i == j)
        up2(a[i][0], a[i][1], i);
      else
        up2(a[i][0], a[j][0], i | j);
    }
  }
  printf("%d %d\n", ans.first, ans.second);
  return 0;
}
