import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    q = int(input())
    for _ in range(q):
        n = int(input())
        a = list(map(int,input().split()))
        p = [[] for k in range(n+1)]
        for k in range(n):
            p[a[k]].append(k)
        ans = 1000000000
        for e in p:
            if len(e) >= 2:
                for l in range(len(e)-1):
                    ans = min(ans,1+abs(e[l]-e[l+1]))
        if ans == 1000000000:
            print(-1)
        else:
            print(ans)
if __name__ == '__main__':
    main()
