t=int(input())
for k in range(t):
    n=int(input())
    count=n
    arr=list(map(int,input().split()))
    if len(arr)==1:
        print(-1)
    elif len(arr)==2:
        if arr[0]==arr[1]:
            print(2)
        else:
            print(-1)
    else:
        i = 0
        j = n-1
        while i<n-1:
            if arr[j] == arr[i]:
                count = min(count, j + 1 - i)
                j -= 1
            else:
                j -= 1

            if j == i:
                j = n - 1
                i += 1
        print(count)
