#include <bits/stdc++.h>
using namespace std;
int ppl[512];
vector<pair<int, int> > pizzas[512];
pair<int, long long> sol = {-1, 0};
pair<int, int> pe = {0, 0};
int k, x, mask;
long long price;
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 1; i <= N; ++i) {
    cin >> k;
    mask = 0;
    for (int j = 0; j < k; ++j) {
      cin >> x;
      mask |= (1 << (x - 1));
    }
    ppl[mask]++;
  }
  for (int i = 1; i <= M; ++i) {
    cin >> price >> k;
    mask = 0;
    for (int j = 0; j < k; ++j) {
      cin >> x;
      mask |= (1 << (x - 1));
    }
    pizzas[mask].push_back({price, i});
  }
  for (int i = 0; i < 512; ++i) sort(pizzas[i].begin(), pizzas[i].end());
  for (int i = 0; i < 512; ++i) {
    for (int j = i; j < 512; ++j) {
      pair<int, int> pe2;
      if (i == j) {
        if (pizzas[i].size() < 2) continue;
        price = -(pizzas[i][0].first + pizzas[i][1].first);
        pe2 = {pizzas[i][0].second, pizzas[i][1].second};
      } else {
        if (pizzas[i].size() < 1) continue;
        if (pizzas[j].size() < 1) continue;
        price = -(pizzas[i][0].first + pizzas[j][0].first);
        pe2 = {pizzas[i][0].second, pizzas[j][0].second};
      }
      int mask = (i | j), pp = 0;
      for (int k = 0; k < 512; ++k) {
        if ((k & mask) == k) pp += ppl[k];
      }
      if (make_pair(pp, price) > sol) {
        sol = {pp, price};
        pe = pe2;
      }
    }
  }
  cout << pe.first << ' ' << pe.second;
  return 0;
}
