package main

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

const MOD = 1e9 + 7

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

func main() {
	defer writer.Flush()
	n := _int()
	k := _int()
	c := map[string]int64{}
	for i := 0; i < n; i++ {
		tmp := make([]rune, k)
		for j := range tmp {
			tmp[j] = rune(_int())
		}
		c[string(tmp)] = 0
	}
	printf("%d\n", sol(n, k, c))
}

func sol(n, k int, c map[string]int64) int64 {
	for s1 := range c {
		for s2 := range c {
			if s1 == s2 {
				continue
			}
			comp := complement(s1, s2)
			if _, ok := c[comp]; ok {
				// debug(s1, s2)
				c[s1]++
				c[s2]++
				c[comp]++
			}
		}
	}
	ret := int64(0)
	for _, i := range c {
		if i%6 != 0 {
			panic("invalid set count")
		}
		i /= 6
		if i < 2 {
			continue
		}
		ret += choose(i, 2)
	}
	return ret
}

func choose(n, k int64) int64 {
	if n < 0 || k < 0 || n < k {
		panic("invalid input")
	}
	numerator := fact(n)
	denominator := (fact(k) * fact(n-k)) % MOD
	return (numerator * mmi(denominator)) % MOD
}

var fv = []int64{1}

func fact(n int64) int64 {
	for i := int64(len(fv)); i <= n; i++ {
		fv = append(fv, (fv[i-1]*i)%MOD)
	}
	return fv[n]
}

func mmi(n int64) int64 {
	return pow(n, MOD-2)
}

func pow(b, p int64) int64 {
	if p == 0 {
		return 1
	}
	if p == 1 {
		return b
	}
	h := pow(b, p/2)
	if p&1 == 0 {
		return (h * h) % MOD
	}
	return (((h * h) % MOD) * b) % MOD
}

func complement(s1, s2 string) string {
	ret := []byte{}
	for i := range s1 {
		if s1[i] == s2[i] {
			ret = append(ret, s1[i])
			continue
		}
		if s1[i] != s2[i] {
			e := [3]bool{}
			e[s1[i]] = true
			e[s2[i]] = true
			for i, b := range e {
				if !b {
					ret = append(ret, byte(i))
				}
			}
		}
	}
	return string(ret)
}

func debug(s1, s2 string) {
	f := func(b []byte) []byte {
		for i := range b {
			b[i] += '0'
		}
		return b
	}
	b1 := f([]byte(s1))
	b2 := f([]byte(s2))
	b3 := f([]byte(complement(s1, s2)))
	printf("%s %s %s\n", b1, b2, b3)
}

package main

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

func get_central_card(card1, card2 []int, card_length int) string {
	central_card := make([]int, card_length)
	for i := 0; i < card_length; i++ {
		central_card[i] = (6-card1[i]-card2[i])%3
	}
	return fmt.Sprintf("%v", central_card)
}

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

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

	v2d := make([][]int, n)
	for i := 0; i < n; i++ {
		v2d[i] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Fscan(r, &v2d[i][j])
		}
	}

	m := make(map[string]int)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			v := get_central_card(v2d[i], v2d[j], k)
			m[v]++
		}
	}

	cnt := 0
	for _, v := range v2d {
		k := fmt.Sprintf("%v", v)
		cnt += (m[k]*(m[k]-1))/2
	}

	fmt.Fprintln(w, cnt)

}

package main

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

func check(x, y string) string {
	n := len(x)
	v := []byte{}
	tmp := []byte{'0', '1', '2'}
	for i := 0; i < n; i++ {
		if x[i] == y[i] {
			v = append(v, x[i])
		} else {
			for j := 0; j < 3; j++ {
				if tmp[j] != x[i] && tmp[j] != y[i] {
					v = append(v, tmp[j])
					break
				}
			}
		}
	}
	return string(v)
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, k int
	fmt.Fscan(in, &n, &k)
	a := make([]string, n+5)
	for i := 1; i <= n; i++ {
		v := []byte{}
		for j := 1; j <= k; j++ {
			var x int
			fmt.Fscan(in, &x)
			v = append(v, byte(x+'0'))
		}
		a[i] = string(v)
	}
	mp := map[string]int{}
	cont := map[string]bool{}
	vis := map[string]bool{}
	for i := 1; i <= n; i++ {
		cont[a[i]] = true
	}
	ans := 0
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			s := check(a[i], a[j])
			f := []string{a[i], a[j], s}
			sort.Strings(f)
			ok := f[0] + f[1] + f[2]
			if cont[s] && !vis[ok] {
				ans += mp[a[i]]
				ans += mp[a[j]]
				ans += mp[s]
				mp[a[i]]++
				mp[a[j]]++
				mp[s]++
				//fmt.Println(a[i], a[j], s, mp[a[i]], mp[a[j]], mp[s])
				vis[ok] = true
			}
		}
	}
	fmt.Fprintln(out, ans)
}

package main

import (
	"fmt"
)

var n, k int
var ar [][]int
const L int = 22
var pow [L]int64

func main() {
	fmt.Scan(&n, &k)
	ar = make([][]int, n)
	for i:=0; i<n; i++ {
		ar[i] = make([]int, k)
		for j:=0; j<k; j++ {
			fmt.Scan(&ar[i][j])
			// fmt.Printf("%d %d %d\n", i, j, ar[i][j])
		}
	}
	// fmt.Printf("%d %d %v", n, k, ar)

	pow[0] = 1
	for i:=1; i<L; i++ {
		pow[i] = pow[i-1] * 3;
	}

	getHash := func(a []int) int64 {
		var res int64 = 0
		for i:=0; i<k; i++ {
			res += int64(a[i]) * pow[i]
		}
		return res
	}

	getRequiresHash := func(i, j int) int64 {

		getOther := func(a, b int) int {
			for val:=0; val<3; val++ {
				if val != a && val != b {
					return val
				}
			}
			return -1
		}

		requires := make([]int, k)
		for x:=0; x<k; x++ {
			if ar[i][x] == ar[j][x] {
				requires[x] = ar[i][x]
				continue
			}
			requires[x] = getOther(ar[i][x], ar[j][x])
		}

		return getHash(requires)
	}

	mp := make(map[int64]int)
	for i:=0; i<n; i++ {
		for j:=i+1; j<n; j++ {
			h := getRequiresHash(i, j)
			if val, ok := mp[h]; ok {
				mp[h] = val + 1
				continue
			}
			mp[h] = 1
		}
	}

	// fmt.Printf("%v\n", mp)

	var res int64 = 0
	for i:=0; i<n; i++ {
		h := getHash(ar[i])
		val, ok := mp[h]
		if !ok {
			continue
		}
		res += int64(val) * int64(val-1) / 2
	}

	fmt.Println(res)
}

package main

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

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

func main() {
	defer out.Flush()

	var n, k int
	fmt.Fscan(in, &n, &k)
	c := make([][20]int8, n)
	exists := make(map[uint32]bool, n)
	for i := 0; i < n; i++ {
		var h uint32
		for j := 0; j < k; j++ {
			fmt.Fscan(in, &c[i][j])
			h = h*3 + uint32(c[i][j])
		}
		exists[h] = true
	}

	var ans int64
	for i := 0; i < n; i++ {
		var sets int64
		for j := 0; j < n; j++ {
			if i == j {
				continue
			}
			var h uint32
			for a := 0; a < k; a++ {
				h *= 3
				if c[i][a] != c[j][a] {
					h += 3 - uint32(c[i][a]+c[j][a])
				} else {
					h += uint32(c[i][a])
				}
			}
			if exists[h] {
				sets++
			}
		}
		sets /= 2
		ans += sets * (sets - 1) / 2
	}

	fmt.Fprintln(out, ans)
}

package main

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

func get_central_card(card1, card2 []int, card_length int) []int {
	central_card := make([]int, card_length)
	for i := 0; i < card_length; i++ {
		central_card[i] = (6-card1[i]-card2[i])%3
	}
	return central_card
}

func get_key(slc []int) int {
	k := 0
	for _, v := range slc {
		k = 3*k + v
	}
	return k
}

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

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

	v2d := make([][]int, n)
	for i := 0; i < n; i++ {
		v2d[i] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Fscan(r, &v2d[i][j])
		}
	}

	m := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			v := get_central_card(v2d[i], v2d[j], k)
			m[get_key(v)]++
		}
	}

	cnt := 0
	for _, v := range v2d {
		k := get_key(v)
		cnt += (m[k]*(m[k]-1))/2
	}

	fmt.Fprintln(w, cnt)

}


package main

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

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

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

	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(bi, "%d", &A[i])
	}

	sort.Ints(A)

	i, s, t := 0, int64(0), int64(0)
	for ; i < n; i++ {
		s = int64(A[i]-1)*int64(A[i])/2 - t
		if s >= int64(m) {
			break
		}
		t += int64(A[i])
	}

	k := 0
	if s != int64(m) {
		var a, b, c float64 = 1, 1, float64(-2 * (int64(m) + t))
		k = int((math.Sqrt(b*b-4*a*c)-b)/2/a) - i
	} else {
		k = A[i] - i - 1
	}

	fmt.Println(k)
	for i, j := 0, 1; k > 0; j++ {
		if i < n && A[i] == j {
			i++
		} else {
			fmt.Printf("%d ", j)
			k--
		}
	}
}

package main

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

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	n := readInt(scanner)
	m := readInt(scanner)
	numbers := getMap(scanner, n)
	result := []int{}
	spentMoney := 0
	for i:=1;i<=300000;i++ {
		if m - spentMoney < i {
			break
		}
		if !numbers[i] {
			spentMoney += i
			result = append(result, i)
		}
	}
	fmt.Println(len(result))
	for i:=0;i<len(result);i++ {
		fmt.Print(result[i], " ")
	}
}
func getMap(scanner *bufio.Scanner, n int) map[int]bool {
	numbers := make(map[int]bool, n)
	for i:=0;i<n;i++ {
		value := readInt(scanner)
		numbers[value] = true
	}
	return numbers
}
func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	value, _ := strconv.Atoi(scanner.Text())
	return value
}

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n, m int
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

func solve(n, m int) {
	in := make(map[int]bool)
	for i := 0; i < n; i++ {
		var t int
		I(&t)
		in[t] = true
	}
	var ans []int
	for i := 1; i <= 1e7; i++ {
		if !in[i] && m-i >= 0 {
			ans = append(ans, i)
			m -= i
		} else if m-i < 0 {
			break
		}
	}
	O(len(ans), "\n")
	for i := range ans {
		sep := " "
		if i == len(ans)-1 {
			sep = "\n"
		}
		O(ans[i], sep)
	}
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	k := 0
	var buy []int

	j := 0
	ok := true
	start := 1
	end := array[j]
	for m > 0 {
		for i := start; i < end; i++ {
			if i <= m {
				m -= i
				k++
				buy = append(buy, i)
			} else {
				ok = false
				break
			}
		}
		if !ok {
			break
		}
		j++
		start = end + 1
		if j < n {
			end = array[j]
		} else {
			end = 1000000000
		}
	}
	fmt.Println(k)
	fmt.Println(strings.Trim(fmt.Sprint(buy), "[]"))
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	k := 0
	var buy []int

	j := 0
	ok := true
	start := 1
	end := array[j]
	for m > 0 {
		for i := start; i < end; i++ {
			if i <= m {
				m -= i
				k++
				buy = append(buy, i)
			} else {
				ok = false
				break
			}
		}
		if !ok {
			break
		}
		j++
		start = end + 1
		if j < n {
			end = array[j]
		} else {
			end = 1000000001
		}
	}
	fmt.Println(k)
	fmt.Println(strings.Trim(fmt.Sprint(buy), "[]"))
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

// Codeforces 659 A
package main

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

var n,m int
var a sort.IntSlice

func main() {
	// reading n,m
	in := bufio.NewReader(os.Stdin)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		return
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	n, err = strconv.Atoi(ss[0])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
	m, err = strconv.Atoi(ss[1])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
    // read a
	s, err = in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		return
	}
	ss = strings.Split(strings.Trim(s, " \n\r"), " ")
    a = sort.IntSlice(make([]int, n))
    for i:=0; i<n; i++ {
        a[i],err = strconv.Atoi(ss[i])
        if err != nil {
		    fmt.Println("n atoi failure", err)
		    return
	    }
    }
    sort.Sort(a)

    // calc result
    nr:=0
    b := make([]int, 0, 100)
    ia := 0
    for i:=1; m>0; i++ {
        if ia<n && i==a[ia] {
            ia++
        } else if i<=m {
            b = append(b,i)
            nr++
            m -= i
        } else {
            break
        }
    }

	// print result
	fmt.Println(nr)
    for i:=0; i<nr; i++ {
        fmt.Printf("%d ", b[i])
    }
    fmt.Println()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	used := make(map[int]bool)
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(reader, &x)

		used[x] = true
	}

	var count, def int
	for i := 1; ; i++ {
		if count+i > m {
			break
		}
		if !used[i] && count+i <= m {
			count += i
			def++
			defer fmt.Print(i, " ")
		}

	}

	fmt.Println(def)

}

package main

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

var N int
var M int
var A map[int]bool

func main() {
	fsc := NewFastScanner()
	N = fsc.NextInt()
	M = fsc.NextInt()
	A = map[int]bool{}
	for i := 0; i < N; i++ {
		val := fsc.NextInt()
		A[val] = true
	}
	var ans []int
	for i := 1; i <= 100000000; i++ {
		if A[i] {
			continue
		}
		if M-i < 0 {
			break
		}
		M -= i
		ans = append(ans, i)
	}
	fmt.Println(len(ans))
	for i := 0; i < len(ans); i++ {
		fmt.Print(ans[i], " ")
	}
	fmt.Println()
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) Next() string {
	s.Pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}

func (s *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) NextInt64() int64 {
	val, err := strconv.ParseInt(s.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

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

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

	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(bi, "%d", &A[i])
	}

	sort.Ints(A)

	i, s, t := 0, 0, 0
	for ; i < n; i++ {
		s = (A[i]-1)*A[i]/2 - t
		if s >= m {
			break
		}
		t += A[i]
	}

	k := 0
	if s != m {
		var a, b, c float64 = 1, 1, float64(-2 * (m + t))
		k = int((math.Sqrt(b*b-4*a*c)-b)/2/a) - i
	} else {
		k = A[i] - i - 1
	}

	fmt.Println(k)
	for i, j := 0, 1; k > 0; j++ {
		if i < n && A[i] == j {
			i++
		} else {
			fmt.Printf("%d ", j)
			k--
		}
	}
}


package main

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

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)

		arr := []int{}
		m := make([]bool, n+1)
		m[0] = true
		req := n
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v <= n && !m[v] {
				m[v] = true
				req--
			} else {
				arr = append(arr, v)
			}
		}

		sort.Ints(arr)
		f := true
		i, pos := n, len(arr)-1
		for i >= 0 && pos >= 0 {
			if m[i] {
				i--
				continue
			}

			d := arr[pos]
			if (d%2 == 0 && i > d/2-1) || (d%2 != 0 && i > d/2) {
				f = false
				break
			}
			i--
			pos--
		}

		if !f {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, req)
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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) int {
	sort.Ints(A)

	ok := make([]bool, n+2)

	for i := 0; i < n; i++ {
		if A[i] >= 1 && A[i] <= n && !ok[A[i]] {
			ok[A[i]] = true
			A[i] = 0
		}
	}
	var res int
	expect := 1

	for i := 0; i < n; i++ {
		if A[i] == 0 {
			continue
		}
		for ok[expect] {
			expect++
		}
		res++
		if 2*expect >= A[i] {
			return -1
		}
		ok[expect] = true
		expect++
	}

	for ok[expect] {
		expect++
	}

	if expect > n {
		return res
	}
	return -1
}

package main

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

func ceil(x, y int) int {
	return int(math.Ceil(float64(x) / float64(y)))
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c, v, k := []int{}, 0, true, 0, map[int]struct{}{}, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			_, ok := v[p]
			if !ok && p > 0 && p <= n {
				v[p] = struct{}{}
			} else {
				o = append(o, p)
			}
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			_, found := v[j]
			if found {
				delete(v, j)
				continue
			}
			if j < ceil(o[k], 2) {
				c++
			} else {
				ok = false
				break
			}
			k++
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
		w.Flush()
	}
}

func main() {
	solve()
}

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(Case int) {
		var n int
		Fscan(in, &n)
		if n == 1 {
			var v int
			Fscan(in, &v)
			if v == 2 {
				Fprintln(out, -1)
			} else if v == 1{
				Fprintln(out, 0)
			} else {
				Fprintln(out, 1)
			}
			return
		}

		mp := func(v int) int {
			v = (v - 1) / 2
			if v > n {
				v = n
			}
			return v
		}
		ori := make([]int, n+1)
		left := make([]int, n+1)
		for i := 0; i < n; i++ {
			var v int
			Fscan(in, &v)
			if v > n {
				v = mp(v)
				left[v]++
			} else {
				ori[v]++
			}
		}
		//for i := range a {
		//	Fscan(in, &a[i])
		//
		//}
		//sort.Ints(a)
		//sort.Ints(left)

		ans := 0
		for cur := n; cur > 0; cur-- {
			if ori[cur] == 0 {
				// use left
				if left[cur] == 0 {
					Fprintln(out, -1)
					return
				}
				ans++
				left[cur-1] += left[cur] - 1
			} else {
				left[cur-1] += left[cur]
				// use ori
				left[mp(cur)] += ori[cur] - 1

			}
		}
		Fprintln(out, ans)
	}

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

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

}

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

package main

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

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

const TAM = 1e5 + 100

var n int
var a [TAM]int
var ok [TAM]bool

func solve() int {
	var ans = 0
	var mex = 1
	for i := 0; i < n; i++ {
		var ai = a[i]
		if 1 <= ai && ai <= n && !ok[ai] {
			ok[ai] = true
			continue
		}
		for ok[mex] {
			mex++
		}
		if mex*2 < ai {
			ok[mex] = true
			ans += 1
		} else {
			return -1
		}
	}
	return ans
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			ok[i+1] = false
		}
		sort.Slice(a[0:n], func(i, j int) bool { return a[i] < a[j] })
		fmt.Fprintf(out, "%d\n", solve())
	}
	out.Flush()
}

package main

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

func ceil(x, y int) int {
	return int(math.Ceil(float64(x) / float64(y)))
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c := []int{}, 0, true, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			if j < ceil(o[j-1], 2) {
				c++
			} else if j == o[j-1] {
				continue
			} else {
				ok = false
				break
			}
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
		w.Flush()
	}
}

func main() {
	solve()
}

package main

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

func ceil(x, y int) int {
	return int(math.Ceil(float64(x) / float64(y)))
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c := []int{}, 0, true, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			if j < ceil(o[j-1], 2) {
				c++
			} else if j == o[j-1] {
				continue
			} else {
				ok = false
				break
			}
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c := []int{}, 0, true, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			if o[j-1] != j && o[j-1]%(o[j-1]-j) != j {
				ok = false
				break
			} else {
				if !(o[j-1] == j) {
					c++
				}
			}
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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)

		arr := make([]int, n)
		m := make([]bool, n+1)
		m[0] = true
		req := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] <= n {
				m[arr[i]] = true
				req--
			}
		}

		sort.Ints(arr)
		pos := sort.SearchInts(arr, n)
		if arr[pos] == n {
			pos++
		}

		var tot int
		for i, v := range m {
			if v {
				continue
			}

			if pos >= n {
				break
			}

			d := arr[pos]
			if (d%2 == 0 && i > d/2-1) || (d%2 != 0 && i > d/2) {
				break
			}
			pos++
			req--
			tot++
		}

		if req > 0 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, tot)
		}
	}
}

package main

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

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)

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}

		sort.Ints(arr)
		f := true
		var tot int
		for i := 0; i < n; i++ {
			if arr[i]-1 == i {
				continue
			} else if (arr[i]%2 == 0 && i < arr[i]/2-1) || (arr[i]%2 != 0 && i < arr[i]/2) {
				tot++
			} else {
				f = false
				break
			}
		}

		if !f {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, tot)
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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) int {
	sort.Ints(A)
	var res int
	expect := 1
	ok := make([]bool, n+1)
	for i := 0; i < n; i++ {
		if A[i] >= 1 && A[i] <= n && !ok[A[i]] {
			ok[A[i]] = true
			continue
		}
		for ok[expect] {
			expect++
		}
		res++
		if 2*expect >= A[i] {
			return -1
		}
		ok[expect] = true
		expect++
	}

	if expect > n {
		return res
	}
	return -1
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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) int {
	sort.Ints(A)
	var res int
	for i := 1; i <= n; i++ {
		if A[i-1] < i {
			return -1
		}
		if A[i-1] == i {
			continue
		}
		if 2*i >= A[i-1] {
			return -1
		}
		res++
	}
	return res
}

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(Case int) {
		var n int
		Fscan(in, &n)
		if n == 1 {
			var v int
			Fscan(in, &v)
			if v == 2 {
				Fprintln(out, -1)
			} else {
				Fprintln(out, 0)
			}
			return
		}
		mp := func(v int) int {
			v = (v - 1) / 2
			if v > n {
				v = n
			}
			return v
		}
		ori := make([]int, n+1)
		left := make([]int, n+1)
		for i := 0; i < n; i++ {
			var v int
			Fscan(in, &v)
			if v > n {
				v = mp(v)
				left[v]++
			} else {
				ori[v]++
			}
		}
		//for i := range a {
		//	Fscan(in, &a[i])
		//
		//}
		//sort.Ints(a)
		//sort.Ints(left)

		ans := 0
		for cur := n; cur > 0; cur-- {
			if ori[cur] == 0 {
				// use left
				if left[cur] == 0 {
					Fprintln(out, -1)
					return
				}
				ans++
				left[cur-1] += left[cur] - 1
			} else {
				left[cur-1] += left[cur]
				// use ori
				left[mp(cur)] += ori[cur] - 1

			}
		}
		Fprintln(out, ans)
	}

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

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

}

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


package main

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

// github.com/EndlessCheng/codeforces-go
func CF1151D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var b int
	var n, ans int64
	Fscan(in, &n)
	a := make(sort.IntSlice, n)
	for i := range a {
		Fscan(in, &a[i], &b)
		a[i] -= b
		ans += int64(b)
	}
	ans *= n - 1
	sort.Sort(sort.Reverse(a))
	for i, v := range a {
		ans += int64(i) * int64(v)
	}
	Fprint(out, ans)
}

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

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/

type Pair struct {
	a int64
	b int64
}

type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p.a*(jj-1) + p.b*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0, sdtNum/3+1)
	q2 := make([]Pair, 0, sdtNum/3+1)
	q3 := make([]Pair, 0, sdtNum/3+1)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i].a - q1[i].b) > (q1[j].a - q1[j].b)
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i].b - q2[i].a) < (q2[j].b - q2[j].a)
	})

	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/
type Pair [2]int64
type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p[0]*(jj-1) + p[1]*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0)
	q2 := make([]Pair, 0)
	q3 := make([]Pair, 0)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i][0] - q1[i][1]) > (q1[j][0] - q1[j][1])
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i][1] - q2[i][0]) < (q2[j][1] - q2[j][0])
	})
	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	b := make([]int, n)
	c := make([]int, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a[i], &b[i])
		ans += int64(b[i])*int64(n) - int64(a[i])
		c[i] = a[i] - b[i]
	}
	sort.Ints(c)
	for j := 1; j <= n; j++ {
		ans += int64(c[j-1]) * int64(n-j+1)
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}

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

package main

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

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

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

func getLine() string {
	s, _ := reader.ReadString('\n')
	if strings.HasSuffix(s, "\r\n") {
		s = s[:len(s)-2]
	} else if strings.HasSuffix(s, "\n") {
		s = s[:len(s)-1]
	}
	return s
}
func getLine2Int() (n int) {
	scanf("%d\n", &n)
	return
}
func getLine2Int2() (a, b int64) {
	scanf("%d %d\n", &a, &b)
	return
}
func getLine2Ints(n int) []int {
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &ans[i])
	}
	scanf("%d\n", &ans[n-1])
	return ans
}

var debug = false

func see(a interface{}) {
	if debug {
		printf("DEBUG:%v\n", a)
	}
}

func main() {
	defer writer.Flush()

	n := getLine2Int()
	diff := make([]int64, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		a, b := getLine2Int2()
		diff[i] = b - a
		ans += a * int64(n-1)
	}
	sort.Slice(diff, func(i, j int) bool { return diff[i] < diff[j] })
	for i := 0; i < n; i++ {
		ans += diff[i] * int64(n-i-1)
	}
	printf("%d\n", ans)
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/

type Pair struct {
	a int
	b int
}

type Queue []Pair

func (q Queue) Score() (score int) {
	n := len(q)
	for j, p := range q {
		j += 1
		score += p.a*(j-1) + p.b*(n-j)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0, sdtNum/3+1)
	q2 := make([]Pair, 0, sdtNum/3+1)
	q3 := make([]Pair, 0, sdtNum/3+1)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt(s)
		b, _ := ScanInt(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i].a - q1[i].b) > (q1[j].a - q1[j].b)
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i].b - q2[i].a) < (q2[j].b - q2[j].a)
	})

	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/
type Pair [2]int64
type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p[0]*(jj-1) + p[1]*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0)
	q2 := make([]Pair, 0)
	q3 := make([]Pair, 0)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i][0] - q1[i][1]) > (q1[j][0] - q1[j][1])
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i][0] - q2[i][1]) < (q2[j][0] - q2[j][1])
	})
	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}


package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		n int
		x int
		y int
	)
	_, _ = fmt.Fscanf(input, "%d %d %d", &n, &x, &y)
	days := make([]int, n)
	for i := 0; i < n; i += 1 {
		_, _ = fmt.Fscan(input, &days[i])
	}

	min := days[0]
	ansIndex := 1
	count := 0
	for i := 1; i < n; i += 1 {
		if count == y {
			break
		}
		if days[i] < min {
			min = days[i]
			ansIndex = i + 1
			count = 0
		} else {
			count += 1
		}
	}
	fmt.Println(ansIndex)
}

package main

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

const maxn = 100000 + 5

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

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	summer := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &summer[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		flag := true
		for j := max(0, i - x); j <= min(n - 1, i + y); j ++ {
			if summer[j] < summer[i] {
				flag = false;
				break;
			}
		}
		if flag {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

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

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

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 main() {
	var n, x, y int
	fmt.Scan(&n, &x, &y)
	a := make([]int, n+1)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		a[i] = nextInt(sc)
	}
	var ok bool
	for day := 1; day <= n; day++ {
		ok = true
		lower := max(day-x, 1)
		upper := min(day+y, n)
		for j := lower; j <= upper; j++ {
			if j != day {
				if j < day {
					if a[j] < a[day] {
						ok = false
						break
					}
				} else {
					if a[j] < a[day] {
						ok = false
						break
					}
				}
			}
		}
		if ok {
			fmt.Println(day)
			return
		}
	}
}

package main

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

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

	var n, x, y int
	fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(input, "%d", &a[i])
	}
	for i := 0; i < n; i++ {
		pass := true
		for j := 0; j < x && i-j-1 > 0 && pass; j++ {
			if a[i] >= a[i-j-1] {
				pass = false
			}
		}
		for j := 0; j < y && i+j+1 < n && pass; j++ {
			if a[i] >= a[i+j+1] {
				pass = false
			}
		}
		if pass {
			fmt.Println(i + 1)
			return
		}
	}
}

package main

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

var n, x, y int

func min(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

func max(vars ...int) int {
	max := vars[0]
	for _, i := range vars {
		if max < i {
			max = i
		}
	}
	return max
}

func main() {
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	var a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(input, "%d", &a[i])
	}
	var end int
	for idx, val := range a {
		if idx == 0 {
			continue
		}
		end = min(idx+y, n)
		bad := false
		for i := max(1, idx-x); i <= end; i++ {
			if i == idx {
				continue
			}
			if a[i] < val {
				bad = true
				break
			}
		}
		if !bad {
			fmt.Println(idx)
			return
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	index = 1
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if (xx >= x || index-x <= 0) && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			yy += 1
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	a := make([]int64, n, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")

	for i := 0; i < n; i++ {
		f := true
		for j := int(math.Max(float64(i-x), float64(0))); j <= int(math.Min(float64(i+y), float64(n-1))); j++ {
			if i != j {
				//	fmt.Println(a[i], a[j])
				if a[i] >= a[j] {
					f = false
				}
			} else {
				//	fmt.Println("--")
			}
		}
		//fmt.Fprintf(writer, "->%d\n", a[i])
		if f {
			fmt.Fprintf(writer, "%d\n", i+1)
			break
		}
	}

	writer.Flush()
}

// Author: sighduck
// URL: https://codeforces.com/contest/1199/problem/A

package main

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

func Solve(n int, x int, y int, a []int) int {
	answerIndex := 0
	right := 0

	for i := 1; i < n; i++ {
		if right == y {
			return answerIndex + 1
		} else if a[i] < a[answerIndex] {
			right = 0
			answerIndex = i
		} else if a[i] > a[answerIndex] {
			right += 1
		}
	}

	return answerIndex + 1
}

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

	var n int
	var x int
	var y int

	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fprintf(writer, "%d\n", Solve(n, x, y, a))
}

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	f := os.Stdin
	// f, _ := os.Open("input.txt")
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

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

func println(s string) {
	print(s)
	print("\n")
}

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

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

func println(s string) {
	print(s)
	print("\n")
}

package main

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

const (
  inf = 0x3f3f3f3f
)

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

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

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

  n, x, y := 0, 0, 0
  fmt.Fscanf(rdr, "%d %d %d\n", &n, &x, &y)

  a := make([]int, n)

  for i := 0; i < n; i++ {
    fmt.Fscanf(rdr, "%d", &a[i])
  }

  res := solve(a, n, x, y)
  fmt.Printf("%d\n", res)
}

func solve(a []int, n, x, y int) int {
  k := max(x, y) + 1
  mins := make([][]int, k)
  for i := range mins {
    mins[i] = make([]int, n + 1)
  }

  for j := 0; j <= n; j++ {
    mins[0][j] = inf
  }
  for i := 1; i < k; i++ {
    for j := 0; j < n; j++ {
      mins[i][j] = min(a[j], mins[i - 1][j + 1])
    }
    mins[i][n] = inf
  }

  res := inf
  for i := 0; i < n; i++ {
    left, right := inf, inf
    if i < x {
      left = mins[i][0]
    } else {
      left = mins[x][i - x]
    }
    right = mins[y][i + 1]
    if a[i] < left && a[i] < right {
      res = i + 1
      break
    }
  }
  return res
}

package main

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

func main() {
	var j, d int64
	nums := readInts()
	n, x, y := nums[0], nums[1], nums[2]
	a := readInts()
	for d = 0; d < n; d++ {
		fnd := true
		j = d - 1
		for j >= 0 && j >= d-x {
			if a[d] >= a[j] {
				fnd = false
				break
			}
			j--
		}
		if fnd {
			j = d + 1
			for j < n && j <= d+y {
				if a[d] >= a[j] {
					fnd = false
					break
				}
				j++
			}
		}
		if fnd {
			fmt.Println(d + 1)
			break
		}
	}
}

var reader = bufio.NewReaderSize(os.Stdin, 1024*1024)

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

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

func readInt() int64 {
	temp := readLine()
	res, _ := strconv.ParseInt(temp, 10, 64)
	return res
}

func readInts() []int64 {
	nums := strings.Split(readLine(), " ")
	res := make([]int64, len(nums))
	for i, str := range nums {
		res[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return res
}

func readLine() string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}
	return strings.TrimRight(string(str), "\r\n")
}

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOOS == "windows" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

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

func println(s string) {
	print(s)
	print("\n")
}

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	println(runtime.GOROOT())

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

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

func println(s string) {
	print(s)
	print("\n")
}

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	f, _ := os.Open("input.txt")
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

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

func println(s string) {
	print(s)
	print("\n")
}

package main

import (
  "fmt"
)

const (
  inf = 0x3f3f3f3f
)

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

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

func main() {
  n, x, y := 0, 0, 0
  fmt.Scanf("%d%d%d", &n, &x, &y)

  a := make([]int, n)

  for i := 0; i < n; i++ {
    fmt.Scanf("%d", &a[i])
  }

  res := solve(a, n, x, y)
  fmt.Printf("%d\n", res)
}

func solve(a []int, n, x, y int) int {
  k := max(x, y) + 1
  mins := make([][]int, k)
  for i := range mins {
    mins[i] = make([]int, n + 1)
  }

  for j := 0; j <= n; j++ {
    mins[0][j] = inf
  }
  for i := 1; i < k; i++ {
    for j := 0; j < n; j++ {
      mins[i][j] = min(a[j], mins[i - 1][j + 1])
    }
    mins[i][n] = inf
  }

  res := inf
  for i := 0; i < n; i++ {
    left, right := inf, inf
    if i < x {
      left = mins[i][0]
    } else {
      left = mins[x][i - x]
    }
    right = mins[y][i + 1]
    if a[i] < left && a[i] < right {
      res = i + 1
      break
    }
  }
  return res
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		n int
		x int
		y int
	)
	_, _ = fmt.Fscanf(input, "%d %d %d", &n, &x, &y)
	days := make([]int, n)
	for i := 0; i < n; i += 1 {
		_, _ = fmt.Fscan(input, &days[i])
	}

	min := days[0]
	ansIndex := 1
	count := 0
	for i := 1; i < n; i += 1 {
		if days[i] < min {
			min = days[i]
			ansIndex = i + 1
			count = 0
		} else {
			count += 1
			if count == y {
				break
			}
		}
	}
	fmt.Println(ansIndex)
}

package main

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

const maxn = 100000 + 5

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

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	summer := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &summer[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		flag := true
		for j := i - 1; j >= max(0, i-x); j-- {
			if summer[j] < summer[j+1] {
				flag = false
				break
			}
		}
		if !flag {
			continue
		}
		for j := i + 1; j <= min(n-1, i+y); j++ {
			if summer[j] < summer[j-1] {
				flag = false
				break
			}
		}
		if flag {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

import (
	"fmt"
)

var n, x, y int

func min(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

func max(vars ...int) int {
	max := vars[0]
	for _, i := range vars {
		if max < i {
			max = i
		}
	}
	return max
}

func main() {
	fmt.Scan(&n, &x, &y)
	var a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&a[i])
	}
	var end int
	for idx, val := range a {
		if idx == 0 {
			continue
		}
		end = min(idx+y, n)
		bad := false
		for i := max(1, idx-x); i <= end; i++ {
			if i == idx {
				continue
			}
			if a[i] < val {
				bad = true
				break
			}
		}
		if !bad {
			println(idx)
			return
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if xx >= x && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			if xx < x {
				xx = 0
				yy = 0
			} else {
				yy += 1
			}
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	index = 1
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if (xx >= x || index-x < 0) && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			yy += 1
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	index = 1
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if xx >= x && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			if xx < x {
				xx = 0
				yy = 0
			} else {
				yy += 1
			}
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	a := make([]int64, n, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")

	for i := x; i < n-y; i++ {
		f := true
		for j := i - x; j <= i+y; j++ {
			if i != j {
				if a[i] >= a[j] {
					f = false

				}
			}
		}
		if f {
			fmt.Fprintf(writer, "%d\n", i+1)
			break
		}
	}

	writer.Flush()
}


package main

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

// github.com/EndlessCheng/codeforces-go
func Sol439D(_r io.Reader, _w io.Writer) {
	ternarySearchInt := func(l, r int, f func(x int) int64) int64 {
		for l+3 <= r {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Ints(b)

	min, max := a[0], b[m-1]
	if min >= max {
		Fprintln(out, 0)
		return
	}
	Fprint(out, ternarySearchInt(min, max, func(tar int) (cnt int64) {
		for _, v := range a {
			if v >= tar {
				break
			}
			cnt += int64(tar - v)
		}
		for i := m - 1; i >= 0; i-- {
			if b[i] <= tar {
				break
			}
			cnt += int64(b[i] - tar)
		}
		return
	}))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol439D(_r io.Reader, _w io.Writer) {
	ternarySearchInt := func(l, r int, f func(x int) int64) int {
		for l+3 <= r {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min, minI := f(l), l
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min, minI = v, i
			}
		}
		return minI
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Ints(b)

	min, max := a[0], b[m-1]
	if min >= max {
		Fprintln(out, 0)
		return
	}
	f := func(tar int) (cnt int64) {
		for _, v := range a {
			if v >= tar {
				break
			}
			cnt += int64(tar - v)
		}
		for i := m - 1; i >= 0; i-- {
			if b[i] <= tar {
				break
			}
			cnt += int64(b[i] - tar)
		}
		return
	}
	i := ternarySearchInt(min, max, f)
	Fprintln(out, f(i))
}

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

package main

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

func f(k int64, a, b []int64) int64 {
	ans := int64(0)
	for i := 0; i < len(a); i++ {
		if a[i] < k {
			ans += k - a[i]
		}
	}
	for i := 0; i < len(b); i++ {
		if b[i] > k {
			ans += b[i] - k
		}
	}
	return ans
}

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

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

	a := make([]int64, n)
	b := make([]int64, m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}

	lo, hi := int64(0), int64(10e9)
	resp := int64(10e15)

	for i := 0; i < 100; i++ {
		mid1 := lo + (hi - lo) / 3
		mid2 := hi - (hi - lo) / 3

		if f(mid1, a, b) < resp {
			resp = f(mid1, a, b)
		}

		if f(mid2, a, b) < resp {
			resp = f(mid2, a, b)
		}

		if f(mid1, a, b) < f(mid2, a, b) {
			hi = mid2
		} else {
			lo = mid1
		}
	}

	fmt.Println(resp)
}

package main

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

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func f(k int, a, b []int) int64 {
	ans := int64(0)
	for i := 0; i < len(a); i++ {
		if a[i] < k {
			ans += int64(k - a[i])
		}
	}

	for i := 0; i < len(b); i++ {
		if b[i] > k {
			ans += int64(b[i] - k)
		}
	}

	return ans
}

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

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

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

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fscanf(reader, "\n")

	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}

	lo, hi := 0, 1000000000
	ans := int64(10e16)

	for i := 0; i < 100; i++ {
		if lo > hi {
			break
		}
		mid1 := lo + (hi - lo) / 3
		mid2 := hi - (hi - lo) / 3

		if f(mid1, a, b) < ans {
			ans = f(mid1, a, b)
		}

		if f(mid2, a, b) < ans {
			ans = f(mid2, a, b)
		}

		if f(mid1, a, b) >= f(mid2, a, b) {
			lo = mid1
		} else {
			hi = mid2
		}
	}

	fmt.Println(ans)

}

package main

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

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func f(k int, a, b []int) int64 {
	ans := int64(0)
	for i := 0; i < len(a); i++ {
		if a[i] < k {
			ans += int64(k - a[i])
		}
	}

	for i := 0; i < len(b); i++ {
		if b[i] > k {
			ans += int64(b[i] - k)
		}
	}

	return ans
}

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

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

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

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fscanf(reader, "\n")

	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}

	lo, hi := 0, 1000000000
	ans := int64(10e10)

	for i := 0; i < 100; i++ {
		if lo > hi {
			break
		}
		mid1 := lo + (hi - lo) / 3
		mid2 := hi - (hi - lo) / 3

		if f(mid1, a, b) < ans {
			ans = f(mid1, a, b)
		}

		if f(mid2, a, b) < ans {
			ans = f(mid2, a, b)
		}

		if f(mid1, a, b) >= f(mid2, a, b) {
			lo = mid1
		} else {
			hi = mid2
		}
	}

	fmt.Println(ans)

}


package main

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

/*
	对于相邻的两个数，一个偶数和下一奇数，那么它们的异或就是1
	1.l[0] != r[0] => 10..00(n)^01..11(n) => 11.11(n)
	2.l == r => l或r
	3.l[0] == r[0] && l != r {
		策略 => 保留r[0] =>奇数个数异或
		设区间[x,y] => 包含数字 y-x+1 = z(奇数个)
		对于前面z-1个数，若是以偶数开始奇数结束
		若((z-1)/2)%2 == 0(偶数对偶数和奇数)结果为0
		若((z-1)/2)%2 == 1(奇数对偶数和奇数)结果为1
		再与第z个数异或，结果为z或z+1
		若是以奇数开始偶数结束同理
	} => r是奇数 => r+1, r是偶数 => r

*/
var l, r string

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	Fscan(in, &n, &l, &r)
	if l[0] != r[0] {
		var b strings.Builder
		for i := 0; i < n; i++ {
			b.WriteByte('1')
		}
		Fprint(out, b.String())
		return
	}
	i := 0
	for ; i < n && !(l[i] == '0' && r[i] == '1'); i++ {
	}
	for i++; i < n; i++ {
		if l[i] == '0' || r[i] == '1' {
			Fprint(out, r[:n-1]+"1")
			return
		}
	}
	Fprint(out, r)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	var n int
	var l, r string
	Fscan(bufio.NewReader(os.Stdin), &n, &l, &r)
	if l[0] != r[0] {
		Print(strings.Repeat("1", n))
		return
	}
	i := 0
	for ; i < n; i++ {
		if r[i] == '1' && l[i] == '0' {
			break
		}
	}
	for i++; i < n; i++ {
		if r[i] == '1' || l[i] == '0' {
			Print(r[:n-1] + "1")
			return
		}
	}
	Print(r)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/big"
	"os"
	"strings"
)

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

	var n int
	var l, r string
	Fscan(in, &n, &l, &r)
	if n == 1 {
		Fprint(out, r)
	} else if l[0] == '0' {
		Fprint(out, strings.Repeat("1", n))
	} else if r[n-1] == '1' {
		Fprint(out, r)
	} else {
		v, w := &big.Int{}, &big.Int{}
		Sscanf(l, "%b", v)
		Sscanf(r, "%b", w)
		if w.Sub(w, v).Cmp(big.NewInt(1)) > 0 {
			Fprint(out, r[:n-1]+"1")
		} else {
			Fprint(out, r)
		}
	}
}

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

package main

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

/*
	对于相邻的两个数，一个偶数和下一奇数，那么它们的异或就是1
	1.l[0] != r[0] => 10..00(n)^01..11(n) => 11.11(n)
	2.l == r => l或r
	3.l[0] == r[0] && l != r {
		策略 => 保留r[0] =>奇数个数异或
		设区间[x,y] => 包含数字 y-x+1 = z(奇数个)
		对于前面z-1个数，若是以偶数开始奇数结束
		若((z-1)/2)%2 == 0(偶数对偶数和奇数)结果为0
		若((z-1)/2)%2 == 1(奇数对偶数和奇数)结果为1
		再与第z个数异或，结果为z或z+1
		若是以奇数开始偶数结束同理
	} => r是奇数 => r+1, r是偶数 => r

*/
var l, r string

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	Fscan(in, &n, &l, &r)
	if l[0] != r[0] {
		var b strings.Builder
		for i := 0; i < n; i++ {
			b.WriteByte('1')
		}
		Fprint(out, b.String())
		return
	}
	i := 0
	for ; i < n && !(l[i] == '0' && r[i] == '1'); i++ {
	}
	for i++; i < n; i++ {
		if l[i] == '0' && r[i] == '1' {
			Fprint(out, r[:n-1]+"1")
			return
		}
	}
	Fprint(out, r)
}

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

package main

import (
	. "fmt"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	var n int
	var l, r string
	Scan(&n, &l, &r)
	if l[0] == '0' {
		Print(strings.Repeat("1", n))
		return
	}
	i := 0
	for ; i < n; i++ {
		if r[i] == '1' && l[i] == '0' {
			break
		}
	}
	for i++; i < n; i++ {
		if r[i] == '1' || l[i] == '0' {
			Print(r[:n-1] + "1")
			return
		}
	}
	Print(r)
}


package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		ind, ele, p := make([]int, n+1), make([]int, n+1), 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			ind[p], ele[j+1] = j+1, p
		}
		s, e := ind[n], n
		for {
			mx := 0
			if s == 0 || e == 0 {
				break
			}
			for j := s; j <= e; j++ {
				fmt.Fprint(w, ele[j], " ")
				if ele[j] > mx {
					mx = ele[j]
				}
				ind[ele[j]] = 0
			}
			for {
				if mx > 0 && ind[mx] == 0 {
					mx--
				} else {
					break
				}
			}
			s, e = ind[mx], s-1
		}
		fmt.Fprintln(w, "")
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func solveB() {
	var n int
	scan(&n)

	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}

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

	to := n - 1
	i := n - 1
	need := mx[n-1]
	for ; i >= 0; i-- {
		if a[i] == need {
			for j := i; j <= to; j++ {
				printf("%d ", a[j])
			}
			to = i - 1
			if i > 0 {
				need = mx[i-1]
			}
		}
	}
	printf("\n")
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solveB()
	}
}

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

func main() {

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

	var t int32
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			b[i] = i
		}
		for i := 1; i < n; i++ {
			if a[b[i - 1]] > a[i] {
				b[i] = b[i - 1]
			}
		}
		x := n - 1
		for x >= 0 {
			y := b[x]
			for i := y; i <= x; i++ {
				fmt.Fprint(out, a[i], " ")
			}
			x = y - 1
		}
		fmt.Fprint(out, "\n")
	}

}

package main

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

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

	var t, n int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		p := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &p[i])
		}
		moved := make([]bool, n+1)
		curMax, end := n, n
		for i := n - 1; i >= 0; i-- {
			if p[i] == curMax {
				for j := i; j < end; j++ {
					fmt.Printf("%d ", p[j])
					moved[p[j]] = true
				}
				for ; moved[curMax]; curMax-- {
				}
				end = i
			}
			//fmt.Println(p, moved, curMax)
		}
		fmt.Println()
	}
}

/*
URL:
https://codeforces.com/contest/1492/problem/B
*/

package main

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

var (
	println = fmt.Println

	t int
	n int
	P []int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n = readi()
		P = readis(n)

		solve()
	}
}

func solve() {
	memo := make([]int, n+1)

	A := []int{}
	target := n
	befIdx := n
	curIdx := n - 1
	for curIdx >= 0 {
		memo[target]++
		// targetを探す
		for curIdx >= 0 && P[curIdx] != target {
			memo[P[curIdx]]++
			curIdx--
		}

		A = append(A, P[curIdx:befIdx]...)
		befIdx = curIdx
		curIdx--

		// targetの更新
		for target > 0 && memo[target] > 0 {
			target--
		}
	}

	printf("%s\n", PrintIntsLine(A...))
}

/*******************************************************************/

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	reads = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// chmin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func chmin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// chmax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func chmax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

// abs is integer version of math.Abs
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := pow(a, halfE)
		return half * half
	}

	return a * pow(a, e-1)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  func() string
	stdout *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

func _readInt64() int64 {
	i, err := strconv.ParseInt(reads(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// readis returns an integer slice that has n integers.
func readis(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readi()
	}
	return b
}

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	return b
}

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

func _readFloat64() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func readfs(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = readf()
	}
	return b
}

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// Printf is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func printf(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// debugf is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func debugf(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

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(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		pos := make([]int, n+1)
		for i := range a {
			Fscan(in, &a[i])
			pos[a[i]] = i
		}
		vis := make([]bool, n+1)
		ans := []int{}
		cur := n
		for len(a) > 0 {
			for vis[cur] {
				cur--
			}
			ans = append(ans, a[pos[cur]:]...)
			for _, v := range a[pos[cur]:] {
				vis[v] = true
			}
			a = a[:pos[cur]]
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

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

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

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		pos := make([]int64, n+1)
		for i := 0; i < n; i++ {
			scanf("%d", &arr[i])
			pos[arr[i]] = int64(i)
		}
		scanf("\n")
		p := make([]int64, 0, n)

		visited := make([]bool, n+1)
		curMax := n
		for len(arr) > 0 {
			for visited[curMax] {
				curMax--
			}
			newP := arr[pos[curMax]:]
			p = append(p, newP...)
			arr = arr[:pos[curMax]]
			for _, i := range newP {
				visited[i] = true
			}
		}
		for i := range p {
			printf("%d ", p[i])
		}
		printf("\n")
	}
}

package main

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

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

func solve() {
	n := getI()
	nums := make([]int64, n)
	idxs := make(map[int64]int64)

	for i := int64(0); i < n; i++ {
		cur := getI()
		nums[i] = cur
		idxs[cur] = i
	}

	prev := idxs[n]
	res := nums[prev:]
	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:prev]...)
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

func getI() int64 {
	s := getS()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

func nsort(data ...int64) {
	sort.Slice(data, func(i, j int) bool { return data[i] < data[j] })
}

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

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

func solve() {
	n := getI()
	var nums []int64
	var res []int64
	idxs := make(map[int64]int64)
	var f bool

	for i := int64(0); i < n; i++ {
		cur := getI()
		idxs[cur] = i

		if cur == n {
			f = true
		}

		if f {
			res = append(res, cur)
		} else {
			nums = append(nums, cur)
		}
	}

	prev := idxs[n]
	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:]...)
		nums = nums[:cur]
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

func getI() int64 {
	s := getS()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

func nsort(data ...int64) {
	sort.Slice(data, func(i, j int) bool { return data[i] < data[j] })
}

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

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

func solve() {
	n := getI()
	nums := make([]int64, n)
	idxs := make(map[int64]int64)

	for i := int64(0); i < n; i++ {
		cur := getI()
		nums[i] = cur
		idxs[cur] = i
	}

	prev := idxs[n]
	res := nums[prev:]
	nums = nums[:prev]
	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:]...)
		nums = nums[:cur]
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

func getI() int64 {
	s := getS()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

func nsort(data ...int64) {
	sort.Slice(data, func(i, j int) bool { return data[i] < data[j] })
}

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MAX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })
var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var n int

		fmt.Fscan(input, &n)

		XARRAY := make([]int, n)
		init := 0

		ARRAYINDEX := make([]int, 0)

		for i := 0; i < n; i++ {
			fmt.Fscan(input, &XARRAY[i])
			if XARRAY[i] > init {
				init = XARRAY[i]
				ARRAYINDEX = append(ARRAYINDEX, i)

			}

		}
		ARRAYINDEX = append(ARRAYINDEX, n)
		l := len(ARRAYINDEX)

		for i := l - 2; i > -1; i-- {
			for j := ARRAYINDEX[i]; j < ARRAYINDEX[i+1]; j++ {
				fmt.Println(XARRAY[j])
			}

		}

	}

}

package main

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

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

	var t int
	var n int
	fmt.Fscan(in, &t)

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

		p := make([]int, n)

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

		order := make([]bool, n+1)
		MaxPO, last := n, n
		for i := n - 1; i >= 0; i-- {
			if p[i] == MaxPO {
				for a := i; a < last; a++ {
					fmt.Printf("%d ", p[a])
					order[p[a]] = true
				}
				for ; order[MaxPO]; MaxPO-- {

				}
				last = i
			}
		}
		fmt.Println()
	}

}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	printf("num test case %d\n", T)
	for t := 0; t < T; t++ {
		printf("current test case: %d\n", t)
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for i := range p {
			if i != len(p)-1 {
				printf("%d ", p[i])
			} else {
				printf("%d\n", p[i])
			}
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	printf("num test case %d\n", T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for i := range p {
			if i != len(p)-1 {
				printf("%d ", p[i])
			} else {
				printf("%d\n", p[i])
			}
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for i := range p {
			if i != len(p)-1 {
				printf("%d ", p[i])
			} else {
				printf("%d\n", p[i])
			}
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for _, i := range p {
			printf("%d ", i)
		}
		fmt.Fprintf(writer, "\n")
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for _, i := range p {
			printf("%d ", i)
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for _, i := range p {
			printf("%d ", i)
		}
		printf("\n")
	}
}

package main

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

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

func solve() {
	n := getI()
	idxs := make(map[int64]int64, n-1)
	prev := n
	var nums []int64
	var res []int64

	for i := int64(0); i < n; i++ {
		cur := getI()
		if cur == n {
			prev = i
		}

		if i < prev {
			nums = append(nums, cur)
			idxs[nums[i]] = i
		} else {
			res = append(res, cur)
		}

	}

	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:prev]...)
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

func getI() int64 {
	s := getS()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

func nsort(data ...int64) {
	sort.Slice(data, func(i, j int) bool { return data[i] < data[j] })
}

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}