#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int M = 1e6 + 10;
int rt[M], a[M], l[M], hh[M], tt[M], L[M], tot;
struct Moore {
int nw;
ll cnt;
Moore() {
nw = -1, cnt = 0;
}
void insert(int x) {
if (nw == x) cnt ++ ;
else if (cnt > 0) cnt -- ;
else nw = x, cnt = 1;
}
const Moore operator + (const Moore &b) const {
Moore t;
if (nw == b.nw) {
t.nw = nw, t.cnt = cnt + b.cnt;
return t;
}
if (cnt > b.cnt) {
t.nw = nw, t.cnt = cnt - b.cnt;
} else {
t.nw = b.nw, t.cnt = b.cnt - cnt;
}
return t;
}
};
struct Node {
int l, r;
Moore v;
} tr[22000000]; int total;
void pushup(int u) {
tr[u].v = tr[tr[u].l].v + tr[tr[u].r].v;
}
int merge(int x, int y, int l, int r) {
if (!x || !y) return x | y;
if (l == r) {
tr[x].v.nw = l, tr[x].v.cnt += tr[y].v.cnt;
return x;
}
int mid = l + r >> 1;
tr[x].l = merge(tr[x].l, tr[y].l, l, mid);
tr[x].r = merge(tr[x].r, tr[y].r, mid + 1, r);
tr[x].v = tr[tr[x].l].v + tr[tr[x].r].v;
return x;
}
void modify(int u, int l, int r, int x, int y) {
if (l == r) {
tr[u].v.nw = l, tr[u].v.cnt += y;
return;
}
int mid = l + r >> 1;
if (x <= mid) {
if (!tr[u].l) tr[u].l = ++ total;
modify(tr[u].l, l, mid, x, y);
} else {
if (!tr[u].r) tr[u].r = ++ total;
modify(tr[u].r, mid + 1, r, x, y);
}
pushup(u);
}
int ask(int u, int l, int r, int x) {
if (!u) return 0;
if (l == r) return tr[u].v.cnt;
int mid = l + r >> 1;
if (x <= mid) return ask(tr[u].l, l, mid, x);
else return ask(tr[u].r, mid + 1, r, x);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
total = n + q;
for (int i = 1; i <= n + q; i ++ ) rt[i] = i;
for (int i = 1; i <= n; i ++ ) {
scanf("%d", &l[i]);
for (int j = 0; j < l[i]; j ++ ) {
++ tot;
scanf("%d", &a[tot]);
if (!tt[i]) {
hh[i] = tt[i] = tot;
} else {
L[tot] = tt[i];
tt[i] = tot;
}
modify(rt[i], 0, 1e6, a[tot], 1);
}
}
while (q -- ) {
int op;
scanf("%d", &op);
if (op == 1) {
int x, y;
scanf("%d%d", &x, &y);
a[++ tot] = y;
if (!l[x]) {
hh[x] = tt[x] = tot;
} else {
L[tot] = tt[x], tt[x] = tot;
}
l[x] ++ ;
modify(rt[x], 0, 1e6, y, 1);
} else if (op == 2) {
int x;
scanf("%d", &x);
modify(rt[x], 0, 1e6, a[tt[x]], -1);
l[x] -- ;
tt[x] = L[tt[x]];
} else if (op == 3) {
int k;
scanf("%d", &k);
vector<int> x(k);
Moore t;
for (int i = 0; i < k; i ++ ) {
scanf("%d", &x[i]);
t = t + tr[rt[x[i]]].v;
}
int num = t.nw;
ll sum = 0, cnt = 0;
for (int i = 0; i < k; i ++ ) {
sum += l[x[i]];
cnt += ask(rt[x[i]], 0, 1e6, num);
}
if (cnt > sum / 2) {
printf("%d\n", num);
} else {
puts("-1");
}
} else {
int x1, x2, x3;
scanf("%d%d%d", &x1, &x2, &x3);
if (!l[x1] && !l[x2]) {
continue;
}
l[x3] = l[x1] + l[x2];
l[x1] = l[x2] = 0;
if (l[x1] && l[x2]) L[hh[x2]] = tt[x1];
if (l[x1]) hh[x3] = hh[x1];
else hh[x3] = hh[x2];
if (l[x2]) tt[x3] = tt[x2];
else tt[x3] = tt[x1];
rt[x3] = merge(rt[x1], rt[x2], 0, 1e6);
}
}
return 0;
}