#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a) * (a))
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
template<class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
return out << "(" << a.x << ", " << a.y << ")";
}
template<class A> ostream& operator << (ostream& out, const vector<A> &a) {
out << "[";
for (auto it = a.begin(); it != a.end(); ++it) {
if (it != a.begin())
out << ", ";
out << *it;
}
return out << "]";
}
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
const int INF = 1e9;
const li INF64 = 1e18;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;
mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));
const int N = 100 * 1000 + 11;
const int MSK = 1 << 9;
int n, m;
int cnt[MSK];
vector<pt> pz[N];
bool read() {
if (scanf("%d %d", &n, &m) != 2)
return false;
forn(i, n) {
int k;
scanf("%d", &k);
int msk = 0;
forn(j, k) {
int pos;
scanf("%d", &pos);
--pos;
msk |= (1 << pos);
}
++cnt[msk];
}
return true;
}
void solve() {
forn(i, m) {
int c;
scanf("%d", &c);
int msk = 0;
int k;
scanf("%d", &k);
forn(j, k) {
int pos;
scanf("%d", &pos);
--pos;
msk |= (1 << pos);
}
pz[msk].pb(mp(c, i));
sort(all(pz[msk]));
while (sz(pz[msk]) > 2) pz[msk].pop_back();
}
int ans = 0, cost = 2 * INF + 1;
pt res = mp(-1, -1);
forn(p1, MSK) forn(p2, MSK) {
int curcost = 0;
int pos1, pos2;
if (p1 == p2) {
if (sz(pz[p1]) < 2) continue;
curcost = pz[p1][0].x + pz[p1][1].x;
pos1 = pz[p1][0].y;
pos2 = pz[p1][1].y;
} else {
if (sz(pz[p1]) == 0 || sz(pz[p2]) == 0) continue;
curcost = pz[p1][0].x + pz[p2][0].x;
pos1 = pz[p1][0].y;
pos2 = pz[p2][0].y;
}
int curans = 0;
int curmask = p1 | p2;
forn(pp, MSK) {
if ((curmask & pp) == pp) {
curans += cnt[pp];
}
}
if (curans > ans || (curans == ans && curcost < cost)) {
ans = curans;
cost = curcost;
res = mp(pos1, pos2);
}
}
cout << res.x + 1 << " " << res.y + 1 << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int tt = clock();
#endif
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
#ifdef _DEBUG
while (read()) {
#else
if (read()) {
#endif
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
}