balanced-weights

Today RK has to transport his N items of different weights(in calories) to a near city but before that he wants to make sure that the weights of all items are balanced. In order to check this he puts all items on the weight scale and found that weights are not balanced actually. So to deal with it RK designed his own procedure.
RK chooses two different weights say A and B such that weight(A) > weight(B) and reduce the weight of A by weight(B) calories. He repeats this procedure as many times as he needed. The ultimate goal of RK is to balance all weights by minimizing the sum of all weights using his own procedure. RK has asked for your help as their are large number of items. So make a quick move and help RK .

INPUT :

The first line contains the number of test cases T(1 ≤ T ≤ 10). First line of each test case contains a positive integer N(2 ≤ N ≤ 10^6)-Number of items .Second line contains N space separated positive integers in the interval [1,10^9] representing weight of each item in calories.

OUTPUT :

For each test case output the minimum sum of all balanced weights.

SAMPLE INPUT
2
3
3 6 9
2
2 2

SAMPLE OUTPUT
9
4

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
import fractions
tcs = int(eval(input()))
for i in range(tcs):
	n = eval(input())
	listx = list(map(int, input().split(' ')))
	gcdx = listx[0]
	for i in listx:
		gcdx = fractions.gcd(i, gcdx)
	print((gcdx * int(n)))


import sys,fractions

for __ in range(eval(input())) :
	n=eval(input())
	weights=list(map(int,sys.stdin.readline().split()))
	a=weights[0]
	for i in weights :
		a=fractions.gcd(a,i)
	print(a*n)


t=eval(input())
i=0
while i<t:
	n=eval(input())
	x=input().split(' ')
	if n==1:
		print(int(x[0]))
	elif n==0:
		print("0")
	else:
		a=int(x[0])
		b=int(x[1])
		if a>b:
			while 1:
				if a%b==0:
					gcd=b
					break
				temp=a
				a=b
				b=temp%b
		else:
			while 1:
				if b%a==0:
					gcd=a
					break
				temp=b
				b=a
				a=temp%a
		j=2
		while j<n:
			a=gcd
			b=int(x[j])
			if a>b:
				while 1:
					if a%b==0:
						gcd=b
						break
					temp=a
					a=b
					b=temp%b
			else:
				while 1:
					if b%a==0:
						gcd=a
						break
					temp=b
					b=a
					a=temp%a
			j=j+1
		print(gcd*n)
	i=i+1