#include <bits/stdc++.h>
using namespace std;
const long long cnt = 512;
long long pizza[cnt];
pair<int, int> masks[cnt];
int pers[cnt];
int nums[cnt];
vector<pair<int, int> > salt[cnt];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int first;
    cin >> first;
    int mask = 0;
    for (int j = 0; j < first; j++) {
      int b;
      cin >> b;
      b--;
      mask |= (1 << b);
    }
    pers[mask]++;
  }
  for (int i = 0; i < m; i++) {
    int c, r;
    cin >> c >> r;
    int mask = 0;
    for (int j = 0; j < r; j++) {
      int a;
      cin >> a;
      a--;
      mask |= (1 << a);
    }
    if (pizza[mask] == 0)
      pizza[mask] = c, nums[mask] = i + 1;
    else if (pizza[mask] > c)
      pizza[mask] = c, nums[mask] = i + 1;
    salt[mask].push_back({c, i + 1});
  }
  for (int i = 0; i < cnt; i++)
    if (salt[i].size() > 1) sort(salt[i].begin(), salt[i].end());
  int cost = INT_MAX, mx = -1, b1, b2;
  for (int i = 0; i < cnt; i++) {
    if (salt[i].size() < 2) continue;
    int cur = 0;
    for (int j = i; j > 0; j = ((j - 1) & i)) cur += pers[j];
    int c = salt[i][0].first + salt[i][1].first;
    if (cur > mx || (cur == mx && c < cost)) {
      mx = cur, cost = c;
      b1 = salt[i][0].second;
      b2 = salt[i][1].second;
    }
  }
  for (int i = 0; i < cnt; i++)
    for (int j = i + 1; j < cnt; j++) {
      if (pizza[i] && pizza[j]) {
        int mask = (i | j);
        if (masks[mask].first + masks[mask].second == 0) {
          masks[mask].first = i, masks[mask].second = j;
          continue;
        }
        if (pizza[masks[mask].first] + pizza[masks[mask].second] >
            pizza[i] + pizza[j])
          masks[mask].first = i, masks[mask].second = j;
      }
    }
  for (int i = 0; i < cnt; i++) {
    if (masks[i].first + masks[i].second == 0) continue;
    int cur = 0;
    for (int j = i; j > 0; j = ((j - 1) & i)) cur += pers[j];
    int c = pizza[masks[i].first] + pizza[masks[i].second];
    if (cur > mx || (cur == mx && c < cost)) {
      mx = cur, cost = c;
      b1 = nums[masks[i].first];
      b2 = nums[masks[i].second];
    }
  }
  cout << b1 << ' ' << b2 << '\n';
  return 0;
}
