The Playfair cipher is a manual symmetric encryption technique and was the first digraph substitution cipher. The scheme was invented in by Charles Wheatstone, but it is named after Lord Playfair, who promoted its use.
The Playfair cipher uses a 5 \times 5 table containing each letter in the English alphabet exactly once, except for , which is omitted. This table is the encryption key. To facilitate memorization, the table is usually generated from a key phrase: first, fill the empty table with the letters of the key phrase (omitting spaces and duplicate letters), then fill the remaining spaces with the rest of the letters in alphabetical order. The key phrase is written in the top rows of the table, from left to right. For example, if the key phrase is "playfair example", the encryption key becomes:
To encrypt a message: - Remove all spaces, and split the message into digraphs (groups of letters), so for example, "Hello World" becomes "HE LL OW OR LD". - For each digraph, apply one of the following rules, using the key table:
If both letters are the same (or only one letter remains), add an after the first letter. Encrypt the new pair and continue (this changes all subsequent digraphs).
If the letters are on the same row, replace each with the letter to its immediate right (wrapping to the left side if needed). For example, with the table above, is encrypted as .
If the letters are in the same column, replace each with the letter immediately below (wrapping to the top if needed). For example, with the table above, becomes .
If the letters are not in the same row or column, replace them with the letters in the same rows but at the other corners of the rectangle formed by the original pair. The first letter of the encrypted pair is from the row of the first plaintext letter, and the second is from the row of the second. For example, becomes .
Write a program that reads a key phrase and a plaintext to encrypt, and outputs the encrypted text.
The text to encrypt will not contain two consecutive characters or finish with an , to avoid indefinite application of the first rule.
The input contains two lines. The first line contains the key phrase. The second line contains the text to encrypt. Each line will contain between and characters, inclusive. Each character will be a lowercase English letter, - (except ), or a space. No line will start or end with a space.
Output a single line containing the encrypted text in uppercase, with no spaces.
Example 1