Mail System Design
Submissions: 3895   Accuracy: 19.21%   Difficulty: Medium   Marks: 4

Hattori is very much inspired by the way GMAIL works. He decides to build his own simple version of GMAIL. He divides the mails into 3 categories ,namely : UNREAD , READ and TRASH.
TRASH: The messages deleted by the user.
Now, At any point of time, The user can READ an UNREAD message , Move an UNREAD message to TRASH , Move a READ message to TRASH or restore a deleted message back to READ category. Now , Hattori requires your help in determining the messages left in all the categories aligned in the order of their arrival in that category.
Formally: You are given N messages , with ID from 1 to N. Initially all the messages are in the UNREAD section of the mail.Now, Q queries are given in the form as shown below:
1 X : Move the message with ID X from UNREAD to READ.
2 X : Move the message with ID X from READ to TRASH.
3 X : Move the message with ID X from UNREAD to TRASH.
4 X : Move the message with ID X from TRASH to READ.
Given that all the queries are valid, Help Hattori in Determining the messages in all the 3 sections.

Input:
First line contains the number of test cases. First line of each test case contains two space separated values N and Q respectively. Second line contains 2*Q integers each either of the form as described above.

Output:
First line contains all the space seperated message ID'S left in the UNREAD section.
Second line contains all the space seperated message ID'S left in the READ section.
Third line contains all the space seperated message ID'S left in the TRASH section.
NOTE: In case, any section is empty , print "EMPTY" for that section without double quotes.

Constraints:
1<=T<=20
1<=N<=5000
1<=Q<=10^6

Example:
Input:

1
10 7
1 2 1 5 1 7 1 9 2 5 2 7 4 5
Output:
1 3 4 6 8 10
2 9 5
7

Explaination:
TRASH section : Empty
Query 1 : 1 2
TRASH section : Empty
Query 2 : 1 5
TRASH section : Empty
Query 3 : 1 7
TRASH section : Empty
Query 4 : 1 9
TRASH section : Empty
Query 5 : 2 5
TRASH section : 5
Query 6 : 2 7
TRASH section : 5->7
Query 7 : 4 5
TRASH section : 7

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: RAJAT_GOEL
Author: rajat1140911

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.