Given \(n\) initially empty stacks, there are three types of operations:

1

`s``v`: Push the value \(v\) onto the top of the \(s\)-th stack.2

`s`: Pop the topmost value out of the \(s\)-th stack, and print that value. If the \(s\)-th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.-
3

`s``t`: Move every element in the \(t\)-th stack onto the top of the \(s\)-th stack in order.Precisely speaking, denote the original size of the \(s\)-th stack by \(S(s)\), and the original size of the \(t\)-th stack by \(S(t)\). Denote the original elements in the \(s\)-th stack from bottom to top by \(E(s,1), E(s,2), \dots, E(s,S(s))\), and the original elements in the \(t\)-th stack from bottom to top by \(E(t,1), E(t,2), \dots, E(t,S(t))\).

After this operation, the \(t\)-th stack is emptied, and the elements in the \(s\)-th stack from bottom to top becomes \(E(s,1), E(s,2), \dots, E(s,S(s)), E(t,1), E(t,2), \dots, E(t,S(t))\). Of course, if \(S(t) = 0\), this operation actually does nothing.

There are \(q\) operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

There are multiple test cases. The first line of the input contains an integer \(T\), indicating the number of test cases. For each test case:

The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 3 \times 10^5\)), indicating the number of stacks and the number of operations.

The first integer of the following \(q\) lines will be \(op\) (\(1 \le op \le 3\)), indicating the type of operation.

- If \(op = 1\), two integers \(s\) and \(v\) (\(1 \le s \le n\), \(1 \le v \le 10^9\)) follow, indicating an operation of the first type.
- If \(op = 2\), one integer \(s\) (\(1 \le s \le n\)) follows, indicating an operation of the second type.
- If \(op = 3\), two integers \(s\) and \(t\) (\(1 \le s, t \le n\), \(s \ne t\)) follow, indicating an operation of the third type.

It's guaranteed that neither the sum of \(n\) nor the sum of \(q\) over all test cases will exceed \(10^6\).

提交代码