#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int mod = (1e9) + 7, mod2 = 998244353;
const double eps = 1e-10;
const int siz = 1e5 + 5, siz2 = 19, lg = 31, block = 317, block2 = 1000,
          mxv = 5e5;
int n, m, stsf[512], like[512];
pair<int, int> mncost[512], pizza[siz];
void calcstsf() {
  for (int i = 1; i < 512; i++) {
    int cur = i;
    while (cur != 0) {
      stsf[i] += like[cur];
      cur = (cur - 1) & i;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int f, mask = 0;
    scanf("%d", &f);
    while (f--) {
      int b;
      scanf("%d", &b);
      mask |= (1 << (b - 1));
    }
    like[mask]++;
  }
  calcstsf();
  for (int i = 1; i < 512; i++) {
    mncost[i] = {INT_MAX, INT_MAX};
  }
  for (int i = 0; i < m; i++) {
    int c, r;
    scanf("%d%d", &c, &r);
    int mask = 0;
    while (r--) {
      int a;
      scanf("%d", &a);
      mask |= (1 << (a - 1));
    }
    if (c < mncost[mask].first) {
      mncost[mask].second = mncost[mask].first;
      mncost[mask].first = c;
    } else if (c < mncost[mask].second) {
      mncost[mask].second = c;
    }
    pizza[i] = {mask, c};
  }
  int mxstsf = INT_MIN, mnc = INT_MAX;
  int m1, m2, mnc1, mnc2;
  for (int i = 1; i < 512; i++) {
    for (int j = i; j < 512; j++) {
      int c1 = mncost[i].first, c2 = mncost[j].first, mask = i | j;
      if (i == j) {
        c2 = mncost[j].second;
      }
      if (max(c1, c2) != INT_MAX &&
          (stsf[mask] > mxstsf || (stsf[mask] == mxstsf && c1 + c2 < mnc))) {
        mxstsf = stsf[mask];
        mnc = c1 + c2;
        mnc1 = c1;
        mnc2 = c2;
        m1 = i;
        m2 = j;
      }
    }
  }
  int ind1 = -1, ind2 = -1;
  for (int i = 0; i < m; i++) {
    if (ind1 == -1 && pizza[i].first == m1 && pizza[i].second == mnc1) {
      ind1 = i + 1;
    } else if (ind2 == -1 && pizza[i].first == m2 && pizza[i].second == mnc2) {
      ind2 = i + 1;
    }
  }
  printf("%d %d\n", ind1, ind2);
  return 0;
}
