#include <bits/stdc++.h>
using namespace std;
int read_bitset() {
  int f;
  cin >> f;
  bitset<9> p(0);
  while (f--) {
    int x;
    cin >> x;
    assert(1 <= x && x <= 9);
    p[x - 1] = true;
  }
  return p.to_ulong();
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  array<int, 512> cnt{};
  array<int, 512 * 2> cost, idx;
  fill(cost.begin(), cost.end(), 1.1e9);
  fill(idx.begin(), idx.end(), -1);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int mask = read_bitset();
    for (int i = 0; i < 512; ++i)
      if ((i & mask) == mask) ++cnt[i];
  }
  for (int i = 0; i < m; ++i) {
    int c;
    cin >> c;
    auto mask = read_bitset();
    if (c <= cost[2 * mask]) {
      cost[2 * mask + 1] = cost[2 * mask];
      cost[2 * mask] = c;
      idx[2 * mask + 1] = idx[2 * mask];
      idx[2 * mask] = i + 1;
    } else if (c <= cost[2 * mask + 1]) {
      cost[2 * mask + 1] = c;
      idx[2 * mask + 1] = i + 1;
    }
  }
  int pleased = -1, best = 0;
  pair<int, int> ans{-1, -1};
  auto check = [&](int mask0, int mask1) {
    auto [i0, i1] =
        make_pair(idx[2 * mask0], idx[2 * mask1 + (mask0 == mask1)]);
    if (i0 == -1 || i1 == -1) return;
    int c = cost[2 * mask0] + cost[2 * mask1 + (mask0 == mask1)];
    if (cnt[mask0 | mask1] > pleased ||
        (cnt[mask0 | mask1] == pleased && c < best)) {
      best = c;
      pleased = cnt[mask0 | mask1];
      ans = make_pair(i0, i1);
    }
  };
  for (int i = 0; i < 512; ++i) {
    check(i, i);
    for (int j = i + 1; j < 512; ++j) check(i, j);
  }
  cout << ans.first << ' ' << ans.second << '\n';
}
