package main

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

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

	var n int
	Fscan(in,&n)
	a:=make([]int64,n+1)
	e:=make([][]int,n+1)
	st,ed:=make([]int,0,n+1),make([]int,0,n+1)

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

	for i:=1;i<=n;i++{
		var kk int
		Fscan(in,&kk)
		if kk==-1{
			kk=0
		}
		e[kk]=append(e[kk],i)
	}

	var ans int64
	var dfs func(int) int64
	dfs=func(u int) int64 {
		cur:=a[u]
		for _,v := range(e[u]) {
			val:=dfs(v)
			if val <0 {
				ed=append(ed,v)
			} else {
				cur+=val
				st=append(st,v)
			}
		}
		// Fprintln(out,cur,u)
		if(u>0){
			ans+=cur
		}
		return cur
	}

	dfs(0)
	Fprintln(out,ans)
	// Fprintln(out,st,ed)

	for _,v := range(st) {
		Fprint(out,v," ")
	}

	for i:=len(ed)-1;i>=0;i--{
		Fprint(out,ed[i]," ")
	}
}

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

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.nextInt()
	a, b := io.nextInts(n), io.nextInts(n)

	g := make([][]int, n+1)
	var roots []int
	for i := 0; i < n; i++ {
		if b[i] != -1 {
			g[b[i]] = append(g[b[i]], i+1)
		} else {
			roots = append(roots, i+1)
		}
	}

	var ans int64
	var later, now []int
	var dfs func(u, p int) int64
	dfs = func(u, p int) int64 {
		s := int64(a[u-1])
		for _, v := range g[u] {
			s += dfs(v, u)
		}
		ans += s
		if s > 0 {
			now = append(now, u)
			return s
		}
		later = append(later, u)
		return 0
	}
	for _, u := range roots {
		dfs(u, u)
	}
	for i := len(later) - 1; i >= 0; i-- {
		now = append(now, later[i])
	}

	io.println(ans)
	io.printlnInts(now...)
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.nextInt()
	a, b := io.nextInts(n), io.nextInts(n)

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

	ans := int64(0)
	later := [][]int{}
	var dfs func(u, p int) (int64, []int)
	dfs = func(u, p int) (int64, []int) {
		now, t := []int{}, int64(0)
		for _, v := range g[u] {
			s, y := dfs(v, u)
			t += s
			if len(now) < len(y) {
				now, y = y, now
			}
			now = append(now, y...)
		}
		cur := int64(a[u-1])
		ans += t + cur
		now = append(now, u)
		if t+cur <= 0 {
			later = append(later, now)
			return 0, []int{}
		}
		return t + cur, now
	}

	for i := 0; i < n; i++ {
		if b[i] == -1 {
			_, y := dfs(i+1, i+1)
			later = append(later, y)
		}
	}

	out := []int{}
	for i := len(later) - 1; i >= 0; i-- {
		out = append(out, later[i]...)
	}

	io.println(ans)
	io.printlnInts(out...)
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

/*
URL:
https://codeforces.com/contest/1388/problem/D
*/

package main

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

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

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

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

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

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

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

var (
	n int
	A []int64
	B []int

	G    [200000 + 50][]int
	done [200000 + 50]bool
)

func main() {
	n = ReadInt()
	A = ReadInt64Slice(n)
	B = ReadIntSlice(n)

	for i := 0; i < n; i++ {
		b := B[i]

		if b != -1 {
			G[i] = append(G[i], b-1)
		}
	}

	_, T := tsort(n, G[:n])

	ans := int64(0)
	P := []int{}

	for _, cid := range T {
		if A[cid] <= 0 {
			continue
		}

		ans += A[cid]
		done[cid] = true
		P = append(P, cid+1)

		for _, nid := range G[cid] {
			A[nid] += A[cid]
		}
	}

	revT := Reverse(T)
	for _, cid := range revT {
		if done[cid] {
			continue
		}

		ans += A[cid]
		P = append(P, cid+1)
	}

	fmt.Println(ans)
	fmt.Println(PrintIntsLine(P...))
}

func Reverse(A []int) []int {
	res := []int{}

	n := len(A)
	for i := n - 1; i >= 0; i-- {
		res = append(res, A[i])
	}

	return res
}

// O(|E| + |V|)
// ノードIDは0-based
// ok, ans := tsort(v, edges[:])
// https://onlinejudge.u-aizu.ac.jp/problems/GRL_4_B
func tsort(nn int, edges [][]int) (bool, []int) {
	res := []int{}

	degin := make([]int, nn)
	for s := 0; s < nn; s++ {
		for _, t := range edges[s] {
			degin[t]++
		}
	}

	st := []int{}
	for nid := 0; nid < nn; nid++ {
		if degin[nid] == 0 {
			st = append(st, nid)
		}
	}

	for len(st) > 0 {
		cid := st[len(st)-1]
		res = append(res, cid)
		st = st[:len(st)-1]

		for _, nid := range edges[cid] {
			degin[nid]--
			if degin[nid] == 0 {
				st = append(st, nid)
			}
		}
	}

	if len(res) != nn {
		return false, nil
	}

	return true, res
}

// トポロジカルソート済みリストから最長経路の長さを計算する
// l, dp := longestPath(res, edges[:])
// https://atcoder.jp/contests/abc139/tasks/abc139_e
func longestPath(tsortedNodes []int, edges [][]int) (maxLength int, dp []int) {
	_chmax := func(updatedValue *int, target int) bool {
		if *updatedValue < target {
			*updatedValue = target
			return true
		}
		return false
	}

	dp = make([]int, len(tsortedNodes)+1)

	for i := 0; i < len(tsortedNodes); i++ {
		cid := tsortedNodes[i]
		for _, nid := range edges[cid] {
			_chmax(&dp[nid], dp[cid]+1)
		}
	}

	maxLength = 0
	for i := 0; i < len(tsortedNodes); i++ {
		_chmax(&maxLength, dp[i])
	}

	return maxLength, dp
}

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

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

var (
	// ReadString returns a WORD string.
	ReadString 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()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

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

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

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

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

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

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

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

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

// 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
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

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

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

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


package main

import (
	"math/big"
	"bufio"
	. "fmt"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var N int
	var n, k int

	MOD := int64(1000000007)

	factorials := make([]int, 200005)

	f := int64(1)

	factorials[0] = 1
	for i := 1; i< len(factorials); i++ {
		f = f * int64(i) % MOD
		factorials[i] = int(f)
	}

	Fscan(in, &N)
	for tt:=0; tt < N; tt ++ {
		Fscan(in, &n)
		Fscan(in, &k)
		a := make ([] int, n)
		for i:=0; i<n; i++ {
			Fscan(in, &a[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))

		p := 0
		c := 0

		for i :=0; i< len(a); i++ {
			if a[i] == a[k - 1] {
				p++
				if i < k {
					c++
				}
			}
		}

		ans := C(p, c, factorials)

		out.WriteString(Sprintf("%d\n", ans))
	}

}

func C(n int, k int, factorials []int) int64 {
	MOD := big.NewInt(1000000007)

	mod := int64(1000000007)

	if n < k {
		return 0
	}
	a := new(big.Int).ModInverse(big.NewInt(int64(factorials[k])), MOD)
	b := new(big.Int).ModInverse(big.NewInt(int64(factorials[n - k])), MOD)

	aa := a.Int64()
	bb := b.Int64()

	return int64(factorials[n]) * aa % mod * bb % mod
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)
var mod =1000000007
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	f := [1005][1005]int{}
	f[0][0]=1
	for i:=1;i<=1000;i++{
		for j:=0;j<=i;j++{
			if j==0{
				f[i][j]=1
				continue
			}
			if j==1{
				f[i][j]=i
				continue
			}
			if j==i{
				f[i][j]=1
				continue
			}
			f[i][j]=(f[i-1][j-1]%mod+f[i-1][j]%mod)%mod
		}
	}
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		a := make([]int,n)
		for i:=0;i<n;i++{
			fmt.Fscan(in,&a[i])
		}
		sort.Ints(a)
		mp := map[int]int{}
		for i:=n-1;i>=0;i--{
			mp[a[i]]++
		}
		temp := k
		index := -1
		lst := -1
		for i:=n-1;i>=0;i--{
			if a[i]!=lst{
				if temp-mp[a[i]]>=0{
					temp -= mp[a[i]]
				}else{
					index = i
					break
				}
				lst = a[i]
			}
		}
		if temp == 0{
			fmt.Fprintln(out,1)
			continue
		}
		fmt.Fprintln(out,f[mp[a[index]]][temp])
	}
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

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

	fmt.Print(buf.String())
}

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
}

const MOD = 1000000007

const MAX_N = 1010

var C [MAX_N][MAX_N]int

func init() {
	C[0][0] = 1
	for i := 1; i < MAX_N; i++ {
		C[i][0] = 1
		C[i][i] = 1
		for j := 1; j < i; j++ {
			C[i][j] = C[i-1][j] + C[i-1][j-1]
			if C[i][j] >= MOD {
				C[i][j] -= MOD
			}
		}
	}
}

func solve(n int, k int, A []int) int {
	if n == k {
		return 1
	}
	sort.Ints(A)
	reverse(A)
	// first k
	cur := A[k-1]
	i := k - 1
	for i >= 0 && A[i] == cur {
		i--
	}
	// i < 0 || A[i] > cur
	j := k - 1
	for j < n && A[j] == cur {
		j++
	}
	j--
	// j +1 == n || A[j+1] < cur
	x := j - i
	y := k - 1 - i
	return C[x][y]
}

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"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()

	const mod int64 = 1e9 + 7
	const mx int = 1010
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	invF := [...]int64{mx: pow(F[mx], mod-2)}
	for i := mx; i > 0; i-- {
		invF[i-1] = invF[i] * int64(i) % mod
	}
	C := func(n, k int) int64 {
		return F[n] * invF[k] % mod * invF[n-k] % mod
	}

	solve := func(Case int) {
		var n ,k int
		Fscan(in, &n,&k)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))

		cnt := 0
		left := k
		for _, v := range a {
			if v > a[k-1] {
				left--
			} else if v == a[k-1] {
				cnt++
			}
		}

		ans := C(cnt,left)
		Fprintln(out, ans)
	}

	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"
	"sort"
)

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

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

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

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

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

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

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

type KV struct {
	K int64
	V int64
}

const MOD = 1e9 + 7

func sol(n int64, k int64, a []int64) int64 {
	m := map[int64]int64{}
	for _, i := range a {
		m[i]++
	}
	kvsl := []KV{}
	for k, v := range m {
		kvsl = append(kvsl, KV{k, v})
	}
	sort.Slice(kvsl, func(il, ir int) bool {
		return kvsl[il].K < kvsl[ir].K
	})
	ret := int64(1)
	for i := len(kvsl) - 1; i > -1; i-- {
		kv := kvsl[i]
		ret *= choose(kv.V, min(k, kv.V))
		k -= kv.V
		if k <= 0 {
			break
		}
	}
	return ret % MOD
}

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
	}
	halfn := pow(b, p/2)
	if p&1 == 0 {
		return (halfn * halfn) % MOD
	}
	return (((halfn * halfn) % MOD) * b) % MOD
}

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

package main

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

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

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

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

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

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

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

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

type KV struct {
	K int64
	V int64
}

const MOD = 1e9 + 7

func sol(n int64, k int64, a []int64) int64 {
	m := map[int64]int64{}
	for _, i := range a {
		m[i]++
	}
	kvsl := []KV{}
	for k, v := range m {
		kvsl = append(kvsl, KV{k, v})
	}
	sort.Slice(kvsl, func(il, ir int) bool {
		return kvsl[il].K < kvsl[ir].K
	})
	ret := int64(1)
	for i := len(kvsl) - 1; i > -1; i-- {
		kv := kvsl[i]
		ret *= choose(kv.V, min(k, kv.V))
		k -= kv.V
		if k <= 0 {
			break
		}
	}
	return ret
}

func choose(n, k int64) int64 {
	if n < 0 || k < 0 || n < k {
		panic("invalid input")
	}
	if k > n/2 {
		k = n - k
	}
	b := int64(1)
	for i := int64(1); i <= k; i++ {
		b = (n - k + i) * b / i
		b %= MOD
	}
	return b
}

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

package main

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

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

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

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

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

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

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

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

type KV struct {
	K int64
	V int64
}

func sol(n int64, k int64, a []int64) int64 {
	m := map[int64]int64{}
	for _, i := range a {
		m[i]++
	}
	kvsl := []KV{}
	for k, v := range m {
		kvsl = append(kvsl, KV{k, v})
	}
	sort.Slice(kvsl, func(il, ir int) bool {
		return kvsl[il].K < kvsl[ir].K
	})
	ret := int64(1)
	for i := len(kvsl) - 1; i > -1; i-- {
		kv := kvsl[i]
		ret *= choose(kv.V, min(k, kv.V))
		k -= kv.V
		if k <= 0 {
			break
		}
	}
	return ret
}

func choose(n, k int64) int64 {
	if n < 0 || k < 0 || n < k {
		panic("invalid input")
	}
	if k > n/2 {
		k = n - k
	}
	b := int64(1)
	for i := int64(1); i <= k; i++ {
		b = (n - k + i) * b / i
	}
	return b
}

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


package main

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

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

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

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

		if n == 1 {
			fmt.Fprintln(out, 0)
			continue
		}

		aa[n-1] = aa[0]
		bb := []int{n - 1}
		for i := 1; i < n-1; i++ {
			if aa[i]%2 != aa[0]%2 {
				bb = append(bb, i)
			} else {
				bb = append(bb, i*-1)
			}
		}

		fmt.Fprintln(out, len(bb))
		for _, v := range bb {
			if v > 0 {
				fmt.Fprintln(out, 1, v+1)
			} else {
				fmt.Fprintln(out, v*-1+1, n)
			}
		}
	}
}

package main
import (
	"bufio"
	. "fmt"
	"io"
	"os"
	//. "strconv" Itoa
)
func max(i int, j int) int {
	if (i > j) {
		return i;
	} else {
		return j;
	}
}
func min(i int, j int) int {
	if (i < j) {
		return i;
	} else {
		return j;
	}
}
func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T int
	Fscan(in, &T)

	for ;T>0;T--{
		var n int
		Fscan(in, &n)

		a := make([]int, n+1)
		for i:=1;i<=n;i++{
			Fscan(in, &a[i])
		}
		if n==1{
			Fprintln(out,"0")
			continue
		}
		ans:=[][2]int{}
		ans=append(ans,[2]int{1,n})
		if (a[1]+a[n])%2==0{
			a[1]=a[n]
		}else{
			a[n]=a[1]
		}
		for i:=2;i<n;i++{
			if(a[1]+a[i])%2==0{
				ans=append(ans,[2]int{i,n})
				a[i]=a[n]
			}else{
				ans=append(ans,[2]int{1,i})
				a[i]=a[1]
			}
		}
		Fprintln(out,len(ans))
		for _,a:=range ans{
			for _,i:=range a{
				Fprint(out,i," ")
			}
			Fprintln(out)
		}
	}
}
func main() { solve(os.Stdin, os.Stdout) }

package main
import (
	"bufio"
	. "fmt"
	"io"
	"os"
	//. "strconv" Itoa
)
func max(i int, j int) int {
	if (i > j) {
		return i;
	} else {
		return j;
	}
}
func min(i int, j int) int {
	if (i < j) {
		return i;
	} else {
		return j;
	}
}
func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T int
	Fscan(in, &T)

	for ;T>0;T--{
		var n int
		Fscan(in, &n)

		a := make([]int, n+1)
		for i:=1;i<=n;i++{
			Fscan(in, &a[i])
		}
		Fprintln(out,n-1)
		var x int
		if(a[1]+a[n])%2==0{
			x=a[n]
		}else{
			x=a[1]
		}
		if(n!=1){Fprintln(out,1,n)}
		for i:=2;i<n;i++{
			if(x+a[i])%2==0{
				Fprintln(out,i,n)
			}else{
				Fprintln(out,1,i)
			}
		}
	}
}
func main() { solve(os.Stdin, os.Stdout) }

package main

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

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

type Pair struct {
	First  int
	Second int
}

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		if n == 1 {
			fmt.Fprintln(out, "0")
			continue
		}
		res := make([]Pair, 0)
		res = append(res, Pair{
			First:  1,
			Second: n,
		})
		if (arr[0]+arr[n-1])%2 == 0 {
			arr[0] = arr[n-1]

		} else {
			arr[n-1] = arr[0]
		}
		for i := 1; i < n-1; i++ {
			if (arr[0]+arr[i])%2 == 0 {
				res = append(res, Pair{
					First:  i + 1,
					Second: n,
				})
			} else {
				res = append(res, Pair{
					First:  1,
					Second: i + 1,
				})
			}
		}
		fmt.Fprintln(out, len(res))
		for _, val := range res {
			fmt.Fprintln(out, val.First, val.Second)
		}
	}
}

/*

1 10000 3 0 5
1 10000 3 0 1
1 1 3 0 1
1 1 1 0 1
1 1 1 1 1

*/

package main

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

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

func Println(a ...interface{}) { _, _ = fmt.Fprintln(writer, a...) }

func main() {
	defer func() {
		_ = writer.Flush()
	}()

	T := ReadInt()
	for t := 0; t < T; t++ {
		Solve()
	}
}

func Solve() {
	n := ReadInt()
	a := make([]int, n)
	p := make([]int, n)
	lastOddIndex, lastEvenIndex := -1, -1
	for i := 0; i < n; i++ {
		a[i] = ReadInt()
		if a[i]&1 == 0 {
			lastEvenIndex = i
		} else {
			lastOddIndex = i
		}
		p[i] = -1
	}
	ans := make([][2]int, 0)
	for i := 0; i < n; i++ {
		if a[i]&1 == 0 {
			if i != lastEvenIndex {
				a[i] = a[lastEvenIndex]
				p[i] = lastEvenIndex
			}
		} else {
			if i != lastOddIndex {
				a[i] = a[lastOddIndex]
				p[i] = lastOddIndex
			}
		}
	}
	for i := 0; i < n-1; i++ {
		if a[i] > a[i+1] {
			a[i+1] = a[i]
			p[i+1] = i
		}
	}
	for i := 0; i < n; i++ {
		if p[i] == -1 {
			continue
		}
		if i > p[i] {
			ans = append(ans, [2]int{p[i] + 1, i + 1})
		} else {
			ans = append(ans, [2]int{i + 1, p[i] + 1})
		}
	}
	Println(len(ans))
	for i := 0; i < len(ans); i++ {
		Println(ans[i][0], " ", ans[i][1])
	}
}

func ReadInt() (value int) {
	_, _ = fmt.Fscan(reader, &value)
	return
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int, a []int) string {
	if n == 1 {
		return "0"
	}
	var p int
	if (a[0]+a[n-1])%2 == 1 {
		p = a[0]
	} else {
		p = a[n-1]
	}
	var b []string
	b = append(b, fmt.Sprintf("1 %d", n))
	for i := 1; i < n-1; i++ {
		if (a[i]+p)%2 == 1 {
			b = append(b, fmt.Sprintf("1 %d", i+1))
		} else {
			b = append(b, fmt.Sprintf("%d %d", i+1, n))
		}
	}
	return fmt.Sprint(len(b)) + "\n" + strings.Join(b, "\n")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

const N = 3e5
const inf = 1e9

var iost *Iost

type Iost struct {
	Scanner *bufio.Scanner
	Writer  *bufio.Writer
}

func NewIost(fp io.Reader, wfp io.Writer) *Iost {
	const BufSize = 2000005
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, BufSize), BufSize)
	return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
	if !i.Scanner.Scan() {
		panic("scan failed")
	}
	return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int                 { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int                   { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64             { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64               { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64           { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64           { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{})            { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{})          { fmt.Fprintln(i.Writer, x...) }

func BAAL() {
	in := os.Stdin
	out := os.Stdout
	iost = NewIost(in, out)
}

type pair struct {
	first  int
	second int
}

func main() {
	BAAL()
	defer func() {
		iost.Writer.Flush()
	}()
	t := iost.GetNextInt()
	for t > 0 {
		t--
		n := iost.GetNextInt()
		var a []int
		for i := 0; i < n; i++ {
			a = append(a, iost.GetNextInt())
		}
		var o []pair
		if n == 1 {
			iost.Println(0)
			continue
		}
		if a[0] != a[n-1] {
			o = append(o, pair{
				first:  1,
				second: n})
			if (a[0]+a[n-1])%2 == 0 {
				a[0] = a[n-1]
			} else {
				a[n-1] = a[0]
			}
		}
		for i := 1; i < n-1; i++ {
			if (a[0]+a[i])%2 != 0 {
				o = append(o, pair{
					first:  1,
					second: i + 1,
				})
			} else {
				o = append(o, pair{
					first:  i + 1,
					second: n,
				})
			}
		}
		iost.Println(len(o))
		for _, x := range o {
			iost.Println(x.first, " ", x.second)
		}
	}
}

package main

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

var a [100005]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	fmt.Fscan(in, &T)
	for cs := 0; cs < T; cs++ {
		var n int
		fmt.Fscan(in, &n)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		if n == 1 {
			fmt.Fprintln(out, 0)
			continue
		}
		fmt.Fprintln(out, n-1)
		fmt.Fprintln(out, 1, n)
		if (a[1]+a[n])%2 == 0 {
			a[1] = a[n]
		}
		for i := 2; i < n; i++ {
			if (a[1]+a[i])%2 == 1 {
				fmt.Fprintln(out, 1, i)
			} else {
				fmt.Fprintln(out, i, n)
			}
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; t > 0; t-- {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		digits := make([]int, n, n)
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		for i := 0; i < n; i++ {
			digits[i], _ = strconv.Atoi(arr[i])
		}
		fmt.Println(n - 1)
		if n > 1 {
			fmt.Println(1, n)
		}
		var x int
		if (digits[0]+digits[n-1])%2 == 0 {
			x = digits[n-1]
		} else {
			x = digits[0]
		}
		for i := 1; i < n-1; i++ {
			if (x+digits[i])%2 == 0 {
				fmt.Println(i+1, n)
			} else {
				fmt.Println(1, i+1)
			}
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; t > 0; t-- {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		digits := make([]int, n, n)
		txt, _ = reader.ReadString('\n')
		if n == 1 {
			fmt.Println(0)
			continue
		}
		arr := strings.Split(strings.TrimSpace(txt), " ")
		for i := 0; i < n; i++ {
			digits[i], _ = strconv.Atoi(arr[i])
		}
		fmt.Println(n - 1)
		if n%2 == 0 {
			digits[0] = digits[n-1]
		} else {
			digits[n-1] = digits[0]
		}
		fmt.Println(1, n)
		for i := 1; i < n-1; i++ {
			if (i+n-1)%2 == 0 {
				digits[i] = digits[n-1]
				fmt.Println(i+1, n)
			} else {
				digits[i] = digits[0]
				fmt.Println(1, i+1)
			}
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; t > 0; t-- {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		if n == 1 {
			fmt.Println(0)
			continue
		}
		digits := make([]int, n, n)
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		for i := 0; i < n; i++ {
			digits[i], _ = strconv.Atoi(arr[i])
		}
		fmt.Println(n - 1)
		if n%2 == 0 {
			digits[0] = digits[n-1]
			fmt.Println(1, n)
		} else {
			digits[n-1] = digits[0]
			fmt.Println(n, 1)
		}
		for i := 1; i < n-1; i++ {
			if (i+n-1)%2 == 0 {
				digits[i] = digits[n-1]
				fmt.Println(i+1, n)
			} else {
				digits[i] = digits[0]
				fmt.Println(i+1, 1)
			}
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for i := 0; i < t; i++ {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		digits := make([]int, n, n)
		for j := 0; j < n; j++ {
			digits[j], _ = strconv.Atoi(arr[j])
		}
		cnt := 1
		permutations := make([][2]int, 0)
		for cnt > 0 {
			cnt = 0
			for j := n - 1; j >= 1; j-- {
				if digits[j-1] > digits[j] {
					if (digits[j-1]+digits[j])%2 == 1 {
						digits[j] = digits[j-1]
					} else {
						digits[j-1] = digits[j]
					}
					permutations = append(permutations, [2]int{j - 1, j})
					cnt++
				}
			}
		}
		if len(permutations) > n {
			cnt = 1
			permutations = make([][2]int, 0)
			for cnt > 0 {
				cnt = 0
				for j := 1; j < n; j++ {
					if digits[j-1] > digits[j] {
						if (digits[j-1]+digits[j])%2 == 1 {
							digits[j] = digits[j-1]
						} else {
							digits[j-1] = digits[j]
						}
						permutations = append(permutations, [2]int{j - 1, j})
						cnt++
					}
				}
			}
		}
		fmt.Println(len(permutations))
		for _, p := range permutations {
			fmt.Printf("%d %d\n", p[0], p[1])
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for i := 0; i < t; i++ {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		digits := make([]int, n, n)
		for j := 0; j < n; j++ {
			digits[j], _ = strconv.Atoi(arr[j])
		}
		cnt := 1
		permutations := make([][2]int, 0)
		for cnt > 0 {
			cnt = 0
			for j := n - 1; j >= 1; j-- {
				if digits[j-1] > digits[j] {
					if (digits[j-1]+digits[j])%2 == 1 {
						digits[j] = digits[j-1]
					} else {
						digits[j-1] = digits[j]
					}
					permutations = append(permutations, [2]int{j - 1, j})
					cnt++
				}
			}
		}
		fmt.Println(len(permutations))
		for _, p := range permutations {
			fmt.Printf("%d %d\n", p[0], p[1])
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for i := 0; i < t; i++ {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		digits := make([]int, n, n)
		for j := 0; j < n; j++ {
			digits[j], _ = strconv.Atoi(arr[j])
		}
		cnt := 1
		permutations := make([][2]int, 0)
		for cnt > 0 {
			cnt = 0
			for j := 1; j < n; j++ {
				if digits[j-1] > digits[j] {
					if (digits[j-1]+digits[j])%2 == 1 {
						digits[j] = digits[j-1]
					} else {
						digits[j-1] = digits[j]
					}
					permutations = append(permutations, [2]int{j - 1, j})
					cnt++
				}
			}
		}
		fmt.Println(len(permutations))
		for _, p := range permutations {
			fmt.Printf("%d %d\n", p[0], p[1])
		}
	}
}

package main

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

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

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

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

		aa[n-1] = aa[0]
		bb := []int{n - 1}
		for i := 1; i < n-1; i++ {
			if aa[i]%2 != aa[0]%2 {
				bb = append(bb, i)
			} else {
				bb = append(bb, i*-1)
			}
		}

		fmt.Fprintln(out, len(bb))
		for _, v := range bb {
			if v > 0 {
				fmt.Fprintln(out, 1, v+1)
			} else {
				fmt.Fprintln(out, v*-1+1, n)
			}
		}
	}
}

package main

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

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

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

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

		cc := []int{}
		for i := 1; i < n; i++ {
			if aa[i]%2 != aa[0]%2 {
				cc = append(cc, i)
			}
		}

		for _, idx := range cc {
			aa[idx] = aa[0]
		}

		minIdx := n - 1
		for i := n - 2; i >= 0; i-- {
			if aa[i] > aa[i+1] {
				break
			}
			minIdx = i
		}

		bb := []int{}
		for i := minIdx - 1; i >= 0; i-- {
			if aa[i] > aa[minIdx] {
				bb = append(bb, i)
			}
		}
		fmt.Fprintln(out, len(cc)+len(bb))
		for _, v := range cc {
			fmt.Fprintln(out, 1, v+1)
		}

		for _, v := range bb {
			fmt.Fprintln(out, v+1, minIdx+1)
		}
	}
}

package main

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

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

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

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

		cc := []int{}
		for i := 1; i < n; i++ {
			if aa[i]%2 != aa[0]%2 {
				cc = append(cc, i)
			}
		}

		for _, idx := range cc {
			aa[idx] = aa[0]
		}

		var minIdx int
		for i := n - 2; i >= 0; i-- {
			if aa[i] > aa[i+1] {
				break
			}
			minIdx = i
		}

		bb := []int{}
		for i := minIdx - 1; i >= 0; i-- {
			if aa[i] > aa[minIdx] {
				bb = append(bb, i)
				aa[i] = aa[minIdx]
			}
		}
		fmt.Fprintln(out, len(cc)+len(bb))
		for _, v := range cc {
			fmt.Fprintln(out, 1, v+1)
		}

		for _, v := range bb {
			fmt.Fprintln(out, v+1, minIdx+1)
		}
	}
}

package main

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

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

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

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

		var c int
		for i := 1; i < n; i++ {
			if aa[i]%2 != aa[0]%2 {
				c++
				aa[i] = aa[0]
			}
		}

		var minIdx int
		for i := n - 2; i >= 0; i-- {
			if aa[i] > aa[i+1] {
				break
			}
			minIdx = i
		}

		bb := []int{}
		for i := minIdx - 1; i >= 0; i-- {
			if aa[i] > aa[minIdx] {
				bb = append(bb, i)
			}
		}

		fmt.Fprintln(out, c+len(bb))
		for i := 1; i < n; i++ {
			if aa[i] == aa[0] {
				fmt.Fprintln(out, 1, i+1)
			}
		}

		for _, v := range bb {
			fmt.Fprintln(out, v+1, minIdx+1)
		}
	}
}

package main
import (
	"bufio"
	. "fmt"
	"io"
	"os"
	//. "strconv" Itoa
)
func max(i int, j int) int {
	if (i > j) {
		return i;
	} else {
		return j;
	}
}
func min(i int, j int) int {
	if (i < j) {
		return i;
	} else {
		return j;
	}
}
func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T int
	Fscan(in, &T)

	for ;T>0;T--{
		var n int
		Fscan(in, &n)

		a := make([]int, n+1)
		for i:=1;i<=n;i++{
			Fscan(in, &a[i])
		}
		if n==1{
			Fprintln(out,"0")
			continue
		}
		ans:=[][2]int{}
		ans=append(ans,[2]int{1,n})
		if (a[1]+a[n])%2==0{
			a[n]=a[1]
		}else{
			a[1]=a[n]
		}
		for i:=2;i<n;i++{
			if(a[1]+a[i])%2==0{
				ans=append(ans,[2]int{i,n})
				a[i]=a[n]
			}else{
				ans=append(ans,[2]int{1,i})
				a[i]=a[1]
			}
		}
		Fprintln(out,len(ans))
		for _,a:=range ans{
			for _,i:=range a{
				Fprint(out,i," ")
			}
			Fprintln(out)
		}
	}
}
func main() { solve(os.Stdin, os.Stdout) }

package main

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

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

type Pair struct {
	First  int
	Second int
}

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		if n == 1 {
			fmt.Fprintln(out, "0")
			continue
		}
		res := make([]Pair, 0)
		res = append(res, Pair{
			First:  1,
			Second: n,
		})
		if (arr[0]+arr[n-1])%2 == 0 {
			arr[n-1] = arr[0]
		} else {
			arr[0] = arr[n-1]
		}
		for i := 1; i < n-1; i++ {
			if (arr[0]+arr[i])%2 == 0 {

				res = append(res, Pair{
					First:  i + 1,
					Second: n,
				})
			} else {
				res = append(res, Pair{
					First:  1,
					Second: i + 1,
				})
			}
		}
		fmt.Fprintln(out, len(res))
		for _, val := range res {
			fmt.Fprintln(out, val.First, val.Second)
		}
	}
}

/*

1 10000 3 0 5
1 10000 3 0 1
1 1 3 0 1
1 1 1 0 1
1 1 1 1 1

*/

package main

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

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

type Pair struct {
	First  int
	Second int
}

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		if n == 1 {
			fmt.Fprintln(out, "0")
			continue
		}
		res := make([]Pair, 0)
		res = append(res, Pair{
			First:  1,
			Second: n,
		})
		if (arr[0]+arr[n-1])%2 == 0 {
			arr[n-1] = arr[0]
		} else {
			arr[0] = arr[n-1]
		}
		for i := 1; i < n-1; i++ {
			if (arr[0]+arr[i])%2 == 0 {
				res = append(res, Pair{
					First:  1,
					Second: i + 1,
				})
			} else {
				res = append(res, Pair{
					First:  i + 1,
					Second: n,
				})
			}
		}
		fmt.Fprintln(out, len(res))
		for _, val := range res {
			fmt.Fprintln(out, val.First, val.Second)
		}
	}
}

package main

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

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

func Println(a ...interface{}) { _, _ = fmt.Fprintln(writer, a...) }

func main() {
	defer func() {
		_ = writer.Flush()
	}()

	T := ReadInt()
	for t := 0; t < T; t++ {
		Solve()
	}
}

func Solve() {
	n := ReadInt()
	a := make([]int, n)
	lastOddIndex, lastEvenIndex := -1, -1
	for i := 0; i < n; i++ {
		a[i] = ReadInt()
		if a[i]&1 == 0 {
			lastEvenIndex = i
		} else {
			lastOddIndex = i
		}
	}
	ans := make([][2]int, 0)
	for i := 0; i < n; i++ {
		if a[i]&1 == 0 {
			if i != lastEvenIndex {
				ans = append(ans, [2]int{i + 1, lastEvenIndex + 1})
				a[i] = a[lastEvenIndex]
			}
		} else {
			if i != lastOddIndex {
				ans = append(ans, [2]int{i + 1, lastOddIndex + 1})
				a[i] = a[lastOddIndex]
			}
		}
	}
	for i := 0; i < n-1; i++ {
		if a[i] > a[i+1] {
			ans = append(ans, [2]int{i + 1, i + 2})
			a[i+1] = a[i]
		}
	}
	Println(len(ans))
	for i := 0; i < len(ans); i++ {
		Println(ans[i][0], " ", ans[i][1])
	}
}

func ReadInt() (value int) {
	_, _ = fmt.Fscan(reader, &value)
	return
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int, a []int) string {
	if n == 1 {
		return "0"
	}
	if (a[0]+a[n-1])%2 == 1 {
		a[n-1] = a[0]
	} else {
		a[0] = a[n-1]
	}
	var b []string
	b = append(b, fmt.Sprintf("1 %d", n))
	for i := 1; i < n-1; i++ {
		if a[i]+a[0]%2 == 1 {
			b = append(b, fmt.Sprintf("1 %d", i+1))
		} else {
			b = append(b, fmt.Sprintf("%d %d", i+1, n))
		}
	}
	return fmt.Sprint(len(b)) + "\n" + strings.Join(b, "\n")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}


package main

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

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

func out(f string, a ...interface{}) { _, _ = fmt.Fprintf(writer, f, a...) }
func in(f string, a ...interface{})  { _, _ = fmt.Fscanf(reader, f, a...) }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer func() {
		_ = writer.Flush()
	}()

	var t int
	in("%d\n", &t)
	for ; t > 0; t-- {
		solve()
		clear()
	}
}

var G [][]bool

func init() {
	G = make([][]bool, 20)
	for i, _ := range G {
		G[i] = make([]bool, 20)
	}
}

func clear() {
	for i, _ := range G {
		G[i] = make([]bool, 20)
	}
}

func solve() {
	var n int
	in("%d\n", &n)
	var a, b string
	in("%s\n%s\n", &a, &b)
	for i := 0; i < len(a); i++ {
		if a[i] > b[i] {
			out("-1\n")
			return
		}
		if a[i] != b[i] {
			G[a[i]-'a'][b[i]-'a'] = true
		}
	}
	//pp()
	cel()
}

func pp() {
	for i:=0; i<20; i++ {
		for j:=0; j<20; j++ {
			if G[i][j] {
				fmt.Print("*")
			} else {
				fmt.Print(" ")
			}
		}
		fmt.Println()
	}
}

func cel() {
	var sum int
	for i:=0; i < 20; i++ {
		//pp()
		for j:=i+1; j < 20; j++ {
			if G[i][j] {
				sum++
				for k := j+1; k < 20; k++ {
					G[j][k] = G[j][k] || G[i][k]
				}
				break
			}
		}
	}
	out("%d\n", sum)
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

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

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

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

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

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

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

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := sstring()
		b := sstring()
		printf("%d\n", sol(n, a, b))
	}
}

func sol(n int64, a, b string) int64 {
	m := [20][20]bool{}
	for i := range a {
		if a[i] == b[i] {
			continue
		}
		if b[i] < a[i] {
			return -1
		}
		offA := a[i] - 'a'
		offB := b[i] - 'a'
		m[offA][offB] = true
	}
	// printf("%v\n", m)
	ret := int64(0)
	for i := range m {
		for j := range m[i] {
			if m[i][j] {
				ret++
				m[i][j] = false
				m[j] = merge(m[i], m[j])
				break
			}
		}
	}
	return ret
}

func merge(a, b [20]bool) [20]bool {
	ret := [20]bool{}
	for i := range ret {
		ret[i] = a[i] || b[i]
	}
	return ret
}

package main

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

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

func out(f string, a ...interface{}) { _, _ = fmt.Fprintf(writer, f, a...) }
func in(f string, a ...interface{})  { _, _ = fmt.Fscanf(reader, f, a...) }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer func() {
		_ = writer.Flush()
	}()

	var t int
	in("%d\n", &t)
	for ; t > 0; t-- {
		solve()
		clear()
	}
}

var G [26][26]bool

func clear() {
	for i:=0; i<20; i++ {
		for j:=0; j<20; j++ {
			G[i][j] = false
		}
	}
}

func solve() {
	var n int
	in("%d\n", &n)
	var a, b string
	in("%s\n%s\n", &a, &b)
	for i := 0; i < len(a); i++ {
		if a[i] > b[i] {
			out("-1\n")
			return
		}
		if a[i] != b[i] {
			G[a[i]-'a'][b[i]-'a'] = true
		}
	}
	var sum int
	for i:=0; i < 20; i++ {
		for j:=i+1; j < 20; j++ {
			if G[i][j] {
				sum++
				for k := j+1; k < 20; k++ {
					G[j][k] = G[j][k] || G[i][k]
				}
				break
			}
		}
	}
	out("%d\n", sum)
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func PollLast(q *[]int) int {
	res := (*q)[len(*q)-1]
	(*q) = (*q)[:len(*q)-1]
	return res
}
func PollFirst(q *[]int) int {
	res := (*q)[0]
	(*q) = (*q)[1:]
	return res
}

type DSU struct {
	par, num []int
}

func (dsu *DSU) root(a int) int {
	if dsu.par[a] < 0 {
		return a
	}
	res := dsu.root(dsu.par[a])
	dsu.par[a] = res
	return res
}

func (dsu *DSU) merge(a, b int) {
	a, b = dsu.root(a), dsu.root(b)
	if a == b {
		return
	}
	if dsu.par[a] == dsu.par[b] {
		dsu.par[a]--
	}
	if dsu.par[a] < dsu.par[b] {
		dsu.par[b] = a
		dsu.num[a] += dsu.num[b]
	} else {
		dsu.par[a] = b
		dsu.num[b] += dsu.num[a]
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
outer:
	for cases := nextInt(); cases > 0; cases-- {
		nextInt()
		a, b := []byte(next()), []byte(next())
		dsu := &DSU{make([]int, 20), make([]int, 20)}
		Fill(dsu.par, -1)
		Fill(dsu.num, 1)
		for i := range a {
			if a[i] > b[i] {
				printf("-1\n")
				continue outer
			}
			dsu.merge(int(a[i]-'a'), int(b[i]-'a'))
		}
		res := 0
		for i, v := range dsu.num {
			if dsu.par[i] < 0 {
				res += v - 1
			}
		}
		printf("%d\n", res)
	}
}

package main

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

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

func out(f string, a ...interface{}) { _, _ = fmt.Fprintf(writer, f, a...) }
func in(f string, a ...interface{})  { _, _ = fmt.Fscanf(reader, f, a...) }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer func() {
		_ = writer.Flush()
	}()

	var t int
	in("%d\n", &t)
	for ; t > 0; t-- {
		solve()
		clear()
	}
}

var G [][]bool

func init() {
	G = make([][]bool, 20)
	for i, _ := range G {
		G[i] = make([]bool, 20)
	}
}

func clear() {
	for i, _ := range G {
		G[i] = make([]bool, 20)
	}
}

func solve() {
	var n int
	in("%d\n", &n)
	var a, b string
	in("%s\n%s\n", &a, &b)
	for i := 0; i < len(a); i++ {
		if a[i] > b[i] {
			out("-1\n")
			return
		}
		if a[i] != b[i] {
			G[a[i]-'a'][b[i]-'a'] = true
		}
	}
	cel()
}

func cel() {
	var sum int
	for i:=0; i < 20; i++ {
		for j:=i+1; j < 20; j++ {
			if G[i][j] {
				sum++
				for k := j+1; k < 20; k++ {
					G[j][k] = G[i][k]
				}
				break
			}
		}
	}
	out("%d\n", sum)
}

package main

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

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

func out(f string, a ...interface{}) { _, _ = fmt.Fprintf(writer, f, a...) }
func in(f string, a ...interface{})  { _, _ = fmt.Fscanf(reader, f, a...) }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer func() {
		_ = writer.Flush()
	}()

	var t int
	in("%d\n", &t)
	for ; t > 0; t-- {
		solve()
		clear()
	}
}

var G [][]bool

func init() {
	G = make([][]bool, 20)
	for i, _ := range G {
		G[i] = make([]bool, 20)
	}
}

func clear() {
	for i, _ := range G {
		G[i] = make([]bool, 20)
	}
}

func solve() {
	var n int
	in("%d\n", &n)
	var a, b string
	in("%s\n%s\n", &a, &b)
	for i := 0; i < len(a); i++ {
		if a[i] > b[i] {
			out("-1\n")
			return
		}
		if a[i] != b[i] {
			G[a[i]-'a'][b[i]-'a'] = true
		}
	}
	pp()
	cel()
}

func pp() {
	for i:=0; i<20; i++ {
		for j:=0; j<20; j++ {
			if G[i][j] {
				fmt.Print("*")
			} else {
				fmt.Print(" ")
			}
		}
		fmt.Println()
	}
}

func cel() {
	var sum int
	for i:=0; i < 20; i++ {
		pp()
		for j:=i+1; j < 20; j++ {
			if G[i][j] {
				sum++
				for k := j+1; k < 20; k++ {
					G[j][k] = G[j][k] || G[i][k]
				}
				break
			}
		}
	}
	out("%d\n", sum)
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func PollLast(q *[]int) int {
	res := (*q)[len(*q)-1]
	(*q) = (*q)[:len(*q)-1]
	return res
}
func PollFirst(q *[]int) int {
	res := (*q)[0]
	(*q) = (*q)[1:]
	return res
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
outer:
	for cases := nextInt(); cases > 0; cases-- {
		nextInt()
		a, b := []byte(next()), []byte(next())
		has := [256][256]bool{}
		for i := range a {
			if a[i] > b[i] {
				printf("-1\n")
				continue outer
			}
			has[a[i]][b[i]] = true
		}
		res := 0
		for i, u := range has {
			for j, v := range u {
				if i == j {
					continue
				}
				for k := i + 1; k < j; k++ {
					if has[i][k] && has[k][j] {
						v = false
					}
				}
				if v {
					res++
				}
			}
		}
		printf("%d\n", res)
	}
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func PollLast(q *[]int) int {
	res := (*q)[len(*q)-1]
	(*q) = (*q)[:len(*q)-1]
	return res
}
func PollFirst(q *[]int) int {
	res := (*q)[0]
	(*q) = (*q)[1:]
	return res
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
outer:
	for cases := nextInt(); cases > 0; cases-- {
		nextInt()
		a, b := []byte(next()), []byte(next())
		has := [256][256]bool{}
		for i := range a {
			if a[i] > b[i] {
				printf("-1\n")
				continue outer
			}
			has[a[i]][b[i]] = has[a[i]][b[i]] || a[i] < b[i]
		}
		res := 0
		for i, u := range has {
			for j, v := range u {
				for k := 0; k < i; k++ {
					if has[k][i] && has[k][j] {
						v = false
					}
				}
				if v {
					res++
				}
			}
		}
		printf("%d\n", res)
	}
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func PollLast(q *[]int) int {
	res := (*q)[len(*q)-1]
	(*q) = (*q)[:len(*q)-1]
	return res
}
func PollFirst(q *[]int) int {
	res := (*q)[0]
	(*q) = (*q)[1:]
	return res
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
outer:
	for cases := nextInt(); cases > 0; cases-- {
		nextInt()
		a, b := []byte(next()), []byte(next())
		has := [26][26]bool{}
		for i := range a {
			if a[i] > b[i] {
				printf("-1\n")
				continue outer
			}
			if a[i] < b[i] {
				has[a[i]-'a'][b[i]-'a'] = true
			}
		}
		res := 0
		for i := 25; i >= 0; i-- {
			for j := 25; j > i; j-- {
				x := has[i][j]
				for k := i + 1; k < j; k++ {
					if has[i][k] && has[k][j] {
						x = false
					}
				}
				if x {
					res++
				}
				for k := j + 1; k < 26; k++ {
					if has[i][j] && has[j][k] {
						has[i][k] = true
					}
				}
			}
		}
		printf("%d\n", res)
	}
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func PollLast(q *[]int) int {
	res := (*q)[len(*q)-1]
	(*q) = (*q)[:len(*q)-1]
	return res
}
func PollFirst(q *[]int) int {
	res := (*q)[0]
	(*q) = (*q)[1:]
	return res
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
outer:
	for cases := nextInt(); cases > 0; cases-- {
		nextInt()
		a, b := []byte(next()), []byte(next())
		has := [20][20]bool{}
		dist := [20][20]int{}
		for i := range a {
			if a[i] > b[i] {
				printf("-1\n")
				continue outer
			}
			if a[i] < b[i] {
				dist[a[i]-'a'][b[i]-'a'] = -1
				has[a[i]-'a'][b[i]-'a'] = true
			}
		}
		res := 0
		for k := range dist {
			for i := range dist {
				for j := range dist {
					if i < k && k < j && dist[i][k]+dist[k][j] < dist[i][j] {
						dist[i][j] = dist[i][k] + dist[k][j]
					}
				}
			}
		}
		for i, u := range dist {
			for j, v := range u {
				if v == -1 && has[i][j] {
					res++
				}
			}
		}
		printf("%d\n", res)
	}
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func PollLast(q *[]int) int {
	res := (*q)[len(*q)-1]
	(*q) = (*q)[:len(*q)-1]
	return res
}
func PollFirst(q *[]int) int {
	res := (*q)[0]
	(*q) = (*q)[1:]
	return res
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
outer:
	for cases := nextInt(); cases > 0; cases-- {
		nextInt()
		a, b := []byte(next()), []byte(next())
		has := [256][256]bool{}
		for i := range a {
			if a[i] > b[i] {
				printf("-1\n")
				continue outer
			}
			has[a[i]][b[i]] = a[i] < b[i]
		}
		res := 0
		for i, u := range has {
			for j, v := range u {
				for k := 0; k < i; k++ {
					if has[k][i] && has[k][j] {
						v = false
					}
				}
				if v {
					res++
				}
			}
		}
		printf("%d\n", res)
	}
}


package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

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

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

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

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

func MAXX(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 GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

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

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

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

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

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, m int
		fmt.Fscan(input, &n, &m)

		ARRAY := make([]int, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(input, &ARRAY[i])

		}
		var Ans int

		for i := 0; i < m; i++ {
			for j := 0; j < i; j++ {
				if ARRAY[i] > ARRAY[j] {
					Ans++
				}
			}
		}
		fmt.Println(Ans)

	}
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

func solve(a []int, rows, cols int) int64 {
	order := make([]int, len(a))
	for i := 0; i < len(a); i++ {
		order[i] = i
	}
	sort.Slice(order, func(i, j int) bool {
		if a[order[i]] == a[order[j]] {
			return order[i] > order[j]
		}
		return a[order[i]] < a[order[j]]
	})

	occupied := make([]bool, len(a))

	res := int64(0)
	for i := range a {
		var l int
		for ; order[l] != i; l++ {
			if occupied[l] {
				res++
			}
		}
		occupied[l] = true
	}
	return res
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i++ {
		n, m := stdin.Int(), stdin.Int()
		a := stdin.Ints(n * m)
		fmt.Println(solve(a, n, m))
	}
}

package main

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

type IndexTree struct {
	n            int
	arr          []int
	f            func(i, j int) int
	defaultValue int
}

type Aggregation interface {
	DefaultValue() int
	Aggregate(left, right int) int
}

type MinIndex struct{}

func (m MinIndex) DefaultValue() int { return math.MaxInt32 }
func (m MinIndex) Aggregate(left, right int) int {
	if left < right {
		return left
	} else {
		return right
	}
}

type MaxIndex struct{}

func (m MaxIndex) DefaultValue() int { return math.MinInt32 }
func (m MaxIndex) Aggregate(left, right int) int {
	if left > right {
		return left
	} else {
		return right
	}
}

type SumIndex struct{}

func (m SumIndex) DefaultValue() int             { return 0 }
func (m SumIndex) Aggregate(left, right int) int { return left + right }

func NewIndexTree(n int, a Aggregation) *IndexTree {
	arr := make([]int, n*4)
	for i := 0; i < len(arr); i++ {
		arr[i] = a.DefaultValue()
	}

	return &IndexTree{n, arr, a.Aggregate, a.DefaultValue()}
}

func (i *IndexTree) Add(k, v int) {
	i.traverse(1, 0, i.n, func(node, b, e int) bool {
		if b <= k && e >= k {
			i.arr[node] = i.f(i.arr[node], v)
			return true
		}
		return false
	})
}

func (i *IndexTree) Get(qb, qe int) int {
	res := i.defaultValue
	i.traverse(1, 0, i.n, func(node, b, e int) bool {
		if b >= qb && e <= qe {
			res = i.f(res, i.arr[node])
			return false
		}
		return !(qe < b || qb > e)
	})
	return res
}

func (i *IndexTree) traverse(node int, b, e int, do func(node, b, e int) bool) {
	if !do(node, b, e) {
		return
	}
	m := (b + e) / 2
	if b != e {
		i.traverse(node*2, b, m, do)
		i.traverse(node*2+1, m+1, e, do)
	}
}

type pair struct {
	a, i int
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int

	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscanf(r, "%d %d\n", &n, &m)
		arr := make([]pair, n*m)
		for i := 0; i < len(arr); i++ {
			arr[i].i = i
			fmt.Fscanf(r, "%d", &arr[i].a)
		}
		fmt.Fscanf(r, "\n")

		sort.Slice(arr, func(i, j int) bool {
			if arr[i].a != arr[j].a {
				return arr[i].a < arr[j].a
			}
			return arr[i].i > arr[j].i
		})

		idx := NewIndexTree(n*m, SumIndex{})

		res := 0
		for _, v := range arr {
			res += idx.Get(0, v.i)
			idx.Add(v.i, 1)
		}

		fmt.Println(res)
	}
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

func solve(a []int, rows, cols int) int64 {
	order := make([]int, len(a))
	for i := 0; i < len(a); i++ {
		order[i] = i
	}
	sort.Slice(order, func(i, j int) bool {
		if a[order[i]] == a[order[j]] {
			return i > j
		}
		return a[order[i]] < a[order[j]]
	})

	occupied := make([]bool, len(a))

	res := int64(0)
	for i := range a{
		var l int
		for ;order[l] != i; l++ {
			if occupied[l] {
				res ++
			}
		}
		occupied[l] = true
	}
	return res
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i++ {
		n, m := stdin.Int(), stdin.Int()
		a := stdin.Ints(n * m)
		fmt.Println(solve(a, n, m))
	}
}


package main

import "fmt"

func main() {
    var n,d int
    fmt.Scan(&n,&d)
    x := make([]int,n)
    for i := 0;i<n;i++{
        fmt.Scan(&x[i])
    }
    t := 2
    for i:=0;i<n-1;i++{
        if x[i+1] - x[i] >2*d {
            t +=2
        } else if x[i+1] - x[i] == 2*d {
            t +=1
        }
    }
	fmt.Println(t)
}

package main

import "fmt"

const (
    M int = 105
)

var (
    n int
    d int
    a [M]int
)

func gao() int {
    rt := 0
    mp := make(map[int]int)
    fmt.Scan(&n, &d)
    if n == 1 {
        return 2
    }
    for i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    for i := 1; i < n - 1; i++ {
        if a[i]-d >= a[i-1]+d && mp[a[i]-d] == 0{
            rt += 1
            mp[a[i]-d] = 1
        }
        if a[i]+d <= a[i+1]-d && mp[a[i]+d] == 0{
            rt += 1
            mp[a[i]+d] = 1
        }
    }
    if a[0]+d <= a[1]-d && mp[a[0]+d] == 0 {
        rt += 1
        mp[a[0]+d] = 1
    }
    if a[n-1]-d >= a[n-2]+d && mp[a[n-1]-d] == 0 {
        rt += 1
        mp[a[n-1]-d] = 1
    }
    return rt + 2
}

func main() {
    ans := gao()
    fmt.Println(ans)
}

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
)

var a [105]int

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

	for i := 1; i <= n; i++ {
		fmt.Scan(&a[i])
	}

	ans := 2
	for i := 1; i <= n-1; i++ {
		if a[i]+d < a[i+1]-d {
			ans += 2
		}
		if a[i]+d == a[i+1]-d {
			ans++
		}
	}
	fmt.Println(ans)
}

package main

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

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

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

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

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	n := readi()
	d := readi()
	x := make([]int, n)
	for i := range x {
		x[i] = readi()
	}
	var ans = 2
	for i := 0; i+1 < n; i++ {
		if x[i+1]-x[i] == 2*d {
			ans++
		} else if 2*d < x[i+1]-x[i] {
			ans += 2
		}
	}
	println(ans)
}

package main

import ("fmt")

func main(){
	var n, d, ans int64
	scan(&n, &d)
	var a []int64 = make([]int64, n)
	for i := int64(0); i < n; i++{
		scan(&a[i])
	}
	for i := int64(1); i < n; i++{
		if a[i] - a[i - 1] == d * 2{ans++}
		if a[i] - a[i - 1] > d * 2{ans += 2}
	}
	sout(ans + 2)
}

func scan(in ... interface{}){
	for _,k := range in{
		fmt.Scan(k)
	}
}

func sout(obj ... interface{}){
	for _, k := range obj{
		fmt.Print(k, " ")
	}
	fmt.Println()
}

package main

import (
	"fmt"
)

func main() {
	var n, d int
	fmt.Scanf("%d %d\n", &n, &d)
	tab := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &tab[i])
	}
	var results []int
	results = append(results, tab[0]-d)
	for i := 0; i < n-1; i++ {
		if tab[i]+d <= tab[i+1]-d {
			results = append(results, tab[i]+d)
		}
		if tab[i]+d < tab[i+1]-d {
			results = append(results, tab[i+1]-d)
		}
	}
	results = append(results, tab[n-1]+d)
	fmt.Printf("%d\n", len(results))

}

package main

import "fmt"

const (
    M int = 105
)

var (
    n int
    d int
    a [M]int
)

func gao() int {
    rt := 0
    mp := make(map[int]int)
    fmt.Scan(&n, &d)
    if n == 1 {
        return 2
    }
    for i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    for i := 1; i < n - 1; i++ {
        if a[i]-d >= a[i-1]+d && mp[a[i]-d] == 0{
            rt += 1
            mp[a[i]-d] = 1
        }
        if a[i]+d <= a[i+1]-d && mp[a[i]+d] == 0{
            rt += 1
            mp[a[i]+d] = 1
        }
    }
    if a[0]+d <= a[1]-d && mp[a[0]+d] == 0 {
        rt += 1
    }
    if a[n-1]-d >= a[n-2]+d && mp[a[n-1]-d] == 0 {
        rt += 1
    }
    return rt + 2
}

func main() {
    ans := gao()
    fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var n, d int
	fmt.Scanf("%d %d", &n, &d)
	tab := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &tab[i])
	}
	var results []int
	results = append(results, tab[0]-d)
	for i := 0; i < n-1; i++ {
		if tab[i]+d <= tab[i+1]-d {
			results = append(results, tab[i]+d)
		}
		if tab[i]+d < tab[i+1]-d {
			results = append(results, tab[i+1]-d)
		}
	}
	results = append(results, tab[n-1]+d)
	fmt.Printf("%d\n", len(results))

}


package main

import "fmt"

func main() {
	var n int
	var count int = 0
	fmt.Scan(&n)
	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			count++
		}
	}
	fmt.Println(count)
	a[n] = 1
	for i := 0; i < n; i++ {
		if a[i+1] == 1 {
			fmt.Printf("%v ", a[i])
		}
	}
}

package main

import (
	"fmt"
	"strconv"
	str "strings"
)

func main() {
	var n int
	fmt.Scanln(&n)
	el := make([]int, n)
	for i := range el {
		fmt.Scan(&el[i])
	}
	stairs := 0
	stairMax := make([]string, 0)
	for i := range el {
		if len(el) == 1 {
			stairs++
		} else {
			if el[i] == 1 {
				stairs++
				if i > 0 {
					stairMax = append(stairMax, strconv.Itoa(el[i-1]))
				} else if i == len(el)-1 {
					stairMax = append(stairMax, strconv.Itoa(el[i-1]))
				}
			}
		}
	}
	stairMax = append(stairMax, strconv.Itoa(el[len(el)-1]))
	fmt.Println(stairs)
	fmt.Println(str.Join(stairMax, " "))
}

package main

import "fmt"

func main()  {
	var n, cnt int;
	fmt.Scan(&n)
	var res []int
	for i, x := 0, 0; i < n; i++ {
		fmt.Scan(&x)
		if x == 1 {
			if cnt > 0 {
				res = append(res, cnt)
			}
			cnt = 0
		}
		cnt++
	}
	if cnt > 0 {
		res = append(res, cnt)
	}
	fmt.Println(len(res))
	for _, val := range res {
		fmt.Print(val, " ")
	}
	fmt.Println()
}

package main

import "fmt"

func main() {
	var buf []int
	var n, k, colvo int
	fmt.Scan(&k)
	for i:=0; i<k; i++ {
		fmt.Scan(&n)
		buf = append(buf, n)
	}
	buf = append(buf, 1)
	for i:=0; i<k; i++ {
		if buf[i] == 1 {
			colvo++
		}
	}
	fmt.Println(colvo)
	for i:=0; i<k+1; i++ {
		if buf[i] == 1 &&  i != 0 {
			fmt.Print(buf[i-1], " ")
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

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

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

	var steps []int
	last := -1
	for _, v := range list {
		if v == 1 && last != -1 {
			steps = append(steps, last)
		}

		last = v
	}

	steps = append(steps, last)
	output := fmt.Sprint(steps)
	output = strings.Replace(output, "[", "", -1)
	output = strings.Replace(output, "]", "", -1)

	fmt.Println(len(steps))
	fmt.Println(output)
}

package main
import "fmt"
func main() {
	var n, x int
	l := 0
	r := []int{}
	fmt.Scan(&n)
	for i:=0; i < n; i++ {
		fmt.Scan(&x)
		if x <= l {
			r = append(r, l)
		}
		l = x
	}
	r = append(r, l)
	fmt.Println(len(r))
	for _,x := range r {
		fmt.Printf("%d ", x)
	}
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

//Item eh um elemento da fila de prioridades
type Item struct {
	value    int
	priority int
	index    int
}

//PriorityQueue implmenta a heap interface
type PriorityQueue []*Item

//Len retorna o tamanho da  pq
func (pq PriorityQueue) Len() int { return len(pq) }

//Less verifica se um tem maior priotidade que o outro
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].priority < pq[j].priority }

//Swap troca os elementos de lugar
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

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

//Pop remove um elemento da pq
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

//update modifica o valor e a prioridade de um item na fila
func (pq *PriorityQueue) update(item *Item, value int, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

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

type pair struct {
	first  interface{}
	second interface{}
}

type intPair struct {
	first  int64
	second int64
}

type vertex struct {
	list []intPair
}

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

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

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

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

/*************************************************************************/
func main() {
	var (
		n        int
		num      int
		stairway []int
		cnt      int
		degraus  []int
	)

	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&num)
		stairway = append(stairway, num)
	}

	for i := 1; i < n; i++ {
		if stairway[i] <= stairway[i-1] {
			cnt++
			degraus = append(degraus, stairway[i-1])
		}
	}

	cnt++
	degraus = append(degraus, stairway[n-1])

	fmt.Println(cnt)

	for i := 0; i < len(degraus); i++ {
		if i > 0 {
			fmt.Print(" ")
		}
		fmt.Print(degraus[i])
	}
	fmt.Println()
}

package main

import ("fmt")

func main(){
	var n, cur int
	scan(&n, &cur)
	ct := 0
	q := nQueue()
	for i := 1; i < n; i++{
		var t int
		scan(&t)
		ct++
		if cur + 1 != t{
			q.add(ct)
			ct = 0
		}
		cur = t
	}
	q.add(ct + 1)
	sout(q.size())
	r := q.toArray()
	for _,k := range r{
		sout(k)
	}
}

func scan(in ... interface{}){
	for _,k := range in{
		fmt.Scan(k)
	}
}
func sout(obj ... interface{}){
	for _, k := range obj{
		fmt.Print(k, " ")
	}
	fmt.Println()
}

type Queue struct{
	sz int
	val *int
	current, next, last *Queue
}

func nQueue()*Queue{
	q := Queue{}
	q.last = &q
	q.current = &q
	return &q
}

func (self *Queue)add(v int){
	if self.val != nil{
		nxt := Queue{}
		nxt.val = &v
		self.last.next = &nxt
		self.last = &nxt
	}else{
		self.val = &v
	}
	self.sz++
}

func (self *Queue)poll()int{
	v := *self.current.val
	self.current = self.current.next
	self.sz--
	return v
}

func (self *Queue)size()int{return self.sz}

func (self *Queue)peek()int{return *self.current.val}

func (self *Queue)toArray()[]int{
	ret := make([]int, self.sz)
	cur := self.current
	i := 0
	for cur != nil{
		ret[i] = *cur.val
		cur = cur.next
		i++
	}
	return ret
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var l []int
	p :=0
	m :=0
	for i:=0;i<n;i++{
		var s int
		fmt.Scan(&s)
		if m<s {
			p++
		} else {
			l = append(l,p)
			p=1
		}
		m = s
	}
	l = append(l,p)
	fmt.Println(len(l))
	sl := fmt.Sprint(l)
	fmt.Println(sl[1:len(sl)-1])
}

package main

import "fmt"

func main() {
	var n int
	var ans [1000]int
	fmt.Scanf("%d\n", &n)
	count := 0
	for i := 0; i < n; i++ {
		var x int
		fmt.Scanf("%d", &x)
		if x > ans[count] {
			ans[count]++
		} else {
			count++
			ans[count]++
		}
	}
	fmt.Println(count + 1)
	for i := 0; i <= count	; i++ {
		fmt.Print(ans[i], " ")
	}
	fmt.Println()
}

package main

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

func main() {
	var n, a int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	la := 0
	count := 0
	ans := make([]int, 0)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a)
		if a <= la {
			count++
			ans = append(ans, la)
		}
		la = a
	}
	count++
	ans = append(ans, a)
	fmt.Println(count)
	for _, v := range ans {
		fmt.Printf("%d ", v)
	}
	fmt.Println()
}

// A. Tanya and Stairways
/*
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from 1 to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains 3 steps, and the second contains 4 steps, she will pronounce the numbers 1,2,3,1,2,3,4.

You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.

The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.

Input
The first line contains 𝑛 (1≤𝑛≤1000) — the total number of numbers pronounced by Tanya.

The second line contains integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with 𝑥 steps, she will pronounce the numbers 1,2,…,𝑥 in that order.

The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.

Output
In the first line, output 𝑡— the number of stairways that Tanya climbed. In the second line, output 𝑡 numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.

Examples
Input
7
1 2 3 1 2 3 4

Output
2
3 4

Input
4
1 1 1 1

Output
4
1 1 1 1

Input
5
1 2 3 4 5

Output
1
5

Input
5
1 2 1 2 1

Output
3
2 2 1
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input1))

	input2, _ := reader.ReadString('\n')
	arr := strings.Split(strings.TrimSpace(input2), " ")

	tStarirways, steps := 0, 0
	tSteps := []int{}
	for i := 0; i < n; i++ {
		if arr[i] == "1" {
			tStarirways++

			if steps > 0 {
				tSteps = append(tSteps, steps)
				steps = 0
			}
		}
		steps++
	}

	if steps > 0 {
		tSteps = append(tSteps, steps)
	}
	fmt.Println(tStarirways)

	for _, v := range tSteps {
		fmt.Printf("%v ", v)
	}
}

package main

import "fmt"

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

	a := make([]int, n)
	t := []int{}
	for i := range a {
		fmt.Scan(&a[i])

		if i == 0 || a[i-1] >= a[i] {
			t = append(t, a[i])
		} else {
			t[len(t)-1] = a[i]
		}
	}

	fmt.Println(len(t))
	for i, v := range t {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(v)
	}
	fmt.Println()
}

package main

import "fmt"

func main() {
	var n int
	var ans [1000]int
	fmt.Scanf("%d", &n)
	count := 0
	for i := 0; i < n; i++ {
		var x int
		fmt.Scanf("%d", &x)
		if x != (ans[count] + 1) {
			count++
			ans[count] = ans[count] + 1
		} else {
			ans[count] = ans[count] + 1
		}
	}
	fmt.Println(count + 1)
	for i := 0; i <= count	; i++ {
		fmt.Print(ans[i], " ")
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"strconv"
	str "strings"
)

func main() {
	var n int
	fmt.Scanln(&n)
	el := make([]int, n)
	for i := range el {
		fmt.Scan(&el[i])
	}
	stairs := 0
	stairMax := make([]string, 0)
	for i := range el {
		if i > 0 {
			if el[i] == 1 {
				stairs++

				stairMax = append(stairMax, strconv.Itoa(el[i-1]))
			} else if i == len(el)-1 {
				stairs++

				stairMax = append(stairMax, strconv.Itoa(el[i]))
			}
		}
	}
	fmt.Println(stairs)
	fmt.Println(str.Join(stairMax, " "))
}