#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
const int N = 2e5 + 100;
int n, m;
int cara[N];
int custo[N];
int sabor[N];
int quantos[N];
vector<int> baratao[N];
vector<int> atend[(1 << 9)];
bool compare(int a, int b) { return custo[a] < custo[b]; }
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < (1 << 9); i++)
    for (int j = 0; j < (1 << 9); j++)
      if ((i & j) == i) atend[i].push_back(j);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int cu;
      scanf("%d", &cu);
      cu--;
      cara[i] |= (1 << cu);
    }
    for (int x : atend[cara[i]]) {
      quantos[x]++;
    }
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &custo[i]);
    int k;
    scanf("%d", &k);
    while (k--) {
      int cu;
      scanf("%d", &cu);
      cu--;
      sabor[i] |= (1 << cu);
    }
    baratao[sabor[i]].push_back(i);
  }
  for (int i = 0; i < (1 << 9); i++)
    sort(baratao[i].begin(), baratao[i].end(), compare);
  int QNT = 0;
  int CU = 2e9 + 10;
  int I = 1;
  int J = 2;
  for (int i = 0; i < (1 << 9); i++) {
    for (int j = i; j < (1 << 9); j++) {
      if (i == j) {
        if (baratao[i].size() <= 1) continue;
        int qnt = quantos[(i | j)];
        if (QNT < qnt)
          QNT = qnt, I = baratao[i][0], J = baratao[i][1],
          CU = custo[baratao[i][0]] + custo[baratao[i][1]];
        else if (QNT == qnt) {
          if (custo[baratao[i][0]] + custo[baratao[i][1]] < CU)
            CU = custo[baratao[i][0]] + custo[baratao[i][1]], I = baratao[i][0],
            J = baratao[i][1];
        }
      } else {
        if (baratao[i].size() == 0 or baratao[j].size() == 0) continue;
        int qnt = quantos[(i | j)];
        if (QNT < qnt)
          QNT = qnt, I = baratao[i][0], J = baratao[j][0],
          CU = custo[baratao[i][0]] + custo[baratao[j][0]];
        else if (QNT == qnt) {
          if (custo[baratao[i][0]] + custo[baratao[j][0]] < CU)
            CU = custo[baratao[i][0]] + custo[baratao[j][0]], I = baratao[i][0],
            J = baratao[j][0];
        }
      }
    }
  }
  cout << I << " " << J << endl;
}
