#include <bits/stdc++.h>
using namespace std;
int N, M;
int cost[(1 << 9)][2];
int id[(1 << 9)][2];
int cnt[(1 << 9)];
int x, y;
int num;
int ans = 2e9 + 1;
int NN;
void work(int s, int s1) {
  int t = (s | s1);
  int sum = 0;
  for (int i = 1; i <= NN; i++)
    if ((i & t) == i) sum += cnt[i];
  if (sum > num) {
    num = sum;
    if (s != s1) {
      ans = cost[s][0] + cost[s1][0];
      x = id[s][0];
      y = id[s1][0];
    } else {
      ans = cost[s][0] + cost[s][1];
      x = id[s][0];
      y = id[s][1];
    }
  }
  if (sum == num) {
    if (s != s1 && cost[s][0] + cost[s1][0] < ans) {
      ans = cost[s][0] + cost[s1][0];
      x = id[s][0];
      y = id[s1][0];
    }
    if (s == s1 && cost[s][0] + cost[s][1] < ans) {
      ans = cost[s][0] + cost[s][1];
      x = id[s][0];
      y = id[s][1];
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  memset(cost, 0x3f, sizeof(cost));
  scanf("%d%d", &N, &M);
  int f, b;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &f);
    int p = 0;
    while (f--) {
      scanf("%d", &b);
      b--;
      p += (1 << b);
    }
    cnt[p]++;
  }
  int c;
  for (int i = 1; i <= M; i++) {
    scanf("%d%d", &c, &f);
    int p = 0;
    while (f--) {
      scanf("%d", &b);
      b--;
      p += (1 << b);
    }
    if (c <= cost[p][0]) {
      cost[p][1] = cost[p][0];
      cost[p][0] = c;
      id[p][1] = id[p][0];
      id[p][0] = i;
    } else if (c < cost[p][1])
      cost[p][1] = c, id[p][1] = i;
  }
  NN = (1 << 9) - 1;
  for (int i = 1; i <= NN; i++)
    for (int j = 1; j <= NN; j++) {
      if (i != j && cost[i][0] != 1061109567 && cost[j][0] != 1061109567)
        work(i, j);
      if (i == j && cost[i][0] != 1061109567 && cost[j][1] != 1061109567)
        work(i, j);
    }
  printf("%d %d", x, y);
  return 0;
}
