Triumph (English)

2 secs 1024 MB
take44444's icon take44444

Problem Statement

There are NN cards for each of spades, clovers, diamonds, and hearts, and there are NN cards with numbers 11 to NN for each mark. For example, "1" of spade, "1" of clover, "1" of diamond, and "1" of heart are represented by "S1", "C1", "D1", and "H1", respectively. Initially, the cards are arranged in the order of {S1,...,SN,C1,...CN,D1,...,DN,H1,...,HN}\{S1,...,SN,C1,...CN,D1,...,DN,H1,...,HN\}.

"Shuffle" according to 'Q' queries. Specifically, in the i(1iQ)i (1 \leq i \leq Q)-th query, do the following:

  • LiL_i and RiR_i are given. Bring all the cards from card LiL_i to card RiR_i to the beginning while keeping their order. That is, when Li=aj,Ri=ak(1jk4N)L_i=a_j,R_i=a_k (1 \leq j \leq k \leq 4N) in the sequence {a1,,aj1,aj,,ak,ak+1...,a4N}\{a_1,…,a_{j-1},a_j,…,a_k,a_{k+1}...,a_{4N}\} of the card, the sequence is changed to {aj,,ak,a1,,aj1,ak+1,...a4N}\{a_j,…,a_k,a_1,…,a_{j−1},a_{k+1},...a_{4N}\}.

Process QQ queries in order, and Output the subsequence consisting only of all spade cards, the subsequence consisting only of all clover cards, the subsequence consisting only of all diamond cards, and the subsequence consisting only of all heart cards in this order.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Constraints

  • 1N1051 \leq N \leq {10}^5
  • 1Q1051 \leq Q \leq {10}^5

Input

N QN\ Q

Li Ri(1iQ)L_i\ R_i(1 \leq i \leq Q)

LiL_i and RiR_i are 4N4N kinds of character strings that can be made by arranging one character of "S" or "C" or "D" or "H" and the number j(1jN)j (1 \leq j \leq N) respectively.

Output

Output the subsequence consisting only of spade cards, the subsequence consisting only of clover cards, the subsequence consisting only of diamond cards, and the subsequence consisting only of heart cards in this order in 4 rows.

Sample

Sample Input 1

1 2
C1 D1
D1 S1

Sample Output 1

S1
C1
D1
H1

The initial sequence is {S1,C1,D1,H1}\{S1,C1,D1,H1\}. By the first query, C1C1D1D1 goes to the beginning, so it is changed to {C1,D1,S1,H1}\{C1,D1,S1,H1\}. By the second query, D1D1S1S1 goes to the beginning, so it is changed to {D1,S1,C1,H1}\{D1,S1,C1,H1\}. If N=1N=1, the output result is obvious because there is only one for each, no matter how you sort it.

Sample Input 2

3 2
C2 D1
S3 H3

Sample Output 2

S3 S1 S2
C1 C2 C3
D2 D3 D1
H1 H2 H3

The initial sequence is {S1,S2,S3,C1,C2,C3,D1,D2,D3,H1,H2,H3}\{S1, S2, S3, C1, C2, C3, D1, D2, D3, H1, H2, H3\}. By the first query, C2C2D1D1 goes to the beginning, so it is changed to {C2,C3,D1,S1,S2,S3,C1,D2,D3,H1,H2,H3}\{C2, C3, D1, S1, S2, S3, C1, D2, D3, H1, H2, H3\}. By the first query, S3S3H3H3 goes to the beginning, so it is changed to {S3,C1,D2,D3,H1,H2,H3,C2,C3,D1,S1,S2}\{S3, C1, D2, D3, H1, H2, H3, C2, C3, D1, S1, S2\}. Subsequences consisting of only each mark are {S3,S1,S2}\{S3,S1,S2\} for spades, {C1,C2,C3}\{C1,C2,C3\} for clovers, {D2,D3,D1}\{D2,D3,D1\} for diamonds, and {H1,H2,H3}\{H1,H2,H3\} for hearts.

Sample Input 3

2 1
C2 C2

Sample Output 3

S1 S2
C2 C1
D1 D2
H1 H2

In some cases, LiL_i and RiR_i are equal. In that case, bring that one card to the beginning.

Submit


Go (1.21)