Recovering a Linear Congruential Sequence

Time limit2sMemory limit128 MB

Summary
Given odd-indexed terms of a hidden linear congruential generator mod 10001, find (a,b) and output the even-indexed terms forming the lexicographically smallest valid sequence.
Level

Medium6 of 10

Topics
Math, Number theory, Brute force
Solved
No attempts yet

Problem

A contest organizer builds their judge data with a linear congruential generator. First they choose three integers x1x_1, aa, and bb, each between 00 and 1000010000 inclusive. Then, for i=2,3,…,2Ti = 2, 3, \dots, 2T, the remaining values are produced by the recurrence

xi=(a⋅xi−1+b) mod 10001x_i = (a \cdot x_{i-1} + b) \bmod 10001

In the resulting sequence, the odd-indexed values x1,x3,…,x2T−1x_1, x_3, \dots, x_{2T-1} are used as input data and the even-indexed values x2,x4,…,x2Tx_2, x_4, \dots, x_{2T} are used as output data.

You are given the input data x1,x3,…,x2T−1x_1, x_3, \dots, x_{2T-1}. Recover output data x2,x4,…,x2Tx_2, x_4, \dots, x_{2T} for which some pair (a,b)(a, b) is consistent with every given value. Because more than one (a,b)(a, b) may be consistent, output the sequence (x2,x4,…,x2T)(x_2, x_4, \dots, x_{2T}) that is lexicographically smallest among all consistent sequences.

Input

The first line contains TT (1≤T≤1001 \le T \le 100).

Each of the next TT lines contains x2i−1x_{2i-1} on its ii-th line (0≤x2i−1≤100000 \le x_{2i-1} \le 10000).

Every input is data that was actually produced by the process above, so at least one consistent pair (a,b)(a, b) is guaranteed to exist.

Output

Print TT lines. The ii-th line contains x2ix_{2i}. The whole output sequence (x2,x4,…,x2T)(x_2, x_4, \dots, x_{2T}) must be the lexicographically smallest one among all sequences consistent with the given input.

Examples3

  1. Example 1

    Input
    3
    17
    822
    3014
    
    Expected output
    9727
    1918
    4110
    
  2. Example 2

    Input
    1
    500
    
    Expected output
    0
    
  3. Example 3

    Input
    3
    5
    5
    5
    
    Expected output
    0
    0
    0