#include <bits/stdc++.h>
using namespace std;
const int64_t N = 512;
const int64_t INF = 1e18;
const int64_t MOD = 1e9 + 7;
set<int64_t> people;
vector<pair<int64_t, pair<int64_t, int64_t>>> pizza;
map<int64_t,
    priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                   greater<pair<int64_t, int64_t>>>>
    m;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int64_t n, colp;
  cin >> n >> colp;
  for (int64_t i = 0; i < n; i++) {
    int64_t t;
    cin >> t;
    int64_t mask = 0;
    for (int64_t j = 0; j < t; j++) {
      int64_t x;
      cin >> x;
      x--;
      mask |= (1LL << x);
    }
    people.insert(mask);
  }
  for (int64_t i = 0; i < colp; i++) {
    int64_t cost;
    cin >> cost;
    int64_t t;
    cin >> t;
    int64_t mask = 0;
    for (int64_t j = 0; j < t; j++) {
      int64_t x;
      cin >> x;
      x--;
      mask |= (1LL << x);
    }
    m[mask].push({cost, i + 1});
  }
  for (auto x : m) {
    for (int64_t i = 0; n > 50000 ? i < min(2LL, (long long)x.second.size())
                                  : i <= min(2LL, (long long)x.second.size());
         i++) {
      pizza.push_back({x.first, {x.second.top().first, x.second.top().second}});
      x.second.pop();
    }
  }
  int64_t ans = 0;
  int64_t cost = INF;
  pair<int64_t, int64_t> e = {-1, -1};
  for (int64_t i = 0; i < pizza.size() - 1; i++) {
    for (int64_t j = i + 1; j < pizza.size(); j++) {
      int64_t temp = 0;
      int64_t newcost = pizza[i].second.first + pizza[j].second.first;
      int64_t mask = (pizza[i].first | pizza[j].first);
      for (int64_t peo : people) {
        if ((mask & peo) == peo) temp++;
      }
      if (temp > ans) {
        ans = temp;
        cost = newcost;
        e = {pizza[i].second.second, pizza[j].second.second};
      } else if (temp == ans) {
        if (cost > newcost) {
          cost = newcost;
          e = {pizza[i].second.second, pizza[j].second.second};
        }
      }
    }
  }
  cout << e.first << " " << e.second << endl;
}
