#include <bits/stdc++.h>
using namespace std;
const int n = 520;
const long long INF = 1000000000000000ll;
int N, M;
int PC[n];
pair<long long, int> CP[n], CP2[n];
pair<long long, pair<int, int>> PP[n];
int PH[n];
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    int f;
    cin >> f;
    int m = 0;
    for (int j = 0; j < f; ++j) {
      int b;
      cin >> b;
      m |= 1 << (b - 1);
    }
    PH[m]++;
  }
  for (int i = 0; i < 513; ++i) {
    CP2[i] = CP[i] = {INF, -1};
    PP[i] = {INF, {-1, -1}};
  }
  for (int i = 0; i < M; ++i) {
    int c, r, a;
    cin >> c >> r;
    int m = 0;
    for (int j = 0; j < r; ++j) {
      cin >> a;
      m |= 1 << (a - 1);
    }
    PC[m]++;
    if (c < CP[m].first) {
      CP2[m] = CP[m];
      CP[m] = {c, i};
    } else if (c < CP2[m].first) {
      CP2[m] = {c, i};
    }
  }
  for (int i = 0; i < 512; ++i) {
    for (int j = i; j < 512; ++j) {
      if (i == j) {
        int m = i;
        long long c = CP[i].first + CP2[j].first;
        if (c < PP[m].first) {
          PP[m] = {c, {CP[i].second, CP2[j].second}};
        }
      } else {
        int m = i | j;
        long long c = CP[i].first + CP[j].first;
        if (c < PP[m].first) {
          PP[m] = {c, {CP[i].second, CP[j].second}};
        }
      }
    }
  }
  long long bestH = 0, bestC = 2000000002, bestI = -1;
  for (int i = 0; i < 512; ++i) {
    int ph = 0;
    for (int j = 0; j < 512; ++j) {
      if ((i | j) == i) {
        ph += PH[j];
      }
    }
    if (PP[i].first < INF &&
        (ph > bestH || (ph == bestH && PP[i].first < bestC))) {
      bestH = ph;
      bestC = PP[i].first;
      bestI = i;
    }
  }
  cout << PP[bestI].second.first + 1 << " " << PP[bestI].second.second + 1
       << endl;
  return 0;
}
