This page is still under construction.

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

Insecurity

Time limit1sMemory limit128 MB

Summary
Given a hexadecimal bitstring and lists of usernames and passwords, find which username concatenated with which password encrypts to that string under a growing left-shift XOR scheme.
Level

Hard8 of 10

Topics
Bit manipulation, Brute force, Hash map, String matching
Solved
No attempts yet

Problem

Throughout the history of operating systems, security has always been a concern, and storing passwords safely has been studied for a long time. Passwords must not be stored in readable form, so today many systems store them as a hash (for example, an MD5 hash).

One attack against hash-based storage is to precompute the hashes of many commonly used passwords and compare them against the stored hashes. To counter this, a company claimed that encrypting the username and password together would be safe. That claim is weaker than it sounds. To demonstrate the weakness, you obtained from one of their servers a list of usernames, a list of passwords, and an encrypted string.

The encryption works as follows. Let ww be the word to be encrypted, with characters w0,w1,…,wn−1w_0, w_1, \dots, w_{n-1}; each character is represented by 8 bits using ordinary ASCII encoding. Let cic_i be the encryption of the first i+1i+1 characters of ww. Here cic_i is treated as a single bitstring, not as a sequence of characters. The rules are:

c0=w0c_0 = w_0

ci=(ci−1≪4)⊕wi(i≥1)c_i = (c_{i-1} \ll 4) \oplus w_i \quad (i \ge 1)

Here ≪\ll is a bitwise left shift. For example, the bitstring 00101011 becomes 0010101100 when shifted left by 2. During encryption the bitstring may keep growing and no bits are ever discarded. Zero bits matter, so even leading zero bits are never dropped; the effect of the shift is to append zero bits on the right.

The operator ⊕\oplus is a bitwise XOR: it yields 0 when the two bits are equal and 1 otherwise. For example, 100011 XOR 0101 = 100110 (the shorter operand is right-aligned).

The word to be encrypted is the concatenation of the username and the password, that is, username + password.

Input

The first line contains the number of test cases. Each test case has the following format:

  • One line with the encrypted username and password that must be cracked. The bitstring is given in upper-case hexadecimal digits, with every 4 bits shown as one hexadecimal digit. For example, the bitstring 110010101101 is written as CAD.
  • One line with mm (1≤m≤1051 \le m \le 10^5), the number of users in the system.
  • The next mm lines each contain one username.
  • The next mm lines each contain one password.

Usernames and passwords contain only the following characters: a-z, A-Z, 0-9, and the special characters _ - = + ! @ # $ % { } & * ( ) [ ] \ | / < > , .. Each username and each password has length between 8 and 30, inclusive. The ASCII values of the characters are used during encryption.

All usernames are distinct, and all passwords are distinct.

Output

For each test case, output the username and the password that were concatenated and encrypted to form the given input, on two separate lines: the username on the first line and the password on the second. The input is guaranteed to admit a unique solution for every test case.

Examples3

  1. Example 1

    Input
    2
    7237B23A13EC745236
    5
    amsterdam
    teamdelft
    eindhoven
    enschede
    groningen
    abcdefgh
    ijklmnop
    qrstuvwx
    yzabcdef
    ghijklmn
    62652F07224264EB08455
    2
    skywalker
    darthvader
    dark_Force
    by1XWing
    
    Expected output
    teamdelft
    yzabcdef
    darthvader
    dark_Force
    
  2. Example 2

    Input
    1
    76644184361253E39
    4
    aaaaaaaa
    bbbbbbbb
    password
    zzzzzzzz
    12345678
    abcdefgh
    qwertyui
    ZZZZZZZZ
    
    Expected output
    password
    qwertyui
    
  3. Example 3

    Input
    1
    672BFA74986064744B1841C
    4
    user_name!!
    admin@root#1
    p+q=r-s==8
    guest<>guest
    s3cr3t{$}%
    p@ss|word\
    (a)[b]&c*d
    x/y,z.<w>
    
    Expected output
    admin@root#1
    p@ss|word\