import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Comparator;
import java.util.Collections;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int t = in.nextInt();
            for (int i = 0; i < t; i++)
            {
                int n = in.nextInt();
                int nums[] = CPUtils.readIntArray(n, in);
                if (nums.length == 1)
                {
                    out.println(-1);
                    continue;
                }
                //find dominant
                int dom = getDom(nums);
                if (dom == -1)
                {
                    out.println(-1);
                    continue;
                }
                Set<Integer> set = new HashSet<>();
                boolean found = false;
                int min = Integer.MAX_VALUE;
                for (int k = 0; k < n; k++)
                {
                    if (set.contains(nums[k]) && nums[k] == dom)
                    {
                        found = true;
                        min = Math.min(min, set.size() + 1);
                        set.clear();
                        k--;
                    } else
                        set.add(nums[k]);
                }
                if (found)
                    out.println(min);
                else
                    out.println(-1);
            }
        }

        private int getDom(int[] nums)
        {
            Map<Integer, Integer> map = new HashMap<>();
            for (int k = 0; k < nums.length; k++)
            {
                int occ = map.getOrDefault(nums[k], 0);
                occ++;
                map.put(nums[k], occ);
            }
            Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
            Comparator<Map.Entry<Integer, Integer>> valueComparator = new Comparator<Map.Entry<Integer, Integer>>()
            {

                public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2)
                {
                    Integer v1 = e1.getValue();
                    Integer v2 = e2.getValue();
                    return v2.compareTo(v1);
                }
            };
            List<Map.Entry<Integer, Integer>> listOfEntries = new ArrayList<Map.Entry<Integer, Integer>>(entries);
            Collections.sort(listOfEntries, valueComparator);
            if (listOfEntries.get(0).getValue() == 1 || (listOfEntries.size() > 1 && (int) listOfEntries.get(0).getValue() == (int) listOfEntries.get(1).getValue()))
            {
                return -1;
            }
            return listOfEntries.get(0).getKey();
        }

    }

    static class CPUtils
    {
        public static int[] readIntArray(int size, Scanner in)
        {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = in.nextInt();
            }
            return array;
        }

    }
}

