count of maximum giving wrong answer !!

import java.util.;
import java.io.
;
class max
{

	static int maxcount(String name)
	{
		return name.length();
	}
	
	public static void main(String args[])
	{
		int counter=0,a=0;
		Scanner sc=new Scanner(System.in);
		int test=sc.nextInt();
		int[]num=new int[test];
		int []temp=new int[test];
		for(int i=0;i<test;i++)
		{
			num[i]=0;
			temp[i]=0;
		}
		for(int i=0;i<test;i++)
		{
			a=sc.nextInt();
			int b[]=new int[a];
			for(int j=0;j<a;j++)
			{
				b[j]=0;
			}
			
			for(int j=0;j<a;j++)
				b[j]=sc.nextInt();
			if(a==1)
			{
				num[i]=b[0];
				temp[i]=1;
				continue;
			}
			for(int j=0;j<a;j++)
			{
				for(int k=j+1;k<a;k++)
				{
					if(b[k]>b[j])
					{
						b[k]=b[k]+b[j];
						b[j]=b[k]-b[j];
						b[k]=b[k]-b[j];
					}
				}
			}
		
			String name="";
			for(int j=0;j<a;j++)
			{
				for(int k=j;k<a;k++)
				{					
					if(b[j]==b[k])
					{
						name=name+b[j];		
						if(k==a-1 && name.length()>=temp[i])
						{	
							num[i]=b[j];
							temp[i]=name.length();
							j=a;
							break;
						}
						if(k==a-1 && name.length()<temp[i])
						{
							j=a;
							break;
						}
					}
					else if(b[j]!=b[k])
					{
						counter=maxcount(name);
						if(counter>=temp[i])
						{
							num[i]=b[j];
							temp[i]=counter;
							counter=0;
						}
						j=k-1;
						name="";
						break;
					}
				}
			}
		}		
		for(int i=0;i<test;i++)
			System.out.println(num[i]+" "+temp[i]);	
	}
}

/*9

5

1 2 3 2 5

6

1 2 2 1 1 2

1

1

5

1 2 3 4 5

5

1 1 1 1 1

4

2 1 1 1

10

1 1 1 1 3 3 0 0 0 1

50

1 2 4 8 2 3 7 6 4 6 1 2 3 4 5 7 8 4 8 5 2 4 3 4 5 4 6 3 4 5 6 6 6 5 6 5 4 5 5 4 7 7 5 5 5 6 5 6 8 5

4

2 1 1 1*/