
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class C {
    public static void main(String[] args) {
        FastReader s = new FastReader();

        int T = s.nextInt();
        StringBuilder res = new StringBuilder();

        for (int i = 0; i < T; i++) {

            int n = s.nextInt();
            int[] arr = new int[n];
            for (int j = 0; j < n; j++) {
                arr[j]=s.nextInt();
            }

            int answer=-1; // default
            boolean flag=false;

            // find if arr is dominated at all ?

            //special case //
            if(n==1){ res.append(answer).append("\n"); continue;}

            // else ... //
            int[] counters = new int[n+1];
            for (int j = 0; j < arr.length; j++) counters[arr[j]]++;
            int maxCounter=-1, maxCountNum=-1;
            for (int j = 1; j < counters.length; j++) {
                if(maxCounter<counters[j])
                {
                    maxCounter=counters[j];
                    maxCountNum=j;
                }
            }
            for (int j = 1; j < counters.length; j++) {
                if(j!=maxCountNum)
                {
                    if(counters[j]==maxCounter)
                    {
                        flag=true;
                        break;
                    }
                }
            }

            // in case is dominated, find shortest dominated subArr ! //
            if(!flag)
            {
                int first=0, last= arr.length-1;
                while (first<last)
                {
                    if(arr[first]==maxCountNum && arr[last]==maxCountNum)
                    {
                        if(maxCounter==2) break;
                        else
                        {
                            maxCounter--;
                            first++; // arbitrary choice !
                        }
                    }
                    else if(arr[first]==maxCountNum) last--;
                    else first++;
                }
                answer=last-first+1;
            }

            res.append(answer);
            res.append("\n");
        }
        System.out.println(res);
    }



    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}
