import java.util.Scanner;
public class testString
{
	public static void main(String[] args)
	{
	   Scanner input = new Scanner(System.in);
	   int t = input.nextInt();
	   while((t--)>0){
	   int n = input.nextInt();
	   String s[] = new String[n];
	   input.nextLine();
	   for(int i=0;i<n;i++) s[i]=input.next();
	   
	   String demo[] = new String[n];
       for(int i=0;i<n;i++)
	   {
		   demo[i] = new String(s[i]);
	   }
   
       int countArr[] = new int[n];
       
	   int max=0;
        for(int i=0;i<n;i++)
		{
			int count=1;
			for(int j=i+1;j<n;j++)
			{
				if(demo[i]!=null){
					if(demo[i].equals(demo[j]))
					{
						count++;
						demo[j]= null;
					}
				}
			}countArr[i]=count;
			if(countArr[i]>countArr[max])
			{
				if(demo[i]!=null)max = i;
			}
		}
        boolean flag = true;
		if(s.length<2)flag=false;
		if(flag)
		{
			
			int arr[] = new int[countArr[max]];
			int c =0;
			int d=0;
			for(String x : s)
			{
				if(x.equals(s[max]))
				{
					arr[c] =  d;
					c++;
				}d++;
			}
			
			
			
			int maxIndex = arr[1]-arr[0];
            for(int i=1;i<c;i++)
			{
				int q = arr[i]-arr[i-1];
				
				if(q<maxIndex){
					maxIndex = q;
				
				}
			}System.out.println(++maxIndex);
          			
		}else System.out.println(-1);
	   }
 
	}
}