# Flip-Flop the Squarelotron

Anita is the baby siter of Baron Von Hauser’ kids, a famous Physics Assistant of ITESM Campus Monterrey Physics department. As such Von Hauser kids have weird toys, all of which Anita has to master to be able to effectively entertain Baron’ Kids. While Anita was cleaning the bathtub she found a new toy. It is extremely weird, and posses a lot of mathematical symmetry, it is a Squarelotron game. She is determined to understand this new toy, otherwise she won’t be able to play with Von Hauser’ kids. However the complexity of such extreme toy makes it dificult to play. Thats why Anita asked the judges of this ITESM Campus Monterrey internal ACM programming contest eliminatory to put this problem, so that answers could be given by the best sttudents of computer science and engineering of this Campus. A Squarelotron consist basically of a matrix of numbers. However this matrix can be decomposed as squared rings which can rotate independently in 4 diferent ways, Upside-Down, Left-Rigt, through the Main Inverse Diagonal and through the Main Diagonal. For example Consider the following Squarelotrons. Squarelotron a) as 2 rings while squarelotron b) has 3. A Upside-Down Flip of the outmost ring of Squarelotron a) yields: A Left-Rigth Flip of the 2 ring of squarelotron b) yields:

2/4 A Flip through the Main Inverse Diagonal of the second ring of squarelotron a) yields: A Flip through the Main Diagonal of the outermost ring of squarelotron b) yields: Anita wants you to do a program which performs the following. She will give you a Squarelotron and your program will perform several of the flips described earlier for each of the rings of the given Squarelotron. The output of your program should be the final state of the Squarelotron. Input The first line contains the number M of different cases to process, consisting of blocks of lines. Each of these blocks consist of the following. The first line of each block contains a number N which describes the order N ×N of the Squarelotron. Following comes N lines of N numbers each, which describes the Squarelotron itself. Next comes an equal number of lines as number of rings of the Squarelotron. Each of these lines begins with a number T, followed by T numbers C which identifies the move to perform on the ring. All these numbers are in the following ranges:

3/4 • 0 < M ≤ 1000 • 1 ≤ N < 101 • 0 < T ≤ Number of Rings •1≤C<5 The numbers in the squarelotron are smaller than 216. The Moves are identifies as follows. ‘1’ means Upside-Down Flip, ‘2’ Means Left-Right Flip, ‘3’ means flip through the Main Diagonal, ‘4’ means a flip through the Main Inverse Diagonal. Output For each of the M cases, output a N × N squarelotron at the state it is supposed to be after all the moves. This squarelotron should be N lines with N numbers each. No blank line in between each case should be output. Sample Input 4 3 123 456 789 212 41234 4 1234 5678 9123 4566 212 234 5 12345 67891 23456 78912 34567 234 213 41324 6 123456 789123 456789 123456 789123 456789 241 223 11

4/4 Sample Output 987 654 321 6654 3219 8765 4321 76543 28377 69482 11596 54321 471471 528522 613413 796794 885285 936936