#include <bits/stdc++.h>
using ul = std::uint32_t;
using li = std::int32_t;
using ll = std::int64_t;
using ull = std::uint64_t;
using vul = std::vector<ul>;
using vvul = std::vector<vul>;
using pulul = std::pair<ul, ul>;
using llf = long double;
ul n, m;
ul cnt[1 << 9];
ul mn1[1 << 9];
ul mn1id[1 << 9];
ul mn2[1 << 9];
ul mn2id[1 << 9];
ul mn[1 << 9];
ul mnid1[1 << 9];
ul mnid2[1 << 9];
int main() {
  std::scanf("%u%u", &n, &m);
  for (ul i = 0; i != n; ++i) {
    ul f;
    std::scanf("%u", &f);
    ul mask = 0;
    for (ul j = 0; j != f; ++j) {
      ul b;
      std::scanf("%u", &b);
      mask |= 1 << b - 1;
    }
    ++cnt[mask];
  }
  for (ul i = 0; i != 9; ++i) {
    for (ul set = (1 << 9) - 1 & ~(1 << i), curr = set;;
         curr = curr - 1 & set) {
      cnt[curr | 1 << i] += cnt[curr];
      if (!curr) {
        break;
      }
    }
  }
  for (ul i = 0; i != (1 << 9); ++i) {
    mn[i] = 2e9 + 1;
    mn1[i] = 2e9 + 1;
    mn2[i] = 2e9 + 1;
  }
  for (ul i = 1; i <= m; ++i) {
    ul c, r;
    std::scanf("%u%u", &c, &r);
    ul mask = 0;
    for (ul j = 0; j != r; ++j) {
      ul a;
      std::scanf("%u", &a);
      mask |= 1 << a - 1;
    }
    if (c < mn2[mask]) {
      mn2[mask] = c;
      mn2id[mask] = i;
      if (mn2[mask] < mn1[mask]) {
        std::swap(mn2[mask], mn1[mask]);
        std::swap(mn1id[mask], mn2id[mask]);
      }
    }
  }
  for (ul i = 0; i != (1 << 9); ++i) {
    for (ul j = 0; j != (1 << 9); ++j) {
      ul u = i | j;
      if (i != j) {
        if (mn1[i] + mn1[j] < mn[u]) {
          mn[u] = mn1[i] + mn1[j];
          mnid1[u] = mn1id[i];
          mnid2[u] = mn1id[j];
        }
      } else {
        if (mn1[i] + mn2[i] < mn[u]) {
          mn[u] = mn1[i] + mn2[i];
          mnid1[u] = mn1id[i];
          mnid2[u] = mn2id[i];
        }
      }
    }
  }
  ul mxmask;
  ul mxcnt = 0;
  ul mncost = 2e9 + 1;
  for (ul i = 0; i != (1 << 9); ++i) {
    if (mn[i] == 2e9 + 1) {
      continue;
    }
    if (cnt[i] > mxcnt) {
      mxcnt = cnt[i];
      mncost = mn[i];
      mxmask = i;
    } else if (cnt[i] == mxcnt && mn[i] < mncost) {
      mncost = mn[i];
      mxmask = i;
    }
  }
  std::printf("%u %u\n", mnid1[mxmask], mnid2[mxmask]);
  return 0;
}
