You and your friend Ilya are participating in an individual programming contest consisting of multiple stages. A contestant can get between $$$0$$$ and $$$100$$$ points, inclusive, for each stage, independently of other contestants.Points received by contestants in different stages are used for forming overall contest results. Suppose that $$$k$$$ stages of the contest are completed. For each contestant, $$$k - \lfloor \frac{k}{4} \rfloor$$$ stages with the highest scores are selected, and these scores are added up. This sum is the overall result of the contestant. (Here $$$\lfloor t \rfloor$$$ denotes rounding $$$t$$$ down.)For example, suppose $$$9$$$ stages are completed, and your scores are $$$50, 30, 50, 50, 100, 10, 30, 100, 50$$$. First, $$$7$$$ stages with the highest scores are chosen — for example, all stages except for the $$$2$$$-nd and the $$$6$$$-th can be chosen. Then your overall result is equal to $$$50 + 50 + 50 + 100 + 30 + 100 + 50 = 430$$$.As of now, $$$n$$$ stages are completed, and you know the points you and Ilya got for these stages. However, it is unknown how many more stages will be held. You wonder what the smallest number of additional stages is, after which your result might become greater than or equal to Ilya's result, at least in theory. Find this number!

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of completed stages. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$) — your points for the completed stages. The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i \le 100$$$) — Ilya's points for the completed stages. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print a single integer — the smallest number of additional stages required for your result to be able to become greater than or equal to Ilya's result. If your result is already not less than Ilya's result, print $$$0$$$.

NoteIn the first test case, you have scored $$$100$$$ points for the first stage, while Ilya has scored $$$0$$$. Thus, your overall result ($$$100$$$) is already not less than Ilya's result ($$$0$$$).In the second test case, you have scored $$$0$$$ points for the first stage, while Ilya has scored $$$100$$$. A single stage with an opposite result is enough for both your and Ilya's overall scores to become equal to $$$100$$$.In the third test case, your overall result is $$$30 + 40 + 50 = 120$$$, while Ilya's result is $$$100 + 100 + 100 = 300$$$. After three additional stages your result might become equal to $$$420$$$, while Ilya's result might become equal to $$$400$$$.In the fourth test case, your overall result after four additional stages might become equal to $$$470$$$, while Ilya's result might become equal to $$$400$$$. Three stages are not enough.

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		a := make([]int, n, n)
		b := make([]int, n, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&a[j])
		}
		for j := 0; j < n; j++ {
			fmt.Scan(&b[j])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		sort.Sort(sort.Reverse(sort.IntSlice(b)))
		last := n - n/4 - 1
		var sum1, sum2 int
		for j := 0; j <= last; j++ {
			sum1 += a[j]
			sum2 += b[j]
		}
		offset := 0
		newN := n
		for sum1 < sum2 {
			sum1 += 100
			newN++
			if newN%4 == 0 {
				if last-offset >= 0 {
					sum1 -= a[last-offset]
				} else {
					sum1 -= 100
				}
				offset++
			} else if last+newN-n-offset < n {
				sum2 += b[last+newN-n-offset]
			}
			//fmt.Println(newN, sum1, sum2)
		}
		fmt.Println(newN - n)
	}
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    input, _ := reader.ReadString('\n')

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var n int
		n, _ = GetInt(input)

		input, _ = reader.ReadString('\n')
		// Get scores
		mine, llya := make([]int, n), make([]int, n)
		for i:=0; i<n; i++ {
			mine[i], input = GetInt(input)
		}
		input, _ = reader.ReadString('\n')
		for i:=0; i<n; i++ {
			llya[i], input = GetInt(input)
		}

		// Sort first
		sort.Slice(mine, func(l, r int) bool {
			return mine[l] > mine[r]
		})
		sort.Slice(llya, func(l, r int) bool {
			return llya[l] > llya[r]
		})

		// Binary search
		left, right := 0, 3*n
		for left < right {
			mid := left + (right - left) >> 1
			if check(mine, llya, mid) {
				right = mid

			} else {
				left = mid + 1
			}
		}
		fmt.Println(left)
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func check(a, b []int, mid int) bool {
	total := mid+len(a)
	score_a, score_b := 0, 0
	if mid >= total - (total>>2) {
		score_a = (total - total/4) * 100
		for _, val := range b {
			score_b += val
		}

	} else {
		score_a = 100*mid
		for i:=0; i<(total-total/4)-mid; i++ {
			score_a += a[i]
		}
		for i:=0; i<(total-total/4) && i<len(b); i++ {
			score_b += b[i]
		}
	}

	return score_a >= score_b
}

package main

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

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

	var tcs int
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var n int
		_, _ = fmt.Fscan(in, &n)

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

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

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

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

		var goal, current int
		pq := make(IntHeap, 0, 2*n)

		for i := n / 4; i < n; i++ {
			goal += b[i]
			current += a[i]

			pq = append(pq, a[i])
		}

		heap.Init(&pq)

		var ans int
		for i, j := 0, n/4; current < goal; i++ {
			current += 100
			heap.Push(&pq, 100)

			if (n+i+1)/4 > (n+i)/4 {
				x := heap.Pop(&pq)
				current -= x.(int)
			} else {
				j--
				if j >= 0 {
					goal += b[j]
				}
			}

			ans++
		}

		_, _ = fmt.Fprintln(out, ans)
	}

	_ = out.Flush()
}

// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func abs(n int) int {
	if n < 0 {
		n = -n
	}

	return n
}

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
}

package main

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

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

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

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

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

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

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

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

func sol(n int, a, b []interface{}) int {
	suma := sum(a)
	sumb := 0
	heapaMin := BuildHeap(a, less)
	heapbMax := BuildHeap(b, more)
	ret := 0
	for {
		for heapaMin.Len() > n-n/4 {
			min := heapaMin.Pop().(int)
			suma -= min
		}
		for heapbMax.Len() > n/4 {
			max := heapbMax.Pop().(int)
			sumb += max
		}
		if suma >= sumb {
			// printf("%d %d %d\n", n, suma, sumb)
			return ret
		}
		n++
		ret++
		heapaMin.Push(100)
		suma += 100
		heapbMax.Push(0)
		sumb += 0
	}
}

func less(a, b interface{}) bool {
	return a.(int) < b.(int)
}

func more(a, b interface{}) bool {
	return a.(int) > b.(int)
}

func sum(a []interface{}) int {
	ret := 0
	for _, n := range a {
		ret += n.(int)
	}
	return ret
}

type Heap struct {
	vals []interface{}
	less func(interface{}, interface{}) bool
}

func BuildHeap(vals []interface{}, less func(interface{}, interface{}) bool) Heap {
	h := Heap{
		vals: vals,
		less: less,
	}
	h.Init()
	return h
}

func (h Heap) Init() {
	for i := len(h.vals)/2 - 1; i >= 0; i-- {
		h.fixDown(i)
	}
}

func (h Heap) Len() int {
	return len(h.vals)
}

func (h Heap) Peek() interface{} {
	return h.vals[0]
}

func (h *Heap) Push(i interface{}) {
	h.vals = append(h.vals, i)
	h.fixUp(len(h.vals) - 1)
}

func (h *Heap) Pop() interface{} {
	ret := h.vals[0]
	h.vals[0] = h.vals[len(h.vals)-1]
	h.vals = h.vals[:len(h.vals)-1]
	h.fixDown(0)
	return ret
}

func (h Heap) fixUp(i int) {
	if i == 0 {
		return
	}
	up := (i+1)/2 - 1
	if h.less(h.vals[i], h.vals[up]) {
		h.swap(i, up)
		h.fixUp(up)
	}
}

func (h Heap) fixDown(i int) {
	down := i
	left := (i+1)*2 - 1
	if left < len(h.vals) && h.less(h.vals[left], h.vals[down]) {
		down = left
	}
	right := left + 1
	if right < len(h.vals) && h.less(h.vals[right], h.vals[down]) {
		down = right
	}
	if down != i {
		h.swap(i, down)
		h.fixDown(down)
	}
}

func (h Heap) swap(i, j int) {
	h.vals[i], h.vals[j] = h.vals[j], h.vals[i]
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		a := make([]int, n, n)
		b := make([]int, n, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&a[j])
		}
		for j := 0; j < n; j++ {
			fmt.Scan(&b[j])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		sort.Sort(sort.Reverse(sort.IntSlice(b)))
		last := n - n/4 - 1
		var sum1, sum2 int
		for j := 0; j <= last; j++ {
			sum1 += a[j]
			sum2 += b[j]
		}
		offset := 0
		newN := n
		for sum1 < sum2 {
			sum1 += 100
			newN++
			if newN%4 == 0 {
				if last-offset >= 0 {
					sum1 -= a[last-offset]
				} else {
					sum1 -= 100
				}
				offset++
			} else if last+newN-n < n {
				sum2 += b[last+newN-n]
			}
		}
		fmt.Println(newN - n)
	}
}


Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:   In the end the absolute difference between the number of wins and loses is equal to k;  There is no hand such that the absolute difference before this hand was equal to k. Help Roma to restore any such sequence.

The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000). The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.

If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO. Otherwise print this sequence. If there are multiple answers, print any of them.

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	k, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	s := scanner.Text()

	dp := make([][][]bool, n)
	for i := 0; i < n; i++ {
		dp[i] = make([][]bool, 2*k-1)
		for j := 0; j < 2*k-1; j++ {
			dp[i][j] = make([]bool, 3)
		}
	}
	// 0 - W
	// 1 - D
	// 2 - L
	if s[n-1] == '?' {
		dp[n-1][2*k-2][0] = true
		dp[n-1][0][2] = true
	}
	if s[n-1] == 'W' {
		dp[n-1][2*k-2][0] = true
	}
	if s[n-1] == 'L' {
		dp[n-1][0][2] = true
	}
	for i := n - 2; i > -1; i-- {
		if s[i] == '?' {
			for j := 0; j < 2*k-1; j++ {
				if okay(dp[i+1][j]) {
					dp[i][j][1] = true
					if j < 2*k-2 {
						dp[i][j+1][2] = true
					}
					if j > 0 {
						dp[i][j-1][0] = true
					}
				}
			}
		}
		if s[i] == 'W' {
			for j := 0; j < 2*k-1; j++ {
				if okay(dp[i+1][j]) && j > 0 {
					dp[i][j-1][0] = true
				}
			}
		}
		if s[i] == 'L' {
			for j := 0; j < 2*k-1; j++ {
				if okay(dp[i+1][j]) && j < 2*k-2 {
					dp[i][j+1][2] = true
				}
			}
		}
		if s[i] == 'D' {
			for j := 0; j < 2*k-1; j++ {
				if okay(dp[i+1][j]) {
					dp[i][j][1] = true
				}
			}
		}
	}
	answer := make([]rune, n)
	cur := k - 1
	for i := 0; i < n; i++ {
		c := find(dp[i][cur])
		if c == 'X' {
			fmt.Println("NO")
			os.Exit(0)
		}
		answer[i] = c
		if c == 'W' {
			cur++
		}
		if c == 'L' {
			cur--
		}
	}
	fmt.Println(string(answer))
}

func okay(x []bool) bool {
	for _, xx := range x {
		if xx {
			return true
		}
	}
	return false
}

func find(x []bool) rune {
	for i, xx := range x {
		if xx {
			if i == 0 {
				return 'W'
			}
			if i == 1 {
				return 'D'
			}
			if i == 2 {
				return 'L'
			}
		}
	}
	return 'X'
}


Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...There are two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. Initially, an $$$ans$$$ is equal to $$$0$$$ and the following operation is defined:   Choose position $$$i$$$ ($$$1 \le i \le n$$$);  Add $$$a_i$$$ to $$$ans$$$;  If $$$b_i \neq -1$$$ then add $$$a_i$$$ to $$$a_{b_i}$$$. What is the maximum $$$ans$$$ you can get by performing the operation on each $$$i$$$ ($$$1 \le i \le n$$$) exactly once?Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.

The first line contains the integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of arrays $$$a$$$ and $$$b$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$−10^6 \le a_i \le 10^6$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$ or $$$b_i = -1$$$). Additional constraint: it's guaranteed that for any $$$i$$$ ($$$1 \le i \le n$$$) the sequence $$$b_i, b_{b_i}, b_{b_{b_i}}, \ldots$$$ is not cyclic, in other words it will always end with $$$-1$$$.

In the first line, print the maximum $$$ans$$$ you can get. In the second line, print the order of operations: $$$n$$$ different integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$). The $$$p_i$$$ is the position which should be chosen at the $$$i$$$-th step. If there are multiple orders, print any of them.

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


"The zombies are lurking outside. Waiting. Moaning. And when they come...""When they come?""I hope the Wall is high enough."Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are.The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide.

The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows:    each of the R lines contains a string of length C,  the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise.  B

The number of wall segments in the input configuration.

NoteIn the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second.

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	text = strings.TrimRight(text, "\r\n")
	s := strings.Split(text, " ")

	n, _ := strconv.Atoi(s[0])
	//m, _ := strconv.Atoi(s[1])

	for i := 0; i < n; i++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")
		text = "." + text + "."
		if i == n-1 {
			c := 0
			s := '.'
			for _, j := range text {
				if j != s {
					s = j
					if j == 'B' {
						c++
					}
				}
			}
			fmt.Println(c)
		}
	}

}


Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of $$$n$$$ different bloggers. Blogger numbered $$$i$$$ has $$$a_i$$$ followers.Since Masha has a limited budget, she can only sign a contract with $$$k$$$ different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.Help her, find the number of ways to select $$$k$$$ bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).For example, if $$$n=4$$$, $$$k=3$$$, $$$a=[1, 3, 1, 2]$$$, then Masha has two ways to select $$$3$$$ bloggers with the maximum total number of followers:   conclude contracts with bloggers with numbers $$$1$$$, $$$2$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_1 + a_2 + a_4 = 6$$$.  conclude contracts with bloggers with numbers $$$2$$$, $$$3$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_2 + a_3 + a_4 = 6$$$. Since the answer can be quite large, output it modulo $$$10^9+7$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 1000$$$) — the number of bloggers and how many of them you can sign a contract with. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots a_n$$$ ($$$1 \le a_i \le n$$$) — the number of followers of each blogger. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.

For each test case, on a separate line output one integer — the number of ways to select $$$k$$$ bloggers so that the total number of their followers is maximum possible.

NoteThe test case is explained in the statements.In the second test case, the following ways are valid:   conclude contracts with bloggers with numbers $$$1$$$ and $$$2$$$. In this case, the number of followers will be equal to $$$a_1 + a_2 = 2$$$;  conclude contracts with bloggers with numbers $$$1$$$ and $$$3$$$. In this case, the number of followers will be equal to $$$a_1 + a_3 = 2$$$;  conclude contracts with bloggers with numbers $$$1$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_1 + a_4 = 2$$$;  conclude contracts with bloggers with numbers $$$2$$$ and $$$3$$$. In this case, the number of followers will be equal to $$$a_2 + a_3 = 2$$$;  conclude contracts with bloggers with numbers $$$2$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_2 + a_4 = 2$$$;  conclude contracts with bloggers with numbers $$$3$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_3 + a_4 = 2$$$. In the third test case, the following ways are valid:   concludes a contract with a blogger with the number $$$2$$$. In this case, the number of followers will be equal to $$$a_2 = 2$$$.

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

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
}


You are given an array $$$a$$$ with $$$n$$$ non-negative integers. You can apply the following operation on it.   Choose two indices $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$).  If $$$a_l + a_r$$$ is odd, do $$$a_r := a_l$$$. If $$$a_l + a_r$$$ is even, do $$$a_l := a_r$$$. Find any sequence of at most $$$n$$$ operations that makes $$$a$$$ non-decreasing. It can be proven that it is always possible. Note that you do not have to minimize the number of operations.An array $$$a_1, a_2, \ldots, a_n$$$ is non-decreasing if and only if $$$a_1 \le a_2 \le \ldots \le a_n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$)  — the array itself. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, print one integer $$$m$$$ ($$$0 \le m \le n$$$), the number of operations, in the first line. Then print $$$m$$$ lines. Each line must contain two integers $$$l_i, r_i$$$, which are the indices you chose in the $$$i$$$-th operation ($$$1 \le l_i < r_i \le n$$$). If there are multiple solutions, print any of them.

NoteIn the second test case, $$$a$$$ changes like this:  Select indices $$$3$$$ and $$$4$$$. $$$a_3 + a_4 = 3$$$ is odd, so do $$$a_4 := a_3$$$. $$$a = [1, 1000000000, 3, 3, 5]$$$ now.  Select indices $$$1$$$ and $$$2$$$. $$$a_1 + a_2 = 1000000001$$$ is odd, so do $$$a_2 := a_1$$$. $$$a = [1, 1, 3, 3, 5]$$$ now, and it is non-decreasing. In the first and third test cases, $$$a$$$ is already non-decreasing.

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

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


Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter $$$y$$$ Koa selects must be strictly greater alphabetically than $$$x$$$ (read statement for better understanding). You can make hacks in these problems independently.Koa the Koala has two strings $$$A$$$ and $$$B$$$ of the same length $$$n$$$ ($$$|A|=|B|=n$$$) consisting of the first $$$20$$$ lowercase English alphabet letters (ie. from a to t).In one move Koa:  selects some subset of positions $$$p_1, p_2, \ldots, p_k$$$ ($$$k \ge 1; 1 \le p_i \le n; p_i \neq p_j$$$ if $$$i \neq j$$$) of $$$A$$$ such that $$$A_{p_1} = A_{p_2} = \ldots = A_{p_k} = x$$$ (ie. all letters on this positions are equal to some letter $$$x$$$).  selects a letter $$$y$$$ (from the first $$$20$$$ lowercase letters in English alphabet) such that $$$y>x$$$ (ie. letter $$$y$$$ is strictly greater alphabetically than $$$x$$$).  sets each letter in positions $$$p_1, p_2, \ldots, p_k$$$ to letter $$$y$$$. More formally: for each $$$i$$$ ($$$1 \le i \le k$$$) Koa sets $$$A_{p_i} = y$$$. Note that you can only modify letters in string $$$A$$$.Koa wants to know the smallest number of moves she has to do to make strings equal to each other ($$$A = B$$$) or to determine that there is no way to make them equal. Help her!

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$A$$$ and $$$B$$$. The second line of each test case contains string $$$A$$$ ($$$|A|=n$$$). The third line of each test case contains string $$$B$$$ ($$$|B|=n$$$). Both strings consists of the first $$$20$$$ lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other ($$$A = B$$$) or $$$-1$$$ if there is no way to make them equal.

5
3
aab
bcc
4
cabc
abcb
3
abc
tsr
4
aabd
cccd
5
abcbd
bcdda

Note  In the $$$1$$$-st test case Koa:   selects positions $$$1$$$ and $$$2$$$ and sets $$$A_1 = A_2 = $$$ b ($$$\color{red}{aa}b \rightarrow \color{blue}{bb}b$$$).  selects positions $$$2$$$ and $$$3$$$ and sets $$$A_2 = A_3 = $$$ c ($$$b\color{red}{bb} \rightarrow b\color{blue}{cc}$$$).   In the $$$2$$$-nd test case Koa has no way to make string $$$A$$$ equal $$$B$$$.  In the $$$3$$$-rd test case Koa:   selects position $$$1$$$ and sets $$$A_1 = $$$ t ($$$\color{red}{a}bc \rightarrow \color{blue}{t}bc$$$).  selects position $$$2$$$ and sets $$$A_2 = $$$ s ($$$t\color{red}{b}c \rightarrow t\color{blue}{s}c$$$).  selects position $$$3$$$ and sets $$$A_3 = $$$ r ($$$ts\color{red}{c} \rightarrow ts\color{blue}{r}$$$).

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

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