PRINTMOVE - Move it, please!


One day, Lius tried to work at a database company. At that time, he was asked to handle data in a form of a matrix of NxM. At first, the data were numbered from 1 to N*M and sorted from top-left to the bottom-right. Then, his boss started to give him some commands to shift the data. Lius had to do all of his boss command(s) one by one and previewing it to him before finishing another command. There are 4 types of commands:

  • "up" : Shift the data 1 row up, the data in top row is moved to the bottom row.
  • "down" : Shift the data 1 row below, the data in bottom row is moved to the top row.
  • "left" : Shift the data 1 columns to the left, the data in the first column is moved to the last column.
  • "right" : Shift the data 1 columns to the right, the data in the last column is moved to the first column.
At first, Lius tried to finish it manually. After a few hours, he realized that the data were too many for him to handle manually, so he felt frustated. Yet, he remembered that he has a best friend who is an experienced programmer , so he asked you to create a program to help him finish his work.

Note: Put zeroes before the data numbers if the digits of the numbers are less than the digit of the largest number in the matrix (see input & output examples for clarification).
PS: My fastest time is 0.15s :D

Input

First line of input consists of integer N and M (1 ≤ N*M ≤ 10^3), denoting the number of rows and columns of the matrix. The next line of input is an integer C (0 ≤ C ≤ 10^3), denoting the number of commands that should be done, followed by C lines, the orders that should be done by the program.

Output

First output the initial value of the matrix of N x M,followed by a newline. After that, for each commands print"Command #X:",where X (1 ≤ X ≤ C) is the command number, followed by a newline. After that, print the new set of data after affected by the command(s) given by his boss. There must be no trailing spaces at the end of printed lines, neither empty characters. Print a newline after each testcase.

Example

Input:
3 4
5
up
down
left
right
down

Output:
01 02 03 04
05 06 07 08
09 10 11 12
Command #1:
05 06 07 08
09 10 11 12
01 02 03 04
Command #2:
01 02 03 04
05 06 07 08
09 10 11 12
Command #3:
02 03 04 01
06 07 08 05
10 11 12 09
Command #4:
01 02 03 04
05 06 07 08
09 10 11 12
Command #5:
09 10 11 12
01 02 03 04
05 06 07 08

Warning: large Input/Output data, be careful with certain languages!

hide comments
hanstan: 2016-12-06 15:19:41

Solve http://www.spoj.com/problems/PRINTMOVE2/ after solving this one for additional challenge :D

vengatesh15: 2016-11-15 14:49:50

Easy one...
But read the output format carefully that cost me 3 WA

Last edit: 2016-11-15 14:50:14

Added by:hanstan
Date:2016-05-17
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:Self