#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
const int DMAX = 530;
struct nume {
  long long int pret, id;
};
inline bool operator<(nume x, nume y) { return x.pret < y.pret; }
vector<int> vec;
vector<nume> arb[DMAX];
int V[DMAX];
bool uz[DMAX];
int n, m;
long long int ans = 1e18, cates;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t, i, j;
  long long int x, y, pret;
  long long int var, cate;
  int mask1, mask2;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    var = 0;
    cin >> x;
    while (x--) {
      cin >> y;
      var += (1 << (y - 1));
    }
    V[var]++;
  }
  for (i = 1; i <= m; i++) {
    var = 0;
    cin >> pret;
    cin >> x;
    while (x--) {
      cin >> y;
      var += (1 << (y - 1));
    }
    arb[var].push_back({pret, i});
  }
  for (i = 1; i <= 511; i++) sort(arb[i].begin(), arb[i].end());
  for (i = 1; i <= 511; i++) {
    arb[i].push_back({(long long int)1e12, 0});
    arb[i].push_back({(long long int)1e12, 0});
  }
  for (mask1 = 1; mask1 <= 511; mask1++)
    for (mask2 = 1; mask2 <= 511; mask2++) {
      if (arb[mask1][0].pret == 1e12 || arb[mask2][0].pret == 1e12) continue;
      memset(uz, 0, sizeof(uz));
      cate = 0;
      pret = 0;
      for (i = 1; i <= 511; i++)
        if (!uz[i] && ((mask1 & i) == i)) {
          uz[i] = true;
          cate += V[i];
        }
      if (mask1 == mask2) {
        if (arb[mask1][1].pret == 1e12) continue;
        pret = arb[mask1][0].pret + arb[mask1][1].pret;
        if (cate > cates || (cate == cates && pret < ans)) {
          cates = cate;
          ans = pret;
          x = arb[mask1][0].id;
          y = arb[mask1][1].id;
        }
        continue;
      }
      int mask = mask1 | mask2;
      for (i = 1; i <= 511; i++)
        if (!uz[i] && ((mask & i) == i)) {
          uz[i] = true;
          cate += V[i];
        }
      pret = arb[mask1][0].pret + arb[mask2][0].pret;
      if (cate > cates || (cate == cates && pret < ans)) {
        cates = cate;
        ans = pret;
        x = arb[mask1][0].id;
        y = arb[mask2][0].id;
      }
    }
  cout << x << ' ' << y << '\n';
  return 0;
}
