This page is still under construction.

Parts of this page are still being built. What you see may change.

Hidden Code

Time limit2sMemory limit512 MB

Summary
Given plaintext/ciphertext pairs encrypted with a common repeating key, recover the shortest key or report that none works.
Level

Medium6 of 10

Topics
String, Math, Implementation, Brute force
Solved
No attempts yet

Problem

It's time to put your hacking skills to the test! You've been called upon to help crack enemy codes in the current war on... something or another. Anyway, the point is that you have discovered the encryption technique used by the enemy; it is quite simple, and proceeds as follows. Note that all strings contain only uppercase letters of the alphabet.

  1. We are given a key KK and a plaintext PP, which is encrypted character-by-character to produce a ciphertext CC of the same length.
  2. If ∣K∣|K| is the length of the key KK, then the first ∣K∣|K| characters of CC are obtained by adding the first ∣K∣|K| characters of PP to the characters of KK, where adding two letters means interpreting them as numbers (A=0A = 0, B=1B = 1, and so on) and taking the sum modulo 26. That is, Ci=(Pi+Ki) mod 26C_i = (P_i + K_i) \bmod 26 for i=1,…,∣K∣i = 1, \dots, |K|. If ∣K∣>∣P∣|K| > |P|, then the extra characters in KK are ignored.
  3. The remaining characters of PP, i.e. PiP_i for i>∣K∣i > |K|, are encrypted using the previous ciphertext characters by Ci=(Pi+Ci−∣K∣) mod 26C_i = (P_i + C_{i-|K|}) \bmod 26 for i=∣K∣+1,…,∣P∣i = |K| + 1, \dots, |P|.

As an example, consider the encryption of the string "STANFORD" using the key "ACM":

  STA NFORD
+ ACM SVMFA
  ----------
  SVM FAAWD

Knowing this, you are well on your way to being able to read the enemy's communications. Luckily, you also have several pairs of plaintexts and ciphertexts that your team recovered, all of which are known to be encrypted with the same key. Help find the key that the enemy is using. Because the key is uniquely determined by the longest recovered plaintext, the shortest valid key is unique.

Input

The input consists of multiple test cases. Each test case begins with a line containing a single integer NN (1≤N≤1001 \le N \le 100), the number of plaintext/ciphertext pairs you will receive. Each of the next NN lines contains two strings PP and CC, the plaintext and the ciphertext respectively. PP and CC contain only uppercase letters (A-Z) and have the same length (at most 100 characters). The input terminates with a line containing N=0N = 0, which is not processed.

Output

For each test case, print a single line containing the shortest possible key, or Impossible if no key could have produced all of the given encryptions.

Examples1

  1. Example 1

    Input
    1
    A B
    3
    STANFORD SVMFAAWD
    AVOWIENR AXAWFEJW
    VAMRI VCYMK
    3
    ABCDEFGHIJKLMNOPQRSTUVWXYZ AAAAAAAAAAAAAAAAAAAAAAAAAA
    Y Y
    Z Z
    2
    A B
    B A
    0
    
    Expected output
    B
    ACM
    AZYXWVUTSRQPONMLKJIHGFEDCB
    Impossible