package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	sc.NextInt()
	A := sc.NextIntArray()

	A = append(A, -1)
	K := maxA(A...)

	l := -1
	mx := 0
	for i := 0; i < len(A); i++ {
		if K == A[i] {
			if l == -1 {
				l = i
			}
		} else {
			if l >= 0 {
				mx = max(mx, i-l)
				l = -1
			}
		}
	}
	fmt.Println(mx)

}

func max(a int, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxA(arg ...int) int {
	var s = arg[0]
	for _, v := range arg {
		s = max(s, v)
	}
	return s
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()

	var n, max, tmp, counter, maxCounter int
	max = -1
	scanf("%d\n", &n)
	// input := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &tmp)
		if tmp > max {
			max = tmp
			counter = 1
			maxCounter = 1
		} else if tmp == max {
			counter++
		} else if tmp != max {
			counter = 0
		}
		if counter > maxCounter {
			maxCounter = counter
		}
	}
	// count maximum length of sequence where each elem equal to max
	printf("%+v\n", maxCounter)
}

package main

import (
 "bufio"
 "fmt"
 "os"
)

func main() {
	  in := bufio.NewReader(os.Stdin)
    best, ile := -1,-1;
		running := 0;
		var n int;
    fmt.Fscanf(in, "%d\n", &n);
		for ;n != 0; n-- {
			var x int;
			fmt.Fscanf(in, "%d", &x);
			if x < best {
				running = 0;
			} else if x == best {
					running++;
					if running > ile {
						ile = running;
					}
			}
			if x > best {
				best = x;
				ile = 1;
				running = 1;
			}
		}
		fmt.Println(ile);
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	buf := bufio.NewReader(os.Stdin)

	var maxx int = -1
	var n ,ans ,v ,cnt int
	fmt.Fscanf(buf, "%d\n", &n)
	ans=1
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		if v > maxx{
			maxx=v
			cnt=1
			ans=1
			continue
		}
		if v == maxx{
			cnt++
			if cnt > ans{
				ans = cnt
			}
		}else{
			cnt=0
		}
	}
	fmt.Printf("%d\n",ans)

}

/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main()  {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n,x int
	fmt.Fscan(in,&n)
	a := make([]int,0)
	mn := 0
	for i := 0 ; i <n ;i++{
		fmt.Fscan(in,&x)
		a = append(a,x)
		mn = max(mn,x)
	}
	len := 0
	ans := 0
	for i := 0 ;i < n;i++{
		if(a[i] == mn){
			len ++
		}else{
				ans = max(len,ans)
				len = 0
		}
	}
	ans = max(len,ans)
	fmt.Fprint(out,ans)

}

func max(i int, i2 int) int {
	if(i < i2){
		return i2
	}else{
		return i
	}
}
func min(i int , i2 int)  int {
	if(i < i2){
		return i
	}else{
		return i2
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	defer writer.Flush()
	var n, mx, t1 int
	mx = -1
	last := -2
	ans := 0
	cur := 0

	arr := []int{}

	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		scanf("%d", &t1)
		arr = append(arr, t1)
		if t1 > mx {
			mx = t1
		}
	}

	for i := 0; i < n; i++ {
		t1 = arr[i]
		if t1 == mx && last == mx {
			cur++
			ans = max(ans, cur)
		} else if t1 == mx {
			cur = 1
			ans = max(ans, cur)
		} else {
			cur = 0
		}
		last = t1
	}
	printf("%d", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()

	var n, max, tmp, counter, maxCounter int
	max = -1
	scanf("%d\n", &n)
	// input := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &tmp)
		if tmp > max {
			max = tmp
			counter = 1
		} else if tmp == max {
			counter++
		} else if tmp != max {
			counter = 0
		}
		if counter > maxCounter {
			maxCounter = counter
		}
	}
	// count maximum length of sequence where each elem equal to max
	printf("%+v\n", maxCounter)
}

package main

import (
 "bufio"
 "fmt"
 "os"
)

func main() {
	  in := bufio.NewReader(os.Stdin)
    best, ile := -1,-1;
		running := 0;
		var n int;
    fmt.Fscanf(in, "%d", &n);
		for ;n != 0; n-- {
			var x int;
			fmt.Fscanf(in, "%d", &x);
			if x < best {
				running = 0;
			} else if x == best {
					running++;
					if running > ile {
						ile = running;
					}
			}
			if x > best {
				best = x;
				ile = 1;
				running = 1;
			}
		}
		fmt.Println(ile);
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	buf := bufio.NewReader(os.Stdin)

	var maxx int = -1
	var n ,ans ,v ,cnt int
	fmt.Fscanf(buf, "%d\n", &n)
	ans=1
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		if v > maxx{
			maxx=v
			cnt=1
			continue
		}
		if v == maxx{
			cnt++
			if cnt > ans{
				ans = cnt
			}
		}else{
			cnt=0
		}
	}
	fmt.Printf("%d\n",ans)

}

/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	buf := bufio.NewReader(os.Stdin)

	var maxx int = -1
	var n ,ans ,v ,cnt int
	fmt.Fscanf(buf, "%d\n", &n)
	cnt=1
	for i := 0; i < n; i++ {

		fmt.Fscanf(buf, "%d", &v)
		if v > maxx{
			maxx=v
			cnt=1
			continue
		}
		if v == maxx{
			cnt++
			if cnt > ans{
				ans = cnt
			}
		}else{
			cnt=0
		}
	}
	fmt.Printf("%d\n",ans)

}

/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main()  {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n,x int
	fmt.Fscan(in,&n)
	a := make([]int,0)
	mn := 0
	for i := 0 ; i <n ;i++{
		fmt.Fscan(in,&x)
		a = append(a,x)
		mn = max(mn,x)
	}
	len := 0
	ans := 0
	for i := 0 ;i < n;i++{
		if(a[i] == mn){
			len ++
		}else{
				ans = max(len,ans)
				len = 0
		}
	}
	fmt.Fprint(out,ans)

}

func max(i int, i2 int) int {
	if(i < i2){
		return i2
	}else{
		return i
	}
}
func min(i int , i2 int)  int {
	if(i < i2){
		return i
	}else{
		return i2
	}

}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	cnt := map[int]int{}
	ans := 0
	is_zero := false
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
		if nums[i] == 0 {
			is_zero = true
			continue
		}
		cnt[nums[i]]++
		if cnt[nums[i]] == 2 {
			cnt[nums[i]]--
			ans++
		}
	}
	if ans > 0 || is_zero {
		ans += len(cnt)
	} else {
		ans = 1 + len(cnt)
	}
	fmt.Fprintln(w, ans)
}

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const MAXN = 2e5

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var ntests int
	fmt.Fscan(in, &ntests)
	for iiter := 0; iiter < ntests; iiter++ {
		var n int
		fmt.Fscan(in, &n)
		m := make(map[int]bool)
		same := false
		zero := false
		res := n
		for i := 0; i < n; i++ {
			var el int
			fmt.Fscan(in, &el)
			if el == 0 {
				zero = true
				res--
			}
			if _, ok := m[el]; ok {
				same = true
			} else {
				m[el] = true
			}
		}
		if zero {
			fmt.Fprintln(out, res)
		} else {
			if same {
				fmt.Fprintln(out, res)
			} else {
				fmt.Fprintln(out, res+1)
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var bufferedStdin = bufio.NewReader(os.Stdin)
var bufferedStdout = bufio.NewWriter(os.Stdout)

func solve() {
	var n int
	_, _ = fmt.Fscan(bufferedStdin, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscan(bufferedStdin, &a[i])
	}
	sort.Ints(a)
	var cnt = 0
	for i := 0; i < n; i++ {
		if a[i] != 0 {
			cnt++
		}
	}
	if a[0] == 0 {
		_, _ = fmt.Fprintf(bufferedStdout, "%d\n", cnt)
		return
	}
	for i := 1; i < n; i++ {
		if a[i] == a[i-1] {
			_, _ = fmt.Fprintf(bufferedStdout, "%d\n", n)
			return
		}
	}
	_, _ = fmt.Fprintf(bufferedStdout, "%d\n", n+1)
}

func main() {
	var t int
	_, _ = fmt.Fscan(bufferedStdin, &t)
	for i := 0; i < t; i++ {
		solve()
	}
	_ = bufferedStdout.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		sb := make([]bool,101)
		cnt := 0; cntzero := 0
		for _,a := range A {
			if a == 0 { cntzero++ }
			if !sb[a] { cnt++; sb[a] = true }
		}
		ans := 0
		if cntzero > 0 {
			ans = N - cntzero // Can make at most one zero in each operation
		} else if cnt < N {
			ans = N // already have a matching pair, so can use that to make first zero, and then one zero per op afterwards
		} else {
			ans = N+1 // need to use min to create matching pair, then same as above
		}
		fmt.Fprintln(wrtr,ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type CF1678A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1678A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1678A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1678A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1678A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1678A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1678A
Date: 6/06/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1678/A
Problem: CF1678A
**/
func (in *CF1678A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		mapa := map[int]int{}

		repeat := false
		for i := 0; i < n; i++ {
			num := in.NextInt()
			value, _ := mapa[num]
			mapa[num] = value + 1
			if value+1 >= 2 {
				repeat = true
			}
		}

		zero, exist := mapa[0]
		ans := 0
		if exist {
			ans = n - zero
		} else if repeat {
			ans = n
		} else {
			ans = n + 1
		}
		fmt.Println(ans)
	}
}

func NewCF1678A(r *bufio.Reader) *CF1678A {
	return &CF1678A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1678A(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int
		Fscan(in, &n)
		ma := make(map[int]int)
		mx := 0
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			ma[x]++
			if ma[x] > mx {
				mx = ma[x]
			}
		}

		ans := n - ma[0]

		if ma[0] == 0 {
			if mx == 1 {
				ans += 1
			}
		}
		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		m := make([]int, 101)
		var z int
		var hasDuplicate bool
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v == 0 {
				z++
			} else {
				if m[v] > 0 {
					hasDuplicate = true
				}
				m[v]++
			}
		}

		if z > 0 {
			fmt.Fprintln(out, n-z)
		} else if hasDuplicate {
			fmt.Fprintln(out, n)
		} else {
			fmt.Fprintln(out, n+1)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
)

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	for t := io.ReadInt(); t > 0; t-- {
		n := io.ReadInt()
		a := io.ReadIntSlice(n)
		sort.Ints(a)
		var od bool
		var zeros int
		for i := range a {
			if a[i] == 0 {
				zeros++
			}
			if i != 0 && a[i] == a[i-1] {
				od = true
			}
		}

		if zeros != 0 {
			fmt.Println(len(a) - zeros)
		} else {
			if od {
				fmt.Println(len(a))
			} else {
				fmt.Println(len(a) + 1)
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	_r = bufio.NewReader(os.Stdin)
	_w = bufio.NewWriter(os.Stdout)
)

type Tint interface {
	int | int32 | int64
}

func min[T Tint](x, y T) T {
	if x <= y {
		return x
	}
	return y
}
func max[T Tint](x, y T) T {
	if x >= y {
		return x
	}
	return y
}
func main() {
	defer _w.Flush()
	var _t int
	// n = 1
	fmt.Fscan(_r, &_t)
	for ; _t > 0; _t-- {
		var n int
		fmt.Fscan(_r, &n)
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			var x int
			fmt.Fscan(_r, &x)
			mp[x]++
		}
		ans := 0
		f := false
		for k, v := range mp {
			if k == 0 {
				f = true
				continue
			}
			if v > 1 {
				f = true
			}
			ans += v
		}
		if f == false {
			ans++
		}
		fmt.Fprintln(_w, ans)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)

	mp := make(map[int]bool)

	var a int
	zeroCnt := 0
	dup := false
	for i := 0; i < n; i++ {
		Fscan(in, &a)

		if a == 0 {
			zeroCnt++
		}

		_, ok := mp[a]
		if ok {
			dup = true
		} else {
			mp[a] = true
		}
	}

	var ans int
	if zeroCnt != 0 {
		ans = n - zeroCnt
	} else if dup {
		ans = n
	} else {
		ans = n + 1
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		same := false
		c := make([]int, 110)
		for i := 0; i < n; i++ {
			v := myio.ReadInt()
			c[v]++
			if c[v] > 1 {
				same = true
			}
		}
		if c[0] == 0 {
			if same {
				myio.Println(n)
			} else {
				myio.Println(n + 1)
			}
		} else {
			myio.Println(n - c[0])
		}
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	_r = bufio.NewReader(os.Stdin)
	_w = bufio.NewWriter(os.Stdout)
)

type Tint interface {
	int | int32 | int64
}

func min[T Tint](x, y T) T {
	if x <= y {
		return x
	}
	return y
}
func max[T Tint](x, y T) T {
	if x >= y {
		return x
	}
	return y
}
func main() {
	var _t int
	// n = 1
	fmt.Fscan(_r, &_t)
	for ; _t > 0; _t-- {
		var n int
		fmt.Fscan(_r, &n)
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			var x int
			fmt.Fscan(_r, &x)
			mp[x]++
		}
		ans := 0
		f := false
		for k, v := range mp {
			if k == 0 {
				f = true
				continue
			}
			if v > 1 {
				f = true
			}
			ans += v
		}
		if f == false {
			ans++
		}
		fmt.Fprintln(_w, ans)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)

	mp := make(map[int]bool)

	var a int
	zeroCnt := 0
	dup := false
	for i := 0; i < n; i++ {
		Fscan(in, &a)

		if a == 0 {
			zeroCnt++
		}

		_, ok := mp[a]
		if ok {
			dup = true
		} else {
			mp[a] = true
		}
	}

	var ans int
	if zeroCnt != 0 {
		ans = n - zeroCnt
	} else if dup {
		ans = n + 1
	} else {
		ans = n + 2
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		same := false
		c := make([]int, 110)
		for i := 0; i < n; i++ {
			v := myio.ReadInt()
			c[v]++
			if c[v] > 1 {
				same = true
			}
		}
		if c[0] == 0 {
			if same {
				fmt.Println(n)
			} else {
				fmt.Println(n + 1)
			}
		} else {
			myio.Println(n - c[0])
		}
	}
	for ; t > 0; t-- {
		sol()
	}
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	resetBit()
	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
		updateBit(p[0], p[1])
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := 1; i <= n; i++ {
		j := match[i]
		if j > 0 {
			cur := 1
			jj := queryBit(j)
			for jj > j {
				j = jj
				cur++
				jj = queryBit(j)
			}
			ans += (acc[j] - acc[i-1]) - (cur + 1)
			i = j
		}
	}

	printf("%d\n", ans)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	pr := make([]int, n+1)
	for i := range a {
		Fscan(in, &a[i])
		pr[a[i]] = i
	}
	ans := n
	for i, l := 0, 0; i < n; {
		ans--
		r := l
		for _, v := range a[i : l+1] {
			if pr[v] > r {
				r = pr[v]
			}
		}
		if r == l {
			i = l + 1
			l++
		} else {
			i = l
			l = r
		}
	}
	Fprint(out, ans)
}

func main() { run(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	resetBit()
	updateBit(n+1, acc[n])
	for i := n; i >= 1; i-- {
		j := match[i]
		if j > 0 {
			// dp[i] = dp[k] + acc[k-1]-acc[i], k <= j+1
			dp[i] = max(dp[i+1], queryBit(j)-acc[i])
			// dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
			// printf("dp[%d] = max(%d,%d + %d-%d)\n", i, dp[i], dp[j], acc[j-1], acc[i])
		}
		updateBit(i, dp[i]+acc[i-1])
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}

	for i := n; i >= 1; i-- {
		j := match[i]
		if j > 0 {
			// dp[i] = dp[k] + acc[k-1]-acc[i], k <= j
			dp[i] = max(dp[i+1], queryBit(j-1)-acc[i])
			dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
			// printf("dp[%d] = max(%d,%d + %d-%d)\n", i, dp[i], dp[j], acc[j-1], acc[i])
			updateBit(i, dp[i]+acc[i-1])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		if last[i] == 0 {
			reach := queryBit(first[i] - 1)
			if reach > first[i] {
				ans++
			}
			first[i] = 0
		} else {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := n; i >= 1; i-- {
		dp[i] = dp[i+1]
		j := match[i]
		if j > 0 {
			dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := n; i >= 1; i-- {
		dp[i] = dp[i+1]
		j := match[i]
		if j > 0 {
			dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] >= pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] > pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		if i < lo {
			dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] > pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		if i > lo {
			dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] > pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	var lonelyPair []bool
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
			lonelyPair = append(lonelyPair, reach < first[i])
		}
	}

	// printf("ans1 = %d\n", ans)
	// printf("pairs = %v\n", pairs)
	// printf("lonelyPair = %v\n", lonelyPair)

	resetBit()
	for i := 0; i < len(pairs); i++ {
		newFirst := n - pairs[i][1] + 1
		newLast := n - pairs[i][0] + 1
		pairs[i][0] = newFirst
		pairs[i][1] = newLast
		updateBit(pairs[i][0], pairs[i][1])
	}

	entangledPairs := 0
	for i, pair := range pairs {
		reach := queryBit(pair[0] - 1)
		lonelyPair[i] = lonelyPair[i] && reach < pair[0]
		if !lonelyPair[i] {
			entangledPairs++
		}
	}

	// printf("pairs = %v\n", pairs)
	// printf("lonelyPair = %v\n", lonelyPair)

	// printf("entangledPairs = %d\n", entangledPairs)
	ans += max(entangledPairs-1, 0)
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	var lonelyPair []bool
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
			lonelyPair = append(lonelyPair, reach < first[i])
		}
	}

	// printf("ans1 = %d\n", ans)

	resetBit()
	for i := 0; i < len(pairs); i++ {
		newFirst := n - pairs[i][1] + 1
		newLast := n - pairs[i][0] + 1
		pairs[i][0] = newFirst
		pairs[i][1] = newLast
		updateBit(pairs[i][0], pairs[i][1])
	}

	entangledPairs := 0
	for i, pair := range pairs {
		reach := queryBit(pair[0] - 1)
		lonelyPair[i] = lonelyPair[i] && reach < pair[0]
		if !lonelyPair[i] {
			entangledPairs++
		}
	}

	// printf("entangledPairs = %d\n", entangledPairs)
	ans += entangledPairs - 1
	printf("%d\n", ans)
}