import math

def main():
    for _ in range(int(input())):
        n=int(input())
        a=list(map(str,input().split()))
        b=set(a)
        a="".join(a)
        mini=math.inf
        for x in b:
            temp=a.split(x)
            try:
                for y in temp[-2:0:-1]:
                    mini=min(mini,len(y))
            except:
                break

        if mini!=math.inf:
            print(mini+2)
        else:
            print(-1)

main()
