from collections import defaultdict
def inp1():return map(int,input().split())
def inp():return list(inp1())
def num():return int(input())

for _ in range(num()):
    n=num()
    a=inp()
    d=defaultdict(list)
    for i in range(n):
        try:
            d[a[i]].append(i)
        except KeyError:
            d[a[i]]=[i]

    ans=99999999999
    if len(d)==1:
        if len(d.pop(a[i]))==1:
            print(-1)
        else:
            print(2)
    else:
        for i in d.values():
            for j in range(1,len(i)):
                temp=i[j]-i[j-1]+1
                if temp<ans:
                    ans=temp
        print(ans)
    
