from collections import defaultdict as d
for i3 in xrange(input()):
    n=input()
    nums=map(int,raw_input().split())
    dic=d(list)
    for i in xrange(n):
        x=nums[i]
        dic[x].append(i)
    dicDiff=[]
    for ls in dic:
        ls=dic[ls]
        dicDiff.append([ls[i+1]-ls[i] for i in xrange(len(ls)-1)])
    mins=sorted(map(lambda x: min(x+[200009]),dicDiff))
    if len(mins)>2 and mins[0]==mins[1]:
        print -1
    else:
        print -1 if (mins[0]==200009) else (mins[0]+1)
