#include <bits/stdc++.h>
using namespace std;
const int ms = 1 << 10;
const int sigma = 30;
int n, m, k, t, a, b, c, q;
pair<int, int> price[ms];
int freq[ms];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int qt, total = 0;
  for (int i = 0; i < n; i++) {
    cin >> qt;
    int cur = 0;
    while (qt--) {
      cin >> a;
      a--;
      cur += (1 << a);
    }
    total |= cur;
    for (int j = 0; j < ms; j++) {
      if ((j & cur) == cur) {
        freq[j]++;
      }
    }
  }
  int p;
  pair<int, int> ans = {-1, -n};
  int ansa = 0, ansb = 0;
  for (int i = 0; i < m; i++) {
    cin >> p;
    cin >> qt;
    int cur = 0;
    while (qt--) {
      cin >> a;
      a--;
      cur += (1 << a);
    }
    cur = cur & total;
    if (price[cur].first == 0) {
      price[cur] = {p, i};
    } else {
      pair<int, int> nAns = {freq[cur], -p - price[cur].first};
      if (nAns > ans) {
        ans = nAns;
        ansa = price[cur].second, ansb = i;
      }
      price[cur] = min(price[cur], {p, i});
    }
  }
  int va, vb;
  for (int i = 0; i < ms; i++) {
    for (int j = i + 1; j < ms; j++) {
      if (price[i].first == 0 || price[j].first == 0) continue;
      pair<int, int> ca = price[j], cb = price[i];
      va = i, vb = j;
      p = ca.first + cb.first;
      int qtN = freq[(va | vb)];
      pair<int, int> nAns = {qtN, -p};
      if (nAns >= ans) {
        ansa = cb.second, ansb = ca.second;
        ans = nAns;
      }
    }
  }
  cout << ansa + 1 << ' ' << ansb + 1 << '\n';
}
