Problem A: The Six-Letter Cipher

The Six-Letter Cipher is a method of encoding a secret message that involves both substitution and transposition. The encryption starts by randomly filling a $6\times 6$ grid with the alphabet letters from A to Z and the digits from 0 to 9 (36 symbols in total). This grid must be known to both the sender and receiver of the message. The rows and columns of the grid are labeled with the letters A, B, C, D, E, F, as in the following example:

  A B C D E F
A 8 P 3 D 1 N
B L T 4 O A H
C 7 K B C 5 Z
D J U 6 W G M
E X S V I R 2
F 9 E Y 0 F Q

The first stage of encryption is to take each letter of the message (ignoring spaces and punctuation signs), locate it in the grid and replace it with the row/column letters that label its' position. For the example, `8' is replaced by `AA' and `E' is replaced by `FB'.

Message: M E E T I N G A T 9 P M  
Stage 1 ciphertext: DF FB FB BB ED AF DE BE BB FA AB DF  

The second stage involves a secret keyword, also known to both sender and receiver, for example ``MARK''. The letters of this keyword are written on top of a fresh grid. Next, the text from the first stage is written underneath it, in row-wise order. Then we re-arrange the columns of the grid so that the letters of the keyword are in alphabetical order. The final ciphertext is obtained by reading the sorted grid column-wise from left to right.

M A R K
D F F B
F B B B
E D A F
D E B E
B B F A
A B D F
$\stackrel{\mbox{sort columns}}{\longrightarrow}$
A K M R
F B D F
B B F B
D F E A
E E D B
B A B F
B F A D
Final ciphertext: FBDEBBBBFEAFDFEDBAFBABFD

The final ciphertext contains only six different letters A, B, C, D, E, F (the grid labels), hence the name for this cipher.

Notice that in order to avoid ambiguity in the second stage transposition, the keyword should have no letters repeated. Also, the keyword length must divide the double of the message length so that we fill the grid completely.

Problem

Write a program that reads the grid, the keyword and a plaintext message and outputs the ciphertext.

Input specification

The input consists of the grid, keyword and message text, separated by newlines. The grid consists of six lines of six alphanumeric upper-case characters each. The keyword consists of upper-case alphanumeric characters without repetitions. The message text consists of upper-case alphanumeric characters.

You can assume that the keyword contains no repetitions and is of an acceptable length for the encryption process. You can also assume that the message text is at most 1000 characters length and the keyword is at most 80 characters length.

Output specification

The output should be the characters for the ciphertext, ended by a newline.

Sample input

8P3D1N
LT4OAH
7KBC5Z
JU6WGM
XSVIR2
9EY0FQ
MARK
MEETINGAT9PM

Sample output

FBDEBBBBFEAFDFEDBAFBABFD


ACM Programming Contest -- SWERC'2001 practice-session -- University of Porto, Portugal