from collections import defaultdict
for _ in range(int(input())):
    l=int(input())
    arr=list(map(int,input().split()))
    if l==1:
        print(-1)
    else:
        idx_map=defaultdict(list)
        for i,e in enumerate(arr):
            idx_map[e].append(i)
        max_occ=idx_map[list(idx_map.keys())[0]]
        still_min_subarray_len=float('inf')
        for k in range(len(max_occ)-1):
            if len(set(arr[max_occ[k]+1:max_occ[k+1]]))== \
                max_occ[k+1]-max_occ[k]-1:
                still_min_subarray_len=min(
                    still_min_subarray_len,
                    max_occ[k+1]-max_occ[k]+1
                )
        print(still_min_subarray_len)





