#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXF = 1 << 9;
int N, M;
vector<pair<int, int> > Pizza[MAXF];
vector<int> PizzaMasks;
vector<int> Friend(MAXF);
vector<int> FriendMask;
void read() {
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    int cnt;
    cin >> cnt;
    int mask = 0;
    while (cnt--) {
      int x;
      cin >> x;
      x--;
      mask |= (1 << x);
    }
    if (Friend[mask] == 0) FriendMask.push_back(mask);
    Friend[mask]++;
  }
  for (int i = 1; i <= M; i++) {
    int cnt, cost;
    cin >> cost >> cnt;
    int mask = 0;
    while (cnt--) {
      int x;
      cin >> x;
      x--;
      mask |= (1 << x);
    }
    if (Pizza[mask].size() == 0) PizzaMasks.push_back(mask);
    Pizza[mask].push_back(make_pair(cost, i));
  }
}
void simplify() {
  for (int i = 0; i < MAXF; i++) {
    if (Pizza[i].empty()) continue;
    sort(Pizza[i].begin(), Pizza[i].end());
    while (Pizza[i].size() > 2) Pizza[i].pop_back();
  }
}
bool isSubMask(int mask, int submask) {
  for (int i = 0; i < MAXF; i++) {
    if (((1 << i) & submask) && !((1 << i) & mask)) return false;
  }
  return true;
}
int countFriends(int mask) {
  int cnt = 0;
  for (auto friendMask : FriendMask) {
    cnt += isSubMask(mask, friendMask);
  }
  return cnt;
}
vector<pair<int, int> > toBeCheckedFrom(MAXF);
vector<int> toBeCheckedCost(MAXF);
vector<int> availableMasks;
void resolve();
void solve() {
  int nrFriends = -1;
  int minCost = 1e9;
  pair<int, int> sol = {-1, -1};
  for (auto mask1 : PizzaMasks)
    for (auto mask2 : PizzaMasks) {
      if (mask1 == mask2 && Pizza[mask1].size() != 2) continue;
      int mask = mask1 | mask2;
      int cost = 0;
      pair<int, int> from = {-1, -1};
      if (mask1 == mask2) {
        cost = Pizza[mask1][0].first + Pizza[mask1][1].first;
        from = {Pizza[mask1][0].second, Pizza[mask1][1].second};
      } else {
        cost = Pizza[mask1][0].first + Pizza[mask2][0].first;
        from = {Pizza[mask1][0].second, Pizza[mask2][0].second};
      }
      if (toBeCheckedCost[mask] == 0) {
        toBeCheckedCost[mask] = cost;
        toBeCheckedFrom[mask] = from;
        availableMasks.push_back(mask);
      } else if (cost < toBeCheckedCost[mask]) {
        toBeCheckedCost[mask] = cost;
        toBeCheckedFrom[mask] = from;
      }
    }
  resolve();
}
void resolve() {
  int maxFriends = -1;
  int cost = 1e9;
  pair<int, int> from = {-1, -1};
  for (auto mask : availableMasks) {
    int nrFriends = 0;
    for (int submask = mask; submask; submask = (submask - 1) & mask) {
      nrFriends += Friend[submask];
    }
    if (nrFriends > maxFriends ||
        (nrFriends == maxFriends && toBeCheckedCost[mask] < cost)) {
      maxFriends = nrFriends;
      cost = toBeCheckedCost[mask];
      from = toBeCheckedFrom[mask];
    }
  }
  cout << from.first << " " << from.second;
}
int main() {
  read();
  simplify();
  solve();
  return 0;
}
