Leaky Cryptography
The ACM ICPC judges are very careful about not leaking their problems, and all communi-cations are encrypted. However, one does sometimes make mistakes, like using too weak an encryption scheme. Here is an example of that.
The encryption chosen was very simple: encrypt each chunk of the input by flipping some bits according to a shared key. To provide reasonable security, the size of both chunk and key is 32 bits.
That is, suppose the input was a sequence of m
32-bit integers.
N[1] N[2] N[3] ... N[m]
After encoding with the key K
it becomes the following sequence of m 32-bit integers.
(N[1] ^ K) (N[2] ^ K) (N[3] ^ K) ... (N[m] ^ K)
where a ^ b
is the bitwise exclusive or of a
and b
.
Exclusive or is the logical operator which is 1 when only one of its operands is 1, and 0 otherwise. Here is its definition for 1-bit integers.
As you can see, it is identical to addition modulo 2. For two 32-bit integers a
and b
, their bitwise exclusive or a ^ b
is defined as follows, using their binary representations, composed of 0's and 1's.
a ^ b = a[31] ... a[1] a[0]^b[31] ... b[1] b[0] = c[31] ... c[1]c[0]
where
For instance, using binary notation, 11010110 ^ 01010101 = 10100011
, or using hexadecimal, d6 ^ 55 = a3
.
Since this kind of encryption is notoriously weak to statistical attacks, the message has to be compressed in advance, so that it has no statistical regularity. We suppose that N[1] N[2] ... N[m]
is already in compressed form.
However, the trouble is that the compression algorithm itself introduces some form of regularity: after every 8integers of compressed data, it inserts a checksum, the sum of these integers. That is, in the above input,
N[9] = N[1] + ... + N[8]
,
where additions are modulo 2^32
.
Luckily, you could intercept a communication between the judges. Maybe it contains a problem for the finals!
As you are very clever, you have certainly seen that you can easily find the lowest bit of the key, denoted by K[0]
. On the one hand, if K[0] = 1
, then after encoding, the lowest bit of Σ1 ≤ i ≤ 8
N[i] ^ K
is unchanged, as K[0]
is added an even number of times, but the lowest bit of N9 ^ K
is changed, so they shall differ. On the other hand, if K[0] = 0
, then after encoding, the lowest bit of Σ1 ≤ i ≤ 8
N[i] ^ K
shall still be identical to the lowest bit of N[9]
^K
, as they do not change. For instance, if the lowest bits after encoding are 1 1 1 1 1 1 1 1 1
then K[0]
must be 1, but if they are 1 1 1 1 1 1 1 0 1
thenK[0]
must be 0.
So far, so good. Can you do better?
You should find the key used for encoding.
Input
The input starts with a line containing only a positive integer S
, indicating the number of datasets in the input. S
is no more than 1000.
It is followed by S
datasets. Each dataset is composed of nine 32-bit integers corresponding to the first nine chunks of a communication. They are written in hexadecimal notation, using digits '0' to '9' and lowercase letters a
to f
, and with no leading zeros. They are separated by a space or a newline. Each dataset is ended by a newline.
Output
For each dataset you should output the key used for encoding. Each key shall appear alone on its line, and be written in hexadecimal notation, using digits '0' to '9' and lowercase letters a
to f
, and with no leading zeros.