package main

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

func solve(reader io.Reader, writer io.Writer) {
	min := func(vals ...int) int {
		ans := vals[0]
		for _, val := range vals[1:] {
			if val < ans {
				ans = val
			}
		}
		return ans
	}
	max := func(vals ...int) int {
		ans := vals[0]
		for _, val := range vals[1:] {
			if val > ans {
				ans = val
			}
		}
		return ans
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()
	const mod int = 1e9 + 7

	var n int64
	Fscan(in, &n)
	mat := make([][]int, n)
	cntok :=0
	badMax := make([]int, 0, n)
	mins, maxs := make([]int, n), make([]int, n)
	bads := make([]bool, n)
	for i := range mat {
		var m int
		Fscan(in, &m)
		mat[i] = make([]int, m)
		for j := range mat[i] {
			Fscan(in, &mat[i][j])
		}
		newMat := make([]int, m)
		copy(newMat, mat[i])
		sort.Ints(newMat)
		bad := true
		for i ,v:= range mat[i] {
			if v != newMat[m-1-i] {
				bad=false
			}
		}
		mins[i] = min(mat[i]...)
		maxs[i] = max(mat[i]...)
		if !bad {
			cntok++
		} else {
			bads[i]=true
			badMax = append(badMax, maxs[i])
		}
	}
	//maxss := make([]int, n)
	//copy(maxss,maxs)
	sort.Ints(badMax)

	ans := int64(0)

	//println(cntok)
	for i ,v := range mins {
		if !bads[i] {
			ans += n
			//println(i, ans)
		} else {

			ans += int64(cntok)
			j := sort.SearchInts(badMax, v+1)
			ans += int64(len(badMax)-j)
			//println(i, ans)

		}
	}
	Fprintln(out, ans)

}

func main() {
	solve(os.Stdin, os.Stdout)
	//in, _ := os.Open("input.txt")
	//out, _ := os.Open("output.txt")
	//solve(in, out)
}

func ifElseI(cond bool, a, b int) int {
	if cond {
		return a
	}
	return b
}
func ifElseS(cond bool, a, b string) string {
	if cond {
		return a
	}
	return b
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	var result uint64
	scanner.Scan()
	var n, ind int
	n, _ = strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind = 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		} else {
			maxv = append(maxv, 1e9)
			minv = append(minv, -1e9)
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		result += uint64(n - sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		}))
	}

	fmt.Println(result)
}

package main

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

type Seq struct {
	Min, Max int
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	return t
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	n := readInt(scanner)
	s := make([]Seq, 0, n)

	// read each sequence, record the min, and max, and whether it
	// has an ascent
	for i := 0; i < n; i++ {
		ll := readInt(scanner)

		min := 0
		max := 0
		ascent := false

		for j := 0; j < ll; j++ {
			x := readInt(scanner)

			if j == 0 {
				min = x
				max = x
			}

			if x < min {
				min = x
			}
			if x > max {
				max = x
			}
			if x > min {
				ascent = true
			}
		}

		if !ascent {
			s = append(s, Seq{min, max})
		}
	}

	// sort by max
	sort.Slice(s, func(i, j int) bool {
		return s[i].Max < s[j].Max
	})

	ns := uint64(len(s)) // non-ascent sequences
	na := uint64(n) - ns // ascent sequences
	l := na*na + 2*na*ns

	// count ascents amongst non-ascent sequences
	for _, seq := range(s) {
		// number of sequences with max > seq.Min
		// smallest index for which f(i) is true
		// assuming f(i) => f(i+1)
		i := sort.Search(int(ns), func(i int) bool { return s[i].Max > seq.Min })
		l += ns - uint64(i)
	}

	fmt.Println(l)
}

package main

import (
    "os"
    "bufio"
    "fmt"
    "strconv"
    "sort"
)
var scanner *bufio.Scanner

func read() string {
    if !scanner.Scan() {
		panic("Scan returned false")
	}
	return scanner.Text()
}
func readInt() int {
	num, _ := strconv.Atoi(read())
	return num
}
func readFloat() float64 {
    num, _ := strconv.ParseFloat(read(), 64)
    return num
}
func main() {
    scanner = bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    t := readInt()

    var minv, maxv []float64
    for i := 0; i < t; i++ {
        l := readInt()

        min := 1e9
        max := -1e9
        asc := false
        for j := 0; j < l; j++ {
            v := readFloat()

            if v > min {
                asc = true
            }
            if v < min {
                min = v
            }
            if v > max {
                max = v
            }
        }
        if asc {
            min = -1e9
            max = 1e9
        }
        minv = append(minv, min)
        maxv = append(maxv, max)
    }
    sort.Float64s(maxv)

    var cnt uint64

    for i := 0; i < t; i++ {
        ub := sort.Search(len(maxv), func(j int) bool { return maxv[j] > minv[i] })
        cnt += uint64(t - ub)
    }
    fmt.Println(cnt)
}

/*
a maintain minimums
b maintain maximums
if a[mins] < b[max]:
ascent += 1
sort the minimum array decreasingly
sort the maximum array increasingly
iterate minimum array,
*/
package main

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

type Pair struct {
	index int64
	val   int64
}

type Pairs []Pair

func (u Pairs) Len() int { return len(u) }

func (u Pairs) Less(i, j int) bool { return u[i].val < u[j].val }

func (u Pairs) Swap(i, j int) {
	u[i], u[j] = u[j], u[i]
}

const max_int = 1000001

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, i, j, input, min, max, l, prev_input int64
	fmt.Fscanln(reader, &n)
	minimums := make([]Pair, 0, n)
	maximums := make([]Pair, 0, n)
	var minimums_length, maximums_length, has_ascents_length int64
	for i = 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &l)
		min = max_int
		max = -1
		strictly_decreasing := true
		for j = 0; j < l; j++ {
			if j == l-1 {
				fmt.Fscanf(reader, "%d\n", &input)
			} else {
				fmt.Fscanf(reader, "%d", &input)
			}
			if j > 0 && prev_input < input {
				strictly_decreasing = false
			}
			prev_input = input
			if input <= min {
				min = input
			}
			if input >= max {
				max = input
			}
		}
		if !strictly_decreasing {
			has_ascents_length += 1
			continue
		}
		minimums = append(minimums, Pair{index: minimums_length, val: min})
		maximums = append(maximums, Pair{index: maximums_length, val: max})
		minimums_length += 1
		maximums_length += 1
	}
	sort.Sort(Pairs(minimums))
	sort.Sort(Pairs(maximums))
	// fmt.Println(minimums)
	// fmt.Println(maximums)
	// fmt.Println(has_ascent)
	var minindex, maxindex, ascents int64
	for minindex < minimums_length && maxindex < maximums_length {
		for maxindex < maximums_length &&
			minimums[minindex].val >= maximums[maxindex].val {
			maxindex += 1
		}
		if maxindex == maximums_length {
			break
		}
		ascents += maximums_length - maxindex
		minindex += 1
	}
	ascents += has_ascents_length * has_ascents_length
	ascents += ((n - has_ascents_length) * 2) * has_ascents_length
	fmt.Fprintln(writer, ascents)
}

package main

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

type WordScanner struct {
	*bufio.Scanner
}

func NewWordScanner() *WordScanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &WordScanner{Scanner: s}
}

func (s *WordScanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *WordScanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *WordScanner) NextString() string {
	s.Scan()
	return s.Text()
}

type seq struct {
	min int64
	max int64
}

type minSorter []seq

func (m minSorter) Len() int {
	return len(m)
}

func (m minSorter) Less(a, b int) bool {
	return m[a].min < m[b].min
}

func (m minSorter) Swap(a, b int) {
	m[a], m[b] = m[b], m[a]
}

func main() {
	wscn := NewWordScanner()
	n := wscn.NextInt64()
	totalCombs := n * n
	seqs := []seq{}
	for ; n > 0; n-- {
		l := wscn.NextInt64()
		s := wscn.NextInt64()
		tmp := seq{
			min: s,
			max: s,
		}
		hasStep := false
		for ; l > 1; l-- {
			s = wscn.NextInt64()
			if s > tmp.min {
				hasStep = true
			}
			if s < tmp.min {
				tmp.min = s
			}
			if s > tmp.max {
				tmp.max = s
			}
		}
		if !hasStep {
			seqs = append(seqs, tmp)
		}
	}
	fmt.Println(totalCombs - sol(seqs))
}

func binSearch(max int64, seqs []seq) int64 {
	l, r := 0, len(seqs)-1
	for l <= r {
		mid := l + (r-l)/2
		if max <= seqs[mid].min {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	//fmt.Println(max, len(seqs)-l, seqs)
	return int64(len(seqs) - l)
}

func sol(seqs []seq) int64 {
	sort.Sort(minSorter(seqs))
	ret := int64(0)
	for _, s := range seqs {
		ret += binSearch(s.max, seqs)
	}
	return ret
}

package main

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

type Seq struct {
	Min, Max int
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	return t
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	n := readInt(scanner)
	s := make([]Seq, 0, n)

	// read each sequence, record the min, and max, and whether it
	// has an ascent
	for i := 0; i < n; i++ {
		scanner.Scan()

		min := 0
		max := 0
		ascent := false

		for j, w := range(strings.Split(scanner.Text(), " ")[1:]) {
			x, _ := strconv.Atoi(w)

			if j == 0 {
				min = x
				max = x
			}

			if x < min {
				min = x
			}
			if x > max {
				max = x
			}
			if x > min {
				ascent = true
			}
		}

		if !ascent {
			s = append(s, Seq{min, max})
		}
	}

	// sort by max
	sort.Slice(s, func(i, j int) bool {
		return s[i].Max < s[j].Max
	})

	ns := len(s) // non-ascent sequences
	na := n - ns // ascent sequences
	l := na*na + 2*na*ns

	// count ascents amongst non-ascent sequences
	for _, seq := range(s) {
		// number of sequences with max > seq.Min
		// smallest index for which f(i) is true
		// assuming f(i) => f(i+1)
		i := sort.Search(ns, func(i int) bool { return s[i].Max > seq.Min })
		l += ns - i
	}

	fmt.Println(l)
}

package main

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

type Seq struct {
	Min, Max int
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	return t
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	n := readInt(scanner)
	s := make([]Seq, 0, n)

	// read each sequence, record the min, and max, and whether it
	// has an ascent
	for i := 0; i < n; i++ {
		ll := readInt(scanner)

		min := 0
		max := 0
		ascent := false

		for j := 0; j < ll; j++ {
			x := readInt(scanner)

			if j == 0 {
				min = x
				max = x
			}

			if x < min {
				min = x
			}
			if x > max {
				max = x
			}
			if x > min {
				ascent = true
			}
		}

		if !ascent {
			s = append(s, Seq{min, max})
		}
	}

	// sort by max
	sort.Slice(s, func(i, j int) bool {
		return s[i].Max < s[j].Max
	})

	ns := len(s) // non-ascent sequences
	na := n - ns // ascent sequences
	l := na*na + 2*na*ns

	// count ascents amongst non-ascent sequences
	for _, seq := range(s) {
		// number of sequences with max > seq.Min
		// smallest index for which f(i) is true
		// assuming f(i) => f(i+1)
		i := sort.Search(ns, func(i int) bool { return s[i].Max > seq.Min })
		l += ns - i
	}

	fmt.Println(l)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	sMap := make([][]int, n)
	var result int

	for sCount := 0; sCount < n; sCount++ {
		scanner.Scan()

		sList := strings.Split(scanner.Text(), " ")
		l, _ := strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		sMap[sCount] = s
	}

	for _, s1 := range sMap {
		if isVoshod(s1) {
			result += len(sMap)
			continue
		}

		for _, s2 := range sMap {
			if isVoshod(append(s1, s2...)) {
				result++
			}
		}
	}

	fmt.Println(result)
}

func isVoshod(sList []int) bool {
	i := sList[0]
	for _, el := range sList {
		if el < i {
			return true
		}
	}

	return false
}

package main

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

type pp struct {
	mas  []int
	lent int
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = uint32(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		ut := sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		})

		result -= uint32(ut)
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 200000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	sMap := make(map[int][]int, n)
	var result int

	for sCount := 0; sCount < n; sCount++ {
		scanner.Scan()

		sList := strings.Split(scanner.Text(), " ")
		l, _ := strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		sMap[sCount] = s
	}

	for _, s1 := range sMap {
		for _, s2 := range sMap {
			if isVoshod(append(s1, s2...)) {
				result++
			}
		}
	}

	fmt.Println(result)
}

func isVoshod(sList []int) bool {
	i := -1
	for elInd, el := range sList {
		if el < i || i == -1 {
			i = el

			for intInd := elInd + 1; intInd < len(sList); intInd++ {
				if sList[intInd] > i {
					return true
				}
			}
		}
	}

	return false
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	sMap := make(map[int][]int, n)
	var result int

	for sCount := 0; sCount < n; sCount++ {
		scanner.Scan()

		sList := strings.Split(scanner.Text(), " ")
		l, _ := strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		sMap[sCount] = s
	}

	for _, s1 := range sMap {
		for _, s2 := range sMap {
			if isVoshod(append(s1, s2...)) {
				result++
			}
		}
	}

	fmt.Println(result)
}

func isVoshod(sList []int) bool {
	i := -1
	for elInd, el := range sList {
		if el < i || i == -1 {
			i = el

			for intInd := elInd + 1; intInd < len(sList); intInd++ {
				if sList[intInd] > i {
					return true
				}
			}
		}
	}

	return false
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = int64(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		ut := sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		})

		result -= int64(ut)
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	sMap := make(map[int][]int, n)
	var result int

	for sCount := 0; sCount < n; sCount++ {
		scanner.Scan()

		sList := strings.Split(scanner.Text(), " ")
		l, _ := strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		sMap[sCount] = s
	}

	for _, s1 := range sMap {
		if isVoshod(s1) {
			result += len(sMap) + 1
			continue
		}

		for _, s2 := range sMap {
			if isVoshod(append(s1, s2...)) {
				result++
			}
		}
	}

	fmt.Println(result)
}

func isVoshod(sList []int) bool {
	i := -1
	for elInd, el := range sList {
		if el < i || i == -1 {
			i = el

			for intInd := elInd + 1; intInd < len(sList); intInd++ {
				if sList[intInd] > i {
					return true
				}
			}
		}
	}

	return false
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()
	var n, ind int
	n, _ = strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = int32(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind = 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		result -= int32(sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		}))
	}

	fmt.Println(result)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	_, _ = fmt.Scan(n)

	sMap := make([][]int, n)
	var result = uint32(n * n)
	var sCount, nCount, l int

	for ; sCount < n; sCount++ {
		_, _ = fmt.Scan(l)

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			_, _ = fmt.Scan(s[ind])
		}

		if sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Sort(sort.IntSlice(s))
			sMap[nCount] = s
			nCount++
		}
	}

	lenS := len(sMap)
	var i, j int

	for i = 0; i < lenS; i++ {
		if sMap[i] == nil {
			continue
		}
		for j = 0; j < lenS; j++ {
			if sMap[j] == nil {
				continue
			}

			if sMap[i][0] >= sMap[j][len(sMap[j])-1] {
				result--
			}
		}
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = uint64(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		ut := sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		})

		result -= uint64(ut)
	}

	fmt.Println(result)
}

package main

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

type pp struct {
	mas  []int
	lent int
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = uint64(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		ut := sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		})

		result -= uint64(ut)
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()
	var n, ind int
	n, _ = strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = uint32(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind = 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		result -= uint32(sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		}))
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()
	var n, ind int
	n, _ = strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = int64(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind = 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		result -= int64(sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		}))
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()
	var n, ind int
	n, _ = strconv.Atoi(scanner.Text())

	var maxv []int
	var minv []int
	var result = uint64(n * n)
	var sCount, l int

	var sList []string
	for ; sCount < n; sCount++ {
		scanner.Scan()

		sList = strings.Split(scanner.Text(), " ")
		l, _ = strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind = 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if l == 1 || sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Ints(s)
			maxv = append(maxv, s[l-1])
			minv = append(minv, s[0])
		}
	}

	sort.Ints(maxv)

	var i int

	for i = 0; i < len(maxv); i++ {
		result -= uint64(sort.Search(len(maxv), func(j int) bool {
			return maxv[j] > minv[i]
		}))
	}

	fmt.Println(result)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, 1000001000)
	scanner.Scan()

	n, _ := strconv.Atoi(scanner.Text())

	sMap := make([][]int, n)
	result := n * n
	nCount := 0
	for sCount := 0; sCount < n; sCount++ {
		scanner.Scan()

		sList := strings.Split(scanner.Text(), " ")
		l, _ := strconv.Atoi(sList[0])

		s := make([]int, l)

		for ind := 0; ind < l; ind++ {
			s[ind], _ = strconv.Atoi(sList[ind+1])
		}

		if sort.IsSorted(sort.Reverse(sort.IntSlice(s))) {
			sort.Sort(sort.IntSlice(s))
			sMap[nCount] = s
			nCount++
		}
	}

	lenS := len(sMap)
	for i := 0; i < lenS; i++ {
		if sMap[i] == nil {
			continue
		}
		for j := 0; j < lenS; j++ {
			if sMap[j] == nil {
				continue
			}

			if sMap[i][0] >= sMap[j][len(sMap[j])-1] {
				result--
			}
		}
	}

	fmt.Println(result)
}

package main

import (
    "os"
    "bufio"
    "fmt"
    "strconv"
    "sort"
)
var scanner *bufio.Scanner

func read() string {
    if !scanner.Scan() {
		panic("Scan returned false")
	}
	return scanner.Text()
}
func readInt() int {
	num, _ := strconv.Atoi(read())
	return num
}
func readFloat() float64 {
    num, _ := strconv.ParseFloat(read(), 64)
    return num
}
func main() {
    scanner = bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    t := readInt()

    var minv, maxv []float64
    for i := 0; i < t; i++ {
        l := readInt()

        min := 1e9
        max := -1e9
        asc := false
        for j := 0; j < l; j++ {
            v := readFloat()

            if v > min {
                asc = true
            }
            if v < min {
                min = v
            }
            if v > max {
                max = v
            }
        }
        if asc {
            min = -1e9
            max = 1e9
        }
        minv = append(minv, min)
        maxv = append(maxv, max)
    }
    sort.Float64s(maxv)

    cnt := 0

    for i := 0; i < t; i++ {
        ub := sort.Search(len(maxv), func(j int) bool { return maxv[j] > minv[i] })
        cnt += t - ub
    }
    fmt.Println(cnt)
}

package main

import (
    "os"
    "bufio"
    "fmt"
    "strconv"
    "sort"
)
var scanner *bufio.Scanner

func read() string {
    if !scanner.Scan() {
		panic("Scan returned false")
	}
	return scanner.Text()
}
func readInt() int {
	num, _ := strconv.Atoi(read())
	return num
}
func readFloat() float64 {
    num, _ := strconv.ParseFloat(read(), 64)
    return num
}
func main() {
    scanner = bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    t := readInt()

    var minv, maxv []float64
    for i := 0; i < t; i++ {
        l := readInt()

        min := 1e9
        max := -1e9
        asc := false
        for j := 0; j < l; j++ {
            v := readFloat()

            if v > min {
                asc = true
            }
            if v < min {
                min = v
            }
            if v > max {
                max = v
            }
        }
        if asc {
            min = -1e9
            max = 1e9
        }
        minv = append(minv, min)
        maxv = append(maxv, max)
    }
    sort.Float64s(maxv)

    var cnt float64

    for i := 0; i < t; i++ {
        ub := sort.Search(len(maxv), func(j int) bool { return maxv[j] > minv[i] })
        cnt += float64(t - ub)
    }
    fmt.Println(cnt)
}

package main

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

type WordScanner struct {
	*bufio.Scanner
}

func NewWordScanner() *WordScanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &WordScanner{Scanner: s}
}

func (s *WordScanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *WordScanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *WordScanner) NextString() string {
	s.Scan()
	return s.Text()
}

type seq struct {
	min int
	max int
}

type minSorter []seq

func (m minSorter) Len() int {
	return len(m)
}

func (m minSorter) Less(a, b int) bool {
	return m[a].min < m[b].min
}

func (m minSorter) Swap(a, b int) {
	m[a], m[b] = m[b], m[a]
}

func main() {
	wscn := NewWordScanner()
	n := wscn.NextInt()
	totalCombs := int64(n) * int64(n)
	seqs := []seq{}
	for ; n > 0; n-- {
		l := wscn.NextInt()
		s := wscn.NextInt()
		tmp := seq{
			min: s,
			max: s,
		}
		hasStep := false
		for ; l > 1; l-- {
			s = wscn.NextInt()
			if s > tmp.min {
				hasStep = true
			}
			if s < tmp.min {
				tmp.min = s
			}
			if s > tmp.max {
				tmp.max = s
			}
		}
		if !hasStep {
			seqs = append(seqs, tmp)
		}
	}
	fmt.Println(totalCombs - int64(sol(seqs)))
}

func binSearch(max int, seqs []seq) int {
	l, r := 0, len(seqs)-1
	for l <= r {
		mid := l + (r-l)/2
		if max <= seqs[mid].min {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	//fmt.Println(max, len(seqs)-l, seqs)
	return len(seqs) - l
}

func sol(seqs []seq) int {
	sort.Sort(minSorter(seqs))
	ret := 0
	for _, s := range seqs {
		ret += binSearch(s.max, seqs)
	}
	return ret
}

package main

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

type WordScanner struct {
	*bufio.Scanner
}

func NewWordScanner() *WordScanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &WordScanner{Scanner: s}
}

func (s *WordScanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *WordScanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *WordScanner) NextString() string {
	s.Scan()
	return s.Text()
}

type seq struct {
	min int
	max int
}

type minSorter []seq

func (m minSorter) Len() int {
	return len(m)
}

func (m minSorter) Less(a, b int) bool {
	return m[a].min < m[b].min
}

func (m minSorter) Swap(a, b int) {
	m[a], m[b] = m[b], m[a]
}

func main() {
	wscn := NewWordScanner()
	n := wscn.NextInt()
	totalCombs := n * n
	seqs := []seq{}
	for ; n > 0; n-- {
		l := wscn.NextInt()
		s := wscn.NextInt()
		tmp := seq{
			min: s,
			max: s,
		}
		hasStep := false
		for ; l > 1; l-- {
			s = wscn.NextInt()
			if s > tmp.min {
				hasStep = true
			}
			if s < tmp.min {
				tmp.min = s
			}
			if s > tmp.max {
				tmp.max = s
			}
		}
		if !hasStep {
			seqs = append(seqs, tmp)
		}
	}
	fmt.Println(totalCombs - sol(seqs))
}

func binSearch(max int, seqs []seq) int {
	l, r := 0, len(seqs)-1
	for l <= r {
		mid := l + (r-l)/2
		if max <= seqs[mid].min {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	//fmt.Println(max, len(seqs)-l, seqs)
	return len(seqs) - l
}

func sol(seqs []seq) int {
	sort.Sort(minSorter(seqs))
	ret := 0
	for _, s := range seqs {
		ret += binSearch(s.max, seqs)
	}
	return ret
}


package main

import "fmt"
import "bufio"
import "os"

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

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

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

	var n, m int
	fmt.Fscanf(bufin, "%d %d\n", &n, &m)

	a := make([]int, n)
	cnt := make([]int, m+1)

	for i := 0; i <= m; i++ {
		cnt[i] = 0
	}

	remain := 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(bufin, "%d\n", &a[i])
		if (a[i] >= 1) && (a[i] <= m) {
			cnt[a[i]]++
		} else {
			remain++
		}
	}

	k := n / m
	r := n - k*m

	for i := 1; i <= m; i++ {
		if cnt[i] > k {
			x := min(cnt[i]-k, r)
			v := k + x
			r -= x
			remain += cnt[i] - v
			cnt[i] = v
		}
	}

	changes := 0
	for i := 1; i <= m; i++ {
		if cnt[i] < k {
			x := k - cnt[i]
			changes += x
			remain -= x
			cnt[i] = k
		}
	}

	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = -1
	}

	for i := 0; i < n; i++ {
		if (a[i] >= 1) && (a[i] <= m) && (cnt[a[i]] > 0) {
			b[i] = a[i]
			cnt[a[i]]--
		}
	}

	x := 1
	for i := 0; i < n; i++ {
		if b[i] == -1 {
			for (x <= m) && (cnt[x] == 0) {
				x++
			}
			if (x <= m) && (cnt[x] > 0) {
				b[i] = x
				cnt[x]--
			} else {
				b[i] = a[i]
			}
		}
	}

	fmt.Fprintf(bufout, "%d %d\n", k, changes)
	for i := 0; i < n; i++ {
		fmt.Fprintf(bufout, "%d ", b[i])
	}
	fmt.Fprintf(bufout, "\n")

}

package main

import (
	"fmt"
)

func I(t interface{}, n bool) {
	if n {
		fmt.Scanln(t)
	} else {
		fmt.Scan(t)
	}
}

func O(o interface{}, n bool) {
	if n {
		fmt.Println(o)
	} else {
		fmt.Printf("%v ", o)
	}
}

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

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

func main() {
	var n, m int
	I(&n, false)
	I(&m, true)

	var a []int
	num := 0
	var u *[]int = new([]int)
	*u = make([]int, m+1)
	for i := 0; i < n; i++ {
		var tmp int
		I(&tmp, false)
		a = append(a, tmp)
		if a[len(a)-1] <= m {
			(*u)[a[len(a)-1]]++
		}
	}
	o := n / m
	O(o, false)
	var add, remove []int
	for i := 1; i < len(*u); i++ {
		if (*u)[i] != o {
			if (*u)[i] < o {
				w := o - (*u)[i]
				num += w
				for p := 0; p < w; p++ {
					add = append(add, i)
				}
			} else {
				w := (*u)[i] - o
				for p := 0; p < w; p++ {
					remove = append(remove, i)
				}
			}
		}
	}
	O(num, true)

	for i := 0; i < n; i++ {
		var v bool
		if len(add) > 0 {
			for q := range remove {
				if remove[q] == a[i] {
					remove = append(remove[:q], remove[q+1:]...)
					v = true
					break
				}
			}
			if a[i] > m || v {
				a[i] = add[len(add)-1]
				add = add[:len(add)-1]
			}
		}
		if i == n-1 {
			O(a[i], true)
		} else {
			O(a[i], false)
		}
	}
}

package main

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

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m := nextInt(), nextInt()
	a := make([]int, n)
	b := make([]int, m+1)

	for i := range a {
		a[i] = nextInt()

		if a[i] <= m {
			b[a[i]]++
		}
	}

	ret := 0
	min := n / m

	// for i := range b {
	// 	if b[i] < min {
	// 		minChange += min - b[i]
	// 	}
	// }
	// println(b)
	// println(a)
	for i, j := 0, 1; i < len(a); i++ {
		for j < len(b) && b[j] >= min {
			j++
		}

		if j == len(b) {
			break
		}

		if a[i] > m || b[a[i]] > min {
			// println(i, a[i])

			if a[i] <= m {
				b[a[i]]--
			}

			b[j]++
			a[i] = j
			ret++
		}
	}

	println(min, ret)

	for i := range a {
		print(a[i], " ")
	}
	println()
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import "fmt"
import "bufio"
import "os"

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

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

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

	var n, m int
	fmt.Fscanf(bufin, "%d %d\n", &n, &m)

	a := make([]int, n)
	cnt := make([]int, m+1)

	for i := 0; i <= m; i++ {
		cnt[i] = 0
	}

	remain := 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(bufin, "%d\n", &a[i])
		if (a[i] >= 1) && (a[i] <= m) {
			cnt[a[i]]++
		} else {
			remain++
		}
	}

	k := n / m
	r := n - k*m

	for i := 1; i <= m; i++ {
		if cnt[i] > k {
			x := min(cnt[i]-k, r)
			v := k + x
			r -= x
			remain += cnt[i] - v
			cnt[i] = v
		}
	}

	changes := 0
	for i := 1; i <= m; i++ {
		if cnt[i] < k {
			x := k - cnt[i]
			changes += x
			remain -= x
			cnt[i] = k
		}
	}

	if remain > 0 {
		cnt[1] += remain
		changes += remain
	}

	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = -1
	}

	for i := 0; i < n; i++ {
		if (a[i] >= 1) && (a[i] <= m) && (cnt[a[i]] > 0) {
			b[i] = a[i]
			cnt[a[i]]--
		}
	}

	x := 1
	for i := 0; i < n; i++ {
		if b[i] == -1 {
			for (x <= m) && (cnt[x] == 0) {
				x++
			}
			if (x <= m) && (cnt[x] > 0) {
				b[i] = x
				cnt[x]--
			}
		}
	}

	fmt.Fprintf(bufout, "%d %d\n", k, changes)
	for i := 0; i < n; i++ {
		fmt.Fprintf(bufout, "%d ", b[i])
	}
	fmt.Fprintf(bufout, "\n")
}


package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		m := map[int]int{}
		for i := 0; i < n; i++ {
			num := 0
			fmt.Fscan(in, &num)
			m[num]++
		}
		ans := -1
		for i, c := range m {
			if c >= 3 {
				ans = i
				break
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main
import ("fmt"
    "bufio"
    "os")

func solve(reader *bufio.Reader) {
    var n int
    fmt.Fscan(reader, &n)
    cnt := make([]int, n + 1)
    //fmt.Println(n)
    for  i := 0; i < n; i++ {
        var num int
        fmt.Fscan(reader, &num)
        cnt[num]++
    }
    //fmt.Println(cnt)
    for i := 1; i <= n; i++ {
        if(cnt[i] >= 3) {
            fmt.Println(i)
            return
        }
    }
    fmt.Println(-1)
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    var tc int
    fmt.Scan(&tc)
    for i := 0; i < tc; i++ {
        solve(reader)
    }
    return;
}

package main

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

// func nextRound(n, k int) {
// 	res, kth := k, -1
// 	var cur, last int
// 	fmt.Scanln(&last)
// 	end := false
// 	for i := 0; i < n; i++ {
// 		fmt.Scanln(&cur)
// 		if !end {

// 		}
// 	}
// 	fmt.Print(res)
// }
func dominoPiling(m, n int) int {
	res := m / 2 * n
	if m&1 == 1 {
		res += n / 2
	}
	return res
}

func bitAdd(n int) int {
	cur := 0
	var r string
	in := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &r)
		if r[0] == '+' || r[2] == '+' {
			cur++
		} else {
			cur--
		}
	}
	return cur
}
func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	// fmt.Print(bitAdd(n))
	// fmt.Scanln(&k)	// fmt.Scanln(&n)
	// fmt.Fscan(in, &m)
	// nextRound(n, k)
	for i := 0; i < n; i++ {
		var len int
		fmt.Fscan(in, &len)
		m := make(map[int]int, len/10)
		res := -1
		for j := 0; j < len; j++ {
			var cur int
			fmt.Fscan(in, &cur)
			if res != -1 {
				continue
			}
			if _, e := m[cur]; !e {
				m[cur] = 1
			} else {
				m[cur]++
			}
			if m[cur] == 3 {
				res = cur
			}
		}
		fmt.Println(res)
		// var cur, length int
		// fmt.Fscan(in,&n)
		// Scanf(in,&length)
		// fmt.Println("length", length)

		// fmt.Print("Division ")
		// fmt.Println(rate)
	}
	return
}

package main

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

func main() {
	var ts int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &ts)

	var n, d int
	var p bool
	for ; ts > 0; ts-- {
		fmt.Fscan(in, &n)

		an := make([]int, n+1)
		p = false
		for ; n > 0; n-- {
			fmt.Fscan(in, &d)
			an[d]++
			if an[d] > 2 && !p {
				p = true
				fmt.Println(d)
				if n != 0 {
					in.ReadString('\n')
				}
				break
			}
		}
		if !p {
			fmt.Println(-1)
		}
	}
}

package main
import(
    "fmt"
    "sort"
    "bufio"
    "os"
)
func main() {
	var t int
	var in = bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		var list []int = make([]int,n)
		for j:=0;j<n;j++{
			fmt.Fscan(in,&list[j])
		}
		if n < 3 {
			fmt.Println(-1)
			continue
		}
		sort.Ints(list)
		for k:=1;k<=n-2;k++{
			counter:=0
			for j:=0;j<3;j++ {
				if list[n-k] == list[n-k-j] {
					counter++
				}
				if counter == 3 {
					fmt.Println(list[n-k])
					goto out
				}
			}
		}
		fmt.Println(-1)
		out:
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	m := make(map[int]int)
	n, x := 0, 0
	scan(&n)
	for i := 0; i < n; i++ {
		scan(&x)
		e, ok := m[x]
		if ok {
			m[x] = e + 1
		} else {
			m[x] = 1
		}
	}
	for k, v := range m {
		if v >= 3 {
			printf("%d\n", k)
			return
		}
	}
	printf("-1\n")

}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	var a [2*100000 + 1]int
	n, x := 0, 0
	scan(&n)
	for i := 0; i < n; i++ {
		scan(&x)
		a[x]++
	}
	for i := 1; i <= n; i++ {
		if a[i] >= 3 {
			printf("%d\n", i)
			return
		}
	}
	printf("-1\n")

}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprint(wr, x...)
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	solve()
}

func solve() {
	T:=nextInt()
	for Case:=0;Case<T;Case++{
		n:=nextInt()
		cnt:=make(map[int]int)
		ans:=-1
		for i:=0;i<n;i++{
			t:=nextInt()
			cnt[t]++
			if cnt[t]>=3{
				ans=t
			}
		}
		fmt.Printf("%d\n",ans)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprint(wr, x...)
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	solve()
}

func solve() {
	T:=nextInt()
	for Case:=0;Case<T;Case++{
		n:=nextInt()
		cnt:=make([]int,200010)
		ans:=-1
		for i:=0;i<n;i++{
			t:=nextInt()
			cnt[t]++
			if cnt[t]>=3{
				ans=t
			}
		}
		fmt.Printf("%d\n",ans)
	}
}

package main

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

func main(){
	//var v []int = make([] int, 200000)
	var t int
	var v int
	fmt.Scan(&t)
	Reader := bufio.NewReader(os.Stdin)
	for ;t>0; t--{
		var n int
		var toint = make(map[int]int)
		//fmt.Scan(&n)
		fmt.Fscan(Reader, &n)
		for ;n>0;n--{
			//fmt.Scan(&v)
			//Scanner.Scan()
			fmt.Fscan(Reader, &v)
			//fmt.Println(v)
			toint[v] ++
		}
		var succ bool = false
		for k, v := range toint{
			if v >= 3 {
				succ = true
				fmt.Println(k)
				break
			}
		}
		if succ == false{
			fmt.Println(-1)
		}
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	t := nextInt()
	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	n := nextInt()

	m := make(map[int]int)
	for i := 0; i < n; i++ {
		a := nextInt()
		elem, ok := m[a]
		if ok {
			m[a] = elem + 1
		} else {
			m[a] = 1
		}
	}

	for k, v := range m {
		if v >= 3 {
			out(k)
			return
		}
	}
	out(-1)
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprint(wr, x...)
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	t := nextInt()

	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	n := nextInt()
	mp := make(map[int]int)

	for i := 0; i < n; i++ {
		a := nextInt()
		elem, ok := mp[a]
		if ok {
			mp[a] = elem + 1
		} else {
			mp[a] = 1
		}
	}

	for key, element := range mp {
		if element >= 3 {
			out(key, "\n")
			return
		}
	}
	out(-1, "\n")
}

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		var num int
		mmid := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &num)
			mmid[num]++
		}
		flag := true
		for k, v := range mmid {
			if v >= 3 {
				flag = false
				fmt.Fprintln(out, k)
				break
			}
		}
		if flag {
			fmt.Fprintln(out, -1)
		}

	}
}

// @Title
// @Description
// @Author
// @Update
package main

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

func main() {
        var t int
        cin := bufio.NewReader(os.Stdin)
        fmt.Fscan(cin, &t)
        for l := 1; l <= t; l++ {
                var n int
                mp := make(map[int]int)
                fmt.Fscan(cin, &n)
                ans := -1
                x := 1
                for i := 1; i <= n; i++ {
                        fmt.Fscan(cin, &x)
                        mp[x]++
                        if mp[x] >= 3 {
                                ans = x
                        }
                }
                fmt.Println(ans)
        }

}

package main

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

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

func main() {
	defer out.Flush()
	var T int
	var N, X int
	var A [200001]int
	fmt.Fscan(in, &T)
	for i := 0; i < T; i++ {
		fmt.Fscan(in, &N)
		for ii := 0; ii < N; ii++ {
			fmt.Fscan(in, &X)
			A[X]++
		}
		nothing := true
		for ii := 0; ii <= N; ii++ {
			if A[ii] >= 3 && nothing {
				nothing = false
				fmt.Fprintln(out, ii)
			}
			A[ii] = 0
		}
		if nothing {
			fmt.Fprintln(out, -1)
		}
	}
}

package main

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

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

func solve() {
	var n, num, ans int
	scanf("%d\n", &n)

	ans = -1

	mp := map[int]int{}
	for i := 0; i < n; i++ {
		scanf("%d ", &num)
		mp[num] += 1
		if mp[num] == 3 {
			ans = num
		}
	}
	scanf("\n")
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	var cases int

	scanf("%d\n", &cases)
	// cases = 1

	for c := 0; c < cases; c++ {
		solve()
	}
}

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

func IntArray(size int) []int {
	arr := make([]int, size)
	for i := 0; i < size; i++ {
		scanf("%d", &arr[i])
	}
	scanf("\n")
	return arr
}

func Max(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] > m {
			m = args[i]
		}
	}
	return m
}

func Min(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] < m {
			m = args[i]
		}
	}
	return m
}

package main

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

var in = bufio.NewReader(os.Stdin)

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func pl(a ...interface{})                { fmt.Println(a...) }
func pf(format string, a ...interface{}) { fmt.Printf(format, a...) }
func scan(a ...interface{})              { fmt.Fscan(in, a...) }

func solve() {
	var n int
	scan(&n)
	m := make(map[int]int)
	for i := 0; i < n; i++ {
		var x int
		scan(&x)
		m[x]++
	}
	for k, v := range m {
		if v >= 3 {
			pl(k)
			return
		}
	}
	pl(-1)
}

func main() {
	var T int
	scan(&T)
	for ; T > 0; T-- {
		solve()
	}
}

func ys(b bool) string { return map[bool]string{true: "YES", false: "NO"}[b] }

package main

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

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

func solve(a []int) int {
	mp := map[int]int{}
	for i := range a {
		mp[a[i]]++
		if mp[a[i]] == 3 {
			return a[i]
		}
	}
	return -1
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		fmt.Fprintln(out, solve(a))
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	str, _ := reader.ReadString('\n')
	t, _ := strconv.ParseInt(strings.TrimSpace(str), 10, 32)
	tt := int(t)
	for i := 0; i < tt; i++ {
		solve(reader)
	}
}

func solve(reader *bufio.Reader) {
	ns := make(map[int]int)
	n := ReadInteger(reader, '\n')
	ans := []int{}
	for i := 0; i < n; i++ {
		var cur int
		if i == n-1 {
			cur = ReadInteger(reader, '\n')
		} else {
			cur = ReadInteger(reader, ' ')
		}
		ns[cur]++
		if ns[cur] == 3 {
			ans = append(ans, cur)
		}
	}

	if len(ans) == 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans[0])
	}

}

func ReadInteger(reader *bufio.Reader, lst byte) int {
	str, _ := reader.ReadString(lst)
	n, _ := strconv.ParseInt(strings.TrimSpace(str), 10, 32)
	return int(n)
}

package main

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

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

	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)

		store := map[int]int{}
		ans := -1
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(in, &val)

			if sum, ok := store[val]; ok {
				store[val] = sum + 1
				if sum+1 == 3 {
					ans = val
				}
			} else {
				store[val] = 1
			}
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

func main() {
    defer writer.Flush()

    var numTests int
    fmt.Fscan(reader,&numTests)
    for t := 1; t <= numTests; t++ {
        var n int
        fmt.Fscan(reader,&n)

        cnt := make([]int,n + 1)

        ans := -1
        for i := 1; i <= n; i++ {
            var value int
            fmt.Fscan(reader,&value)
            cnt[value] += 1
            if cnt[value] >= 3 {
                ans = value
            }
        }
        fmt.Fprintf(writer,"%d\n",ans)
    }
}

package main

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

func CF1669B(_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 + 1)
		for i := 0;i < n; i++ {
			x := 0
			Fscan(in, &x)
			a[x]++
		}
		flag := true
		for i := 1;i <= n; i++ {
			if a[i] >= 3 {
				Fprintln(out, i)
				flag = false
				break
			}
		}
		if flag {
			Fprintln(out, -1)
		}
	}
}

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

package main

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

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

func (in *CF1669B) 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 *CF1669B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1669B
Date: 22/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1669/B
Problem: CF1669B
**/
func (in *CF1669B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		sort.Ints(arr)
		ans := -1
		for i := 2; i < n; i++ {
			if arr[i] == arr[i-1] && arr[i] == arr[i-2] {
				ans = arr[i]
				break
			}
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

func solve(cin *scanner, cout *writer, cerr *writer) {
	n := cin.NextInt()
	a := cin.NextInts(n)
	cnt := make(map[int]int)
	for _, v := range a {
		cnt[v]++
	}
	for k, v := range cnt {
		if v >= 3 {
			cout.Println(k)
			return
		}
	}
	cout.Println(-1)
}

func main() {
	cin := NewScanner(os.Stdin)
	cout, cerr := NewWriter(os.Stdout), NewWriter(os.Stderr)
	defer cout.Flush()
	defer cerr.Flush()
	t := cin.NextInt()
	// t := 1
	for ti := 0; ti < t; ti++ {
		solve(cin, cout, cerr)
	}
}

// Template Bolierplate {{{
type scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader) *scanner {
	s := bufio.NewScanner(r)
	s.Split(bufio.ScanWords)
	s.Buffer(nil, 1024*1024)
	return &scanner{s}
}

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

func (i *scanner) Nexts(n int) []string {
	res := make([]string, n)
	for j := range res {
		res[j] = i.Next()
	}
	return res
}

func (i *scanner) NextInt() int {
	v, _ := strconv.Atoi(i.Next())
	return v
}

func (i *scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(i.Next(), 10, 64)
	return v
}

func (i *scanner) NextInts(n int) []int {
	res := make([]int, n)
	for j := range res {
		res[j] = i.NextInt()
	}
	return res
}

func (i *scanner) NextInt64s(n int) []int64 {
	res := make([]int64, n)
	for j := range res {
		res[j] = i.NextInt64()
	}
	return res
}

type writer struct {
	w *bufio.Writer
}

func NewWriter(w io.Writer) *writer {
	return &writer{bufio.NewWriter(w)}
}

func (i *writer) Print(a ...interface{}) {
	fmt.Fprint(i.w, a...)
}

func (i *writer) Println(a ...interface{}) {
	fmt.Fprintln(i.w, a...)
}

func (i *writer) Printf(format string, a ...interface{}) {
	fmt.Fprintf(i.w, format, a...)
}

func (i *writer) Flush() {
	i.w.Flush()
}

// }}}

// vim: fdm=marker

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var T,N,a,count int
	judg := true
	fmt.Fscan(in, &T)

	for i := 0;i < T;i++{
		fmt.Fscan(in, &N)
		m := make(map[int]int)
		for j := 0;j < N;j++{
			fmt.Fscan(in, &a)
			if val,ok := m[a];ok{
				if val == 1{
					m[a] = 2
				}else{
					m[a] = 3
					if judg{
						fmt.Println(a)
					}
					count = 10
					judg = false
				}
			}else{
				m[a] = 1
			}
		}
		if count < 8{
			fmt.Println(-1)
		}
		judg = true
		count = 0
	}
}

package main

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

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

func CF16669B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var x int
	var n int

loop:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		r := []int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r = append(r, x)
		}

		h := map[int]int{}
		for _, v := range r {
			h[v]++
			if h[v] >= 3 {
				Fprintln(out, v)
				continue loop
			}
		}

		Fprintln(out, -1)
	}
}

package main

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

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

func CF1669B(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		n := 0
		fmt.Fscan(in, &n)
		mp := make(map[int]int)
		for j := 0; j < n; j++ {
			v := 0
			fmt.Fscan(in, &v)
			mp[v]++
		}
		ans := -1
		for v, cnt := range mp {
			if cnt >= 3 {
				ans = v
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

func Scan(a ...interface{}) { fmt.Fscan(in, a...) }
func Scanln(s *string) {
	for *s, _ = in.ReadString('\n'); *s == "\n"; {
		*s, _ = in.ReadString('\n')
	}
	*s = (*s)[:len(*s)-1]
}
func Print(a ...interface{})                 { fmt.Fprint(out, a...) }
func Printf(format string, a ...interface{}) { fmt.Fprintf(out, format, a...) }
func Log(a ...interface{}) {
	pc, _, line, _ := runtime.Caller(1)
	fmt.Fprint(out, "log ", runtime.FuncForPC(pc).Name(), ":", line, " ")
	fmt.Fprint(out, a...)
	fmt.Fprint(out, "\n")
}

const N = 2e5 + 10

func solve() {
	var (
		n   int
		cnt [N]int
	)
	Scan(&n)

	ans := -1
	for i := 0; i < n; i++ {
		var a int
		Scan(&a)
		cnt[a]++
		if cnt[a] >= 3 {
			ans = a
		}
	}
	Print(ans, "\n")
}

func main() {
	defer out.Flush()

	T := 1
	Scan(&T)
	for test := 0; test < T; test++ {
		solve()
	}
}

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)
	isok := false
	nums := make(map[int]int)
	for i:=0;i<n;i++ {
		var a int
		fmt.Fscan(r, &a)
		nums[a]++
		if !isok && nums[a] >= 3 {
			fmt.Fprintln(w, a)
			isok = true
		}
	}
	if !isok {
		fmt.Fprintln(w, -1)
	}
}

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

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

package main

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

/*

 */

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	numToCnt := make(map[int]int)

	for i := 0; i < n; i++ {
		var num int
		scanf("%d", &num)

		numToCnt[num]++
	}
	scanf("\n")

	for num, cnt := range numToCnt {
		if cnt >= 3 {
			fmt.Fprintln(writer, num)
			return
		}
	}

	fmt.Fprintln(writer, -1)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnhandledErrorResult
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

	for ftr := 2; ftr <= sqrtA; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > sqrtA {
		factors = append(factors, num)
	}

	return factors
}

package main

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

const MOD int64 = 1000000007

var n int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {
	mp := make(map[int]int)

	for i := 0; i < n; i++ {
		mp[a[i]]++
	}

	for k , v := range mp {
		if v >= 3 {
			fmt.Println(k)
			return
		}
	}
	fmt.Println(-1)
}

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

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)

		d := -1
		m := make([]int, n+1)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			m[v]++
			if d < 0 && m[v] >= 3 {
				d = v
			}
		}

		fmt.Fprintln(out, d)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(curCase int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		cnt := map[int]int{}
		for _, v := range a {
			cnt[v]++
		}
		for v, c := range cnt {
			if c > 2 {
				Fprintln(out, v)
				return
			}
		}
		Fprintln(out, -1)
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		solve(curCase)
	}

	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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

package main
import ("fmt"
    "bufio"
    "os")

func solve() {
    in := bufio.NewReader(os.Stdin)
    var n int;
    fmt.Fscan(in, &n)
    cnt := make([]int, n + 1)
    for  i := 0; i < n; i++ {
        var num int
        fmt.Fscan(in, &num)
        cnt[num]++
    }
    for i := 1; i <= n; i++ {
        if(cnt[i] >= 3) {
            fmt.Println(i)
            return
        }
    }
    fmt.Println(-1)
}

func main() {
    var tc int
    fmt.Scanln(&tc)
    for i := 0; i < tc; i++ {
        solve()
    }
    return;
}

package main
import (
	"fmt"
	"sort"
)

func solve() {
    var n int;
    fmt.Scan(&n)
    a := make([]int, n)
    for  i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    sort.Slice(a, func(i, j int) bool {
        return a[i] < a[j]
    })
    fmt.Println(a);
    var num, cnt int = 0, 0
    for i := 0; i < n; i++ {
        if a[i] != num {
            num, cnt = a[i], 1
        } else {
            cnt++
        }
        if(cnt == 3) {
            fmt.Println(num)
            return;
        }
    }
    fmt.Println(-1)
}

func main() {
    var tc int
    fmt.Scanln(&tc)
    for i := 0; i < tc; i++ {
        solve()
    }
}

package main
import (
	"fmt"
	"sort"
)

func solve() {
    var n int;
    fmt.Scanln(&n)
    a := make([]int, n)
    for  i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    sort.Slice(a, func(i, j int) bool {
        return a[i] < a[j]
    })
    fmt.Println(a);
    var num, cnt int = 0, 0
    for i := 0; i < n; i++ {
        if a[i] != num {
            num, cnt = a[i], 1
        } else {
            cnt++
        }
        if(cnt == 3) {
            fmt.Println(num)
            return;
        }
    }
    fmt.Println(-1)
}

func main() {
    var tc int
    fmt.Scanln(&tc)
    for i := 0; i < tc; i++ {
        solve()
    }
}

package main
import (
	"fmt"
	"sort"
)

func solve() {
    var n int;
    fmt.Scanln(&n)
    a := make([]int, n)
    for  i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    sort.Slice(a, func(i, j int) bool {
        return a[i] < a[j]
    })
    var num, cnt int = 0, 0
    for i := 0; i < n; i++ {
        if a[i] != num {
            num, cnt = a[i], 1
        } else {
            cnt++
        }
        if(cnt == 3) {
            fmt.Println(num)
            return;
        }
    }
    fmt.Println(-1)
}

func main() {
    var tc int
    fmt.Scanln(&tc)
    for i := 0; i < tc; i++ {
        solve()
    }
}

package main

import "fmt"

// 对于超过10的长度的字符串缩写
func suoxie(n int) {
	for i := 0; i < n; i++ {
		var s string
		fmt.Scanln(&s)
		a := len(s)
		// fmt.Println(s)
		// fmt.Println(s[0])
		if a > 10 {
			fmt.Print(string(s[0]))
			fmt.Print(a - 2)
			fmt.Println(string(s[a-1]))
		} else {
			fmt.Println(s)
		}
	}
}

// 三人中至少两人才做题
func teamSolveProblem(n int) {
	res := 0
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < 3; j++ {
			var sc int
			fmt.Scan(&sc)
			if sc == 1 {
				cnt++
			}
		}
		if cnt >= 2 {
			res++
		}
	}
	fmt.Println(res)
}

// func nextRound(n, k int) {
// 	res, kth := k, -1
// 	var cur, last int
// 	fmt.Scanln(&last)
// 	end := false
// 	for i := 0; i < n; i++ {
// 		fmt.Scanln(&cur)
// 		if !end {

// 		}
// 	}
// 	fmt.Print(res)
// }
func main() {
	var n int
	fmt.Scanln(&n)
	// fmt.Scanln(&k)
	// nextRound(n, k)
	for i := 0; i < n; i++ {
		var cur, length int
		fmt.Scan(&length)
		// fmt.Println("length", length)
		res := make([]int, 0)
		m := make(map[int]int, 0)
		for j := 0; j < length; j++ {
			fmt.Scan(&cur)
			if _, e := m[cur]; !e {
				m[cur] = 1
			} else {
				m[cur]++
				if m[cur] == 3 {
					res = append(res, cur)
				}
			}
		}
		if len(res) == 0 {
			fmt.Println(-1)
		} else {
			for _, v := range res {
				fmt.Print(v, " ")
			}
			fmt.Println()
		}
		// fmt.Print("Division ")
		// fmt.Println(rate)
	}
	return
}

package main

import "fmt"

// 对于超过10的长度的字符串缩写
func suoxie(n int) {
	for i := 0; i < n; i++ {
		var s string
		fmt.Scanln(&s)
		a := len(s)
		// fmt.Println(s)
		// fmt.Println(s[0])
		if a > 10 {
			fmt.Print(string(s[0]))
			fmt.Print(a - 2)
			fmt.Println(string(s[a-1]))
		} else {
			fmt.Println(s)
		}
	}
}

// 三人中至少两人才做题
func teamSolveProblem(n int) {
	res := 0
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < 3; j++ {
			var sc int
			fmt.Scan(&sc)
			if sc == 1 {
				cnt++
			}
		}
		if cnt >= 2 {
			res++
		}
	}
	fmt.Println(res)
}

// func nextRound(n, k int) {
// 	res, kth := k, -1
// 	var cur, last int
// 	fmt.Scanln(&last)
// 	end := false
// 	for i := 0; i < n; i++ {
// 		fmt.Scanln(&cur)
// 		if !end {

// 		}
// 	}
// 	fmt.Print(res)
// }
func main() {
	var n int
	fmt.Scanln(&n)
	// fmt.Scanln(&k)
	// nextRound(n, k)
	for i := 0; i < n; i++ {
		var cur, length int
		fmt.Scan(&length)
		// fmt.Println("length", length)
		res := make([]int, 0)
		m := make(map[int]int, 0)
		for j := 0; j < length; j++ {
			fmt.Scan(&cur)
			if _, e := m[cur]; !e {
				m[cur] = 1
			} else {
				m[cur]++
				if m[cur] == 3 {
					res = append(res, cur)
				}
			}
		}
		if len(res) == 0 {
			fmt.Println(-1)
		} else {
			for _, v := range res {
				fmt.Print(v, " ")
			}
		}
		// fmt.Print("Division ")
		// fmt.Println(rate)
	}
}

package main

import (
	"fmt"
)

func main() {
	var ts int
	fmt.Scanln(&ts)

	var n, d int
	var p bool
	for ; ts > 0; ts-- {
		fmt.Scanln(&n)

		an := make([]int, n+1)
		p = false
		for ; n > 0; n-- {
			fmt.Scan(&d)
			an[d]++
		}
		for i, d := range an {
			if d > 2 {
				p = true
				fmt.Println(i)
				break
			}
		}
		if !p {
			fmt.Println(-1)
		}
	}
}

package main

import (
	"fmt"
)

func main(){
	//var v []int = make([] int, 200000)
	var t int
	var v int
	fmt.Scan(&t)
	for ;t>0; t--{
		var n int
		var toint = make(map[int]int)
		fmt.Scan(&n)
		for ;n>0;n--{
			fmt.Scan(&v)
			toint[v] = toint[v] + 1
		}
		var succ bool = false
		for k, v := range toint{
			if v >= 3 {
				succ = true
				fmt.Println(k)
			}
		}
		if succ == false{
			fmt.Println(-1)
		}
	}
}

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		var num int
		mmid := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &num)
			mmid[num]++
		}
		flag := true
		for k, v := range mmid {
			if v >= 3 {
				flag = false
				fmt.Fprintln(out, k)
			}
		}
		if flag {
			fmt.Fprintln(out, -1)
		}

	}
}

// @Title
// @Description
// @Author
// @Update
package main

import (
        "fmt"
)

func main() {
        var t int

        fmt.Scanln(&t)
        for l := 1; l <= t; l++ {
                var n int
                mp := make(map[int]int)
                fmt.Scanf("%d", &n)
                ans := -1
                x := 1
                for i := 1; i <= n; i++ {
                        fmt.Scanf("%d", &x)
                        mp[x]++
                        if mp[x] >= 3 {
                                ans = x
                        }
                }
                fmt.Println(ans)
        }

}

package main

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

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

func solve() {
	var n, num, ans int
	scanf("%d\n", &n)

	ans = -1

	mp := map[int]int{}
	for i := 0; i < n; i++ {
		scanf("%d ", &num)
		if val, ok := mp[num]; ok {
			if val == 2 {
				ans = val
			} else {
				mp[num] += 1
			}
		} else {
			mp[num] += 1
		}
	}
	scanf("\n")
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	var cases int

	scanf("%d\n", &cases)
	// cases = 1

	for c := 0; c < cases; c++ {
		solve()
	}
}

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

func IntArray(size int) []int {
	arr := make([]int, size)
	for i := 0; i < size; i++ {
		scanf("%d", &arr[i])
	}
	scanf("\n")
	return arr
}

func Max(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] > m {
			m = args[i]
		}
	}
	return m
}

func Min(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] < m {
			m = args[i]
		}
	}
	return m
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1383B(_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)
		s := uint(0)
		a := make([]uint, n)
		for i := range a {
			Fscan(in, &a[i])
			s ^= a[i]
		}
		if s == 0 {
			Fprintln(out, "DRAW")
			continue
		}
		b := bits.Len(s) - 1
		o := 0
		for _, v := range a {
			if v>>b&1 > 0 {
				o++
			}
		}
		if o&3 == 3 && (n-o)&1 == 0 {
			Fprintln(out, "LOSE")
		} else {
			Fprintln(out, "WIN")
		}
	}
}

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

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

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

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%s\n", sol(a, n))
	}
}

func sol(a []int64, n int64) string {
	tmp := [64]int64{}
	for _, ai := range a {
		curr := 0
		for ai > 0 {
			if ai&1 == 1 {
				tmp[curr]++
			}
			curr++
			ai >>= 1
		}
	}
	// printf("%v\n", tmp)
	for i := len(tmp) - 1; i >= 0; i-- {
		n1 := tmp[i]
		n0 := n - n1

		// even number of zeroes
		if n0 & 1 == 0 {
			mod := n1 % 4
			if mod == 1 {
				return "WIN"
			}
			if mod == 3 {
				return "LOSE"
			}
			continue
		}

		// odd number of zeroes
		mod := n1 % 4
		if mod == 1 || mod == 3 {
			return "WIN"
		}
	}
	return "DRAW"
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

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

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%s\n", sol(a, n))
	}
}

func sol(a []int64, n int64) string {
	tmp := [64]int64{}
	for _, ai := range a {
		curr := 0
		for ai > 0 {
			if ai&1 == 1 {
				tmp[curr]++
			}
			curr++
			ai >>= 1
		}
	}
	printf("%v\n", tmp)
	for i := len(tmp) - 1; i >= 0; i-- {
		n1 := tmp[i]
		n0 := n - n1

		// even number of zeroes
		if n0 & 1 == 0 {
			mod := n1 % 4
			if mod == 1 {
				return "WIN"
			}
			if mod == 3 {
				return "LOSE"
			}
			continue
		}

		// odd number of zeroes
		mod := n1 % 4
		if mod == 1 {
			return "WIN"
		}
	}
	return "DRAW"
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

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

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%s\n", sol(a, n))
	}
}

func sol(a []int64, n int64) string {
	tmp := [64]int64{}
	for _, ai := range a {
		curr := 0
		for ai > 0 {
			if ai&1 == 1 {
				tmp[curr]++
			}
			curr++
			ai >>= 1
		}
	}
	// printf("%v\n", tmp)
	for i := len(tmp) - 1; i >= 0; i-- {
		n1 := tmp[i]
		n0 := n - n1

		// even number of zeroes
		if n0 & 1 == 0 {
			mod := n1 % 4
			if mod == 1 {
				return "WIN"
			}
			if mod == 3 {
				return "LOSE"
			}
			continue
		}

		// odd number of zeroes
		mod := n1 % 4
		if mod == 1 {
			return "WIN"
		}
	}
	return "DRAW"
}