#include <bits/stdc++.h>
using namespace std;
int n, m;
int cnt[513];
pair<int, int> val[513];
pair<int, int> id[513];
int max_z = -1;
int min_p = -1;
pair<int, int> res;
set<int> dif_val;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int f;
    cin >> f;
    int cur = 0;
    for (int j = 1; j <= f; j++) {
      int b;
      cin >> b;
      b--;
      cur |= (1 << b);
    }
    cnt[cur]++;
  }
  for (int i = 0; i <= 512; i++) val[i].first = val[i].second = -1;
  for (int i = 1; i <= m; i++) {
    int p, f;
    cin >> p >> f;
    int cur = 0;
    for (int j = 1; j <= f; j++) {
      int b;
      cin >> b;
      b--;
      cur |= (1 << b);
    }
    if (val[cur].first == -1) {
      val[cur].first = p;
      id[cur].first = i;
    } else if (p < val[cur].first) {
      val[cur].second = val[cur].first;
      id[cur].second = id[cur].first;
      val[cur].first = p;
      id[cur].first = i;
    } else if (p < val[cur].second || val[cur].second == -1) {
      val[cur].second = p;
      id[cur].second = i;
    }
    dif_val.insert(cur);
  }
  for (int i = 1; i <= 512; i++) {
    for (int j = i; j <= 512; j++) {
      if (i == j && val[j].second == -1) continue;
      if (val[i].first != -1 && val[j].first != -1) {
        int cur = (i | j);
        int cnt_z = 0;
        int cur_p;
        if (i != j)
          cur_p = val[i].first + val[j].first;
        else
          cur_p = val[i].first + val[j].second;
        for (int k = 1; k <= 512; k++)
          if ((cur & k) == k) cnt_z += cnt[k];
        if (cnt_z > max_z || (cnt_z == max_z && cur_p < min_p)) {
          max_z = cnt_z;
          min_p = cur_p;
          res.first = id[i].first;
          if (i == j)
            res.second = id[j].second;
          else
            res.second = id[j].first;
        }
      }
    }
  }
  cout << res.first << ' ' << res.second;
}
