Under construction

Still being built. Things may change

My Brother's Diary

Interview

Time limit1sMemory limit128 MB

Summary
Find the Caesar shift whose most frequent ciphertext letter maps to E, print the smallest valid shift and the decrypted text, or NOT POSSIBLE if several shifts work.
Level

Easy3 of 10

Topics
String, Implementation, Hash map, Brute force
Solved
No attempts yet

Problem

These days, people who want to communicate securely use asymmetric encryption algorithms such as RSA. My older brother, however, protects his diary with a simpler method: a substitution cipher in which every letter of the plaintext is replaced by another letter of the alphabet, always shifted by the same fixed distance. If this fixed distance dd is 55, then A is replaced by F, B by G, C by H, and so on, wrapping around so that Y becomes D and Z becomes E.

If the distance dd were fixed and known, decryption would be easy. But my brother chooses a random distance for each diary entry, so to read an entry I first have to guess its distance dd. To do this I rely on the well-known fact that the letter E occurs more often than any other letter in English text.

Can you write a program that, assuming the most frequent letter in an encrypted entry stands for the plaintext letter E, works out the distance dd? Naturally, I would also like to see the decrypted text.

Input

The first line contains the number of test cases cc (1≤c≤100)(1 \le c \le 100). Each of the next cc lines contains exactly one diary entry. Diary entries use only uppercase letters (A–Z) and spaces, and each entry contains at most 10001000 characters (letters and spaces together).

Output

For each test case, print one line containing the smallest possible distance dd (0≤d≤25)(0 \le d \le 25) followed by the decrypted text. If decryption is impossible because more than one distance satisfies the rule above, print NOT POSSIBLE instead. Spaces are never encrypted.

Examples3

  1. Example 1

    Input
    4
    RD TQIJW GWTYMJWX INFWD JSYWNJX ZXJ F XNRUQJ JSHWDUYNTS YJHMSNVZJ
    THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
    XVIDRE TFCCVXZRKV GIFXIRDDZEX TFEKVJK UVTIPGKZFE
    XVIDRE TFCCVXZRKV GIFXIRDDZEX TFEKVJK
    
    Expected output
    5 MY OLDER BROTHERS DIARY ENTRIES USE A SIMPLE ENCRYPTION TECHNIQUE
    10 JXU GKYSA RHEMD VEN ZKCFI ELUH JXU BQPO TEW
    17 GERMAN COLLEGIATE PROGRAMMING CONTEST DECRYPTION
    NOT POSSIBLE
    
  2. Example 2

    Input
    1
    EEE ABC
    
    Expected output
    0 EEE ABC
    
  3. Example 3

    Input
    1
    AB
    
    Expected output
    NOT POSSIBLE