#include #include #include #include using namespace std; void getint(int &x) { int ch = x = 0; while (!isdigit(ch = getchar())) ; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; } const int N = 3e5 + 10; list S[N]; int main() { int T, n, q, op, s, v, t; getint(T); while (T--) { getint(n), getint(q); for (int i = 1; i <= n; i++) S[i].clear(); while (q--) { getint(op); if (op == 1) { getint(s), getint(v); S[s].push_back(v); } if (op == 2) { getint(s); if (S[s].empty()) puts("EMPTY"); else printf("%d\n", S[s].back()), S[s].pop_back(); } if (op == 3) { getint(s), getint(t); S[s].splice(S[s].end(), S[t]); } } } return 0; }