You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.The tree is specified by an array of parents $$$p$$$ containing $$$n$$$ numbers: $$$p_i$$$ is a parent of the vertex with the index $$$i$$$. The parent of a vertex $$$u$$$ is a vertex that is the next vertex on the shortest path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the parent of $$$5$$$ is $$$1$$$.The root has no parent, so for it, the value of $$$p_i$$$ is $$$i$$$ (the root is the only vertex for which $$$p_i=i$$$).Find such a set of paths that:  each vertex belongs to exactly one path, each path can contain one or more vertices;  in each path each next vertex — is a son of the current vertex (that is, paths always lead down — from parent to son);  number of paths is minimal. For example, if $$$n=5$$$ and $$$p=[3, 1, 3, 3, 1]$$$, then the tree can be divided into three paths:   $$$3 \rightarrow 1 \rightarrow 5$$$ (path of $$$3$$$ vertices),  $$$4$$$ (path of $$$1$$$ vertices).  $$$2$$$ (path of $$$1$$$ vertices).     Example of splitting a root tree into three paths for $$$n=5$$$, the root of the tree — node $$$3$$$.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case consists of two lines. The first of them contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). It is the number of vertices in the tree. The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the $$$p$$$ array encodes some rooted tree. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 \cdot 10^5$$$.

For each test case on the first line, output an integer $$$m$$$ — the minimum number of non-intersecting leading down paths that can cover all vertices of the tree. Then print $$$m$$$ pairs of lines containing path descriptions. In the first of them print the length of the path, in the second — the sequence of vertices specifying that path in the order from top to bottom. You can output the paths in any order. If there are several answers, output any of them.

/*
	hello,it's main package!
*/
package main

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

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

func nextInt()int{
	sc.Scan()
	if n,err:= strconv.Atoi(sc.Text());err==nil{
		return n
	}
	fmt.Fprint(wr,"scanf error!\n")
	return 0
}

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

	T:=nextInt()
	for i:=0;i<T;i++{
		solve()
		fmt.Fprint(wr,"\n")
	}
}
func solve(){
	n:=nextInt()
	fa:=make([]int,n+1)
	notLeaf:=make([]bool,n+1)
	for i:=1;i<=n;i++{
		fa[i]=nextInt()
		if(fa[i]!=i){
			notLeaf[fa[i]]=true
		}
	}
	vis:=make([] bool,n+1)
	ans:=-1
	for _,val:=range notLeaf{
		if !val {
			ans++
		}
	}
	fmt.Fprint(wr,ans,"\n")
	for idx,val:=range notLeaf{
		if(idx<1||idx>n){
			continue
		}
		if !val{
			ch:= make([] int,0)
			x:=idx
			ch=append(ch, x)
			vis[x]= true
			res:=1
			for x!=fa[x] && !vis[fa[x]] {
				x=fa[x]
				vis[x] = true
				// fmt.Fprint(wr," ",x)
				ch=append(ch, x)
				res++
			}
			fmt.Fprint(wr,res,"\n")
			// close(ch)
			for idx:=len(ch)-1;idx>=0;idx--{
				fmt.Fprint(wr,ch[idx]," ")
			}
			fmt.Fprint(wr,"\n")
		}

	}
}

package main

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

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

func slove(end int) {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	child := make([][]int, n)
	root := 0
	for i := range nums {
		if nums[i] == i+1 {
			root = i
			continue
		}
		child[nums[i]-1] = append(child[nums[i]-1], i)
	}
	ans := make([][]int, 1)
	cur := 0
	var dfs func(i int)
	dfs = func(i int){
		if len(child[i]) == 0 {
			ans[cur] = append(ans[cur], i)
			ans = append(ans, make([]int, 0))
			cur++
			return
		}
		ans[cur] = append(ans[cur], i)
		for _, j := range child[i] {
			dfs(j)
		}
	}
	dfs(root)
	ans = ans[:len(ans)-1]
	fmt.Fprintln(w, len(ans))
	for i := range ans {
		fmt.Fprintln(w, len(ans[i]))
		for j := range ans[i] {
			fmt.Fprintf(w, "%d", ans[i][j]+1)
			if j == len(ans[i])-1 {
				fmt.Fprintf(w, "\n")
			} else {
				fmt.Fprintf(w, " ")
			}
		}
	}
	if end != 0 {
		fmt.Fprintf(w, "\n")
	}

}

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		p := make([]int, n)
		vis := make([]bool, n)
		for i := range p {
			Fscan(in, &p[i])
			p[i]--
			if p[i] != i {
				vis[p[i]] = true
			}
		}

		ls := []int{}
		for v, b := range vis {
			if !b {
				ls = append(ls, v)
			}
		}
		Fprintln(out, len(ls))

		for _, v := range ls {
			path := []int{v}
			for v = p[v]; vis[v]; v = p[v] {
				path = append(path, v)
				vis[v] = false
				if p[v] == v {
					break
				}
			}
			Fprintln(out, len(path))
			for i := len(path) - 1; i >= 0; i-- {
				Fprint(out, path[i]+1, " ")
			}
			Fprintln(out)
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		p := make([]int, n)
		d := make([]int, n)
		for i := range p {
			Fscan(in, &p[i])
			p[i]--
			if p[i] != i {
				d[p[i]]++
			}
		}
		ans := 1
		ls := []int{}
		for v, d := range d {
			if d > 0 {
				ans += d - 1
			} else {
				ls = append(ls, v)
			}
		}
		Fprintln(out, ans)
		for _, v := range ls {
			path := []int{}
			for ; d[v] >= 0; v = p[v] {
				path = append(path, v)
				d[v] = -1
				if p[v] == v {
					break
				}
			}
			Fprintln(out, len(path))
			for i := len(path) - 1; i >= 0; i-- {
				Fprint(out, path[i]+1, " ")
			}
			Fprintln(out)
		}
		Fprintln(out)
	}
}

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

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n int
	Fscan(r, &n)
	t := 0
	var p int
	for t < n {
		t++
		Fscan(r, &p)
		a := make([]int, p+1)
		flag := make([]int, p+1)
		k := make([]int, p+1)
		for i := 1; i <= p; i++ {
			Fscan(r, &a[i])
			flag[a[i]] = 1
		}
		if p == 1{
			Fprintln(w, 1)
			Fprintln(w, 1)
			Fprintln(w, 1)
			Fprintln(w, )
			continue
		}
		q := 0
		aa := make([][]int, 0)
		for i := 1; i <= p; i++ {
			if flag[i] == 0 {
				q++
				s := make([]int, 0)
				s = append(s, i)
				temp := i
				k[temp] = 1
				for a[temp] != temp && k[a[temp]] == 0 {
					temp = a[temp]
					s = append(s, temp)
					k[temp] = 1
				}
				reverse(s)
				aa = append(aa, s)

			}
		}
		Fprintln(w, q)
		for i := range aa{
			Fprintln(w, len(aa[i]))
			for j := range aa[i] {
				if j != len(aa[i])-1 {
					Fprintf(w, "%d ", aa[i][j])
				} else {
					Fprintln(w, aa[i][j])
				}
			}
		}
		Fprintln(w, )
	}
}

func reverse(a []int) {
	length := len(a)
	for i := 0; i < length/2; i++ {
		a[i], a[length-i-1] = a[length-i-1], a[i]
	}
}

package main

import (
	"bufio"
	"container/list"
	. "fmt"
	"os"
)

type Int int64

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		f := make([]int, n+1)
		v := make([]bool, n+1)
		use := make([]bool, n+1)
		for i := 1; i <= n; i++ {
			Fscan(in, &f[i])
			v[f[i]] = true
		}
		if n == 1 {
			Fprintln(out, 1)
			Fprintln(out, 1)
			Fprintln(out, 1)
			continue
		}
		cnt := 0
		for i := 1; i <= n; i++ {
			if !v[i] {
				cnt++
			}
		}

		Fprintln(out, cnt)
		for i := 1; i <= n; i++ {
			if !v[i] {
				path := list.New()
				k := i
				use[k] = true
				path.PushFront(k)
				for !use[f[k]] && f[k] != k {
					use[f[k]] = true
					path.PushFront(f[k])
					k = f[k]
				}
				Fprintln(out, path.Len())
				for e := path.Front(); e != nil; e = e.Next() {
					Fprint(out, e.Value, " ")
				}
				Fprintln(out, "")
			}
		}
		Fprintln(out, "")
	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type deque[T any] struct {
	q []T
}

func (q *deque[T]) emplace(n T) {
	q.q = append(q.q, n)
}

func (q deque[T]) empty() bool {
	return len(q.q) == 0
}

func (q deque[T]) back() T {
	if q.empty() {
		panic("back() performed on empty deque")
	}
	return q.q[len(q.q)-1]
}

func (q *deque[T]) popback() T {
	if q.empty() {
		panic("popback() performed on empty deque")
	}
	res := q.back()
	q.q = q.q[:len(q.q)-1]
	return res
}

func (q deque[T]) front() T {
	if q.empty() {
		panic("front() performed on empty deque")
	}
	return q.q[0]
}

func (q *deque[T]) popfront() T {
	if q.empty() {
		panic("popfront() performed on empty deque")
	}
	res := q.front()
	q.q = q.q[1:]
	return res
}

func getdeque[T any](capacity int) deque[T] {
	return deque[T]{ make([]T, 0, capacity) }
}

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

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

		p := make([]int, n+1)
		for i := 1; i <= n; i++ { fmt.Fscan(r, &p[i]) }

		notaleaf := make([]bool, n+1)
		for i := 1; i <= n; i++ {
			if i != p[i] { notaleaf[p[i]] = true }
		}

		leaves := 0
		for i := 1; i <= n; i++ {
			if ! notaleaf[i] { leaves++ }
		}

		fmt.Fprintln(w, leaves)
		stk := getdeque[int](n)
		vis := make([]bool, n+1)
		for i := 1; i <= n; i++ {
			if ! notaleaf[i] {
				plen := 1
				t := i
				for {
					stk.emplace(t)
					vis[t] = true
					if t == p[t] || vis[p[t]] { break }
					plen++
					t = p[t]
				}
				fmt.Fprintln(w, plen)
				for ! stk.empty() { fmt.Fprintf(w, "%d ", stk.popback()) }
				fmt.Fprintln(w, "")
			}
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	reverse := func(a []int) { i,j := 0,len(a)-1; for i<j { a[i],a[j] = a[j],a[i]; i++; j--} }
	for tt:=1;tt<=T;tt++ {
		N := gi(); P := gis(N); for i:=0;i<N;i++ { P[i]-- }
		gr := make([][]int,N); root := -1
		for i,p := range P { if i == p { root = p } else { gr[p] = append(gr[p],i) } }
		paths := make([][]int,0)
		var dfs func(int) []int
		dfs = func(n int) []int {
			if len(gr[n]) == 0 { return []int{n} }
			for i:=1;i<len(gr[n]);i++ { x := dfs(gr[n][i]); paths = append(paths,x) }
			x := dfs(gr[n][0]); x = append(x,n); return x
		}
		paths = append(paths,dfs(root))
		fmt.Fprintln(wrtr,len(paths))
		for _,p := range paths {
			reverse(p)
			for i:=0;i<len(p);i++ { p[i]++ }
			fmt.Fprintln(wrtr,len(p))
			fmt.Fprintln(wrtr,vecintstring(p))
		}
		fmt.Fprintln(wrtr,"")
	}

}

package main

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

/*

 */

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

	p := make([]int, n)

	for i := range p {
		scanf("%d", &p[i])
	}
	scanf("\n")

	nodeToChild := make(map[int][]int)

	for i, par := range p {
		if i+1 != par {
			nodeToChild[par-1] = append(nodeToChild[par], i)
		}
	}

	used := make([]bool, n)

	paths := make([][]int, 0)

	for i := range p {
		isLeaf := len(nodeToChild[i]) == 0
		if isLeaf {
			path := []int{i + 1}
			used[i] = true

			j := i
			for !used[p[j]-1] {
				used[p[j]-1] = true
				path = append(path, p[j])
				j = p[j] - 1
			}

			revPath := make([]int, 0)
			for k := len(path) - 1; k >= 0; k-- {
				node := path[k]
				revPath = append(revPath, node)
			}

			paths = append(paths, revPath)
		}
	}

	fmt.Fprintln(writer, len(paths))

	for _, path := range paths {
		fmt.Fprintln(writer, len(path))
		for _, node := range path {
			fmt.Fprintf(writer, "%d ", node)
		}
		fmt.Fprintln(writer)
	}
	fmt.Fprintln(writer)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

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

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

	return factors
}

package main

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

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

	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		tree := make([][]int, n)
		root := 0
		for i := 0; i < n; i++ {
			p := 0
			fmt.Fscan(in, &p)
			p--
			if p == i {
				root = p
				continue
			}
			tree[p] = append(tree[p], i)
		}
		//fmt.Println(tree, root)
		var ans [][]int
		var dfs func(i int, temp []int)
		dfs = func(i int, temp []int) {
			temp = append(temp, i)
			count := len(tree[i])
			if count == 0 {
				ans = append(ans, temp)
				return
			}
			dfs(tree[i][0], temp)
			for j := 1; j < len(tree[i]); j++ {
				dfs(tree[i][j], []int{})
			}
		}
		dfs(root, []int{})
		fmt.Fprintln(out, len(ans))
		for _, an := range ans {
			fmt.Fprintln(out, len(an))
			for i := range an {
				fmt.Fprint(out, an[i]+1)
				if i == len(an)-1 {
					fmt.Fprintln(out)
				} else {
					fmt.Fprint(out, " ")
				}
			}
		}
	}
}

/*
	hello,it's main package!
*/
package main

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

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

func nextInt()int{
	sc.Scan()
	if n,err:= strconv.Atoi(sc.Text());err==nil{
		return n
	}
	fmt.Fprint(wr,"scanf error!\n")
	return 0
}

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

	T:=nextInt()
	for i:=0;i<T;i++{
		solve()
		fmt.Fprint(wr,"\n")
	}
}
func solve(){
	n:=nextInt()
	fa:=make([]int,n+1)
	notLeaf:=make([]bool,n+1)
	for i:=1;i<=n;i++{
		fa[i]=nextInt()
		if(fa[i]!=i){
			notLeaf[fa[i]]=true
		}
	}
	vis:=make([] bool,n+1)
	ans:=-1
	for _,val:=range notLeaf{
		if !val {
			ans++
		}
	}
	fmt.Fprint(wr,ans,"\n")
	for idx,val:=range notLeaf{
		if(idx<1||idx>n){
			continue
		}
		if !val{
			ch:= make([] int,0)
			x:=idx
			ch=append(ch, x)
			vis[x]= true
			res:=1
			for x!=fa[x] && !vis[fa[x]] {
				x=fa[x]
				vis[x] = true
				// fmt.Fprint(wr," ",x)
				ch=append(ch, x)
				res++
			}
			fmt.Fprint(wr,res,"\n")
			// close(ch)
			for _,val:=range ch{
				fmt.Fprint(wr,val," ")
			}
			fmt.Fprint(wr,"\n")
		}

	}
}

/*
	hello,it's main package!
*/
package main

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

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

func nextInt()int{
	sc.Scan()
	if n,err:= strconv.Atoi(sc.Text());err==nil{
		return n
	}
	fmt.Fprint(wr,"scanf error!\n")
	return 0
}

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

	T:=nextInt()
	for i:=0;i<T;i++{
		solve()
	}
}
func solve(){
	n:=nextInt()
	fa:=make([]int,n+1)
	notLeaf:=make([]bool,n+1)
	for i:=1;i<=n;i++{
		fa[i]=nextInt()
		if(fa[i]!=i){
			notLeaf[fa[i]]=true
		}
	}
	vis:=make([] bool,n+1)
	ans:=-1
	for _,val:=range notLeaf{
		if !val {
			ans++
		}
	}
	fmt.Fprint(wr,ans,"\n")
	for idx,val:=range notLeaf{
		if(idx<1||idx>n){
			continue
		}
		if !val{
			ch:= make([] int,0)
			x:=idx
			ch=append(ch, x)
			vis[x]= true
			res:=1
			for x!=fa[x] && !vis[fa[x]] {
				x=fa[x]
				vis[x] = true
				// fmt.Fprint(wr," ",x)
				ch=append(ch, x)
				res++
			}
			fmt.Fprint(wr,res,"\n")
			// close(ch)
			for _,val:=range ch{
				fmt.Fprint(wr,val," ")
			}
			fmt.Fprint(wr,"\n")
		}

	}
}

package main

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

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

func slove(end int) {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	child := make([][]int, n)
	root := 0
	for i := range nums {
		if nums[i] == i+1 {
			root = i
			continue
		}
		child[nums[i]-1] = append(child[nums[i]-1], i)
	}
	ans := make([][]int, 1)
	cur := 0
	var dfs func(i int)
	dfs = func(i int){
		if len(child[i]) == 0 {
			ans[cur] = append(ans[cur], i)
			ans = append(ans, make([]int, 0))
			cur++
			return
		}
		ans[cur] = append(ans[cur], i)
		for _, j := range child[i] {
			dfs(j)
		}
	}
	dfs(root)
	ans = ans[:len(ans)-1]
	fmt.Fprintln(w, len(ans)-1)
	for i := range ans {
		fmt.Fprintln(w, len(ans[i]))
		for j := range ans[i] {
			fmt.Fprintf(w, "%d", ans[i][j]+1)
			if j == len(ans[i])-1 {
				fmt.Fprintf(w, "\n")
			} else {
				fmt.Fprintf(w, " ")
			}
		}
	}
	if end != 0 {
		fmt.Fprintf(w, "\n")
	}

}

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

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n int
	Fscan(r, &n)
	t := 0
	var p int
	for t < n {
		t++
		Fscan(r, &p)
		a := make([]int, p+1)
		flag := make([]int, p+1)
		k := make([]int, p+1)
		for i := 1; i <= p; i++ {
			Fscan(r, &a[i])
			flag[a[i]] = 1
			if a[i] == i{
				flag[a[i]] = 0
			}
		}
		q := 0
		aa := make([][]int, 0)
		for i := 1; i <= p; i++ {
			if flag[i] == 0 {
				q++
				s := make([]int, 0)
				s = append(s, i)
				temp := i
				k[temp] = 1
				for a[temp] != temp && k[a[temp]] == 0 {
					temp = a[temp]
					s = append(s, temp)
					k[temp] = 1
				}
				reverse(s)
				aa = append(aa, s)

			}
		}
		Fprintln(w, q)
		for i := range aa{
			Fprintln(w, len(aa[i]))
			for j := range aa[i] {
				if j != len(aa[i])-1 {
					Fprintf(w, "%d ", aa[i][j])
				} else {
					Fprintln(w, aa[i][j])
				}
			}
		}
		Fprintln(w, )
	}
}

func reverse(a []int) {
	length := len(a)
	for i := 0; i < length/2; i++ {
		a[i], a[length-i-1] = a[length-i-1], a[i]
	}
}

package main

import (
	"bufio"
	"container/list"
	. "fmt"
	"os"
)

type Int int64

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		f := make([]int, n+1)
		v := make([]bool, n+1)
		use := make([]bool, n+1)
		for i := 1; i <= n; i++ {
			Fscan(in, &f[i])
			v[f[i]] = true
		}
		if n == 1 {
			Fprintln(out, 1)
			Fprintln(out, 1)
			Fprintln(out, "")
			continue
		}
		cnt := 0
		for i := 1; i <= n; i++ {
			if !v[i] {
				cnt++
			}
		}

		Fprintln(out, cnt)
		for i := 1; i <= n; i++ {
			if !v[i] {
				path := list.New()
				k := i
				use[k] = true
				path.PushFront(k)
				for !use[f[k]] && f[k] != k {
					use[f[k]] = true
					path.PushFront(f[k])
					k = f[k]
				}
				Fprintln(out, path.Len())
				for e := path.Front(); e != nil; e = e.Next() {
					Fprint(out, e.Value, " ")
				}
				Fprintln(out, "")
			}
		}
		Fprintln(out, "")
	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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


You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).

The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.

Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n. If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)

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

	sum := 0

	for _, num := range a {
		sum += num
	}

	ans := make([]int, 0)

	for idx, num := range a {
		if sum - num == num * (n - 1) {
			ans = append(ans, idx + 1)
		}
	}

  	defer writer.Flush()

	fmt.Println(len(ans))
	for _, num := range ans {
		printf("%d ", num)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	a := make([]int, n)

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

	sum := 0

	for _, num := range a {
		sum += num
	}

	ans := make([]int, 0)

	for idx, num := range a {
		if sum - num == num * (n - 1) {
			ans = append(ans, idx)
		}
	}

	fmt.Println(ans)
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)

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

	sum := 0

	for _, num := range a {
		sum += num
	}

	ans := make([]int, 0)

	for idx, num := range a {
		if sum - num == num * (n - 1) {
			ans = append(ans, idx + 1)
		}
	}

	fmt.Println(len(ans))
	for _, num := range ans {
		printf("%d ", num)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	a := make([]int, n)

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

	sum := 0

	for _, num := range a {
		sum += num
	}
	fmt.Printf("%d %d\n", sum, n)
	ans := make([]int, 0)

	for idx, num := range a {
		if sum - num == num * (n - 1) {
			ans = append(ans, idx + 1)
		}
	}

	fmt.Println(len(ans))
	for _, num := range ans {
		fmt.Printf("%d ", num)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	a := make([]int, n)

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

	sum := 0

	for _, num := range a {
		sum += num
	}

	ans := make([]int, 0)

	for idx, num := range a {
		if sum - num == num * (n - 1) {
			ans = append(ans, idx + 1)
		}
	}

	fmt.Println(len(ans))
	for _, num := range ans {
		fmt.Printf("%d ", num)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	a := make([]int, n)

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

	sum := 0

	for _, num := range a {
		sum += num
	}

	ans := make([]int, 0)

	for idx, num := range a {
		if sum - num == num * (n - 1) {
			ans = append(ans, idx + 1)
		}
	}

	fmt.Println(len(ans))
	for _, num := range ans {
		fmt.Print("%d ", num)
	}
}


Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won $$$x$$$ rounds in a row. For example, if Bob won five rounds in a row and $$$x = 2$$$, then two sets ends.You know that Alice and Bob have already played $$$n$$$ rounds, and you know the results of some rounds. For each $$$x$$$ from $$$1$$$ to $$$n$$$, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins $$$x$$$ rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the number of rounds. The second line contains one string $$$s$$$ of length $$$n$$$ — the descriptions of rounds. If the $$$i$$$-th element of the string is 0, then Alice won the $$$i$$$-th round; if it is 1, then Bob won the $$$i$$$-th round, and if it is ?, then you don't know who won the $$$i$$$-th round.

In the only line print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins $$$i$$$ rounds in a row.

NoteLet's consider the first test case:  if $$$x = 1$$$ and $$$s = 110000$$$ or $$$s = 111000$$$ then there are six finished sets;  if $$$x = 2$$$ and $$$s = 110000$$$ then there are three finished sets;  if $$$x = 3$$$ and $$$s = 111000$$$ then there are two finished sets;  if $$$x = 4$$$ and $$$s = 110000$$$ then there is one finished set;  if $$$x = 5$$$ then there are no finished sets;  if $$$x = 6$$$ then there are no finished sets.

package main

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

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

var err error

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

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 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 Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

const (
	oo  = 100000000
	mod = 1
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

var (
	num, nextHi []int
)

func solve() {
	n := nextInt()
	arr := []byte(next())
	num = make([]int, n)
	prev := [2]int{-1, -1}
	for i, c := range arr {
		if c != '?' {
			prev[c-'0'] = i
		}
		num[i] = Max(i-prev[0], i-prev[1])
	}

	nextHi = make([]int, n)
	for i := range nextHi {
		nextHi[i] = i - 1
	}

	for i := 1; i <= n; i++ {
		tot := 0
		for j := n - 1; j >= 0; {
			next := root(j, i)
			if next != -1 {
				tot++
			}
			j = next - i
		}
		printf("%d ", tot)
	}
	printf("\n")
}

func root(i, x int) int {
	if i == -1 || num[i] >= x {
		return i
	}
	res := root(nextHi[i], x)
	nextHi[i] = res
	return res
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		file1, err := os.Open("input.txt")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create("output.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	// start := time.Now()

	// for t := nextInt(); t > 0; t-- {
	solve()
	// }

	// printf("%v\n", time.Now().Sub(start))
}


After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which  is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on.

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly.

Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball.

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n, m := scanInt(scanner), scanInt(scanner)
	center := (m+1)/2
	pos := center
	for i := 0; i < n; i++ {
		writer.WriteString(fmt.Sprintf("%d\n", pos))
		if pos == center && m%2 == 1 {
			pos--
		} else if pos <= center {
			pos = m-pos+1
		} else {
			pos = m-pos
		}
		if pos == 0 {
			pos = center
		}
	}
}

package main

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

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

// type Comparable interface {
// 	compare(p2 *P) int
// 	getValue() int
// }

type P struct {
	x, pos, id int
}

func (p1 *P) compare(p2 *P) int {
	if p1.x < p2.x {
		return -1
	}
	if p1.x > p2.x {
		return 1
	}
	if p1.id < p2.id {
		return -1
	}
	if p1.id > p2.id {
		return 1
	}
	if p1.pos < p2.pos {
		return -1
	}
	if p1.pos > p2.pos {
		return 1
	}
	return 0
}

func (p *P) getValue() int {
	return p.pos
}

type Heap struct {
	size int
	a [200010]P
}

func (heap *Heap) poll() P {
	var ret P = heap.a[1]
	heap.size -= 1
	heap.a[1] = heap.a[heap.size]
	heap.siftDown(1)

	return ret
}

func (heap *Heap) siftDown(v int) {
	for 2 * v < heap.size {
		var j int = v
		if heap.a[2 * v].compare(&heap.a[j]) < 0 {
			j = 2 * v
		}

		if 2 * v + 1 < heap.size && heap.a[2 * v + 1].compare(&heap.a[j]) < 0 {
			j = 2 * v + 1
		}
		if j == v {
			return
		}
		heap.a[j], heap.a[v] = heap.a[v], heap.a[j]
		v = j
	}
}

func (heap *Heap) add(p P) {
	heap.a[heap.size] = p
	heap.siftUp(heap.size-1)
	heap.size += 1
}

func (heap *Heap) siftUp(v int) {
	for v > 1 && heap.a[v/2].compare(&heap.a[v]) > 0 {
		heap.a[v / 2], heap.a[v] = heap.a[v], heap.a[v / 2]
		v = v / 2
	}
}

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	var heap = Heap{size: 1}
	for i := 1; i <= m; i++ {
		var p = P{0, i, abs(m + 1 - 2 * i)}
		heap.add(p)
	}

	for i := 1; i <= n; i++ {
		var top P = heap.poll()
		fmt.Println(top.getValue())
		top.x += 1
		heap.add(top)
	}
}

func next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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

package main

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

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

// type Comparable interface {
// 	compare(p2 *P) int
// 	getValue() int
// }

type P struct {
	x, pos, id int
}

func (p1 *P) compare(p2 *P) int {
	if p1.x < p2.x {
		return -1
	}
	if p1.x > p2.x {
		return 1
	}
	if p1.id < p2.id {
		return -1
	}
	if p1.id > p2.id {
		return 1
	}
	if p1.pos < p2.pos {
		return -1
	}
	if p1.pos > p2.pos {
		return 1
	}
	return 0
}

func (p *P) getValue() int {
	return p.pos
}

type Heap struct {
	size int
	a [200010]P
}

func (heap *Heap) poll() P {
	var ret P = heap.a[1]
	heap.size -= 1
	heap.a[1] = heap.a[heap.size]
	heap.siftDown(1)

	return ret
}

func (heap *Heap) siftDown(v int) {
	for 2 * v < heap.size {
		var j int = v
		if heap.a[2 * v].compare(&heap.a[j]) < 0 {
			j = 2 * v
		}

		if 2 * v + 1 < heap.size && heap.a[2 * v + 1].compare(&heap.a[j]) < 0 {
			j = 2 * v + 1
		}
		if j == v {
			return
		}
		heap.a[j], heap.a[v] = heap.a[v], heap.a[j]
		v = j
	}
}

func (heap *Heap) add(p P) {
	heap.a[heap.size] = p
	heap.siftUp(heap.size-1)
	heap.size += 1
}

func (heap *Heap) siftUp(v int) {
	for v > 1 && heap.a[v/2].compare(&heap.a[v]) > 0 {
		heap.a[v / 2], heap.a[v] = heap.a[v], heap.a[v / 2]
		v = v / 2
	}
}

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	var heap = Heap{size: 1}
	for i := 1; i <= m; i++ {
		var p = P{0, i, abs(m + 1 - 2 * i)}
		heap.add(p)
	}

	for i := 1; i <= n; i++ {
		var top P = heap.poll()
		fmt.Fprintln(out, top.getValue())
		top.x += 1
		heap.add(top)
	}
}

func next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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

package main

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

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

type P struct {
	x, pos, id int
}

func (p1 *P) compare(p2 *P) int {
	if p1.x < p2.x {
		return -1
	}
	if p1.x > p2.x {
		return 1
	}
	if p1.id < p2.id {
		return -1
	}
	if p1.id > p2.id {
		return 1
	}
	if p1.pos < p2.pos {
		return -1
	}
	if p1.pos > p2.pos {
		return 1
	}
	return 0
}

type Heap struct {
	size int
	a [200010]P
}

func (heap *Heap) poll() P {
	var ret P = heap.a[1]
	heap.size -= 1
	heap.a[1] = heap.a[heap.size]
	heap.siftDown(1)

	return ret
}

func (heap *Heap) siftDown(v int) {
	for 2 * v < heap.size {
		var j int = v
		if heap.a[2 * v].compare(&heap.a[j]) < 0 {
			j = 2 * v
		}
		if 2 * v + 1 < heap.size && heap.a[2 * v + 1].compare(&heap.a[j]) < 0 {
			j = 2 * v + 1
		}
		if j == v {
			break
		}
		heap.a[v], heap.a[j] = heap.a[j], heap.a[v]
		v = j
	}
}

func (heap *Heap) add(p P) {
	heap.a[heap.size] = p
	// fmt.Println("before size=", heap.size)
	// fmt.Println("elm=", heap.a[heap.size])
	// fmt.Println("elm=", heap.a[heap.size-1])
	heap.siftUp(heap.size - 1)
	heap.size += 1

}

func (heap *Heap) siftUp(v int) {
	for v > 1 && heap.a[v/2].compare(&heap.a[v]) > 0 {
		heap.a[v/2], heap.a[v] = heap.a[v], heap.a[v/2]
		v /= 2
	}
}

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	var heap = Heap{size: 1}
	for i := 1; i <= m; i++ {
		var p = P{0, i, abs(m + 1 - 2 * i)}
		heap.add(p)
	}

	for i := 1; i <= n; i++ {
		var p P = heap.poll()
		fmt.Fprintln(out, p.pos)
		p.x += 1
		heap.add(p)
	}
}

func next() string {
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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

package main

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

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

// type Comparable interface {
// 	compare(p2 *P) int
// 	getValue() int
// }

type P struct {
	x, pos, id int
}

func (p1 *P) compare(p2 *P) int {
	if p1.x < p2.x {
		return -1
	}
	if p1.x > p2.x {
		return 1
	}
	if p1.id < p2.id {
		return -1
	}
	if p1.id > p2.id {
		return 1
	}
	if p1.pos < p2.pos {
		return -1
	}
	if p1.pos > p2.pos {
		return 1
	}
	return 0
}

func (p *P) getValue() int {
	return p.pos
}

type Heap struct {
	size int
	a [200010]P
}

func (heap *Heap) poll() P {
	var ret P = heap.a[1]
	heap.size -= 1
	heap.a[1] = heap.a[heap.size]
	heap.siftDown(1)

	return ret
}

func (heap *Heap) siftDown(v int) {
	for 2 * v < heap.size {
		var j int = v
		if heap.a[2 * v].compare(&heap.a[j]) < 0 {
			j = 2 * v
		}

		if 2 * v + 1 < heap.size && heap.a[2 * v + 1].compare(&heap.a[j]) < 0 {
			j = 2 * v + 1
		}
		if j == v {
			return
		}
		heap.a[j], heap.a[v] = heap.a[v], heap.a[j]
	}
}

func (heap *Heap) add(p P) {
	heap.a[heap.size] = p
	heap.siftUp(heap.size-1)
	heap.size += 1
}

func (heap *Heap) siftUp(v int) {
	for v > 1 && heap.a[v/2].compare(&heap.a[v]) > 0 {
		heap.a[v / 2], heap.a[v] = heap.a[v], heap.a[v / 2]
		v = v / 2
	}
}

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	var heap = Heap{size: 1}
	for i := 1; i <= m; i++ {
		var p = P{0, i, abs(m + 1 - 2 * i)}
		heap.add(p)
	}

	for i := 1; i <= n; i++ {
		var top P = heap.poll()
		fmt.Println(top.getValue())
		top.x += 1
		heap.add(top)
	}
}

func next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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

package main

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

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

// type Comparable interface {
// 	compare(p2 *P) int
// 	getValue() int
// }

type P struct {
	x, pos, id int
}

func (p1 *P) compare(p2 *P) int {
	if p1.x < p2.x {
		return -1
	}
	if p1.x > p2.x {
		return 1
	}
	if p1.id < p2.id {
		return -1
	}
	if p1.id > p2.id {
		return 1
	}
	if p1.pos < p2.pos {
		return -1
	}
	if p1.pos > p2.pos {
		return 1
	}
	return 0
}

func (p *P) getValue() int {
	return p.pos
}

type Heap struct {
	size int
	a [200010]P
}

func (heap *Heap) poll() P {
	var ret P = heap.a[1]
	heap.size -= 1
	heap.a[1] = heap.a[heap.size]
	heap.sieveDown(1)

	return ret
}

func (heap *Heap) sieveDown(v int) {
	for 2 * v < heap.size {
		var j int = v
		if heap.a[2 * v].compare(&heap.a[j]) < 0 {
			j = 2 * v
		}

		if 2 * v + 1 < heap.size && heap.a[2 * v + 1].compare(&heap.a[j]) < 0 {
			j = 2 * v + 1
		}
		if j == v {
			return
		}
		heap.a[j], heap.a[v] = heap.a[v], heap.a[j]
	}
}

func (heap *Heap) add(p P) {
	heap.a[heap.size] = p
	heap.size += 1
	heap.sieveUp(heap.size-1)
}

func (heap *Heap) sieveUp(v int) {
	for v > 1 && heap.a[v/2].compare(&heap.a[v]) > 0 {
		heap.a[v / 2], heap.a[v] = heap.a[v], heap.a[v / 2]
		v = v / 2
	}
}

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	var heap = Heap{size: 1}
	for i := 1; i <= m; i++ {
		var p = P{0, i, abs(m + 1 - 2 * i)}
		heap.add(p)
	}

	for i := 1; i <= n; i++ {
		var top P = heap.poll()
		fmt.Println(top.getValue())
		top.x += 1
		heap.add(top)
	}
}

func next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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

package main

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

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

type P struct {
	x, pos, id int
}

func (p1 *P) compare(p2 *P) int {
	if p1.x < p2.x {
		return -1
	}
	if p1.x > p2.x {
		return 1
	}
	if p1.id < p2.id {
		return -1
	}
	if p1.id > p2.id {
		return 1
	}
	if p1.pos < p2.pos {
		return -1
	}
	if p1.pos > p2.pos {
		return 1
	}
	return 0
}

type Heap struct {
	size int
	a [200010]P
}

func (heap *Heap) poll() P {
	var ret P = heap.a[1]
	heap.size -= 1
	heap.a[1] = heap.a[heap.size]
	heap.siftDown(1)

	return ret
}

func (heap *Heap) siftDown(v int) {
	for 2 * v < heap.size {
		var j int = v
		if heap.a[2 * v].compare(&heap.a[j]) > 0 {
			j = 2 * v
		}
		if 2 * v + 1 < heap.size && heap.a[2 * v + 1].compare(&heap.a[j]) > 0 {
			j = 2 * v + 1
		}
		if j == v {
			break
		}
		heap.a[v], heap.a[j] = heap.a[j], heap.a[v]
		v = j
	}
}

func (heap *Heap) add(p P) {
	heap.a[heap.size] = p
	// fmt.Println("before size=", heap.size)
	// fmt.Println("elm=", heap.a[heap.size])
	// fmt.Println("elm=", heap.a[heap.size-1])
	heap.siftUp(heap.size - 1)
	heap.size += 1

}

func (heap *Heap) siftUp(v int) {
	for v > 1 && heap.a[v/2].compare(&heap.a[v]) > 0 {
		heap.a[v/2], heap.a[v] = heap.a[v], heap.a[v/2]
		v /= 2
	}
}

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	var heap = Heap{size: 1}
	for i := 1; i <= m; i++ {
		var p = P{0, i, abs(m + 1 - 2 * i)}
		heap.add(p)
	}

	for i := 1; i <= n; i++ {
		var p P = heap.poll()
		fmt.Fprintln(out, p.pos)
		p.x += 1
		heap.add(p)
	}
}

func next() string {
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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


There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a very unusual upgrade of this game. The rules of the game are given below:There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: "X" or "O". Then the player chooses two positive integers a and b (a·b = 12), after that he makes a table of size a × b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters "X" on all cards. Otherwise, the player loses.Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.

The first line of the input contains integer t (1 ≤ t ≤ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line. The description of each test is a string consisting of 12 characters, each character is either "X", or "O". The i-th character of the string shows the character that is written on the i-th card from the start.

For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.

4
OXXXOXOOXOOX
OXOXOXOXOXOX
XXXXXXXXXXXX
OOOOOOOOOOOO

package main

import "fmt"

func main(){
	var n int
	var s string
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&s)
		len := len(s)
		x := make([]int, 12)
		d := 0
		for j:=0; j<len; j++ {
			if string(s[j]) == "X" {
				x[j]=j
				d+=1
			}
		}
		total := 0
		var a, b [12]int
		for k:=1; k<=12; k++ {
			z := 12/k
			if 12%k == 0 {
				c := 0
				var m bool = false
				for l:=0; l<z; l++ {
					c = x[l]
					for n:=0; n<k; n++ {
						if string(s[c]) == "X" {
							m = true
						} else {
							m = false
							break
						}
						c+=z
					}
					if m == true {
						break
					}
				}
				if m == true {
					a[total]=k
					b[total]=z
					total+=1
				}
			}
		}
		fmt.Print(total, " ")
		if total > 0 {
			for f:=0; f<total; f++ {
				fmt.Printf("%dx%d ", a[f], b[f])
			}
		}
		fmt.Println()
	}
}

package main

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

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

//Wrong
func main() {
	// defer func(start time.Time) {
	// 	fmt.Println(time.Since(start))
	// }(time.Now())

	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	t := nextInt()
	ab := []int{1, 2, 3, 4, 6, 12}

	for testCase:=0; testCase<t; testCase++ {
		s := next()
		numberOfWays := 0
		count := 0
		result := ""

		for _, v := range ab {
			for i, length := 0, 12/v; i<length; i++ {
				count = 0

				for j:=i; j<12 && s[j] == 'X'; j+=length {
					count++
				}

				if count==v {
					numberOfWays++
					switch v {
					case 1:
						result += "1x12 "
					case 2:
						result += "2x6 "
					case 3:
						result += "3x4 "
					case 4:
						result += "4x3 "
					case 6:
						result += "6x2 "
					case 12:
						result += "12x1"
					}
					// fmt.Println(result, v)
					break
				}
			}
		}

		fmt.Fprintln(writer, numberOfWays, result)
	}
}

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

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

package main
import (
	"fmt"
	//"sort"
)
func main() {
	var t int
	var s string
	fmt.Scan(&t)
	n:=0
	r:=make([]int,12)

	for i:=0;i<t;i++ {
		n=0
		fmt.Scan(&s)
		for j:=1;j<=12;j++ {
			if 12%j!=0 {
				continue
			}
			ok:=0
			for k:=0;k<j;k++ {
				kk:=1
				for m:=k;m<12;m+=j {
					if s[m] != 'X' {
						kk=0
					}
				}
				if kk!=0 {
					ok=1
					break
				}
			}
			if ok!=0 {
				r[n]=j
				n++
			}
		}
		fmt.Print(n)
		for a:=n-1;a>=0;a-- {
		//fmt.Println(a)
		fmt.Print(" ",12/r[a],"x",r[a])
		}
		fmt.Println()
	}

}

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	all := [...]string{"X", "XX", "XXX", "XXXX", "XXXXXX", "XXXXXXXXXXXX"}
	var s string
	var n int
	var arr [100]int
	var p int
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		p = 0
		fmt.Scanf("%s\n", &s)
		// if s == all[5] {
		// 	arr[0] = 1
		// 	arr[1] = 2
		// 	arr[2] = 3
		// 	arr[3] = 4
		// 	arr[4] = 6
		// 	arr[5] = 12
		// 	p = 6
		// }
		// if s == "OOOOOOOOOOOO" {
		// 	break
		// }
		for ii := 0; ii < 6; ii++ {
			l := int(12 / len(all[ii]))
			tag := true
			//fmt.Printf("-----%d %s-----\n", l, all[ii])
			for j := 0; j < l; j++ {
				tag = true
				for jj := j; jj < 12; jj += l {
					//fmt.Printf("%d=%c ", jj, s[jj])
					if s[jj] != 'X' {
						tag = false
						break
					}
				}
				//fmt.Println(" ", tag)
				if tag {
					//fmt.Println("fuck")
					break
				}
			}
			if tag {
				arr[p] = len(all[ii])
				p++
			}
		}
		fmt.Print(p)
		for i := 0; i < p; i++ {
			fmt.Printf(" %dx%d", arr[i], int(12/arr[i]))
		}
		fmt.Println("")
	}

}

func donothing() {
	i := math.Pi
	i++
	s := "abc"
	strings.ToLower(s)
}

package main

import "fmt"

func main() {
    var t int
    var s string
    a := [...]int{1,2,3,4,6,12}
    fmt.Scan(&t)
    for i := 0; i < t; i++ {
        fmt.Scan(&s)
        cnt := 0
        ans := make([]bool,6)
        for j := 0; (j < 6) && (!ans[j]); j++ {
            r, c := a[j], 12/a[j]
            for ic := 0; ic < c; ic++ {
                ok := true
                for ir := 0; ir < r; ir++ {
                    if s[ic+(ir*c)] == 'O' {
                        ok = false
                        break
                    }
                }
                if ok {
                    ans[j] = true
                    cnt++
                    break
                }
            }
        }
        fmt.Print(cnt)
        for j := 0; j < 6; j++ {
            if ans[j] {
                fmt.Printf(" %dx%d",a[j],12/a[j])
            }
        }
        fmt.Println()
    }
}

/*********************************************************************************
*     File Name           :     A.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-03-05 23:28]
*     Last Modified       :     [2014-03-05 23:51]
*     Description         :
**********************************************************************************/

package main

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

func test(cards string, a, b int) (ret bool) {
    var arr [12][12]byte
    for i := 0; i < a; i++ {
        for j := 0; j < b; j++ {
            arr[i][j] = cards[i*b+j]
        }
    }
    for i := 0; i < b; i++ {
        ret = true
        for j := 0; j < a; j++ {
            if arr[j][i] == 'O' {
                ret = false
                break
            }
        }
        if ret {
            return
        }
    }
    return false
}

func main() {
    scanner.Split(bufio.ScanWords)

    var t = scanner.nextInt()
    var height = [...]int{1, 2, 3, 4, 6, 12}
    for i := 0; i < t; i++ {
        cards := scanner.next()
        var ans = 0
        var res = make([]string, 0)
        for _, a := range height {
            if test(cards, a, 12/a) {
                ans++
                res = append(res, fmt.Sprintf("%dx%d", a, 12/a))
            }
        }
        fmt.Print(ans)
        for i := 0; i < ans; i++ {
            fmt.Printf(" %s", res[i])
        }
        fmt.Println()
    }
}

type Scanner struct {
    *bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

func (self *Scanner) nextInt() int {
    x, err := strconv.Atoi(self.next())
    if err != nil {
        panic(err)
    }
    return x
}

package main

import "fmt"

func main() {
    var T int
    fmt.Scan(&T)
    for t := 0; t < T; t = t+1 {
        var s string
        fmt.Scanf("\n%s", &s)
        rowCounts := []int{1, 2, 3, 4, 6, 12}
        colCounts := []int{12, 6, 4, 3, 2, 1}
        countValid := 0
        valid := make([]int, 6)
        for i := 0; i<6; i = i+1 {
            for j := 0; j<colCounts[i]; j = j+1 {
                good := 1;
                for k := 0; k<rowCounts[i]; k=k+1 {
                    if s[k*colCounts[i] + j] != 'X' {
                        good = 0
                    }
                }
                if good != 0 {
                    if valid[i] == 0 {
                        countValid = countValid + 1
                    }
                    valid[i] = 1
                }
            }
        }
        fmt.Printf("%d", countValid);
        for i := 0; i<6; i=i+1 {
            if valid[i] > 0 {
                fmt.Printf(" %dx%d", rowCounts[i], colCounts[i]);
            }
        }
        fmt.Printf("\n");
    }
}

package main

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

func main() {
    scanner.Split(bufio.ScanWords)
    for cas := scanner.NextInt(); cas > 0; cas-- {
        s := scanner.Next()
        ans := make([]int, 12)
        cnt := 0
        for i := 1; i <= 12; i++ {
            if 12%i == 0 {
                mk := make([]int, 13)
                for j := 0; j < 12; j++ {
                    mk[j] = 1
                }
                for j := 0; j < 12; j++ {
                    if s[j] == 'O' {
                        mk[j%i] = 0
                    }
                }
                for j := 0; j < i; j++ {
                    if mk[j] == 1 {
                        ans[cnt] = i
                        cnt++
                        break
                    }
                }
            }
        }
        fmt.Printf("%d", cnt)
        for i := cnt - 1; i >= 0; i-- {
            fmt.Printf(" %dx%d", 12/ans[i], ans[i])
        }
        fmt.Println()
    }
}

type Scanner struct {
    *bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

func (self *Scanner) NextInt() int {
    x, err := strconv.Atoi(self.Next())
    if err != nil {
        panic(err)
    }
    return x
}

func (self *Scanner) NextInt64() int64 {
    x, err := strconv.ParseInt(self.Next(), 10, 64)
    if err != nil {
        panic(err)
    }
    return x
}

func (self *Scanner) NextDouble() float64 {
    x, err := strconv.ParseFloat(self.Next(), 64)
    if err != nil {
        panic(err)
    }
    return x
}

package main

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

func main() {
    scanner.Split(bufio.ScanWords)
    for cas := scanner.NextInt(); cas > 0; cas-- {
        s := scanner.Next()
        ans := make([]int, 12)
        cnt := 0
        for i := 1; i <= 12; i++ {
            if 12%i == 0 {
                mk := make([]int, 13)
                for j := 0; j < 12; j++ {
                    mk[j] = 1
                }
                for j := 0; j < 12; j++ {
                    if s[j] == 'O' {
                        mk[j%i] = 0
                    }
                }
                for j := 0; j < i; j++ {
                    if mk[j] == 1 {
                        ans[cnt] = i
                        cnt++
                        break
                    }
                }
            }
        }
        fmt.Printf("%d", cnt)
        for i := 0; i < cnt; i++ {
            fmt.Printf(" %dx%d", 12/ans[i], ans[i])
        }
        fmt.Println()
    }
}

type Scanner struct {
    *bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

func (self *Scanner) NextInt() int {
    x, err := strconv.Atoi(self.Next())
    if err != nil {
        panic(err)
    }
    return x
}

func (self *Scanner) NextInt64() int64 {
    x, err := strconv.ParseInt(self.Next(), 10, 64)
    if err != nil {
        panic(err)
    }
    return x
}

func (self *Scanner) NextDouble() float64 {
    x, err := strconv.ParseFloat(self.Next(), 64)
    if err != nil {
        panic(err)
    }
    return x
}

package main

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

func main() {
	scanner.Split(bufio.ScanWords)
	for cas := scanner.NextInt(); cas > 0; cas-- {
		s := scanner.Next()
		ans := make([]int, 12)
		cnt := 0
		for i := 1; i <= 12; i++ {
			if 12%i == 0 {
				mk := make([]int, 13)
				for j := 0; j < 12; j++ {
					mk[j] = 1
				}
				for j := 0; j < 12; j++ {
					if s[j] == 'O' {
						mk[j/i] = 0
					}
				}
				for j := 0; j < 12/i; j++ {
					if mk[j] == 1 {
						ans[cnt] = i
						cnt++
						break
					}
				}
			}
		}
		fmt.Printf("%d", cnt)
		for i := 0; i < cnt; i++ {
			fmt.Printf(" %dx%d", ans[i], 12/ans[i])
		}
		fmt.Println()
	}
}

type Scanner struct {
	*bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
	if !self.Scan() {
		panic(self.Err())
	}
	return self.Text()
}

func (self *Scanner) NextInt() int {
	x, err := strconv.Atoi(self.Next())
	if err != nil {
		panic(err)
	}
	return x
}

func (self *Scanner) NextInt64() int64 {
	x, err := strconv.ParseInt(self.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return x
}

func (self *Scanner) NextDouble() float64 {
	x, err := strconv.ParseFloat(self.Next(), 64)
	if err != nil {
		panic(err)
	}
	return x
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $$$a$$$ to make it a good array. Recall that the prefix of the array $$$a=[a_1, a_2, \dots, a_n]$$$ is a subarray consisting several first elements: the prefix of the array $$$a$$$ of length $$$k$$$ is the array $$$[a_1, a_2, \dots, a_k]$$$ ($$$0 \le k \le n$$$).The array $$$b$$$ of length $$$m$$$ is called good, if you can obtain a non-decreasing array $$$c$$$ ($$$c_1 \le c_2 \le \dots \le c_{m}$$$) from it, repeating the following operation $$$m$$$ times (initially, $$$c$$$ is empty):  select either the first or the last element of $$$b$$$, remove it from $$$b$$$, and append it to the end of the array $$$c$$$. For example, if we do $$$4$$$ operations: take $$$b_1$$$, then $$$b_{m}$$$, then $$$b_{m-1}$$$ and at last $$$b_2$$$, then $$$b$$$ becomes $$$[b_3, b_4, \dots, b_{m-3}]$$$ and $$$c =[b_1, b_{m}, b_{m-1}, b_2]$$$.Consider the following example: $$$b = [1, 2, 3, 4, 4, 2, 1]$$$. This array is good because we can obtain non-decreasing array $$$c$$$ from it by the following sequence of operations:  take the first element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2, 1]$$$, $$$c = [1]$$$;  take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2]$$$, $$$c = [1, 1]$$$;  take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4]$$$, $$$c = [1, 1, 2]$$$;  take the first element of $$$b$$$, so $$$b = [3, 4, 4]$$$, $$$c = [1, 1, 2, 2]$$$;  take the first element of $$$b$$$, so $$$b = [4, 4]$$$, $$$c = [1, 1, 2, 2, 3]$$$;  take the last element of $$$b$$$, so $$$b = [4]$$$, $$$c = [1, 1, 2, 2, 3, 4]$$$;  take the only element of $$$b$$$, so $$$b = []$$$, $$$c = [1, 1, 2, 2, 3, 4, 4]$$$ — $$$c$$$ is non-decreasing. Note that the array consisting of one element is good.Print the length of the shortest prefix of $$$a$$$ to delete (erase), to make $$$a$$$ to be a good array. Note that the required length can be $$$0$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer: the length of the shortest prefix of elements you need to erase from $$$a$$$ to make it a good array.

NoteIn the first test case of the example, the array $$$a$$$ is already good, so we don't need to erase any prefix.In the second test case of the example, the initial array $$$a$$$ is not good. Let's erase first $$$4$$$ elements of $$$a$$$, the result is $$$[4, 5, 2]$$$. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.

package main
// https://codeforces.com/contest/1380/submission/87050098
import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type FastIn struct {
	scanner *bufio.Scanner
}

func MakeFastIn() (obj FastIn)  {
	obj = FastIn{}
	obj.scanner = bufio.NewScanner(os.Stdin)
	obj.scanner.Split(bufio.ScanWords)
	return
}

func (fi *FastIn) ReadText() string {
	fi.scanner.Scan()
	return fi.scanner.Text()
}

func (fi *FastIn) ReadInt() int {
	ret, err := strconv.Atoi(fi.ReadText())
	if err != nil {
		panic(err)
	}
	return ret
}

func (fi *FastIn) ReadInts(arr []int) {
	for i := 0; i < len(arr); i++ {
		arr[i] = fi.ReadInt()
	}
}
func Min (a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func (fi *FastIn) ReadInt64() int64 {
	ret, err := strconv.ParseInt(fi.ReadText(), 10, 64)
	if err != nil {
		panic(err)
	}
	return ret
}

var x,k,y int64

func GetCost(arr []int, left_b int, right_b int) (int64, bool) {
	any_gt := false
	max_bound := left_b
	if max_bound < right_b {
		max_bound = right_b
	}
	for _,v := range arr {
		if  v > max_bound {
			any_gt = true
		}
	}

	len := int64(len(arr))
	if any_gt {
		if len < k {
			return 0, false
		}
		return Min((len/k)*x+(len%k)*y,x+(len-k)*y), true
	} else {
		return Min((len/k)*x+(len%k)*y, len * y), true
	}
}

func chk(a []int) (bool){
	i,j:=0,len(a)-1
	pvr:=0
	for i<=j {
		if a[i] < a[j] {
			if pvr>a[i] {
				return false
			}
			pvr=a[i]
			i++
		} else {
			if pvr>a[j] {
				return false
			}
			pvr=a[j]
			j--
		}
	}
	return true
}

func main() {
	fin := MakeFastIn()
	T := fin.ReadInt()
	for ;T>0;T--{
		n := fin.ReadInt()
		a:= make([]int, n)
		fin.ReadInts(a)

		l,r:=-1,n
		for r-l>1 {
			m:= (l+r)/2
			if chk(a[m:]) {
				r=m
			} else {
				l=m
			}
		}
		fmt.Println(r)
	}
}

package main

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

var input *bufio.Reader
func main()  {
	var t int
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input,&t)
	for j:=0 ; j<t ;j++{
		var n int
		fmt.Fscan(input,&n)
		v:=make([]int,n)
		for i:=0;i<n;i++ {
			fmt.Fscan(input, &v[i])
		}
		i:=n-1
		for i>0 && v[i]<=v[i-1]{i--}
		for i>0 && v[i]>=v[i-1] {i--}
		fmt.Println(i)
	}

}

package main

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

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

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = io.nextInt()
		}

		lo, hi := 0, n
		for lo < hi {
			mid := (lo + hi) / 2
			l, r := mid, n-1
			ok := true
			last := 0
			for l < r {
				var min int
				if a[l] < a[r] {
					min = a[l]
					l++
				} else {
					min = a[r]
					r--
				}
				if min < last {
					ok = false
				}
				last = min
			}
			if ok {
				hi = mid
			} else {
				lo = mid + 1
			}
		}

		io.println(lo)
	}
}

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

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		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)
	}
	return n
}

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

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

// https://codeforces.com/contest/x/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input *bufio.Reader

func main() {
	var T int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n int
	fmt.Fscan(input, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
	}

	pos := len(a) - 1
	for pos >= 1 && a[pos] <= a[pos - 1] {pos--}
	for pos >= 1 && a[pos] >= a[pos - 1] {pos--}
	fmt.Println(pos)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 1024*1024)
	scanner.Buffer(buffer, 1024*1024)
	scanner.Split(bufio.ScanWords)

	input := func() int {
		scanner.Scan()
		i, _ := strconv.Atoi(scanner.Text())
		return i
	}

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

		return b
	}

	for t := input(); t > 0; t-- {
		n := input()
		arr := make([]int, n)

		for i := range arr {
			arr[i] = input()
		}

		size := 0
		prev := -1
		i := 0
		beg := 0
		j := n - 1

		arrsize := n
		var where bool

		for size < arrsize {
			// fmt.Println(i, j, prev, size, arrsize)
			k := min(arr[i], arr[j])
			if k >= prev {
				if k == arr[i] {
					i++
					where = false
				} else {
					j--
					where = true
				}
				size++
				prev = k
			} else {
				if where {
					i++
				}

				beg = i
				j = n - 1
				size = 0
				arrsize = n - (i + 1)
				prev = -1
			}
		}

		fmt.Println(beg)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		var a int
		var aPrev int
		var isPrevGrow bool
		var isPrevEqual bool
		equalStart := 0
		grow := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			if j == 0 {
				aPrev = a
				isPrevGrow = true
				continue
			}
			if a >= aPrev && !isPrevGrow {
				if a == aPrev && !isPrevEqual {
					isPrevEqual = true
					equalStart = j - 1
				} else if a > aPrev {
					if isPrevEqual {
						grow = equalStart
					} else {
						grow = j - 1
					}
					isPrevGrow = true
					isPrevEqual = false
				}
			} else if a < aPrev {
				isPrevGrow = false
				isPrevEqual = false
			}

			aPrev = a
		}
		fmt.Println(grow)
	}
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		readLine(reader)
		line := readLine(reader)
		strs := strings.Split(string(line), " ")
		i := len(strs) - 1
		current, _ := strconv.ParseInt(strs[i], 10, 64)
		turn := 0
		for ; i >= 0; i-- {
			temp, _ := strconv.ParseInt(strs[i], 10, 64)
			if turn == 0 { // 递增
				if temp < current {
					turn = 1
				}
			} else if turn == 1 {
				if temp > current {
					turn = 2
					break
				}
			}
			current = temp
		}
		if turn <= 1 {
			fmt.Println(0)
		}else {
			fmt.Println(i+1)
		}
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		Fprintln(out, sort.Search(n-2, func(i int) bool {
			for j, v := n-1, 0; i < j; {
				if a[i] < a[j] {
					if a[i] < v {
						return false
					}
					v = a[i]
					i++
				} else {
					if a[j] < v {
						return false
					}
					v = a[j]
					j--
				}
			}
			return true
		}))
	}
}

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

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := range a {
		a[i] = readInt()
	}

	back := 0
	for i := n - 1; i > 0; i-- {
		if a[i-1] < a[i] {
			back = i
			break
		}
	}

	front := -1
	for front < back && len(a[front+1:]) > 0 {
		a = a[front+1:]
		back -= front + 1
		front = 0
		for i := 1; i < len(a); i++ {
			if a[i-1] > a[i] {
				break
			}
			front = i
		}
		// fmt.Println(front, back)
	}

	// fmt.Println("done:", front, back)
	// fmt.Println("ans:", n-len(a))
	fmt.Println(n - len(a))
}

func main() {
	k := readInt()
	for i := 0; i < k; i++ {
		solve()
	}
}

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

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

func readInt() int {
	return int(readInt64())
}

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

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

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

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

func sumInt64(a []int64) int64 {
	var ret int64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

package main

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

func main() {
	defer writer.Flush()
	t := NextInt()
	for t > 0 {
		t--

		n := NextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
		}

		isAsc := true
		isDesc := true
		for i := 1; i < n; i++ {
			if a[i] < a[i-1] {
				isAsc = false
			}
			if a[i] > a[i-1] {
				isDesc = false
			}
		}
		if isDesc || isAsc {
			Println(0)
			continue
		}

		pos := n-1
		for a[pos] <= a[pos-1] {
			pos--
		}
		for pos > 0 && a[pos] >= a[pos-1] {
			pos--
		}
		if pos < 0 {
			pos = 0
		}
		Println(pos)
	}
}

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

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

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

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

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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

package main

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

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

func main() {
	defer out.Flush()
	var nTest int
	fmt.Fscanf(in, "%d\n", &nTest)
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}
func solveCase() {
	var n int
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanln(in)
	down := false
	ans := n - 1
	for i := n - 2; i >= 0; i-- {
		if a[i] < a[i+1] {
			down = true
		}
		if down && a[i] > a[i+1] {
			break
		}
		ans = i
	}
	fmt.Fprintf(out, "%d\n", ans)
}

package main

import "fmt"

func main()  {
	var t int
	fmt.Scanf("%d\n",&t)
	for j:=0 ; j<t ;j++{
		var n int
		fmt.Scanf("%d\n",&n)
		v:=make([]int,n)
		for i:=0;i<n;i++ {fmt.Scanf("%d",&v[i])}
		i:=n-1
		for i>0 && v[i]<=v[i-1]{i--}
		for i>0 && v[i]>=v[i-1] {i--}
		fmt.Printf("%d\n",i)
	}

}

// https://codeforces.com/contest/x/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input *bufio.Reader

func main() {
	var T int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n int
	fmt.Fscan(input, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
	}

	pos := len(a) - 1
	for pos >= 1 && a[pos] <= a[pos - 1] {pos--}
	for pos >= 1 && a[pos] > a[pos - 1] {pos--}
	fmt.Println(pos)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		var a int
		var aPrev int
		var isPrevGrow bool
		grow := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			if j == 0 {
				aPrev = a
				isPrevGrow = true
				continue
			}
			if a > aPrev && !isPrevGrow {
				grow = j - 1
				isPrevGrow = true
			} else if a < aPrev {
				isPrevGrow = false
			}

			aPrev = a
		}
		fmt.Println(grow)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		var a int
		var aPrev int
		var isPrevGrow bool
		grow := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			if j == 0 {
				aPrev = a
				isPrevGrow = true
				continue
			}

			if a >= aPrev && !isPrevGrow {
				grow = j - 1
				isPrevGrow = true
			} else if a < aPrev {
				isPrevGrow = false
			}

			aPrev = a
		}
		fmt.Println(grow)
	}
}

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := range a {
		a[i] = readInt()
	}

	ans := 0
	for i := n - 3; i >= 0; i-- {
		if a[i] > a[i+1] && a[i+1] < a[i+2] {
			ans = i + 1
			break
		}
	}
	fmt.Println(ans)
}

func main() {
	k := readInt()
	for i := 0; i < k; i++ {
		solve()
	}
}

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

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

func readInt() int {
	return int(readInt64())
}

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

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

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

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

func sumInt64(a []int64) int64 {
	var ret int64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

package main

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

func main() {
	defer writer.Flush()
	t := NextInt()
	for t > 0 {
		t--

		n := NextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
		}

		isAsc := true
		isDesc := true
		for i := 1; i < n; i++ {
			if a[i] < a[i-1] {
				isAsc = false
			}
			if a[i] > a[i-1] {
				isDesc = false
			}
		}
		if isDesc || isAsc {
			Println(0)
			continue
		}

		pos := n-1
		for a[pos] < a[pos-1] {
			pos--
		}
		for pos > 0 && a[pos] >= a[pos-1] {
			pos--
		}
		if pos < 0 {
			pos = 0
		}
		Println(pos)
	}
}

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

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

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

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

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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