k = int(input())
for i in range(k):
    l = int(input())
    vls = list(map(int,input().split(' ')))
    copyVls = vls.copy()
    if len(vls)==1:
        print(-1)
        continue
    vls.sort(reverse=True)
    if len(set(vls))==1:
        print(2)
        continue
    vlsRsl = {x:vls.count(x) for x in vls}
    vlsRslSorted = sorted(vlsRsl.items(), key = lambda kv: kv[1], reverse=True)
    if vlsRslSorted[0][1] == vlsRslSorted[1][1]:
        print(-1)
    else:
        kV = vlsRslSorted[0][0]
        shortOne = len(copyVls)
        counter = 0
        isCounterStarted = False
        for b in range(len(vls)):
            if copyVls[b] == kV:
                if not isCounterStarted:
                    isCounterStarted = True
                    counter += 1
                else:
                    counter += 1
                    if counter == 2:
                        shortOne = 2
                        break
                    if counter < shortOne:
                        shortOne = counter
                    counter = 1
            else:
                counter +=1
        print(shortOne)