#include <bits/stdc++.h>
using namespace std;
int bit[2005];
int bm[200005], c[200005];
vector<pair<int, int> > v[2005];
int main() {
  int n, m, f, x;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &f);
    int sum = 0;
    for (int j = 0; j < f; j++) {
      scanf("%d", &x);
      x--;
      sum |= (1 << x);
    }
    bit[sum]++;
  }
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < (1 << 9); j++) {
      if ((j >> i) & 1) bit[j] += bit[j ^ (1 << i)];
    }
  }
  int b = (1 << 9) - 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &c[i]);
    scanf("%d", &f);
    int sum = 0;
    for (int j = 0; j < f; j++) {
      scanf("%d", &x);
      x--;
      sum |= (1 << x);
    }
    bm[i] = sum;
    v[b ^ sum].push_back(pair<int, int>(c[i], i));
  }
  for (int i = 0; i < (1 << 9); i++) {
    sort(v[i].begin(), v[i].end());
  }
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < (1 << 9); j++) {
      if ((j >> i) & 1) {
        int t = j ^ (1 << i);
        for (int k = 0; k < v[t].size(); k++) v[j].push_back(v[t][k]);
      }
    }
  }
  for (int i = 0; i < (1 << 9); i++) {
    sort(v[i].begin(), v[i].end());
  }
  int mx = -1, mn = 2e9 + 1;
  int i1, i2;
  for (int i = 1; i <= m; i++) {
    int t = bm[i];
    for (int j = 0; j < (1 << 9); j++) {
      if ((t & j) == t) {
        int tx = b ^ (j ^ t);
        int co = c[i], ii = -1;
        for (int k = 0; k < v[tx].size(); k++) {
          if (v[tx][k].second == i) continue;
          co += v[tx][k].first;
          ii = v[tx][k].second;
          break;
        }
        if (ii == -1) continue;
        if (mx < bit[j] || (mx == bit[j] && co < mn)) {
          mx = bit[j];
          mn = co;
          i1 = i;
          i2 = ii;
        }
      }
    }
  }
  printf("%d %d\n", i1, i2);
}
