<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = trim(fgets(STDIN));
     $c = explode(" ", trim(fgets(STDIN)));
     $d = array();
     for($y = 0; $y < $b; $y++)
     {
          $d[$c[$y]]++;
     }
     arsort($d);
     $e = reset($d);
     $f = each($d);
     $g = $f[0];
     $h = $f[1];
     if($h == 1)
     {
          print min(floor($b / 2), count($d) - 1, $h) . "\n";
     }
     elseif($h > 1)
     {
          if(count($d) > $h)
          {
               print min(floor($b / 2), count($d), $h) . "\n";
          }
          else
          {
               print min(floor($b / 2), count($d), $h - 1) . "\n";
          }
     }
}
?>

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

    function gap($n)
    {
        return $n < GAP_VAL ;
    }

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $n = intval(trim(fgets(STDIN))) ;
        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $count = array_fill(0, $n + 1, 0) ;
        $total_cnt = 0 ;

        for($i = 0; $i < $n; $i ++)
        {
            $in_str[$i] = intval($in_str[$i]) ;

            $count[$in_str[$i]] ++ ;
            if($count[$in_str[$i]] == 1) $total_cnt ++ ;
        }

        $ans = 0 ;
        for($i = 1; $i <= $n; $i ++)
        {
            if($count[$i] == 0) continue ;

            if($total_cnt - 1 >= $count[$i]) $ans = max($ans, $count[$i]) ;
            if($total_cnt >= $count[$i] - 1) $ans = max($ans, $count[$i] - 1) ;
            if($total_cnt <= $count[$i] - 1) $ans = max($ans, $total_cnt) ;
        }

        echo $ans . "\n" ;
    }
?>

<?php
// $stdin = fopen('input.txt', 'r');

for($t = intval(fgets(STDIN)); $t > 0; $t--){

	$n = intval(fgets(STDIN));

	$a = explode(' ', trim(fgets(STDIN)));

	$counts = [];
	foreach ($a as $ai) {
		$counts[$ai]++;
	}

	$m1 = max($counts);
	$m2 = count($counts);
	if($m1 == $m2){
		$m = $m1 - 1;
	} else {
		$m = min($m1, $m2);
	}

	echo min(floor(count($a) / 2), $m) . "\n";

}

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = trim(fgets(STDIN));
     $c = explode(" ", trim(fgets(STDIN)));
     $d = array();
     for($y = 0; $y < $b; $y++)
     {
          $d[$c[$y]]++;
     }
     arsort($d);
     $e = reset($d);
     $f = each($d);
     print min(floor($b / 2), $f[1]) . "\n";
}
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = trim(fgets(STDIN));
     $c = explode(" ", trim(fgets(STDIN)));
     $d = array();
     for($y = 0; $y < $b; $y++)
     {
          $d[$c[$y]]++;
     }
     arsort($d);
     $e = reset($d);
     $f = each($d);
     $g = $f[0];
     $h = $f[1];
     if($h == 1)
     {
          print min(floor($b / 2), count($d) - 1, $h) . "\n";
     }
     elseif($h > 1)
     {
          if(count($d) >= $h)
          {
               print min(floor($b / 2), count($d), $h) . "\n";
          }
          else
          {
               print min(floor($b / 2), count($d), $h - 1) . "\n";
          }
     }
}
?>

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

    function gap($n)
    {
        return $n < GAP_VAL ;
    }

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $n = intval(trim(fgets(STDIN))) ;
        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $count = array_fill(0, $n + 1, 0) ;
        $total_cnt = 0 ;

        for($i = 0; $i < $n; $i ++)
        {
            $in_str[$i] = intval($in_str[$i]) ;

            $count[$in_str[$i]] ++ ;
            if($count[$in_str[$i]] == 1) $total_cnt ++ ;
        }

        $ans = 0 ;
        for($i = 1; $i <= $n; $i ++)
        {
            if($count[$i] == 0) continue ;

            if($total_cnt - 1 >= $count[$i]) $ans = max($ans, $count[$i]) ;
            if($total_cnt >= $count[$i] - 1) $ans = max($ans, $count[$i] - 1) ;
        }

        echo $ans . "\n" ;
    }
?>


<?php

fscanf(STDIN, "%d",$cnt);
$j=1;
  while (fscanf(STDIN, "%d", $r)){
    $table[$j]=$r;
    $leveltable[$j]=0;
    $j++;
 }

function Up2head($i,$table,&$leveltable){
 $tmp=Array();
 $NextTop = $i;
 while (($NextTop!=-1)&&($leveltable[$NextTop]==0)){ //||($leveltable[$NextTop]=0)
   $tmp[] = $NextTop;
   $NextTop = $table[$NextTop];
   }
 for ($k=0;$k<count($tmp);$k++){
   $leveltable[$tmp[$k]]=$leveltable[$NextTop]+count($tmp)-$k;//+$leveltable[$NextTop];
 }
}

for ($i=1; $i<=$cnt;$i++){
  Up2head($i,$table,$leveltable);
}

echo max($leveltable);

?>

<?php

$f = fopen('php://stdin', 'r');
//$f = fopen('test.txt', 'r');
//$a = explode(" ",trim(fgets($f)));

$n = trim(fgets($f));

$a = array();
$start = array();
for($i=1;$i<=$n;$i++){
    $x = trim(fgets($f));
    $a[$i]['director'] = $x;
    $a[$x]['parents'][] = $i;

    if ($x==-1) $start[] = $i;
}

$count = 0;
$maxCount = 0;

function spusk($i,$count){

    global $a,$maxCount;
    $count++;
    $a[$i]['ves'] = $count;

    if ($count>$maxCount){
        $maxCount = $count;
    }
    if (isset($a[$i]['parents'])){
        foreach($a[$i]['parents'] as $k=>$item){
            spusk($item,$count);
        }
    }
}

spusk(-1,0);
echo $maxCount-1;
//print_r($maxCount);

<?php

for(;;){
  $n = fgets(STDIN, 4096);
  if($n === false) break;
  $n = (integer)(trim($n));

  unset($up);
  for($i=0;$i<$n;$i++){
    $k = fgets(STDIN, 4096);
    $k = (integer)(trim($k));

    $up[$i] = $k - 1;
  }

  unset($depth);

  $mx = 0;
  for($i=0;$i<$n;$i++){
    $res = dfs($i, $up, $depth);
    if($mx < $res) $mx = $res;
  }

  echo $mx."\n";
}

function dfs($now, &$up, &$depth){
  if(isset($depth[$now])) return $depth[$now];
  if($up[$now] < 0) $depth[$now] = 1;
  else              $depth[$now] = dfs($up[$now], $up, $depth)+1;
  return $depth[$now];
}

?>

<?php

fscanf(STDIN, "%d",$cnt);
$j=1;
  while (fscanf(STDIN, "%d", $r)){
    $table[$j]=$r;
    $leveltable[$j]=0;
    $j++;
 }

function Up2head($i,$table,&$leveltable){
 $tmp=Array();
 $NextTop = $i;

 while (($NextTop!=-1)){
   $tmp[] = $NextTop;
   $NextTop = $table[$NextTop];
   }

 for ($k=0;$k<count($tmp);$k++){
   $leveltable[$tmp[$k]]=count($tmp)-$k;
 }
}

for ($i=1; $i<=$cnt;$i++){
  Up2head($i,$table,$leveltable);
}

echo max($leveltable);

?>

<?php

fscanf(STDIN, "%d",$cnt);
$j=1;
  while (fscanf(STDIN, "%d", $r)){
	$table[$j]=$r;
	$leveltable[$j]=0;
    $j++;
 }

function GradeLevels(&$leveltable,$table,$cnt,$tolevel){
	$operate = false;
	for($i=1;$i<=$cnt;$i++)
		if ($leveltable[$i]>=($tolevel-1)) {
     		$leveltable[$table[$i]]=$tolevel;
     		$operate = true;
  		}
 	return $operate;
}

$tolevel = 1;
while (GradeLevels($leveltable,$table,$cnt, $tolevel))
	$tolevel++;

echo max($leveltable);

?>

<?php
$boss = array();

function input() {
    return rtrim(fgets(STDIN));
}

function dfs($a)
{
    global $boss;
    if ($boss[$a] == -1) return 1;
    return dfs($boss[$a]) + 1;
}

$n = input();
for ($i = 0; $i < $n; $i++)
{
    $boss[$i] = input();
    if ($boss[$i] != -1) $boss[$i]--;
}
$ret = 1;
for ($i = 0; $i < $n; $i++)
{
    $ret = max($ret, dfs($i));
}
echo "$ret\n";
?>

import java.util.*;

public class Solution
{
    public static void main(String[] args)
    {
        new Solution().calc();
    }

    int[] boss;
    void calc()
    {

        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        boss = new int[n];
        for (int i = 0; i < n; i++)
        {
            boss[i] = cin.nextInt();
            if (boss[i] != -1) boss[i]--;
        }
        int ret = 1;
        for (int i = 0; i < n; i++)
        {
            ret = Math.max(ret, dfs(i));
        }
        System.out.println(ret);
    }

    int dfs(int a)
    {
        if (boss[a] == -1) return 1;
        return dfs(boss[a]) + 1;
    }
}

<?php
fscanf(STDIN, "%d",$n);
  while (fscanf(STDIN, "%d", $r)){
    $table[]=$r;
    $leveltable[]=0;
 }

function GradeLevels(&$leveltable,$table,$tolevel){
    $operate = false;
    for($i=0;$i<count($table);$i++) // посадили всех рабочих
        if ($leveltable[$i]>=($tolevel-1)) {
            $leveltable[$table[$i]]=$tolevel;
            $operate = true;
        }
    return $operate;
}

$tolevel = 1;
while (GradeLevels($leveltable,$tolevel,$tolevel)){
    $tolevel++;
}

echo $tolevel+1;

?>

<?php

$f = fopen('php://stdin', 'r');
//$f = fopen('test.txt', 'r');
//$a = explode(" ",trim(fgets($f)));

$n = trim(fgets($f));

$a = array();
$start = array();
for($i=1;$i<=$n;$i++){
    $x = trim(fgets($f));
    $a[$i]['director'] = $x;
    $a[$x]['parents'][] = $i;

    if ($x==-1) $start[] = $i;
}

$count = 0;
$maxCount = 0;

function spusk($i,$count){

    global $a,$maxCount;
    $count++;
    $a[$i]['ves'] = $count;

    if ($count>=4){
        echo 3;
        exit;
    }
    if ($count>$maxCount){
        $maxCount = $count;
    }
    if (isset($a[$i]['parents'])){
        foreach($a[$i]['parents'] as $k=>$item){
            spusk($item,$count);
        }
    }
}

spusk(-1,0);
echo $maxCount-1;
//print_r($maxCount);


<?php

$fp = fopen('php://stdin', 'r');
$lines = [];

if ($fp) {
    while (($line = fgets($fp)) !== false) {

        $lines[] = trim($line);
    }

    fclose($fp);
}

$n =  $lines[0];

for ($i = 1; $i <= $n; $i++) {
    $result = workFunction($lines[$i]);
    fwrite(STDOUT, $result);
    fwrite(STDOUT, "\n");
}

function workFunction($number): int
{
    if ($number % 2 == 0) {
        return 0;
    }

    $digits  = array_map('intval', str_split($number));

    foreach ($digits as $key => $digit) {
        if ($digit % 2 == 0) {
            if ($key == 0) {
                return 1;
            }

            return 2;
        }
    }

    return -1;
}

<?

$n = trim(fgets(STDIN));

for($i=0;$i<$n;$i++){
	$currDigit = trim(fgets(STDIN));
	if($currDigit%2==0){
		echo "0\n";
	}else{
		$out = -1;
		$arr = str_split($currDigit);
		$k = 0;
		foreach($arr as $oneDigit){
			if($oneDigit%2==0){
				if($k==0){
					$out = 1;
				}else{
					$out = 2;
				}
				break;
			}
			$k++;
		}
		echo $out."\n";
	}
}

<?
$n = trim(fgets(STDIN));

for($i=0;$i<$n;$i++){
	$currDigit = trim(fgets(STDIN));
	if($currDigit%2==0){
		echo "0\n";
	}else{
		$out = -1;
		$arr = str_split($currDigit);
		foreach($arr as $oneDigit){
			if($oneDigit%2==0){
				$out = 2;
				break;
			}
		}
		echo $out."\n";
	}
}

<?
$n = trim(fgets(STDIN));

for($i=0;$i<$n;$i++){
	$currDigit = trim(fgets(STDIN));
	if($currDigit%2==0){
		echo "0\n";
	}else{
		$out = -1;
		$arr = explode("",$currDigit);
		foreach($arr as $oneDigit){
			if($oneDigit%2==0){
				$out = 2;
				break;
			}
		}
		echo $out."\n";
	}
}