#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, b, f, c, r;
  cin >> n >> m;
  vector<int> pl(512);
  for (int i = 0; i < (int)n; i++) {
    cin >> f;
    int bm = 0;
    for (int j = 0; j < (int)f; j++) {
      cin >> b;
      b--;
      bm |= (1 << b);
    }
    for (int j = 0; j < (int)512; j++)
      if ((j & bm) == bm) pl[j]++;
  }
  vector<int> price(m);
  vector<pair<int, int> > cost(512, {-1, -1});
  for (int i = 0; i < (int)m; i++) {
    cin >> price[i] >> r;
    int bm = 0;
    for (int j = 0; j < (int)r; j++) {
      cin >> b;
      b--;
      bm |= (1 << b);
    }
    if (cost[bm].first == -1)
      cost[bm].first = i;
    else if (cost[bm].second == -1) {
      cost[bm].second = i;
      if (price[cost[bm].first] > price[cost[bm].second])
        swap(cost[bm].first, cost[bm].second);
    } else if (price[cost[bm].first] > price[i])
      cost[bm] = {i, cost[bm].first};
    else if (price[cost[bm].second] > price[i])
      cost[bm].second = i;
  }
  vector<int> cost2(512, (2e9 + 1));
  vector<pair<int, int> > id(512, {-1, -1});
  for (int i = 0; i < (int)512; i++)
    if (cost[i].first != -1) {
      for (int j = 0; j < (int)i; j++)
        if (cost[j].first != -1)
          if (cost2[i | j] > price[cost[i].first] + price[cost[j].first]) {
            cost2[i | j] = price[cost[i].first] + price[cost[j].first];
            id[i | j] = {cost[i].first, cost[j].first};
          }
      if (cost[i].second != -1 &&
          cost2[i] > price[cost[i].first] + price[cost[i].second]) {
        cost2[i] = price[cost[i].first] + price[cost[i].second];
        id[i] = cost[i];
      }
    }
  int mx = 0;
  for (int i = 0; i < (int)512; i++)
    if (id[i].first != -1) {
      if (pl[i] > pl[mx] || pl[i] == pl[mx] && cost2[i] < cost2[mx]) mx = i;
    }
  cout << id[mx].first + 1 << " " << id[mx].second + 1 << "\n";
}
