#include <bits/stdc++.h>
using namespace std;
int n, m;
int nroameni[1025], price, p, maxim, ans1, ans2, x1, x2;
long long minprice;
set<pair<int, int>> prices[1025];
bool ok(int q, int w) {
  int bt = 0;
  while (bt <= 8) {
    if (((q >> bt) & 1) == 0 && ((w >> bt) & 1) == 1) return 0;
    bt++;
  }
  return 1;
}
int calcnr(int u) {
  int suma = 0;
  for (int l = 1; l < (1 << 9); l++)
    if (ok(u, l))
      if (nroameni[l] > 0) suma += nroameni[l];
  return suma;
}
int main() {
  cin >> n >> m;
  minprice = 99999999999;
  for (int i = 1; i <= n; i++) {
    cin >> p;
    int bitmask = 0;
    int a;
    for (int j = 1; j <= p; j++) {
      cin >> a;
      bitmask += (1 << (a - 1));
    }
    nroameni[bitmask]++;
  }
  for (int i = 1; i <= m; i++) {
    cin >> price >> p;
    int bitmask = 0;
    int a;
    for (int j = 1; j <= p; j++) {
      cin >> a;
      bitmask += (1 << (a - 1));
    }
    prices[bitmask].insert({price, i});
  }
  for (int i = 1; i < (1 << 9); i++)
    if (!prices[i].empty())
      for (int j = i; j < (1 << 9); j++)
        if (!prices[j].empty()) {
          int totalprice = 0;
          int rez = 0;
          int nr = 0;
          if (i == j) {
            if (prices[i].size() >= 2) {
              auto it = prices[i].begin();
              x1 = (*it).second;
              totalprice += (*it).first;
              it++;
              totalprice += (*it).first;
              x2 = (*it).second;
              rez = i;
              nr = calcnr(rez);
              if (nr > maxim) {
                maxim = nr;
                minprice = totalprice;
                ans1 = x1;
                ans2 = x2;
              }
              if (nr == maxim && totalprice < minprice) {
                maxim = nr;
                minprice = totalprice;
                ans1 = x1;
                ans2 = x2;
              }
            }
          } else {
            auto it = prices[i].begin();
            totalprice += (*it).first;
            x1 = (*it).second;
            it = prices[j].begin();
            totalprice += (*it).first;
            x2 = (*it).second;
            rez = (i | j);
            nr = calcnr(rez);
            if (nr > maxim) {
              maxim = nr;
              minprice = totalprice;
              ans1 = x1;
              ans2 = x2;
            }
            if (nr == maxim && totalprice < minprice) {
              maxim = nr;
              minprice = totalprice;
              ans1 = x1;
              ans2 = x2;
            }
          }
        }
  cout << ans1 << " " << ans2;
  return 0;
}
