spoj#AMSCO2. Decipher the AMSCO cipher

Decipher the AMSCO cipher

Here you have to decipher the AMSCO cipher :

Due to A.M.SCOtt in the 19th century, it's an incomplete columnar transposition cipher
with alternating single letters and digraphs
. The first entry must be a digraph.
In both even and odd periods the first column and the first row always alternate:

7 4 5 6 3 2 1
RI D ER S ON T HE
S TO R MI N TO T
HI S HO U SE W EA
R EB O RN J IM M
OR R IS O N

Input

N lines (N<1000)
Each line of the input contains the numeric key (permutation order of the columns)
and a ciphertext. Ciphertext letters are in [A-Z] only with no punctuation.
The keylength max is 9 and the length of the ciphertext is limited to 250.
The last line ends with EOF.

Output

Output consist of exactly N lines of plaintexts with letters in [A-Z] with no spaces.

Example 1:

Input:
7456321 HETEAMTTOWIMONNSEJNDTOSEBRERRHOOISSMIURNORISHIROR Output:
RIDERSONTHESTORMINTOTHISHOUSEWEAREBORNJIMMORRISON

Example 2:

Input:
41325 CECRTEGLENPHPLUTNANTEIOMOWIRSITDDSINTNALINESAALEMHATGLRGR Output:
INCOMPLETECOLUMNARWITHALTERNATINGSINGLELETTERSANDDIGRAPHS