#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 3;
const int INF = 2e9 + 5;
const int K = 512;
int cnt[K];
vector<pair<int, int> > id[K];
int pls[K][K], cost[K][K];
pair<int, int> ids[K][K];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int f, mask = 0;
    cin >> f;
    while (f--) {
      int b;
      cin >> b;
      b--;
      mask |= (1 << b);
    }
    ++cnt[mask];
  }
  for (int i = 1; i <= m; i++) {
    int c, r, mask = 0;
    cin >> c >> r;
    while (r--) {
      int a;
      cin >> a;
      a--;
      mask |= (1 << a);
    }
    id[mask].push_back({c, i});
  }
  for (int i = 0; i < K; i++) sort(id[i].begin(), id[i].end());
  int mx = 0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      cost[i][j] = INF;
      if (id[i].empty() or id[j].empty()) continue;
      int cst, j1, j2;
      if (i == j) {
        if (id[i].size() < 2) continue;
        cst = id[i][0].first + id[i][1].first;
        j1 = id[i][0].second, j2 = id[i][1].second;
      } else {
        cst = id[i][0].first + id[j][0].first;
        j1 = id[i][0].second, j2 = id[j][0].second;
      }
      int cur = 0, p = (i | j);
      for (int k = 0; k < K; k++) {
        if ((p | k) == p) cur += cnt[k];
      }
      pls[i][j] = cur;
      cost[i][j] = cst;
      ids[i][j] = {j1, j2};
      mx = max(mx, cur);
    }
  }
  int mn = INF;
  pair<int, int> ans;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      if (pls[i][j] == mx and cost[i][j] < mn) {
        mn = cost[i][j];
        ans = ids[i][j];
      }
    }
  }
  cout << ans.first << " " << ans.second;
}
