#include <bits/stdc++.h>
using namespace std;
int MAX = 2e9;
int main() {
  vector<int> friends(1 << 9, 0);
  vector<pair<int, int>> pizza(1 << 9, {-1, MAX});
  vector<pair<int, int>> pizza2(1 << 9, {-1, MAX});
  int n, m, f, c, a, combo;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    combo = 0;
    scanf(" %d", &f);
    for (int j = 0; j < f; ++j) {
      scanf(" %d", &a);
      --a;
      combo |= (1 << a);
    }
    ++friends[combo];
  }
  for (int i = (1 << 9) - 1; i >= 0; --i) {
    if (friends[i] == 0) continue;
    for (int j = 0; j < (1 << 9); ++j) {
      if (i != j && (i & (~j)) == 0) {
        friends[j] += friends[i];
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    combo = 0;
    scanf(" %d %d", &c, &f);
    for (int j = 0; j < f; ++j) {
      scanf(" %d", &a);
      --a;
      combo |= (1 << a);
    }
    if (c < pizza2[combo].second) {
      if (c < pizza[combo].second) {
        pizza2[combo] = pizza[combo];
        pizza[combo] = {i, c};
      } else {
        pizza2[combo] = {i, c};
      }
    }
  }
  pair<int, int> idxs = {0, 0};
  int best = -1;
  int cost = 0;
  for (int i = 0; i < (1 << 9); ++i) {
    if (pizza[i].first == -1) continue;
    if (pizza2[i].first != -1) {
      combo = i;
      c = pizza[i].second + pizza2[i].second;
      if (friends[combo] > best || ((friends[combo] == best) && c < cost)) {
        best = friends[combo];
        cost = c;
        idxs = {pizza[i].first, pizza2[i].first};
      }
    }
    for (int j = i + 1; j < (1 << 9); ++j) {
      if (pizza[j].first == -1) continue;
      combo = i | j;
      c = pizza[i].second + pizza[j].second;
      if (friends[combo] > best || ((friends[combo] == best) && c < cost)) {
        best = friends[combo];
        cost = c;
        idxs = {pizza[i].first, pizza[j].first};
      }
    }
  }
  printf("%d %d", idxs.first + 1, idxs.second + 1);
}
