#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> ffsta(n);
  vector<vector<int>> pizzasta(m + 1), initsta(1 << 9, {INT_MAX, 0}),
      dpsta(1 << 9, {INT_MAX, 0, 0});
  for (int i = 0; i < n; i++) {
    int cnt = 0, sta = 0;
    cin >> cnt;
    for (int j = 0; j < cnt; j++) {
      int b;
      cin >> b;
      sta |= (1 << (b - 1));
    }
    ffsta[i] = sta;
  }
  for (int i = 1; i <= m; i++) {
    int cnt = 0, sta = 0, pri = 0;
    cin >> pri;
    cin >> cnt;
    for (int j = 0; j < cnt; j++) {
      int b;
      cin >> b;
      sta |= (1 << (b - 1));
    }
    pizzasta[i] = {pri, sta};
    if (pri < initsta[sta][0]) {
      initsta[sta] = {pri, i};
    }
  }
  for (int i = 0; i < (1 << 9); i++) {
    if (initsta[i][0] == INT_MAX) continue;
    for (int j = 1; j <= m; j++) {
      if (initsta[i][1] == j) continue;
      int nsta = i | pizzasta[j][1];
      if (initsta[i][0] + pizzasta[j][0] < dpsta[nsta][0]) {
        dpsta[nsta][0] = initsta[i][0] + pizzasta[j][0];
        dpsta[nsta][1] = initsta[i][1];
        dpsta[nsta][2] = j;
      }
    }
  }
  vector<int> ans(4, 0);
  ans[1] = INT_MAX;
  for (int i = 0; i < (1 << 9); i++) {
    if (dpsta[i][0] == INT_MAX) continue;
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if ((i & ffsta[j]) == ffsta[j]) cnt++;
    }
    if (cnt > ans[0]) {
      ans[0] = cnt;
      ans[1] = dpsta[i][0];
      ans[2] = dpsta[i][1];
      ans[3] = dpsta[i][2];
    } else if (cnt == ans[0] && dpsta[i][0] < ans[1]) {
      ans[1] = dpsta[i][0];
      ans[2] = dpsta[i][1];
      ans[3] = dpsta[i][2];
    }
  }
  cout << ans[2] << " " << ans[3] << endl;
  return 0;
}
