#include <bits/stdc++.h>
using namespace std;
const int P = 512;
int m, n, s_max, friends[P], ans1, ans2, best_price = INT_MAX;
set<pair<int, int>> pizza[P], all_pizza;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr),
      cin >> n >> m;
  for (int _ = 0; _ < n; ++_) {
    int f, x, y = 0;
    cin >> f;
    for (int _ = 0; _ < f; ++_) cin >> x, y |= (1 << (x - 1));
    ++friends[y];
  }
  for (int i = 0; i < m; ++i) {
    int c, r, x, y = 0;
    cin >> c >> r;
    for (int _ = 0; _ < r; ++_) cin >> x, y |= (1 << (x - 1));
    pizza[y].emplace(c, i + 1), all_pizza.emplace(c, i + 1);
  }
  for (int i = 1; i < P; ++i)
    if (not pizza[i].empty())
      for (int p = pizza[i].size(), j = i; j < P; ++j)
        if (not pizza[j].empty() and (i != j or p > 1)) {
          int count = 0, k = i;
          if (i != j) k |= j;
          for (int y = 1; y < P; ++y)
            if (friends[y] and (k & y) == y) count += friends[y];
          if (count == 0) continue;
          auto it = pizza[i].begin(), iu = pizza[j].begin();
          if (i == j) ++iu;
          auto [c1, s1] = *it;
          auto [c2, s2] = *iu;
          int price = c1 + c2;
          if (count > s_max or (count == s_max and price < best_price))
            ans1 = s1, ans2 = s2, s_max = count, best_price = price;
        }
  if (best_price == INT_MAX) {
    auto it = all_pizza.begin(), iu = it;
    ++iu;
    auto [c1, s1] = *it;
    auto [c2, s2] = *iu;
    ans1 = s1, ans2 = s2;
  }
  cout << ans1 << ' ' << ans2;
}
