ENIGMAS - Enigma Machine

This challenge is to simulate the three rotor M3 Enigma Machine. For each test three lines of information will be provided; the first line will contain the rotor settings, the second the plugboard, and the third will be the text to encode/decode with the Enigma cipher.

Input

The first line will contain a single value T, for the number of tests to follow, where T <= 100.

For each test there will be three lines:

  • The first line will contain three entries indiciating: the Walzenlage, the Ringstellung, and the Grundstellung in the form '123 AAA BBB'. The Walzenlage only contains numbers 1-5. The Ringstellung and Grundstellung will be provided as triplets of letters in the range A-Z.
  • The second line will contain pairs of letters, from the range A-Z, indicating the settings for the Steckerbrett - there may be up to 13 pairs of letters.
  • The final line of the test will contain an unknown length message to encode/decode - the message will be in the standard quintuple form used at the time, separated with spaces, eg 'ABCDE FGHIJ KL'. The final group may be 1-5 letters in length. Again, only the range of letters A-Z will be used. The line will be terminated in a newline ('0x0A').

The Enigma machine used is the three rotor M3 version. This had five rotors, of which only three would have been installed at any time. The Umkehrwalze in use is the 'B' wiring.

Output

The output is to match the third line of the input, i.e. must be in the same quintuple grouping. One line of output per message is to be produced.

Example

Input:
2
123 JAN DER
2 123 JAN DER SP OJ RU LZ THISX ISXAN XEXAM PLEXI NPUT 543 SPO JPL SH OR TE NI YUQKD YVPSF HCQEI VHAPE NAQZQ I
SP OJ RU LZ
YUQKD YVPSF HCQEI VHAPE NAQZQ I
Output:
SJLKM SVZYM HXTUW VVWYY EDEB
THISX ISXAX DECOD XEDXM ESSAG E

Added by:Jander
Date:2012-10-04
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 SCM qobi
Resource:http://en.wikipedia.org/wiki/Enigma_machine

hide comments
2012-10-19 07:24:37 Jander
@Numerix - Thanks for pointing out that people have been working on it in main Spoj as I don't generally look there. I still want it to be code length so I will move it to Challenge.

Last edit: 2012-10-19 07:24:49
2012-10-19 05:34:59 numerix
@Jander: You put this problem also in SPOJ's main section - a good decision. But actually it is in "classical section" with source length scoring that belongs to "challenge section".
Please read comments below www.spoj.pl/problems/ENIGMAS and decide what to do.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.