import sys
import math

#to read string
get_string = lambda: sys.stdin.readline().strip()
#to read list of integers
get_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
#to read integers
get_int = lambda: int(sys.stdin.readline())
#to print fast
#pt = lambda x: sys.stdout.write(str(x)+'\n')

#--------------------------------WhiteHat010--------------------------------------#
for _ in range(get_int()):
    n = get_int()
    lst = get_list()
    if len(set(lst)) == n:
        print(-1)
    else:
        d = {}
        for i in range(n):
            item = lst[i]
            if item not in d:
                d[item] = [i,float('inf')]
            else:
                index,size = d[item]
                d[item] = [i, min(i-index+1,size) ]
        l = [v for i,v in d.values()]
        print(min(l))