#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int bestpizza[1 << 9];
int bestpizza2[1 << 9];
int idbestpizza[1 << 9];
int idbestpizza2[1 << 9];
int client[1 << 9];
int subset[1 << 9];
int combo1[1 << 9];
int combo2[1 << 9];
int pricecombo[1 << 9];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int nr, mask = 0;
    scanf("%d", &nr);
    for (int j = 0; j < nr; ++j) {
      int x;
      scanf("%d", &x);
      x--;
      mask = (mask | (1 << x));
    }
    client[mask]++;
  }
  for (int i = 0; i < m; ++i) {
    int nr, mask = 0;
    int price;
    scanf("%d%d", &price, &nr);
    for (int j = 0; j < nr; ++j) {
      int x;
      scanf("%d", &x);
      x--;
      mask = (mask | (1 << x));
    }
    if (bestpizza[mask] == 0) {
      bestpizza[mask] = price;
      idbestpizza[mask] = i + 1;
    } else if (price < bestpizza[mask]) {
      bestpizza2[mask] = bestpizza[mask];
      idbestpizza2[mask] = idbestpizza[mask];
      bestpizza[mask] = price;
      idbestpizza[mask] = i + 1;
    } else if (bestpizza2[mask] == 0) {
      bestpizza2[mask] = price;
      idbestpizza2[mask] = i + 1;
    } else if (price < bestpizza2[mask]) {
      bestpizza2[mask] = price;
      idbestpizza2[mask] = i + 1;
    }
  }
  for (int i = 0; i < (1 << 9); ++i)
    for (int j = 0; j < (1 << 9); ++j) {
      int rez = (i | j);
      if (i == j && bestpizza[i] != 0 && bestpizza2[i] != 0) {
        if (combo1[rez] == 0) {
          combo1[rez] = idbestpizza[i];
          combo2[rez] = idbestpizza2[i];
          pricecombo[rez] = bestpizza[i] + bestpizza2[j];
        } else if (bestpizza[i] + bestpizza2[i] < pricecombo[rez]) {
          combo1[rez] = idbestpizza[i];
          combo2[rez] = idbestpizza2[i];
          pricecombo[rez] = bestpizza[i] + bestpizza2[j];
        }
      } else if (i != j && bestpizza[i] != 0 && bestpizza[j] != 0) {
        if (combo1[rez] == 0) {
          combo1[rez] = idbestpizza[i];
          combo2[rez] = idbestpizza[j];
          pricecombo[rez] = bestpizza[i] + bestpizza[j];
        } else if (bestpizza[i] + bestpizza[j] < pricecombo[rez]) {
          combo1[rez] = idbestpizza[i];
          combo2[rez] = idbestpizza[j];
          pricecombo[rez] = bestpizza[i] + bestpizza[j];
        }
      }
    }
  int sat, nr1, nr2, bestprice;
  sat = -1;
  nr1 = nr2 = 0;
  bestprice = -1;
  for (int mask = 0; mask < (1 << 9); ++mask) {
    for (int mask2 = mask; mask2 > 0; mask2 = (mask2 - 1) & mask)
      subset[mask] += client[mask2];
    if (combo1[mask] != 0 && subset[mask] > sat) {
      sat = subset[mask];
      nr1 = combo1[mask];
      nr2 = combo2[mask];
      bestprice = pricecombo[mask];
    } else if (combo1[mask] != 0 && subset[mask] == sat &&
               pricecombo[mask] < bestprice) {
      sat = subset[mask];
      nr1 = combo1[mask];
      nr2 = combo2[mask];
      bestprice = pricecombo[mask];
    }
  }
  printf("%d %d", nr1, nr2);
  return 0;
}
