t = int(input())

for _ in range(t):
        n = int(input())
        l = list(map(int, input().split()))
        d = {}
        ans = float("+inf")
        for i, el in enumerate(l):
            if el not in d:
                d[el] = i
            else:
                ans = min(ans, i-d[el]+1)
                d[el] = i
        if ans == float("+inf"):
            print("-1")
        else:
            print(ans)
    
    
