The Playfair cipher is a manual symmetric encryption technique and was the first digraph substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair, who promoted the use of the cipher.
The Playfair cipher uses a 5×5 table containing each letter of the English alphabet exactly once (except 'Q', which is omitted). This table is the encryption key. To make the table easier to remember, it is usually generated from a key phrase. First fill the empty table with the letters of the key phrase (skipping spaces and any letter that already appears), then fill the remaining cells with the rest of the alphabet in order. The key phrase is written into the top rows of the table, from left to right. For example, if the key phrase is 'playfair example', the encryption key becomes:
| P | L | A | Y | F |
| I | R | E | X | M |
| B | C | D | G | H |
| J | K | N | O | S |
| T | U | V | W | Z |
To encrypt a message, remove all spaces and split the message into digraphs (groups of two letters). For example, 'Hello World' becomes 'HE LL OW OR LD'. Then locate each pair in the key table and apply whichever rule below matches the letter combination:
Write a program that reads a key phrase and a plaintext, and outputs the encrypted text.
The text to encrypt will never contain two 'x's in a row, nor an 'x' as its last character, since either case could make the first rule above repeat indefinitely.
The input consists of two lines. The first line is the key phrase. The second line is the text to encrypt. Each line contains between 1 and 1000 characters, inclusive. Each character is a lowercase English letter, 'a'–'z' (except 'q'), or a space. Neither line begins or ends with a space.
Output a single line containing the encrypted text in upper case. The output must contain no spaces.