#C3556. Taco Reduction

    ID: 46996 Type: Default 1000ms 256MiB

Taco Reduction

Taco Reduction

In this problem, you are given one or more arrays of positive integers. For each array, you must repeatedly perform a reduction operation until all the numbers in the array become equal. In each reduction step, take the maximum element ( x ) and the minimum element ( y ) from the array and replace one occurrence of ( x ) with ( x - y ) (i.e. ( x_{new} = x - y )). The process stops when all numbers in the array are the same. Your task is to output the final number (which will be the greatest common divisor of all the elements) for each array.

For example, consider the array [4, 6, 8]. The final number is 2 since repeatedly subtracting the minimum from the maximum eventually results in all elements being 2. This problem is essentially equivalent to computing the GCD of the numbers in the array.

inputFormat

The input is read from standard input (stdin) and is structured as follows:

  • The first line contains an integer ( T ) representing the number of test cases.
  • Each of the following ( T ) lines represents a test case. Each test case begins with an integer ( n ) (the number of elements in the array) followed by ( n ) space-separated positive integers.

For example:

3 3 4 6 8 2 5 15 4 10 10 10 10

outputFormat

For each test case, output the final remaining number on a separate line to standard output (stdout).## sample

3
3 4 6 8
2 5 15
4 10 10 10 10
2

5 10

</p>