#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long INF = 1e18;
const int mn = 1050, mm = 1e5 + 10;
int cnt[mn], c[mm];
int zui[mn], ci[mn];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int num;
    scanf("%d", &num);
    int temp = 0;
    for (int j = 1; j <= num; j++) {
      int t;
      scanf("%d", &t);
      temp += (1 << (t - 1));
    }
    cnt[temp]++;
  }
  c[0] = inf;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &c[i]);
    int num;
    scanf("%d", &num);
    int temp = 0;
    for (int j = 1; j <= num; j++) {
      int t;
      scanf("%d", &t);
      temp += (1 << (t - 1));
    }
    if (c[i] < c[zui[temp]])
      ci[temp] = zui[temp], zui[temp] = i;
    else if (c[i] < c[ci[temp]])
      ci[temp] = i;
  }
  int ansn = 0, ansx = 1, ansy = 2;
  for (int i = 0; i < (1 << 9); i++) {
    if (!zui[i]) continue;
    for (int j = 0; j < (1 << 9); j++) {
      int a, b;
      if (i == j)
        a = zui[i], b = ci[i];
      else
        a = zui[i], b = zui[j];
      if (!a || !b) continue;
      int temp = i | j, tcnt = 0;
      for (int k = 0; k < (1 << 9); k++)
        if ((temp & k) == k) tcnt += cnt[k];
      if (tcnt > ansn) {
        ansn = tcnt;
        ansx = a, ansy = b;
      } else if (tcnt == ansn) {
        if (c[ansx] + c[ansy] > c[a] + c[b]) ansx = a, ansy = b;
      }
    }
  }
  printf("%d %d\n", ansx, ansy);
  return 0;
}
