import sys
import os.path
import functools
import itertools
import re

def solve():
    # code goes here!!         
    for tests in range(int(input())):
        n = int(input())
        a = list(map(int,input().split()))
        last = [-1] * (n + 2)
        ans = n << 1
        for i,val in enumerate(a,1) :
            if last[val] != -1:
                ans = min(ans,i - last[val] + 1)
            last[val] = i
        print(-1 if ans > n else ans)

def main():
    if os.path.exists('test.inp') :
        sys.stdin = open("test.inp","r")
    solve()

if __name__ == '__main__':
    main()


