import collections
def f(ais):
    cc = collections.Counter()
    i = 0
    ans = +float('inf')
    for j in range(len(ais)):
        cc[ais[j]] +=1
        while(cc[ais[j]] >= 2):
            cc[ais[i]] -=1
            ans = min(ans, j-i+1)
            i+=1
    return ans if ans < float('inf') else -1
for u in range(int(raw_input())):
    n = int(raw_input())
    ais = map(int, raw_input().split())
    print f(ais)