for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))

    if n == 1:
        print(-1)
    else:
        d = {}

        for i in range(n):
            d[a[i]] = d.get(a[i], ()) + (i,)

        mini = float('inf')

        x = sorted((i for i, j in zip(d.keys(), d.values())), key=lambda l: len(d[l]), reverse=True)
        x = [d[i] for i in x if len(d[i]) != 1]
        
        if not x:
            print(-1)
        else:
            if len(x) != 1:
                if len(x[0]) == len(x[1]):
                    print(-1)
                    continue
    
            y = x[0]
    
            mini = float('inf')
    
            for i in range(len(y) - 1):
                if y[i+1] - y[i] + 1 < mini:
                    mini = y[i+1] - y[i] + 1
    
            print(mini)