#include <bits/stdc++.h>
using namespace std;
const int vc = (1000000000 + 100) * 2;
int n, m;
int mp[100100];
pair<pair<int, int>, pair<int, int> > costB[(1 << 9)];
pair<int, pair<int, int> > costR[(1 << 9)];
int main() {
  for (int i = 0; i < (1 << 9); ++i) {
    mp[i] = vc;
    costB[i] = make_pair(make_pair(vc, vc), make_pair(vc, vc));
    costR[i] = make_pair(vc, make_pair(vc, vc));
  }
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int tt;
    scanf("%d", &tt);
    int mask = 0;
    for (int j = 0; j < tt; ++j) {
      int a;
      scanf("%d", &a);
      a--;
      mask |= (1 << a);
    }
    mp[i] = mask;
  }
  for (int i = 0; i < m; ++i) {
    int tt;
    int mask = 0;
    int cc;
    scanf("%d", &cc);
    scanf("%d", &tt);
    for (int j = 0; j < tt; ++j) {
      int a;
      scanf("%d", &a);
      a--;
      mask |= (1 << a);
    }
    if (costB[mask].first.first == vc) {
      costB[mask].first = make_pair(cc, i);
    } else if (costB[mask].second.first == vc) {
      if (costB[mask].first.first <= cc) {
        costB[mask].second = make_pair(cc, i);
      } else {
        costB[mask].second = costB[mask].first;
        costB[mask].first = make_pair(cc, i);
      }
    } else {
      if (cc < costB[mask].first.first) {
        costB[mask].second = costB[mask].first;
        costB[mask].first = make_pair(cc, i);
      } else if (cc < costB[mask].second.first) {
        costB[mask].second = make_pair(cc, i);
      }
    }
  }
  for (int i = 0; i < (1 << 9); ++i)
    for (int j = 0; j < (1 << 9); ++j)
      if (i != j && costB[i].first.first < vc && costB[j].first.first < vc) {
        int h = (i | j);
        costR[h] = min(
            costR[h],
            make_pair(costB[i].first.first + costB[j].first.first,
                      make_pair(costB[i].first.second, costB[j].first.second)));
      } else if (i == j && costB[i].first.first < vc &&
                 costB[i].second.first < vc) {
        costR[i] = min(costR[i],
                       make_pair(costB[i].first.first + costB[i].second.first,
                                 make_pair(costB[i].first.second,
                                           costB[i].second.second)));
      }
  int maxrr = -1;
  int maskIdx = 0;
  for (int i = 0; i < (1 << 9); ++i)
    if (costR[i].first < vc) {
      int rr = 0;
      for (int j = 0; j < n; ++j) {
        if ((mp[j] & i) == mp[j]) {
          rr++;
        }
      }
      if (maxrr < rr) {
        maxrr = rr;
        maskIdx = i;
      } else if (maxrr == rr && costR[maskIdx].first > costR[i].first) {
        maskIdx = i;
      }
    }
  printf("%d %d\n", costR[maskIdx].second.first + 1,
         costR[maskIdx].second.second + 1);
}
