You are given an array A of size N. Output the maximum value of A[i]%A[j] (ie. A[i] mod A[j]) over all 1 ≤ i, j ≤ N.

------ Input ------ 

First line of input contains T, the number of testcases.
The first line of each test case contains N, the number of elements in the array.
Second line of each testcase contains N space separated integers in which the ith integer is A[i].

------ Output ------ 

For each testcase output a single integer in a new line: the maximum value of A[i]%A[j].

------ Constraints ------ 

$1≤T≤10$
$1≤N≤10^{5}$
$1≤A[i]≤10^{6}$

----- Sample Input 1 ------ 
1
2
1 2
----- Sample Output 1 ------ 
1
----- explanation 1 ------ 
The maximum value is obtained by 1%2 = 1.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
case = int(input())
for _ in range(case):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	if n == 1:
		print(0)
		continue
	max_val = max(arr)
	second = 0
	for i in range(n):
		if arr[i] > second and arr[i] < max_val:
			second = arr[i]
	print(second)


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(a) for a in input().split()]
	l.sort(reverse=True)
	if l[0] == l[-1]:
		print('0')
	else:
		denom = l[0]
		for i in range(n):
			if l[i + 1] != denom:
				num = l[i + 1]
				break
		print(num % denom)


for t in range(int(input())):
	n = int(input())
	arr = set(map(int, input().split()))
	arr = list(arr)
	arr.sort(reverse=True)
	if len(arr) == 1:
		print(0)
	else:
		print(arr[1])


t = int(input())
for _ in range(t):
	n = int(input())
	arr = sorted(set(list(map(int, input().split()))))
	print(0 if len(arr) == 1 else arr[-2])


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = list(set(a))
	a.sort()
	if len(a) == 1:
		print(0)
	else:
		print(a[-2])


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	ans = 0
	for j in range(n):
		if a[j] < m and a[j] >= ans:
			ans = a[j]
	print(ans)


for i in range(int(input())):
	n = int(input())
	k = list(map(int, input().split()))
	mx = max(k)
	mx2 = 0
	for y in range(n):
		if k[y] < mx and k[y] >= mx2:
			mx2 = k[y]
	print(mx2)


from sys import stdin

def solve():
	n = int(int(input().strip()))
	K = list(map(int, input().strip().split(' ')))
	max_K = max(K)
	mod_max = 0
	for i in K:
		if mod_max < i < max_K:
			mod_max = i
	print(mod_max)
for _ in range(int(input().strip())):
	solve()


for _ in range(int(input())):
	N = int(input())
	A = sorted(list(map(int, input().split())))
	y = False
	for i in range(0, len(A)):
		if A[-i - 1] != A[-1]:
			print(A[-i - 1])
			y = True
			break
	if not y:
		print(0)


def soln(n, arr):
	unis = set(arr)
	if len(unis) == 1:
		return 0
	maxm = max(unis)
	unis.remove(maxm)
	sec_max = max(unis)
	return sec_max % maxm
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(soln(n, arr))


def solve():
	n = int(input())
	A = list(map(int, input().split()))
	max_A = max(A)
	mod_max = 0
	for i in A:
		if mod_max < i < max_A:
			mod_max = i
	print(mod_max)
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	nli = list(map(int, input().split()))
	mx = max(nli)
	min = 0
	for a in nli:
		if a % mx > min:
			min = a % mx
	print(min)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = max(l)
	sm = 0
	for i in l:
		if sm < i < k:
			sm = i
	print(sm)


t = int(input())
for _ in range(t):
	l = int(input())
	v = list(map(int, input().split()))
	m = max(v)
	min = 0
	for i in v:
		if i % m > min:
			min = i % m
	print(min)
	i += 1


def listin():
	a = list(map(int, input().split()))
	return a
for tastcas in range(int(input())):
	n = int(input())
	a = listin()
	a.sort()
	m = a[-1]
	try:
		while a[-1] == m:
			a.pop(-1)
		else:
			print(a[-1])
	except:
		print(0)


def solve():
	m = -1
	x = max(lst)
	for i in range(n):
		m = max(lst[i] % x, m)
	print(m)
t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	solve()


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = list(set(l))
	l = sorted(l)
	if len(l) > 1:
		print(l[-2] % l[-1])
	elif len(l) == 1:
		print(0)


Test_case = int(input())
for test in range(Test_case):
	n = int(input())
	l = list(map(int, input().split()))
	l = set(l)
	l = list(l)
	m1 = max(l)
	l.remove(m1)
	if len(l) == 0:
		print(0)
	else:
		print(max(l) % m1)


T = int(input())
for _ in range(T):
	n = int(input())
	L = list(map(int, input().split()))
	S = set(L)
	M = list(S)
	m = max(M)
	M.remove(m)
	if len(M) > 0:
		print(max(M))
	else:
		print(0)


from sys import stdin, stdout
import math
from collections import Counter
import functools
import time
import bisect

def tr():
	return range(int(line()))

def sm():
	return map(int, line().split())

def ln():
	return list(sm())

def nl():
	return int(line())

def ssm():
	return map(str, line().split())

def line():
	return stdin.readline().rstrip()

def b(x):
	return bin(x).replace('0b', '')

def o(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + '\n')

def osp(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + ' ')

def ol(x):
	stdout.write(' '.join(map(str, x)))

def wline():
	return stdin.read().split()

def main():
	for _ in tr():
		n = nl()
		a = ln()
		max_a = max(a)
		max_out = 0
		for i in a:
			if i % max_a > max_out:
				max_out = i % max_a
		print(max_out)
main()


try:
	from debug import debug, timer
except ImportError:
	pass
from sys import stdin
input = stdin.readline

def rii():
	return range(int(input().strip()))

def ii():
	return int(input().strip())

def mii():
	return map(int, input().strip().split(' '))

def lmii():
	return list(map(int, input().strip().split(' ')))

def si():
	return str(input().strip())

def getvals():
	while True:
		yield from input().split()

def tri(n):
	return n * (n + 1) // 2

def smallest_first(m, n):
	return (min(m, n), max(m, n))

def do_codechef():
	for t in rii():
		n = ii()
		a = sorted(lmii())
		i = a.pop()
		try:
			while a[-1] == i:
				_ = a.pop()
		except IndexError:
			print(0)
		if len(a) > 0:
			print(a.pop())
do_codechef()
exit()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mx1 = max(l)
	mx2 = 0
	for i in l:
		if i != mx1:
			mx2 = max(i, mx2)
	print(mx2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	min = 0
	for i in a:
		if i % m > min:
			min = i % m
	print(min)
	i = i + 1


for i in range(int(input())):
	x = int(input())
	arr = []
	a = list(map(int, input().split()))
	y = max(a)
	for i in range(x):
		arr.append(a[i] % y)
	print(max(arr))


t = int(input())
for j in range(t):
	l = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	min = 0
	for i in a:
		if i % m > min:
			min = i % m
	print(min)
	i += 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = y = 0
	for i in range(n):
		if a[i] > x:
			y = x
			x = a[i]
		elif a[i] > y and a[i] < x:
			y = a[i]
	print(y)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l = list(set(l))
	l.sort()
	a = l[len(l) - 1]
	b = l[len(l) - 2]
	print(b % a)


import sys
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mod = max(l)
	s = len(l)
	maxim = 0
	for x in range(s):
		if l[x] % mod > maxim:
			maxim = l[x] % mod
	print(maxim)


t = int(input())
for kk in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	m = max(lis)
	li = list(filter(lambda a: a != m, lis))
	if len(li) == 0:
		print(0)
	else:
		print(max(li))


for i in range(int(input())):
	n = int(input())
	list = [int(x) for x in input().split(' ')]
	list.sort()
	maxval = 0
	for j in range(n - 1):
		if list[j] % list[j + 1] > maxval:
			maxval = list[j] % list[j + 1]
	print(maxval)


for test in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = set(l)
	l = list(l)
	l = sorted(l)
	if len(l) != 1:
		print(l[-2] % l[-1])
	else:
		print('0')


for _ in range(int(input())):
	n = int(input())
	l = list([int(x) for x in input().split()])
	p = list(set(l))
	p.sort()
	n = len(p)
	print(p[n - 2] % p[n - 1])


t = int(input())
while t:
	n = int(input())
	arr = list(set(list(map(int, input().split()))))
	arr.sort()
	if len(arr) > 1:
		print(arr[-2])
	else:
		print(0)
	t -= 1


t = int(input())
for j in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if n == 1:
		print(0)
		continue
	(x, y) = (n - 1, n - 2)
	a.sort()
	while y >= 0 and a[y] == a[x]:
		y -= 1
	if y == -1:
		print('0')
	else:
		print(a[y])


t = int(input())
while t:
	t -= 1
	n = int(input())
	numbers = [int(x) for x in input().strip().split()]
	numbers.sort()
	if n == 1:
		print('0')
	else:
		maxm = -1
		i = n - 1
		while i >= 1:
			modulo = numbers[i - 1] % numbers[i]
			if modulo > maxm:
				maxm = modulo
				if modulo != 0:
					break
			i -= 1
		print(maxm)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	mx = -1
	for i in l:
		m = i % l[n - 1]
		if m > mx:
			mx = m
	print(mx)


t = int(input())
while t:
	list = []
	t = t - 1
	n = int(input())
	s = input()
	list2 = s.split()
	for i in range(n):
		list.append(int(list2[i]))
	p = max(list)
	for i in range(n):
		list[i] = list[i] % p
	print(max(list))


for t in range(int(input())):
	N = int(input())
	(l, sl) = (-1, -1)
	for e in input().split():
		e = int(e)
		if e > l:
			sl = l
			l = e
		elif e > sl and l > e:
			sl = e
	if sl == -1:
		print(0)
	else:
		print(sl)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = [int(x) for x in input().split()]
	k = 0
	large = s[0]
	for i in range(n):
		if s[i] > large:
			large = s[i]
	for i in range(n):
		if k < s[i] % large:
			k = s[i] % large
	print(k, end=' ')


for t in range(int(input())):
	N = int(input())
	s = set()
	for i in input().split():
		i = int(i)
		s.add(i)
	a = list(s)
	a.sort()
	a.insert(0, 0)
	print(a[-2])


for i in range(int(input())):
	n = int(input())
	ip = sorted(list(map(int, input().split())))
	if n == 1:
		print(0)
	elif n == 2:
		print(max(ip[0] % ip[1], ip[1] % ip[0]))
	else:
		m = 0
		for j in range(1, n):
			if ip[-j - 1] % ip[-j] > m:
				m = ip[-j - 1] % ip[-j]
		print(m)


T = int(input().strip())
for i in range(T):
	N = int(input().strip())
	arr = [int(x) for x in input().strip().split(' ')]
	maxi1 = max(arr)
	l2 = list(filter(lambda a: a != maxi1, arr))
	if len(l2) == 0:
		print(0)
	else:
		print(max(l2))


from sys import stdin, stdout
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	mo = a[0] % m
	for x in range(n):
		if a[x] % m > mo:
			mo = a[x] % m
	print(mo)


for i in range(int(input())):
	n = int(input())
	nums = set(list(map(int, input().split())))
	m = max(nums)
	nums.remove(m)
	if len(nums) == 0:
		print(0)
		continue
	n = max(nums)
	print(n % m)


for _ in range(int(input())):
	n = int(input())
	a = list(set(map(int, input().split())))
	a.sort(reverse=True)
	if len(a) == 1:
		print(0)
	else:
		print(a[1])


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	m = max(ar)
	ab = [i % m for i in ar]
	print(max(ab))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	x = a[0]
	y = a[0]
	for i in range(1, n):
		if a[i] != a[i - 1]:
			y = a[i]
			break
	ans = 0
	for i in a:
		ans = max(ans, x % i)
	print(max(ans, y % x))


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ar.sort()
	m = 0
	for i in range(n - 1):
		if ar[-2 - i] % ar[-i - 1] > m:
			m = ar[-i - 2] % ar[-i - 1]
	print(m)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = sorted(list(map(int, input().split())))
	i = n - 1
	while i >= 0 and a[i] == a[-1]:
		i -= 1
	if i == -1:
		print(0)
	else:
		print(a[i])


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	i = n - 2
	while i >= 0 and a[i] == a[n - 1]:
		i -= 1
	ans = 0 if i == -1 else a[i]
	print(ans)