#include <bits/stdc++.h>
using namespace std;
int n, m;
int cnt[1050];
vector<pair<int, int> > mask[1050];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int k, x;
    scanf("%d", &k);
    int cur_mask = 0;
    for (int j = 1; j <= k; j++) {
      scanf("%d", &x);
      cur_mask = cur_mask + (1 << (x - 1));
    }
    cnt[cur_mask]++;
  }
  for (int i = 1; i <= m; i++) {
    int cost, x, k;
    scanf("%d %d", &cost, &k);
    int cur_mask = 0;
    for (int j = 1; j <= k; j++) {
      scanf("%d", &x);
      cur_mask = cur_mask + (1 << (x - 1));
    }
    mask[cur_mask].push_back({cost, i});
  }
  for (int i = 0; i < 512; i++) {
    sort(mask[i].begin(), mask[i].end());
    while (((int)(mask[i].size())) > 2) mask[i].pop_back();
  }
  int answer = -1;
  int min_cost = 0;
  pair<int, int> fin_pos = {-1, -1};
  for (int i = 0; i < 512; i++)
    for (int j = i; j < 512; j++) {
      if (((int)(mask[i].size())) == 0 || ((int)(mask[j].size())) == 0)
        continue;
      if (i == j && ((int)(mask[i].size())) < 2) continue;
      int cost1 = 0;
      int cost2 = 0;
      pair<int, int> c_pos;
      if (i == j) {
        cost1 = mask[i][0].first;
        cost2 = mask[i][1].first;
        c_pos = {mask[i][0].second, mask[i][1].second};
      } else {
        cost1 = mask[i][0].first;
        cost2 = mask[j][0].first;
        c_pos = {mask[i][0].second, mask[j][0].second};
      }
      int count = 0;
      int t_mask = i | j;
      for (int k = 0; k < 512; k++) {
        if ((t_mask | k) == t_mask) {
          count = count + cnt[k];
        }
      }
      if (count > answer) {
        answer = count;
        fin_pos = c_pos;
        min_cost = cost1 + cost2;
      } else if (count == answer && cost1 + cost2 < min_cost) {
        fin_pos = c_pos;
        min_cost = cost1 + cost2;
      }
    }
  printf("%d %d", fin_pos.first, fin_pos.second);
  return 0;
}
