program main implicit none integer*8::T,N,i,j,k,prev,num integer*8::L(100),S(100),hist(100),shist(100) read *,T do i=1,T read *,N read *,L(1:N) S(1:N) = qsortr(L(1:N)) hist = 0 prev = -1 k = 0 do j=1, N if(prev.eq.S(j)) then hist(k) = hist(k) + 1 else k = k + 1 hist(k) = 1 prev = S(j) end if end do if(k.lt.3) then print '(i0)',0 cycle end if shist(1:k) = qsortr(hist(1:k)) num = 0 do while(shist(3).ne.0) num = num + 1 shist(1:3) = shist(1:3) - 1 hist = shist shist(1:k) = qsortr(hist(1:k)) end do print '(i0)', num end do contains subroutine printer(L,N) integer*8::L(:),N,i do i=1,N write(*,'(i0," ")',advance='no') L(i) end do write (*,*) end subroutine printer recursive function qsortr(x) result(y) integer*8,intent(in) ::x(:) integer*8,allocatable::y(:) integer*8::pivot,total total = size(x) if (total <=1) then y = x else pivot = x(total/2) y = [qsortr(pack(x, x .gt. pivot)), & pack(x, x .eq. pivot), & qsortr(pack(x, x .lt. pivot))] endif end function qsortr end program main