Under construction

Still being built. Things may change

Ancient Plates

Time limit1sMemory limit128 MB

Summary
Apply given PERM and SHIFT rules to lines, words, or characters of a block of text, then print the result.
Level

Easy3 of 10

Topics
Implementation, Simulation, Array
Solved
No attempts yet

Problem

Some decades ago, archaeologists discovered plates covered with text in an ancient temple. The individual symbols were already known, but how the text as a whole had been encrypted remained a mystery for a long time. Last year, however, a second temple was discovered in the same area. Its interior closely resembled the first, so it is believed to have been built at the same time. This second temple also contained plates, but their contents looked quite different. After a year of research, the archaeologists proposed the theory that these plates hold the rules (the code) needed to decipher the text found in the first temple.

Your task is to write a program that automates deciphering the text on the plates by applying the rules proposed by the archaeologists.

There are two kinds of rules: PERM and SHIFT. Both apply to one of three targets: lines of the text (L), words (W), or characters (C).

A PERM rule consists of the string PERM, a single blank, the letter denoting the target, and one or more index pairs separated by blanks. Each index pair i,j is two positive integers ii and jj joined by a single comma.

Depending on the target, for each pair i,ji, j — processed from left to right — the PERM rule does the following:

  • L: exchange line ii and line jj.
  • W: exchange word ii and word jj in every line.
  • C: exchange character ii and character jj in every word of every line.

If at least one of the objects to be exchanged does not exist, that exchange is skipped. For example, PERM W 2,7 asks to exchange the 2nd and 7th word in every line, but some line may have only five words; in that line nothing is done, while the exchange is still performed in every other line that has at least seven words.

A SHIFT rule consists of SHIFT, a single blank, the target letter, a single blank, and an integer kk. The rule shifts the target ∣k∣|k| times: to the left (upward for lines) when k>0k > 0, and to the right (downward for lines) when k<0k < 0. A single shift works as follows:

  • L: shifting left (up) moves every line up by one, and the first line becomes the last line; shifting right (down) moves every line down by one, and the last line becomes the first line.
  • W: shifting left moves the words of each line one position to the left, and the first word becomes the last word; shifting right does the opposite.
  • C: shifting left moves the characters of each word one position to the left, and the first character becomes the last character; shifting right does the opposite.

If k=0k = 0, no shift is performed.

Input

The first line contains the number of scenarios, i.e. the number of plates to decipher.

For each plate you are first given, on a single line, the number of text lines ll (0≤l≤1000)(0 \le l \le 1000) to decipher. The next ll lines contain that text, and no line exceeds 1000 characters. After that, one line contains the number of rules rr (0≤r≤1000)(0 \le r \le 1000) to apply, and the following rr lines contain those rules in the format described above.

The words of the text are sequences of arbitrary characters (not just letters!) separated by single blanks. No line of the given text starts or ends with a blank, but a line may be empty altogether.

Output

Begin each scenario's output with a line Scenario #i:, where ii is the scenario number starting at 1. Then print the ll lines of text after all rules have been applied. Separate two consecutive scenarios with a single blank line.

Examples1

  1. Example 1

    Input
    2
    3
    First line.
    Second line.
    Third line.
    1
    SHIFT L -1
    6
    ettypr ante e.aid era a
    lsemi is an btiingOr itsh at a satncedi of uhglyro nten-twoyi lilonmi
    rgeurdedan lolwye ns.u setrnwe iarlsp mra of eht lxayGa elsi a allsm
    oeswh ed-dsceneedap flei trelyut sgiaificnntin tlteli ubel enegr aentpl
    dne of eht raF tuo in eht cahntedur cwkbtersaa of eht fsabionahleun
    iknth gtialdi thceswa rsmfo era so aizmglyan iimrivept atth etyh illst
    5
    SHIFT W 3
    PERM C 4,9 2,3
    SHIFT C -2
    SHIFT L 1
    PERM L 3,9 3,4 1,3
    
    Expected output
    Scenario #1:
    Third line.
    First line.
    Second line.
    
    Scenario #2:
    Far out in the uncharted backwaters of the unfashionable end of the
    western spiral arm of the Galaxy lies a small unregarded yellow sun.
    Orbiting this at a distance of roughly ninety-two million miles is an
    utterly insignificant little blue green planet whose ape-descended life
    forms are so amazingly primitive that they still think digital watches
    are a pretty neat idea.