t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    if n == 1:
        print("-1")
    elif n==2:
        if a[0] != a[1]:
            print("-1")
        else:
            print("2")
    else:
        d = {}
        mini = n+1
        for i in range(n):
            if d.get(a[i]):
                d[a[i]].append(i)
            else:
                d[a[i]] = [i]
        for k in d.keys():
            if len(d[k]) >1:
                temp = d[k][0]
                for i in range(1, len(d[k])):
                    mini = min(mini, d[k][i]-temp+1)
                    temp = d[k][i]
        if mini == n+1:
            print("-1")
        else:
            print(mini)
