#include <bits/stdc++.h>
int main() {
  int n, m;
  std::cin >> n >> m;
  std::array<int64_t, 512> cpp;
  for (int i = 0; i < 512; i++) cpp[i] = (LLONG_MAX >> 16);
  std::array<int64_t, 512> pti;
  for (int i = 0; i < 512; i++) pti[i] = 0;
  std::array<int64_t, 512> cpp2;
  for (int i = 0; i < 512; i++) cpp2[i] = (LLONG_MAX >> 16);
  std::array<int64_t, 512> pti2;
  for (int i = 0; i < 512; i++) pti2[i] = 0;
  std::array<int, 512> fpe;
  for (int i = 0; i < 512; i++) fpe[i] = 0;
  for (int i = 0; i < n; i++) {
    int f;
    scanf("%d", &f);
    int mask = 0;
    for (int j = 0; j < f; j++) {
      int b;
      scanf("%d", &b);
      mask |= 1 << (b - 1);
    }
    for (int j = 0; j < 512; j++)
      if (mask == (mask & j)) {
        fpe[j]++;
      }
  }
  for (int i = 0; i < m; i++) {
    int cost, r;
    scanf("%d%d", &cost, &r);
    int mask = 0;
    for (int j = 0; j < r; j++) {
      int a;
      scanf("%d", &a);
      mask |= 1 << (a - 1);
    }
    if (cost < cpp[mask]) {
      cpp2[mask] = cpp[mask];
      pti2[mask] = pti[mask];
      cpp[mask] = cost;
      pti[mask] = i;
    } else if (cost < cpp2[mask]) {
      cpp2[mask] = cost;
      pti2[mask] = i;
    }
  }
  int64_t mfc = -1, lc = LLONG_MAX, bp1 = -1, bp2 = -1;
  for (int p1 = 0; p1 < 512; p1++)
    for (int p2 = p1; p2 < 512; p2++) {
      int64_t cost = cpp[p1] + cpp[p2];
      if (p1 == p2) cost = cpp[p1] + cpp2[p1];
      if (cost > INT_MAX) continue;
      int fc = 0;
      int pi = p1 | p2;
      fc = fpe[pi];
      if (fc > mfc)
        mfc = fc, lc = cost, bp1 = p1, bp2 = p2;
      else if (fc == mfc && cost < lc)
        lc = cost, bp1 = p1, bp2 = p2;
    }
  int i1 = pti[bp1];
  int i2 = pti[bp2];
  if (bp1 == bp2) i2 = pti2[bp2];
  printf("%d %d\n", i1 + 1, i2 + 1);
}
