def minf(a, b):
    if a > b:
        return b
    else:
        return a
        
        
t = input()
for q in range(int(t)):
    min0 = 0
    printed = False
    dic = {}
    n = input()
    ar = input()
    #print("###",ar)
    min1 = int(n)
    if(int(n) < 2):
        print("-1")
        printed = True
        continue
    arr = ar.split()
    for i in range(len(arr)):
        if arr[i] not in dic:
            dic[arr[i]] = [i]
        else:
            dic[arr[i]].append(i)
    for i in dic:
        l = len(dic[i])
        if l >= 2:
            min0 = minf(dic[i][1]-dic[i][0], dic[i][l-1]-dic[i][l-2])
            #print(*dic[i])
        if min0 < min1:
            min1 = min0
    if(min1 > 0) and not printed:
        print(min1+1)        
    elif not printed:
        print(-1)