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 ans(st []int) int {
	a1 := 0
	a2 := 0
	d := 0
	for i := 0; i < len(st); i++ {
		if st[i] >= 2 {
			d += 2
			a1++
			a2++
		} else {
			d += 1
			if a1 < a2 {
				a1++
			} else {
				a2++
			}
		}
	}

	//if st[len(st)-1] >= 2 && st[len(st)-2] == 1 {
	//	if a1 < a2 {
	//		a1++
	//	} else {
	//		a2++
	//	}
	//}

	return min(a1, a2) + d%2
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		res := ans(st)
		//fmt.Println(st)
		for i := 0; i < len(st)/2; i++ {
			st[i], st[len(st)-1-i] = st[len(st)-1-i], st[i]
		}
		//fmt.Println(st)
		res = max(ans(st), res)
		fmt.Println(res)
	}
}

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"math"
	"os"
	"sort"
)

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 int
		Fscan(in, &n)

		a := make([]int, n)
		ma := make(map[int]int)
		mb := make(map[int]int)

		b := make([]int, n)
		c := make([]int, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}

		sort.Ints(a)
		l := 0
		r := 0

		k := 0
		flag := true
		for k < n {
			if k >= 2 && a[k] == a[k-1] && a[k] == a[k-2] {
				k++
				continue
			}
			if flag {
				flag = false
				b[l] = a[k]
				ma[a[k]]++
				l++
				k++
			} else {
				flag = true
				c[r] = a[k]
				mb[a[k]]++
				r++
				k++
			}
		}

		mx := b[0]
		ta := L1
		for i := 1; i < l; i++ {
			if b[i] > mx {
				mx = b[i]
				ta++
			}
		}

		mi := c[0]
		tb := L1

		for i := 1; i < r; i++ {
			if c[i] > mi {
				mi = c[i]
				tb++
			}
		}

		if ta < tb {
			ta, tb = tb, ta
			ma, mb = mb, ma
		}
		for key, v := range ma {
			if v > 0 && mb[key] == 0 {
				tb++
				break
			}
		}

		Fprintln(out, Min(ta, tb))
	}

}

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
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    Int // The value of the item; arbitrary.
	priority Int // The priority of the item in the queue.
	indices  Int
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func NewItem(value, priority Int) *Item {
	return &Item{
		value:    value,
		priority: priority,
		index:    -1,
	}

}

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Int, priority Int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

type SegTree struct {
	st []Segment
}

type Segment struct {
	l, r, val, add, minIndex Int
}

func newSegTree(n Int) SegTree {
	segTree := SegTree{make([]Segment, n<<2)}
	segTree.Build(1, 1, n)
	return segTree
}

func (segTree SegTree) Build(x, l, r Int) {
	segTree.st[x].l = l
	segTree.st[x].r = r
	if l == r {
		segTree.st[x].minIndex = l
		return
	}
	mid := (l + r) >> 1
	segTree.Build(x<<1, l, mid)
	segTree.Build(x<<1|1, mid+1, r)
}

func (segTree SegTree) push(x Int) {
	if segTree.st[x].add != 0 {
		segTree.modifyOne(x<<1, segTree.st[x].add)
		segTree.modifyOne(x<<1|1, segTree.st[x].add)
		segTree.st[x].add = 0
	}
}

func (segTree SegTree) update(x Int) {
	if segTree.st[x<<1].val < segTree.st[x<<1|1].val {
		segTree.st[x].val = segTree.st[x<<1].val
		segTree.st[x].minIndex = segTree.st[x<<1].minIndex
	} else {
		segTree.st[x].val = segTree.st[x<<1|1].val
		segTree.st[x].minIndex = segTree.st[x<<1|1].minIndex
	}
}

func (segTree SegTree) modifyOne(x, add Int) {
	segTree.st[x].val += add
	if segTree.st[x].l != segTree.st[x].r {
		segTree.st[x].add += add
	}
}

func (segTree SegTree) Modify(x, l, r, add Int) {
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		segTree.modifyOne(x, add)
		return
	}
	segTree.push(x)
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		segTree.Modify(x<<1, l, r, add)
	}
	if mid < r {
		segTree.Modify(x<<1|1, l, r, add)
	}
	segTree.update(x)
}

func updateRes(cRes, cIndex, res, index Int) (Int, Int) {
	if cRes > res {
		return res, index
	}
	return cRes, cIndex
}

func (segTree SegTree) Query(x, l, r Int) (Int, Int) {
	res := Int(math.MaxInt64)
	resInd := -L1
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		res, resInd = updateRes(res, resInd, segTree.st[x].val, segTree.st[x].minIndex)
		return res, resInd
	}
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		sonRes, sonInd := segTree.Query(x<<1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	if mid < r {
		sonRes, sonInd := segTree.Query(x<<1|1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	return res, resInd
}

type Graph struct {
	nodeSize Int
	edges    []Edge
	head     []Int
	dis      []Int
	vis      []bool
	nCnt     Int
	ans      []Int
}

type Edge struct {
	u, v, c, indices, next Int
}

func NewGraph(nodeSize Int) *Graph {
	return &Graph{
		nodeSize: nodeSize,
		edges:    make([]Edge, (2e5+10)<<2),
		head:     make([]Int, nodeSize+100),
		dis:      make([]Int, nodeSize+100),
		vis:      make([]bool, nodeSize+100),
		nCnt:     L1,
		ans:      make([]Int, nodeSize),
	}
}

func (g *Graph) addEdge(u Int, v Int, cost Int, indices Int) {
	g.nCnt++
	g.edges[g.nCnt] = Edge{u, v, cost, indices, g.head[u]}
	g.head[u] = g.nCnt
}

func (g *Graph) Dijkstra(begin Int) {
	for i := L1; i <= g.nodeSize; i++ {
		g.dis[i] = math.MaxInt64
	}
	pq := make(PriorityQueue, 0)
	heap.Init(&pq)

	g.dis[begin] = 0
	heap.Push(&pq, &Item{begin, 0, -1, -1})

	id := L0

	for len(pq) > 0 {
		it := heap.Pop(&pq).(*Item)
		u := it.value
		if g.vis[u] {
			continue
		}
		g.vis[u] = true

		if it.indices != -1 && id < g.nodeSize-1 {
			g.ans[id] = it.indices
			id++
		}

		for i := g.head[u]; i != 0; i = g.edges[i].next {
			v := g.edges[i].v
			if g.dis[v] > g.dis[u]+g.edges[i].c {
				g.dis[v] = g.dis[u] + g.edges[i].c

				heap.Push(&pq, &Item{v, g.dis[v], g.edges[i].indices, -1})
			}
		}
	}
}

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		c := map[int]int{}
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &v)
			c[v]++
		}
		s := 0
		for _, c := range c {
			if c > 2 {
				c = 2
			}
			s += c
		}
		Fprintln(out, (s+1)/2)
	}
}

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

package main

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

func A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		Map := make(map[int]int, 0)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			Map[a[i]]++
		}
		var cnt, cnt2 int
		for _, v := range Map {
			if v == 1 {
				cnt++
			} else {
				cnt2++
			}
		}
		Fprintln(out, cnt2+(cnt+1)/2)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A() }

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)
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 gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
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)
		sort.Slice(A,func(i,j int) bool { return A[i] < A[j] } )
		cnts := []int{}
		last := A[0]; cnt := 1
		for i:=1;i<N;i++ { if A[i] == last { cnt++ } else { cnts = append(cnts,cnt); last = A[i]; cnt = 1 } }
		cnts = append(cnts,cnt)
		nummult,numones := 0,0
		for _,c := range cnts { if c == 1 { numones++ } else { nummult++ } }
		ans := (numones+1)/2 + nummult
		fmt.Fprintln(wrtr,ans)
	}
}

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 ans(st []int) int {
	a1 := 0
	a2 := 0
	d := 0
	for i := 0; i < len(st); i++ {
		d += st[i]
		if st[i] >= 2 {
			a1++
			a2++
		} else {
			if a1 < a2 {
				a1++
			} else {
				a2++
			}
		}
	}

	//if st[len(st)-1] >= 2 && st[len(st)-2] == 1 {
	//	if a1 < a2 {
	//		a1++
	//	} else {
	//		a2++
	//	}
	//}

	return min(a1, a2) + d%2
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		res := ans(st)
		//fmt.Println(st)
		for i := 0; i < len(st)/2; i++ {
			st[i], st[len(st)-1-i] = st[len(st)-1-i], st[i]
		}
		//fmt.Println(st)
		res = max(ans(st), res)
		fmt.Println(res)
	}
}

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 ans(st []int) int {
	a1 := 0
	a2 := 0
	d := 0
	for i := 0; i < len(st)-1; i++ {
		if st[i] >= 2 {
			a1++
			a2++
		} else if d == 0 {
			a1++
			a2++
			d++
		} else {
			if a1 < a2 {
				a1++
			} else {
				a2++
			}
		}
	}

	//if st[len(st)-1] >= 2 && st[len(st)-2] == 1 {
	//	if a1 < a2 {
	//		a1++
	//	} else {
	//		a2++
	//	}
	//}

	return min(a1, a2) + 1
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		res := ans(st)
		fmt.Println(st)
		for i := 0; i < len(st)/2; i++ {
			st[i], st[len(st)-1-i] = st[len(st)-1-i], st[i]
		}
		fmt.Println(st)
		res = max(ans(st), res)
		fmt.Println(res)
	}
}

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 ans(st []int) int {
	a1 := 0
	a2 := 0
	for i := 0; i < len(st)-1; i++ {
		if st[i] >= 2 {
			a1++
			a2++
		} else {
			if a1 < a2 {
				a1++
			} else {
				a2++
			}
		}
	}

	if st[len(st)-1] >= 2 && st[len(st)-2] == 1 {
		if a1 < a2 {
			a1++
		} else {
			a2++
		}
	}

	return min(a1, a2) + 1
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		res := ans(st)
		for i := 0; i < len(st)/2; i++ {
			st[i], st[len(st)-1-i] = st[len(st)-1-i], st[i]
		}
		res = max(ans(st), res)
		fmt.Println(res)
	}
}

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 isPossible(st []int, ans int) bool {
	if len(st) < ans {
		return false
	}

	mp := make(map[int]struct{})

	now := ans - 1
	for i := 0; i < len(st)-1; i++ {
		if now == 0 {
			break
		}
		if st[i] >= 2 {
			mp[i] = struct{}{}
			now--
		}
	}
	for i := 0; i < len(st)-1; i++ {
		if now == 0 {
			break
		}
		if _, ok := mp[i]; !ok && st[i] >= 1 {
			mp[i] = struct{}{}
			now--
		}
	}

	remain := ans - 1
	for i := len(st) - 2; i >= 0; i-- {
		if _, ok := mp[i]; (!ok && st[i] >= 1) || (ok && st[i] >= 2) {
			remain--
		}
	}

	//fmt.Println(st, mp, ans, remain)

	return remain <= 0
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		a1 := 0
		a2 := 0
		for i := 0; i < len(st)-1; i++ {
			if st[i] >= 2 {
				a1++
				a2++
			} else {
				if a1 < a2 {
					a1++
				} else {
					a2++
				}
			}
		}

		if st[len(st)-1] >= 2 && st[len(st)-2] == 1 {
			if a1 < a2 {
				a1++
			} else {
				a2++
			}
		}

		fmt.Println(min(a1, a2) + 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 isPossible(st []int, ans int) bool {
	if len(st) < ans {
		return false
	}

	mp := make(map[int]struct{})

	now := ans - 1
	for i := 0; i < len(st)-1; i++ {
		if now == 0 {
			break
		}
		if st[i] >= 2 {
			mp[i] = struct{}{}
			now--
		}
	}
	for i := 0; i < len(st)-1; i++ {
		if now == 0 {
			break
		}
		if _, ok := mp[i]; !ok && st[i] >= 1 {
			mp[i] = struct{}{}
			now--
		}
	}

	remain := ans - 1
	for i := len(st) - 2; i >= 0; i-- {
		if _, ok := mp[i]; (!ok && st[i] >= 1) || (ok && st[i] >= 2) {
			remain--
		}
	}

	//fmt.Println(st, mp, ans, remain)

	return remain <= 0
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		a1 := 0
		a2 := 0
		for i := 0; i < len(st)-1; i++ {
			if st[i] >= 2 {
				a1++
				a2++
			} else {
				if a1 < a2 {
					a1++
				} else {
					a2++
				}
			}
		}

		fmt.Println(min(a1, a2) + 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 isPossible(st []int, ans int) bool {
	if len(st) < ans {
		return false
	}

	mp := make(map[int]struct{})

	now := ans - 1
	for i := 0; i < len(st)-1; i++ {
		if now == 0 {
			break
		}
		if st[i] >= 2 {
			mp[i] = struct{}{}
			now--
		}
	}
	for i := 0; i < len(st)-1; i++ {
		if now == 0 {
			break
		}
		if _, ok := mp[i]; !ok && st[i] >= 1 {
			mp[i] = struct{}{}
			now--
		}
	}

	remain := ans - 1
	for i := len(st) - 2; i >= 0; i-- {
		if _, ok := mp[i]; (!ok && st[i] >= 1) || (ok && st[i] >= 2) {
			remain--
		}
	}

	//fmt.Println(st, mp, ans, remain)

	return remain <= 0
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		l, r := 0, 1_000_000

		for r-l > 1 {
			m := (r + l) / 2
			if isPossible(st, m) {
				l = m
			} else {
				r = m
			}
		}

		fmt.Println(l)
	}
}

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 isPossible(st []int, ans int) bool {
	if len(st) < ans {
		return false
	}

	remain := ans - 1
	now := ans - 1

	for i := len(st) - 2; i >= 0; i-- {
		if now != 0 {
			if st[i] >= 2 {
				remain--
			}
			now--
		} else {
			if st[i] >= 1 {
				remain--
			}
		}
	}

	return remain <= 0
}

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)

		st := make([]int, 1)
		for i := 0; i < len(a); i++ {
			if i != 0 && a[i] != a[i-1] {
				st = append(st, 0)
			}
			st[len(st)-1]++
		}

		if len(st) == 1 {
			fmt.Println(1)
			continue
		}

		l, r := 0, 1_000_000

		for r-l > 1 {
			m := (r + l) / 2
			if isPossible(st, m) {
				l = m
			} else {
				r = m
			}
		}

		fmt.Println(l)
	}
}

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"math"
	"os"
	"sort"
)

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 int
		Fscan(in, &n)

		a := make([]int, n)
		ma := make(map[int]int)
		mb := make(map[int]int)

		b := make([]int, n)
		c := make([]int, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}

		sort.Ints(a)
		l := 0
		r := 0

		k := 0
		flag := true
		for k < n {
			if flag {
				flag = false
				b[l] = a[k]
				ma[a[k]]++
				l++
				k++
			} else {
				flag = true
				c[r] = a[k]
				mb[a[k]]++
				r++
				k++
			}
		}

		mx := b[0]
		ta := L1
		for i := 1; i < l; i++ {
			if b[i] > mx {
				mx = b[i]
				ta++
			}
		}

		mi := c[0]
		tb := L1
		for i := 1; i < r; i++ {
			if c[i] > mi {
				mi = c[i]
				tb++
			}
		}

		if ta < tb {
			ta, tb = tb, ta
			ma, mb = mb, ma
		}
		for key, v := range ma {
			if v > 0 && mb[key] == 0 {
				tb++
				break
			}
		}

		Fprintln(out, Min(ta, tb))
	}

}

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
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    Int // The value of the item; arbitrary.
	priority Int // The priority of the item in the queue.
	indices  Int
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func NewItem(value, priority Int) *Item {
	return &Item{
		value:    value,
		priority: priority,
		index:    -1,
	}

}

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Int, priority Int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

type SegTree struct {
	st []Segment
}

type Segment struct {
	l, r, val, add, minIndex Int
}

func newSegTree(n Int) SegTree {
	segTree := SegTree{make([]Segment, n<<2)}
	segTree.Build(1, 1, n)
	return segTree
}

func (segTree SegTree) Build(x, l, r Int) {
	segTree.st[x].l = l
	segTree.st[x].r = r
	if l == r {
		segTree.st[x].minIndex = l
		return
	}
	mid := (l + r) >> 1
	segTree.Build(x<<1, l, mid)
	segTree.Build(x<<1|1, mid+1, r)
}

func (segTree SegTree) push(x Int) {
	if segTree.st[x].add != 0 {
		segTree.modifyOne(x<<1, segTree.st[x].add)
		segTree.modifyOne(x<<1|1, segTree.st[x].add)
		segTree.st[x].add = 0
	}
}

func (segTree SegTree) update(x Int) {
	if segTree.st[x<<1].val < segTree.st[x<<1|1].val {
		segTree.st[x].val = segTree.st[x<<1].val
		segTree.st[x].minIndex = segTree.st[x<<1].minIndex
	} else {
		segTree.st[x].val = segTree.st[x<<1|1].val
		segTree.st[x].minIndex = segTree.st[x<<1|1].minIndex
	}
}

func (segTree SegTree) modifyOne(x, add Int) {
	segTree.st[x].val += add
	if segTree.st[x].l != segTree.st[x].r {
		segTree.st[x].add += add
	}
}

func (segTree SegTree) Modify(x, l, r, add Int) {
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		segTree.modifyOne(x, add)
		return
	}
	segTree.push(x)
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		segTree.Modify(x<<1, l, r, add)
	}
	if mid < r {
		segTree.Modify(x<<1|1, l, r, add)
	}
	segTree.update(x)
}

func updateRes(cRes, cIndex, res, index Int) (Int, Int) {
	if cRes > res {
		return res, index
	}
	return cRes, cIndex
}

func (segTree SegTree) Query(x, l, r Int) (Int, Int) {
	res := Int(math.MaxInt64)
	resInd := -L1
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		res, resInd = updateRes(res, resInd, segTree.st[x].val, segTree.st[x].minIndex)
		return res, resInd
	}
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		sonRes, sonInd := segTree.Query(x<<1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	if mid < r {
		sonRes, sonInd := segTree.Query(x<<1|1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	return res, resInd
}

type Graph struct {
	nodeSize Int
	edges    []Edge
	head     []Int
	dis      []Int
	vis      []bool
	nCnt     Int
	ans      []Int
}

type Edge struct {
	u, v, c, indices, next Int
}

func NewGraph(nodeSize Int) *Graph {
	return &Graph{
		nodeSize: nodeSize,
		edges:    make([]Edge, (2e5+10)<<2),
		head:     make([]Int, nodeSize+100),
		dis:      make([]Int, nodeSize+100),
		vis:      make([]bool, nodeSize+100),
		nCnt:     L1,
		ans:      make([]Int, nodeSize),
	}
}

func (g *Graph) addEdge(u Int, v Int, cost Int, indices Int) {
	g.nCnt++
	g.edges[g.nCnt] = Edge{u, v, cost, indices, g.head[u]}
	g.head[u] = g.nCnt
}

func (g *Graph) Dijkstra(begin Int) {
	for i := L1; i <= g.nodeSize; i++ {
		g.dis[i] = math.MaxInt64
	}
	pq := make(PriorityQueue, 0)
	heap.Init(&pq)

	g.dis[begin] = 0
	heap.Push(&pq, &Item{begin, 0, -1, -1})

	id := L0

	for len(pq) > 0 {
		it := heap.Pop(&pq).(*Item)
		u := it.value
		if g.vis[u] {
			continue
		}
		g.vis[u] = true

		if it.indices != -1 && id < g.nodeSize-1 {
			g.ans[id] = it.indices
			id++
		}

		for i := g.head[u]; i != 0; i = g.edges[i].next {
			v := g.edges[i].v
			if g.dis[v] > g.dis[u]+g.edges[i].c {
				g.dis[v] = g.dis[u] + g.edges[i].c

				heap.Push(&pq, &Item{v, g.dis[v], g.edges[i].indices, -1})
			}
		}
	}
}

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"math"
	"os"
	"sort"
)

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 int
		Fscan(in, &n)

		a := make([]int, n)
		ma := make(map[int]int)
		mb := make(map[int]int)

		b := make([]int, n)
		c := make([]int, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}

		sort.Ints(a)
		l := 0
		r := 0

		k := 0
		flag := true
		for k < n {
			if flag {
				flag = false
				b[l] = a[k]
				ma[a[k]]++
				l++
				k++
			} else {
				flag = true
				c[r] = a[k]
				mb[a[k]]++
				r++
				k++
			}
		}

		mx := b[0]
		ta := L1
		for i := 1; i < l; i++ {
			if b[i] > mx {
				mx = b[i]
				ta++
			}
		}

		mi := c[0]
		tb := L1
		for i := 1; i < r; i++ {
			if c[i] > mi {
				mi = c[i]
				tb++
			}
		}

		for key, v := range ma {
			if v == 1 && mb[key] == 0 {
				tb++
				break
			}
		}

		Fprintln(out, Min(ta, tb))
	}

}

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
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    Int // The value of the item; arbitrary.
	priority Int // The priority of the item in the queue.
	indices  Int
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func NewItem(value, priority Int) *Item {
	return &Item{
		value:    value,
		priority: priority,
		index:    -1,
	}

}

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Int, priority Int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

type SegTree struct {
	st []Segment
}

type Segment struct {
	l, r, val, add, minIndex Int
}

func newSegTree(n Int) SegTree {
	segTree := SegTree{make([]Segment, n<<2)}
	segTree.Build(1, 1, n)
	return segTree
}

func (segTree SegTree) Build(x, l, r Int) {
	segTree.st[x].l = l
	segTree.st[x].r = r
	if l == r {
		segTree.st[x].minIndex = l
		return
	}
	mid := (l + r) >> 1
	segTree.Build(x<<1, l, mid)
	segTree.Build(x<<1|1, mid+1, r)
}

func (segTree SegTree) push(x Int) {
	if segTree.st[x].add != 0 {
		segTree.modifyOne(x<<1, segTree.st[x].add)
		segTree.modifyOne(x<<1|1, segTree.st[x].add)
		segTree.st[x].add = 0
	}
}

func (segTree SegTree) update(x Int) {
	if segTree.st[x<<1].val < segTree.st[x<<1|1].val {
		segTree.st[x].val = segTree.st[x<<1].val
		segTree.st[x].minIndex = segTree.st[x<<1].minIndex
	} else {
		segTree.st[x].val = segTree.st[x<<1|1].val
		segTree.st[x].minIndex = segTree.st[x<<1|1].minIndex
	}
}

func (segTree SegTree) modifyOne(x, add Int) {
	segTree.st[x].val += add
	if segTree.st[x].l != segTree.st[x].r {
		segTree.st[x].add += add
	}
}

func (segTree SegTree) Modify(x, l, r, add Int) {
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		segTree.modifyOne(x, add)
		return
	}
	segTree.push(x)
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		segTree.Modify(x<<1, l, r, add)
	}
	if mid < r {
		segTree.Modify(x<<1|1, l, r, add)
	}
	segTree.update(x)
}

func updateRes(cRes, cIndex, res, index Int) (Int, Int) {
	if cRes > res {
		return res, index
	}
	return cRes, cIndex
}

func (segTree SegTree) Query(x, l, r Int) (Int, Int) {
	res := Int(math.MaxInt64)
	resInd := -L1
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		res, resInd = updateRes(res, resInd, segTree.st[x].val, segTree.st[x].minIndex)
		return res, resInd
	}
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		sonRes, sonInd := segTree.Query(x<<1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	if mid < r {
		sonRes, sonInd := segTree.Query(x<<1|1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	return res, resInd
}

type Graph struct {
	nodeSize Int
	edges    []Edge
	head     []Int
	dis      []Int
	vis      []bool
	nCnt     Int
	ans      []Int
}

type Edge struct {
	u, v, c, indices, next Int
}

func NewGraph(nodeSize Int) *Graph {
	return &Graph{
		nodeSize: nodeSize,
		edges:    make([]Edge, (2e5+10)<<2),
		head:     make([]Int, nodeSize+100),
		dis:      make([]Int, nodeSize+100),
		vis:      make([]bool, nodeSize+100),
		nCnt:     L1,
		ans:      make([]Int, nodeSize),
	}
}

func (g *Graph) addEdge(u Int, v Int, cost Int, indices Int) {
	g.nCnt++
	g.edges[g.nCnt] = Edge{u, v, cost, indices, g.head[u]}
	g.head[u] = g.nCnt
}

func (g *Graph) Dijkstra(begin Int) {
	for i := L1; i <= g.nodeSize; i++ {
		g.dis[i] = math.MaxInt64
	}
	pq := make(PriorityQueue, 0)
	heap.Init(&pq)

	g.dis[begin] = 0
	heap.Push(&pq, &Item{begin, 0, -1, -1})

	id := L0

	for len(pq) > 0 {
		it := heap.Pop(&pq).(*Item)
		u := it.value
		if g.vis[u] {
			continue
		}
		g.vis[u] = true

		if it.indices != -1 && id < g.nodeSize-1 {
			g.ans[id] = it.indices
			id++
		}

		for i := g.head[u]; i != 0; i = g.edges[i].next {
			v := g.edges[i].v
			if g.dis[v] > g.dis[u]+g.edges[i].c {
				g.dis[v] = g.dis[u] + g.edges[i].c

				heap.Push(&pq, &Item{v, g.dis[v], g.edges[i].indices, -1})
			}
		}
	}
}

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"math"
	"os"
	"sort"
)

type Int int64

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

func solver(b []int, n int) Int {
	ma := b[0]
	ta := L1
	for i := 1; i < n; i++ {
		if b[i] > ma {
			ma = b[i]
			ta++
		}
	}

	mb := b[n-1]
	tb := L1
	for i := n - 2; i >= 0; i-- {
		if b[i] > mb {
			mb = b[i]
			tb++
		}
	}

	return Min(ta, tb)
}

func solver2(b []int, n int) Int {
	ma := b[0]
	ta := L1
	for i := 1; i < n; i++ {
		if b[i] < ma {
			ma = b[i]
			ta++
		}
	}

	mb := b[n-1]
	tb := L1
	for i := n - 2; i >= 0; i-- {
		if b[i] < mb {
			mb = b[i]
			tb++
		}
	}

	return Min(ta, tb)
}

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 int
		Fscan(in, &n)

		a := make([]int, n)
		b := make([]int, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}

		sort.Ints(a)
		l := 0
		r := n - 1

		k := 0
		flag := true
		for k < n {
			if flag {
				flag = false
				b[l] = a[k]
				l++
				k++
			} else {
				flag = true
				b[r] = a[k]
				r--
				k++
			}
		}

		ans := solver(b, n)

		l = 0
		r = n - 1
		k = n - 1
		flag = true
		for k >= 0 {
			if flag {
				flag = false
				b[l] = a[k]
				l++
				k--
			} else {
				flag = true
				b[r] = a[k]
				r--
				k--
			}

		}
		ans = Max(ans, solver2(b, n))

		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
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    Int // The value of the item; arbitrary.
	priority Int // The priority of the item in the queue.
	indices  Int
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func NewItem(value, priority Int) *Item {
	return &Item{
		value:    value,
		priority: priority,
		index:    -1,
	}

}

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Int, priority Int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

type SegTree struct {
	st []Segment
}

type Segment struct {
	l, r, val, add, minIndex Int
}

func newSegTree(n Int) SegTree {
	segTree := SegTree{make([]Segment, n<<2)}
	segTree.Build(1, 1, n)
	return segTree
}

func (segTree SegTree) Build(x, l, r Int) {
	segTree.st[x].l = l
	segTree.st[x].r = r
	if l == r {
		segTree.st[x].minIndex = l
		return
	}
	mid := (l + r) >> 1
	segTree.Build(x<<1, l, mid)
	segTree.Build(x<<1|1, mid+1, r)
}

func (segTree SegTree) push(x Int) {
	if segTree.st[x].add != 0 {
		segTree.modifyOne(x<<1, segTree.st[x].add)
		segTree.modifyOne(x<<1|1, segTree.st[x].add)
		segTree.st[x].add = 0
	}
}

func (segTree SegTree) update(x Int) {
	if segTree.st[x<<1].val < segTree.st[x<<1|1].val {
		segTree.st[x].val = segTree.st[x<<1].val
		segTree.st[x].minIndex = segTree.st[x<<1].minIndex
	} else {
		segTree.st[x].val = segTree.st[x<<1|1].val
		segTree.st[x].minIndex = segTree.st[x<<1|1].minIndex
	}
}

func (segTree SegTree) modifyOne(x, add Int) {
	segTree.st[x].val += add
	if segTree.st[x].l != segTree.st[x].r {
		segTree.st[x].add += add
	}
}

func (segTree SegTree) Modify(x, l, r, add Int) {
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		segTree.modifyOne(x, add)
		return
	}
	segTree.push(x)
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		segTree.Modify(x<<1, l, r, add)
	}
	if mid < r {
		segTree.Modify(x<<1|1, l, r, add)
	}
	segTree.update(x)
}

func updateRes(cRes, cIndex, res, index Int) (Int, Int) {
	if cRes > res {
		return res, index
	}
	return cRes, cIndex
}

func (segTree SegTree) Query(x, l, r Int) (Int, Int) {
	res := Int(math.MaxInt64)
	resInd := -L1
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		res, resInd = updateRes(res, resInd, segTree.st[x].val, segTree.st[x].minIndex)
		return res, resInd
	}
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		sonRes, sonInd := segTree.Query(x<<1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	if mid < r {
		sonRes, sonInd := segTree.Query(x<<1|1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	return res, resInd
}

type Graph struct {
	nodeSize Int
	edges    []Edge
	head     []Int
	dis      []Int
	vis      []bool
	nCnt     Int
	ans      []Int
}

type Edge struct {
	u, v, c, indices, next Int
}

func NewGraph(nodeSize Int) *Graph {
	return &Graph{
		nodeSize: nodeSize,
		edges:    make([]Edge, (2e5+10)<<2),
		head:     make([]Int, nodeSize+100),
		dis:      make([]Int, nodeSize+100),
		vis:      make([]bool, nodeSize+100),
		nCnt:     L1,
		ans:      make([]Int, nodeSize),
	}
}

func (g *Graph) addEdge(u Int, v Int, cost Int, indices Int) {
	g.nCnt++
	g.edges[g.nCnt] = Edge{u, v, cost, indices, g.head[u]}
	g.head[u] = g.nCnt
}

func (g *Graph) Dijkstra(begin Int) {
	for i := L1; i <= g.nodeSize; i++ {
		g.dis[i] = math.MaxInt64
	}
	pq := make(PriorityQueue, 0)
	heap.Init(&pq)

	g.dis[begin] = 0
	heap.Push(&pq, &Item{begin, 0, -1, -1})

	id := L0

	for len(pq) > 0 {
		it := heap.Pop(&pq).(*Item)
		u := it.value
		if g.vis[u] {
			continue
		}
		g.vis[u] = true

		if it.indices != -1 && id < g.nodeSize-1 {
			g.ans[id] = it.indices
			id++
		}

		for i := g.head[u]; i != 0; i = g.edges[i].next {
			v := g.edges[i].v
			if g.dis[v] > g.dis[u]+g.edges[i].c {
				g.dis[v] = g.dis[u] + g.edges[i].c

				heap.Push(&pq, &Item{v, g.dis[v], g.edges[i].indices, -1})
			}
		}
	}
}

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"math"
	"os"
	"sort"
)

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 int
		Fscan(in, &n)

		a := make([]int, n)
		b := make([]int, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}

		sort.Ints(a)
		l := 0
		r := n - 1

		k := 0
		flag := true
		for k < n {
			if flag {
				flag = false
				b[l] = a[k]
				l++
				k++
			} else {
				flag = true
				b[r] = a[k]
				r--
				k++
			}
		}

		ma := b[0]
		ta := L1
		for i := 1; i < n; i++ {
			if b[i] > ma {
				ma = b[i]
				ta++
			}
		}

		mb := b[n-1]
		tb := L1
		for i := n - 2; i >= 0; i-- {
			if b[i] > mb {
				mb = b[i]
				tb++
			}
		}
		Fprintln(out, Min(ta, tb))
	}

}

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
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    Int // The value of the item; arbitrary.
	priority Int // The priority of the item in the queue.
	indices  Int
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func NewItem(value, priority Int) *Item {
	return &Item{
		value:    value,
		priority: priority,
		index:    -1,
	}

}

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Int, priority Int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

type SegTree struct {
	st []Segment
}

type Segment struct {
	l, r, val, add, minIndex Int
}

func newSegTree(n Int) SegTree {
	segTree := SegTree{make([]Segment, n<<2)}
	segTree.Build(1, 1, n)
	return segTree
}

func (segTree SegTree) Build(x, l, r Int) {
	segTree.st[x].l = l
	segTree.st[x].r = r
	if l == r {
		segTree.st[x].minIndex = l
		return
	}
	mid := (l + r) >> 1
	segTree.Build(x<<1, l, mid)
	segTree.Build(x<<1|1, mid+1, r)
}

func (segTree SegTree) push(x Int) {
	if segTree.st[x].add != 0 {
		segTree.modifyOne(x<<1, segTree.st[x].add)
		segTree.modifyOne(x<<1|1, segTree.st[x].add)
		segTree.st[x].add = 0
	}
}

func (segTree SegTree) update(x Int) {
	if segTree.st[x<<1].val < segTree.st[x<<1|1].val {
		segTree.st[x].val = segTree.st[x<<1].val
		segTree.st[x].minIndex = segTree.st[x<<1].minIndex
	} else {
		segTree.st[x].val = segTree.st[x<<1|1].val
		segTree.st[x].minIndex = segTree.st[x<<1|1].minIndex
	}
}

func (segTree SegTree) modifyOne(x, add Int) {
	segTree.st[x].val += add
	if segTree.st[x].l != segTree.st[x].r {
		segTree.st[x].add += add
	}
}

func (segTree SegTree) Modify(x, l, r, add Int) {
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		segTree.modifyOne(x, add)
		return
	}
	segTree.push(x)
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		segTree.Modify(x<<1, l, r, add)
	}
	if mid < r {
		segTree.Modify(x<<1|1, l, r, add)
	}
	segTree.update(x)
}

func updateRes(cRes, cIndex, res, index Int) (Int, Int) {
	if cRes > res {
		return res, index
	}
	return cRes, cIndex
}

func (segTree SegTree) Query(x, l, r Int) (Int, Int) {
	res := Int(math.MaxInt64)
	resInd := -L1
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		res, resInd = updateRes(res, resInd, segTree.st[x].val, segTree.st[x].minIndex)
		return res, resInd
	}
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		sonRes, sonInd := segTree.Query(x<<1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	if mid < r {
		sonRes, sonInd := segTree.Query(x<<1|1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	return res, resInd
}

type Graph struct {
	nodeSize Int
	edges    []Edge
	head     []Int
	dis      []Int
	vis      []bool
	nCnt     Int
	ans      []Int
}

type Edge struct {
	u, v, c, indices, next Int
}

func NewGraph(nodeSize Int) *Graph {
	return &Graph{
		nodeSize: nodeSize,
		edges:    make([]Edge, (2e5+10)<<2),
		head:     make([]Int, nodeSize+100),
		dis:      make([]Int, nodeSize+100),
		vis:      make([]bool, nodeSize+100),
		nCnt:     L1,
		ans:      make([]Int, nodeSize),
	}
}

func (g *Graph) addEdge(u Int, v Int, cost Int, indices Int) {
	g.nCnt++
	g.edges[g.nCnt] = Edge{u, v, cost, indices, g.head[u]}
	g.head[u] = g.nCnt
}

func (g *Graph) Dijkstra(begin Int) {
	for i := L1; i <= g.nodeSize; i++ {
		g.dis[i] = math.MaxInt64
	}
	pq := make(PriorityQueue, 0)
	heap.Init(&pq)

	g.dis[begin] = 0
	heap.Push(&pq, &Item{begin, 0, -1, -1})

	id := L0

	for len(pq) > 0 {
		it := heap.Pop(&pq).(*Item)
		u := it.value
		if g.vis[u] {
			continue
		}
		g.vis[u] = true

		if it.indices != -1 && id < g.nodeSize-1 {
			g.ans[id] = it.indices
			id++
		}

		for i := g.head[u]; i != 0; i = g.edges[i].next {
			v := g.edges[i].v
			if g.dis[v] > g.dis[u]+g.edges[i].c {
				g.dis[v] = g.dis[u] + g.edges[i].c

				heap.Push(&pq, &Item{v, g.dis[v], g.edges[i].indices, -1})
			}
		}
	}
}

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		l, r, lv, rv := 1, 1, a[n-1], a[n-1]
		for i := n - 2; i >= 0; i-- {
			v := a[i]
			if l < r && v < lv {
				lv = v
				l++
			} else if v < rv {
				rv = v
				r++
			}
		}
		ans := min(l, r)
		l, r, lv, rv = 1, 1, a[0], a[0]
		for _, v := range a {
			if l < r && v > lv {
				lv = v
				l++
			} else if v > rv {
				rv = v
				r++
			}
		}
		l = min(l, r)
		if l > ans {
			ans = l
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		l, r := 1, 1
		lv, rv := a[n-1], a[n-1]
		for i := n - 2; i >= 0; i-- {
			if l < r && a[i] < lv {
				lv = a[i]
				l++
			} else if a[i] < rv {
				rv = a[i]
				r++
			}
		}
		if r < l {
			l = r
		}
		Fprintln(out, l)
	}
}

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

package main

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

func A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		Map := make(map[int]int, 0)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			Map[a[i]]++
		}
		var cnt, cnt2 int
		for _, v := range Map {
			if v == 1 {
				cnt++
			} else {
				cnt2++
			}
		}
		Fprintln(out, cnt2+cnt/2)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A() }


package main

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

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

type City struct {
	index int
	beauty int
	cost int
}

// We'll be charged for sum of c_i at least.
// so c_i enables us to travel from city of a_i
// to city with niceness a_i + c_i for "free"
// so we break up TSP on two paths:
//   1. Travel to highest a_i possible
//   2. Then travel back (just sum of unvisited c_i's)
// well, we need to find the cheapest way to travel from 1 to i where c_i is max.
func callIo(in io.Reader, out io.Writer) {
	var n int

	_, _ = Fscan(in, &n)

	cities := make([]City, n)
	cost_sum := uint64(0)
	for i := 0; i < n; i++ {
		cities[i].index = i
		_, _ = Fscan(in, &cities[i].beauty)
		_, _ = Fscan(in, &cities[i].cost)
		cost_sum += uint64(cities[i].cost)
	}

	sort.Slice(cities, func(i, j int) bool {
		if cities[i].beauty < cities[j].beauty {
			return true
		}
		if cities[i].beauty > cities[j].beauty {
			return false
		}
		return false
	})

	// launch greedy algorithm
	freeBeauty := cities[0].beauty + cities[0].cost
	overcost := uint64(0)
	for i := 1; i < n; i++ {
		if cities[i].beauty <= freeBeauty {
			newFreeBeauty := cities[i].beauty + cities[i].cost
			if newFreeBeauty > freeBeauty {
				freeBeauty = newFreeBeauty
			}
		} else {
			// penalty
			overcost += uint64(cities[i].beauty - freeBeauty)
			freeBeauty = cities[i].beauty + cities[i].cost
		}
	}

	// output add_cost[selected] plus sum of all mincosts
	_, _ = Fprintf(out, "%v\n", cost_sum + overcost)
}

func main() {
	in := os.Stdin
	out := os.Stdout

	// buffer reader to for speed up:
	bufIn := bufio.NewReader(in)
	// buffer output to for speed up:
	bufOut := bufio.NewWriter(out)
	defer bufOut.Flush()

	// call your algorithm
	callIo(bufIn, bufOut)
}

package main

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

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

	n := readNum(reader)
	A := make([]int, n)
	C := make([]int, n)
	for i := 0; i < n; i++ {
		A[i], C[i] = readTwoNums(reader)
	}
	res := solve(n, A, C)
	fmt.Println(res)
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, A []int, C []int) int64 {
	P := make([]Pair, n)
	for i := 0; i < n; i++ {
		P[i] = Pair{A[i], C[i]}
	}

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

	var ans int64
	var ac int64
	var sum int64

	for i := 0; i < n; i++ {
		if i != 0 && ac < int64(P[i].first) {
			ans += int64(P[i].first) - ac
		}
		ac = max(ac, int64(P[i].first+P[i].second))
		sum += int64(P[i].second)
	}

	return ans + sum
}

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

type Pair struct {
	first  int
	second int
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, mx int
	Fscan(in, &n)
	a := make([]struct{ a, c int }, n)
	s := int64(0)
	for i := range a {
		Fscan(in, &a[i].a, &a[i].c)
		s += int64(a[i].c)
	}
	sort.Slice(a, func(i, j int) bool { return a[i].a < a[j].a })
	for i, p := range a[:n-1] {
		if p.a+p.c > mx {
			mx = p.a + p.c
		}
		if d := a[i+1].a - mx; d > 0 {
			s += int64(d)
		}
	}
	Print(s)
}

package main

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

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

type City struct {
	index int
	beauty int
	cost int
}

// We'll be charged for sum of c_i at least.
// so c_i enables us to travel from city of a_i
// to city with niceness a_i + c_i for "free"
// so we break up TSP on two paths:
//   1. Travel to highest a_i possible
//   2. Then travel back (just sum of unvisited c_i's)
// well, we need to find the cheapest way to travel from 1 to i where c_i is max.
func callIo(in io.Reader, out io.Writer) {
	var n int

	_, _ = Fscan(in, &n)

	cities := make([]City, n)
	cost_sum := 0
	for i := 0; i < n; i++ {
		cities[i].index = i
		_, _ = Fscan(in, &cities[i].beauty)
		_, _ = Fscan(in, &cities[i].cost)
		cost_sum += cities[i].cost
	}

	sort.Slice(cities, func(i, j int) bool {
		if cities[i].beauty < cities[j].beauty {
			return true
		}
		if cities[i].beauty > cities[j].beauty {
			return false
		}
		return false
	})

	// launch greedy algorithm
	freeBeauty := cities[0].beauty + cities[0].cost
	overcost := 0
	for i := 1; i < n; i++ {
		if cities[i].beauty <= freeBeauty {
			newFreeBeauty := cities[i].beauty + cities[i].cost
			if newFreeBeauty > freeBeauty {
				freeBeauty = newFreeBeauty
			}
		} else {
			// penalty
			overcost += cities[i].beauty - freeBeauty
			freeBeauty = cities[i].beauty + cities[i].cost
		}
	}

	// output add_cost[selected] plus sum of all mincosts
	_, _ = Fprintf(out, "%v\n", cost_sum + overcost)
}

func main() {
	in := os.Stdin
	out := os.Stdout

	// buffer reader to for speed up:
	bufIn := bufio.NewReader(in)
	// buffer output to for speed up:
	bufOut := bufio.NewWriter(out)
	defer bufOut.Flush()

	// call your algorithm
	callIo(bufIn, bufOut)
}


package main

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

// github.com/EndlessCheng/codeforces-go
func CF1539D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	Fscan(in, &n)
	a := make([]struct{ c, lim int64 }, n)
	for i := range a {
		Fscan(in, &a[i].c, &a[i].lim)
	}
	sort.Slice(a, func(i, j int) bool { return a[i].lim < a[j].lim })

	ans := int64(0)
	for l, r, c := 0, n-1, int64(0); l <= r; {
		if a[l].lim > c {
			v := min(a[l].lim-c, a[r].c)
			ans += v * 2
			c += v
			if a[r].c -= v; a[r].c == 0 {
				r--
			}
		} else {
			ans += a[l].c
			c += a[l].c
			l++
		}
	}
	Fprint(out, ans)
}

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	"os"
	"fmt"

	"sort"
)

type pair struct {
	a, b int64
}

type ByB []*pair

func (a ByB) Len() int           { return len(a) }
func (a ByB) Less(i, j int) bool { return a[i].b < a[j].b }
func (a ByB) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type ByA []pair

func (a ByA) Len() int           { return len(a) }
func (a ByA) Less(i, j int) bool { return a[i].a < a[j].a }
func (a ByA) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

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

	var n int
	fmt.Fscan(r, &n)

	a := make([]pair, n)

	for j := 0; j < len(a); j++ {
		fmt.Fscan(r, &a[j].a, &a[j].b)
	}

	fmt.Println(solve(a))
}

func solve(a []pair) int64 {
	mp := make(map[int64]int64)

	for i := 0; i < len(a); i++ {
		mp[a[i].b] += a[i].a
	}

	var pairs []*pair

	for k, v := range mp {
		pairs = append(pairs, &pair{v, k})
	}

	sort.Sort(ByB(pairs))

	s, e := 0, len(pairs) - 1

	var cost int64

	var ab int64

	for ;s <= e; {
		last := pairs[e]
		first := pairs[s]

		if ab >= first.b {
			cost += first.a
			ab += first.a
			s++
			continue
		}

		if ab + last.a >= first.b {

			diff := first.b - ab

			ab += diff
			last.a -= diff

			cost += 2 * diff

			ab += first.a
			cost += first.a

			s++

		} else {
			cost += 2 * last.a
			ab += last.a
			e--
		}
	}

	return cost
}

package main

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

type Node struct{
	a int64
	b int64
}
type Nodes []Node
func (u Nodes)Len()int{
	return len(u)
}
func (u Nodes)Less(i,j int)bool{
	return u[i].b<u[j].b
}
func (u Nodes)Swap(i,j int){
	u[i], u[j] = u[j],u[i]
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n int
	fmt.Fscan(in,&n)
	var a int64
	var b int64
	var i int
	cool := make(Nodes,n+1)
	for i=1;i<=n;i++{
		fmt.Fscan(in,&a,&b)
		cool[i].a=a
		cool[i].b=b
	}
	sort.Sort(cool)
	buys:=int64(0)
	cnt := int64(0)
	for i:=1;i<=n;i++{
		if cool[i].a==0{
			break
		}
		if cool[i].b<=cnt{
			cnt+=cool[i].a
			buys+=cool[i].a
			continue
		}
		l:=i
		r:=n
		ans:=0
		for l<=r{
			mid :=(l+r)/2
			if cool[mid].a>0{
				ans=mid
				l=mid+1
			}else{
				r=mid-1
			}
		}
		for j:=ans;j>=i;j--{
			fin := int64(-1)
			if cool[j].a<(cool[i].b-cnt){
				fin = cool[j].a
				cool[j].a=0
			}else{
				fin = cool[i].b-cnt
				cool[j].a-=(cool[i].b-cnt)
			}
			if cnt < cool[j].b && cnt+fin>cool[j].b{
				buys = (cool[j].b-cnt)*2 + (cnt + fin - cool[j].b)
			}else if cnt>=cool[j].b{
				buys += fin
			}else{
				buys += fin*2
			}
			cnt+=fin
			if cool[i].b==cnt{
				break
			}
		}
		cnt+=cool[i].a
		buys += cool[i].a
	}
	fmt.Fprintln(out,buys)
}

package main

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

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...) }

type Product struct {
	discount int64
	count    int64
}

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

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

func main() {
	var n int
	scanf("%d\n", &n)
	pr := make([]Product, n)
	for i := range pr {
		scanf("%d %d\n", &pr[i].count, &pr[i].discount)
	}
	sort.Slice(pr, func(i, j int) bool {
		return pr[i].discount < pr[j].discount
	})
	var sum2, sum1 int64 = 0, 0
	var l, r int = 0, len(pr) - 1
	for l <= r {
		sum := sum1 + sum2
		if sum >= pr[l].discount {
			sum1 += pr[l].count
			pr[l].count = 0
			l++
		} else {
			delta := min(pr[l].discount-sum, pr[r].count)
			sum2 += delta
			pr[r].count -= delta
			if pr[r].count == 0 {
				r--
			}
		}
	}
	printf("%d\n", 2*sum2+sum1)
	writer.Flush()
}


package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

const maxn = 100005

var n, k, id int
var c [maxn]uint64
var cap [maxn]bool
var f [maxn]uint64
var scap, snot, res uint64

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &k)

	for i := 1; i <= n; i++ {
		fmt.Fscan(bufin, &c[i])
		cap[i] = false
	}

	for i := 1; i <= k; i++ {
		fmt.Fscan(bufin, &id)
		cap[id] = true
	}

	f[0] = 0
	scap = 0
	snot = 0

	for i := 1; i <= n; i++ {
		if cap[i] {
			f[i] = f[i-1] + c[i]*(scap+snot)
			scap += c[i]
		} else {
			f[i] = f[i-1] + c[i]*scap
			if !cap[i-1] {
				f[i] += c[i] * c[i-1]
			}
			snot += c[i]
		}
	}

	res := f[n]
	if (!cap[n]) && (!cap[1]) {
		res += c[n] * c[1]
	}

	fmt.Fprintln(bufout, res)
}

package main

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

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n := NextInt()
	k := NextInt()
	c := make([]int64, n)
	cap := make([]bool, n)
	sum := int64(0)
	sumCap := int64(0)
	for i := 0; i < n; i++ {
		c[i] = int64(NextInt())
		sum += c[i]
	}
	for i := 0; i < k; i++ {
		j := NextInt() - 1
		cap[j] = true
		sumCap += c[j]
	}

	answer := int64(0)
	for i := 0; i < n; i++ {
		if cap[i] {
			answer += c[i] * (sum - c[i])
		} else {
			z := c[(i-1+n)%n] + c[(i+1)%n] + sumCap
			if cap[i] {
				z -= c[i]
			}
			if cap[(i-1+n)%n] {
				z -= c[(i-1+n)%n]
			}
			if cap[(i+1)%n] {
				z -= c[(i+1)%n]
			}
			answer += c[i] * z
		}
	}

	Println(strconv.FormatInt(answer>>1, 10))
}

/******************/
/* IO boilerplate */
/******************/

var scanner *bufio.Scanner
var writer *bufio.Writer

func NextInt() int {
	ret, _ := strconv.Atoi(Next())
	return ret
}

func Next() string {
	scanner.Scan()
	return scanner.Text()
}

func Print(s string) {
	writer.WriteString(s)
}

func Println(s string) {
	writer.WriteString(s)
	writer.WriteByte('\n')
}


package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type House struct {
  Pos, Size int
}
type Houses []*House
func (houses Houses) Len() int {
  return len(houses)
}
func (houses Houses) Swap(i, j int) {
  houses[i], houses[j] = houses[j], houses[i]
}
func (houses Houses) Less(i, j int) bool {
  return houses[i].Pos < houses[j].Pos
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, s := getI(), getI()
  houses := make([]*House, n)
  for i := 0; i < n; i++ {
    houses[i] = &House{ getI(), getI() }
  }
  sort.Sort(Houses(houses))
  result := 2
  for i := 1; i < n; i++ {
    doubleGap := 2*(houses[i].Pos - houses[i-1].Pos)
    doubleGap -= houses[i].Size + houses[i-1].Size
    if doubleGap >= 2*s {
      result++
    }
    if doubleGap > 2*s {
      result++
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

// http://codeforces.com/problemset/problem/15/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

type House struct {
	start, len int
}
type Houses []House

func (p Houses) Len() int {
	return len(p)
}

func (p Houses) Less(i, j int) bool {
	return p[i].start < p[j].start
}

func (p Houses) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func main () {
	var n, t int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &t)

	p := make(Houses, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &p[i].start, &p[i].len)
	}

	sort.Sort(p)
	ans := 2

	for i := 1; i < len(p); i++ {
		free := float64(p[i].start) - float64(p[i].len) / 2 - (float64(p[i - 1].start) + float64(p[i - 1].len) / 2)
		if free == float64(t) {
			ans++
		}
		if free > float64(t) {
			ans+=2
		}
	}

	fmt.Println(ans)
}

package main

import "fmt"
import "sort"
import "math"

type houseList []House

type House struct {
	c, l float64
}

func (hl houseList) Len() int {
	return len(hl)
}

func (hl houseList) Swap(i, j int) {
	hl[i], hl[j] = hl[j], hl[i]
}

func (hl houseList) Less(i, j int) bool {
	return hl[i].c < hl[j].c
}

func main() {
	var n int
	var t float64
	fmt.Scan(&n, &t)

	houses := make(houseList, n)
	lines := make([]float64, (n - 1) * 2)
	np := 2

	k := 0

	for i := 0; i < n; i++ {
		fmt.Scan(&houses[i].c, &houses[i].l)
	}

	sort.Sort(houses)

	for i := 1; i < n; i++ {
		lines[k] = (houses[i - 1].c + (houses[i - 1].l / 2.0))
		lines[k + 1] = (houses[i].c - (houses[i].l / 2.0))
		k += 2
	}

	for i := 0; i < len(lines); i += 2 {
		if math.Abs(lines[i + 1] - lines[i] - t) > 0.00001 {
			if (lines[i + 1] - lines[i]) > t {
				np += 2
			}
		} else {
			np += 1
		}
	}

	fmt.Println(np)
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type House struct {
  Pos, Size int
}
type Houses []*House
func (houses Houses) Len() int {
  return len(houses)
}
func (houses Houses) Swap(i, j int) {
  houses[i], houses[j] = houses[j], houses[i]
}
func (houses Houses) Less(i, j int) bool {
  return houses[i].Pos < houses[j].Pos
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, s := getI(), getI()
  houses := make([]*House, n)
  for i := 0; i < n; i++ {
    houses[i] = &House{ getI(), getI() }
  }
  sort.Sort(Houses(houses))
  result := 2
  for i := 1; i < n; i++ {
    doubleGap := 2*(houses[i].Pos - houses[i-1].Pos)
    doubleGap -= houses[i].Size + houses[i-1].Size
    if doubleGap >= 2*s {
      result++
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

// http://codeforces.com/problemset/problem/15/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

type House struct {
	start, len int
}
type Houses []House

func (p Houses) Len() int {
	return len(p)
}

func (p Houses) Less(i, j int) bool {
	return p[i].start < p[j].start
}

func (p Houses) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func main () {
	var n, t int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &t)

	p := make(Houses, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &p[i].start, &p[i].len)
	}

	sort.Sort(p)
	ans := 2

	for i := 1; i < len(p); i++ {
		free := p[i].start - (p[i - 1].start + p[i - 1].len - 1)
		if free >= t {
			ans += (free - t) + 1
		}
	}

	fmt.Println(ans)
}

// http://codeforces.com/problemset/problem/15/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

type House struct {
	start, len int
}
type Houses []House

func (p Houses) Len() int {
	return len(p)
}

func (p Houses) Less(i, j int) bool {
	return p[i].start < p[j].start
}

func (p Houses) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func main () {
	var n, t int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &t)

	p := make(Houses, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &p[i].start, &p[i].len)
	}

	sort.Sort(p)
	ans := 2

	for i := 1; i < len(p); i++ {
		free := p[i].start + p[i].len / 2 - (p[i - 1].start + p[i - 1].len)
		if free == t {
			ans++
		}
		if free > t {
			ans+=2
		}
	}

	fmt.Println(ans)
}

package main

import "fmt"
import "sort"
import "math"

func main() {
	var n int
	var t float64
	fmt.Scan(&n, &t)

	centers := make([]float64, n)
	lengths := make([]float64, n)
	lines := make([]float64, (n - 1) * 2)
	np := 2

	k := 0

	for i := 0; i < n; i++ {
		fmt.Scan(&centers[i], &lengths[i])
	}

	for i := 1; i < n; i++ {
		lines[k] = (centers[i - 1] + (lengths[i - 1] / 2.0))
		lines[k + 1] = (centers[i] - (lengths[i] / 2.0))
		k += 2
	}

	sort.Float64s(lines)

	for i := 0; i < len(lines); i += 2 {
		if math.Abs(lines[i + 1] - lines[i] - t) > 0.00001 {
			if (lines[i + 1] - lines[i]) > t {
				np += 2
			}
		} else {
			np += 1
		}
	}

	fmt.Println(np)
}