This page is still under construction.

Parts of this page are still being built. What you see may change.

Cubic Art

Time limit1sMemory limit1024 MB

Summary
Given a cube state and a move sequence, apply point updates that replace one move and print the resulting cube state after each update.
Level

Medium7 of 10

Topics
Segment tree, Simulation, Matrix, Implementation
Solved
No attempts yet

Problem

Modern art is unpredictable. While tidying his room, Bob found his old Rubik's cube. Then the moment came. He closed his eyes, listened to his inner voice, made a few moves (up to 65000 of them), and the masterpiece was nearly complete. The final state was not to his liking, though. He realized that he had done some of the moves wrong. If only he could go back in time and change them.

All he needs now is a few changes (again, up to 65000 of them). Each change replaces one move with some other move. Bob wants to see what each change does, but repeating the entire sequence of moves over and over is tedious.

You are given the initial state of Bob's cube. The cube is not necessarily solved in its initial state. You are also given the original sequence of moves Bob performed.

Finally, you are given a sequence of changes. Each change has the form "change the kk-th move into this new move". For each change, print the state of the cube at the end of the entire sequence of moves.

The changes are permanent. The second change applies to the sequence of moves that already carries the first change, not to the original sequence.

Input

The colors of the cube are A, B, C, D, E, F. The middle squares of the faces do not move while you play with the cube, so A is always the color of the center of the top face, B, C, D, E are the centers of the side faces (in order), and F is the center of the bottom face. The surface of the cube unfolds into the following form.

???
?A?
???
????????????
?B??C??D??E?
????????????
???
?F?
???

The top face is attached to the upper edge of the B face, the bottom face is attached to the lower edge of the B face, and the middle band goes once around the cube in the order B, C, D, E.

The first 9 lines of the input contain the starting state of the cube in the form above. The centers of the six faces are colored as shown.

The next line has two integers nn and mm. nn is the number of moves and mm is the number of subsequent changes.

The next nn lines describe Bob's original moves. They have the form "CiC_i did_i", where CiC_i is the color of the center of the rotated face and did_i is −1-1 for a clockwise move and 11 for a counterclockwise move. The direction is the one you see when you look at that face from outside the cube.

The last mm lines describe the changes, in order. Each one has the form "aja_j CjC_j djd_j", where aja_j is the 1-based index of the move that is being replaced and CjC_j djd_j describes the new move.

In all test cases, 1≤n,m≤650001 \le n, m \le 65000 and 1≤aj≤n1 \le a_j \le n.

Output

Let SiS_i be the sequence of moves obtained from the original sequence by applying the first ii changes. For each ii between 11 and mm, inclusive, print 9 lines: the final state of the cube obtained by starting in the initial configuration and performing the sequence of moves SiS_i. Use the same format as in the input.

Hint

In the first example the original moves cancel each other out, so the cube is back in its initial state at the end of the original sequence. After all four changes of that example are applied, the resulting sequence of moves changes the color of every square except the centers of the six faces.

Examples3

  1. Example 1

    Input
    AAA
    AAA
    AAA
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    FFF
    FFF
    FFF
    8 4
    E 1
    E -1
    F 1
    F -1
    B 1
    B -1
    E 1
    E -1
    8 C -1
    2 C -1
    6 D -1
    4 A -1
    
    Expected output
    BAB
    BAB
    BAB
    FBFCCCADAEEE
    FBFCCCADAEEE
    FBFCCCADAEEE
    DFD
    DFD
    DFD
    FAF
    FAF
    FAF
    DBDCCCBDBEEE
    DBDCCCBDBEEE
    DBDCCCBDBEEE
    AFA
    AFA
    AFA
    FCF
    BAB
    FCF
    EFEDFDCACBAB
    DBDCCCBDBEEE
    EFEDFDCACBAB
    AEA
    DFD
    AEA
    CCC
    CAC
    CCC
    FFFDDDAAABBB
    FBFDCDADABEB
    FFFDDDAAABBB
    EEE
    EFE
    EEE
    
  2. Example 2

    Input
    AAA
    AAA
    AAA
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    FFF
    FFF
    FFF
    1 1
    A -1
    1 A 1
    
    Expected output
    AAA
    AAA
    AAA
    EEEBBBCCCDDD
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    FFF
    FFF
    FFF
    
  3. Example 3

    Input
    AAA
    AAA
    AAA
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    BBBCCCDDDEEE
    FFF
    FFF
    FFF
    3 3
    B 1
    C -1
    F 1
    2 C 1
    1 B -1
    3 F -1
    
    Expected output
    AAD
    AAD
    CCD
    BBACCCFDDEEA
    BBACCCFDDEEA
    FFFEDDEEABBC
    BBB
    EFF
    EFF
    AAD
    AAD
    EED
    BBACCCFDDEEF
    BBACCCFDDEEF
    AAACDDEEFBBE
    BBB
    CFF
    CFF
    AAD
    AAD
    EED
    BBACCCFDDEEF
    BBACCCFDDEEF
    EEFBBEAAACDD
    FFC
    FFC
    BBB