package main

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

func ok(b []int) bool {
	for i := range b {
		if b[i] > 1 {
			return false
		}
	}

	return true
}

func isOk(i, m int, a []string, b []int,  s []byte) bool {
	if i == m {
		if ok(b) {
			return true
		}
		return false
	}

	if !ok(b) {
		return false
	}

	l := make(map[byte]bool)

	for j := range a {
		l[a[j][i]] = true
	}

	// fmt.Println(i, l)

	for k := range l {

		s[i] = k

		for j := range a {
			if a[j][i] != k {
				b[j]++
			}
		}

		if isOk(i+1, m, a, b, s) {
			// fmt.Println(s, b)
			return true
		}

		for j := range a {
			if a[j][i] != k {
				b[j]--
			}
		}
	}

	return false
}

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

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

	ins := func() string {
		sc.Scan()
		return sc.Text()
	}

	for t := ini(); t > 0; t-- {
		n, m := ini(), ini()

		a := make([]string, n)
		s := make([]byte, m)

		for i := range a {
			a[i] = ins()
		}

		b := make([]int, n)

		if !isOk(0, m, a, b, s) {
			fmt.Println(-1)
			// fmt.Println(string(s))
			continue
		}

		fmt.Println(string(s))
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t, n, m int
o:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a := make([][]byte, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for _, s := range a {
			ans := make([]byte, m)
			copy(ans, s)
			for i, b := range s {
			o2:
				for c := byte('a'); c <= 'z'; c++ {
					ans[i] = c
					for _, s := range a {
						d := 0
						for j, b := range s {
							if ans[j] != b {
								d++
								if d > 1 {
									continue o2
								}
							}
						}
					}
					Fprintln(out, string(ans))
					continue o
				}
				ans[i] = b
			}
		}
		Fprintln(out, -1)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for tc := 0; tc < t; tc++ {
        var n, m int
        var a [][]byte
		fmt.Scan(&n, &m)
		a = make([][]byte, n)
		for i := 0; i < n; i++ {
		    fmt.Scan(&a[i])
		}
		res := "-1"
		flag := true
		for j := 0; flag && j < m; j++ {
		    for c := 'a'; flag && c <= 'z'; c++ {
		        cur := make([]byte, m)
		        copy(cur, a[0])
		        cur[j] = byte(c)
		        valid := true
		        for i := 1; valid && i < n; i++ {
		            cnt := 0
		            for k := 0; k < m; k++ {
		                if (cur[k] == a[i][k]) {
		                    cnt++
		                }
		            }
		            if (cnt < m - 1) {
		                valid = false
		            }
		        }
		        if (valid) {
		            res = string(cur)
		            flag = false
		        }
		    }
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func is_good(txt []rune, G [][]rune, n int, m int) bool {
	for i := 0; i < n; i++ {
		dist := 0
		for j := 0; j < m; j++ {
			if txt[j] != G[i][j] {
				dist++
			}
			if dist > 1 {
				return false
			}
		}
	}
	return true
}

func main(){
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Scan(&n, &m)
		G := make([][]rune, n)
		for i := 0; i < n; i++ {
			var s string
			fmt.Scan(&s)
			G[i] = []rune(s)
		}
		ans := "-1"
		found := false
		for i := 0; i < n && !found; i++ {
			cand := make([]rune, m)
			copy(cand, G[i])
			for k := 0; k < m && !found; k++ {
				for c := 'a'; c <= 'z'; c++ {
					cand[k] = c
					if is_good(cand, G, n, m) {
						ans = string(cand)
						found = true
						break
					}
				}
				cand[k] = G[i][k]
			}
		}
		fmt.Println(ans)
	}

}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	Int64() int64
	UInt64() uint64
	String() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) Int64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) String() string {
	impl.Scan()
	return impl.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
}

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		n := reader.Int64()
		m := reader.Int64()
		a := make([]string, n)
		for i := range a {
			a[i] = reader.String()
		}
		writer.Printf("%s\n", sol(a, m))
	}
}

func sol(a []string, m int64) string {
	first := []byte(a[0])
	for i := range first {
		curr := first[i]
		for j := byte(0); j < 27; j++ {
			nxtPos := curr + j
			if nxtPos > 'z' {
				nxtPos -= 26
			}
			first[i] = nxtPos
			if isValid(string(first), a[1:]) {
				return string(first)
			}
		}
	}
	return "-1"
}

func isValid(res string, rem []string) bool {
	for _, s := range rem {
		diff := false
		for i := range res {
			if res[i] != s[i] {
				if diff {
					return false
				} else {
					diff = true
				}
			}
		}
	}
	return true
}

package main

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

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		S := make([][]byte, n)
		for i := 0; i < n; i++ {
			S[i], _ = reader.ReadBytes('\n')
		}
		res := solve(n, m, S)
		if len(res) == 0 {
			fmt.Println("-1")
		} else {
			fmt.Println(res)
		}
	}
}

func solve(n, m int, A [][]byte) string {
	buf := make([]byte, m)

	var dfs func(flag int, i int) bool

	dfs = func(flag int, i int) bool {
		if i == m {
			return true
		}

		set := make(map[byte]bool)

		for j := 0; j < n; j++ {
			set[A[j][i]] = true
		}

		for k := range set {
			var flag2 int

			for j := 0; j < n; j++ {
				if A[j][i] != k {
					flag2 |= 1 << j
				}
			}
			if flag2&flag > 0 {
				continue
			}
			buf[i] = k
			if dfs(flag|flag2, i+1) {
				return true
			}
		}
		return false
	}

	res := dfs(0, 0)

	if res {
		return string(buf)
	}

	return ""
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
  	"strings"
  	"strconv"
  	"math"

)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n,m:= reader.NextInt(),reader.NextInt()
		aa := make([][]rune, n)
		for i:=range aa {
			aa[i]=make([]rune,m)
		}
		for i := 0; i <n; i++ {
			s:=reader.NextString()
			aa[i]=[]rune(s)
		}
		sol(aa)
	}
}

func sol(aa [][]rune) {
	ans:=aa[0]
	// fmt.Println(ans)
	if(isValid(ans,aa)){
		writer.Printf("%s\n",string(ans))
		return
	}else{
		for i:=0;i<len(aa[0]);i++ {
			prevLetter:=ans[i]
			for c:='a';c<='z';c++ {
				ans[i]=c
				if(isValid(ans,aa)){
					writer.Printf("%s\n",string(ans))
					return
				}
			}
			ans[i]=prevLetter
		}
	}
	writer.Printf("-1\n")

}

func isValid(benchmark []rune, aa [][]rune) bool{
	for i:=1;i<len(aa);i++ {
		cnt:=0
		for j:=0;j<len(aa[0]);j++ {
			if(benchmark[j]!=aa[i][j]){
				cnt++
			}
		}
		if(cnt>1){
			return false
		}
	}
	return true
}

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

func LevenshteinDistance(a, b string) int {
	// Write your code here.
	rows:=len(a)
	cols:=len(b)
	dp:=make([][]int,rows+1)
	for i:=range dp{
		dp[i]=make([]int,cols+1)
	}
	for i:=0;i<=rows;i++{
		dp[i][0]=i
	}
	for i:=0;i<=cols;i++{
		dp[0][i]=i
	}
	for i:=1;i<=rows;i++{
		for j:=1;j<=cols;j++{
			if(a[i-1]==b[j-1]){
				dp[i][j]=dp[i-1][j-1]
			}else{
				dp[i][j] = 1 + min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
			}
		}
	}

	return dp[rows][cols]
}
func min(a,b,c int) int{
	if(a<=b && a<=c){
		return a
	}
	if(b<=a && b<=c){
		return b
	}else{
		return c
	}
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

/*
URL:
https://codeforces.com/contest/1360/problem/F
*/

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
	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 (
	t    int
	n, m int
	S    [][]rune
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, m = ReadInt2()
		S = [][]rune{}
		for j := 0; j < n; j++ {
			S = append(S, ReadRuneSlice())
		}

		solve(n, m, S)
	}
}

func solve(n, m int, S [][]rune) {
	// 1番目の文字列のi番目のみをすべての文字に変えて試す
	for i := 0; i < m; i++ {
		for r := 'a'; r <= 'z'; r++ {
			str := make([]rune, len(S[0]))
			copy(str, S[0])

			str[i] = r

			if check(str, S) {
				fmt.Println(string(str))
				return
			}
		}
	}
	fmt.Println(-1)
}

func check(target []rune, S [][]rune) bool {
	for i := 0; i < n; i++ {
		if diff(target, S[i]) >= 2 {
			return false
		}
	}
	return true
}

func diff(s, t []rune) int {
	res := 0
	for i := 0; i < m; i++ {
		if s[i] != t[i] {
			res++
			if res >= 2 {
				return res
			}
		}
	}
	return res
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t, n, m int
o:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a := make([][]byte, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		ans := make([]byte, m)
		copy(ans, a[0])
		for i, b := range a[0] {
		o2:
			for c := byte('a'); c <= 'z'; c++ {
				ans[i] = c
				for _, s := range a {
					d := 0
					for j, b := range s {
						if ans[j] != b {
							d++
							if d > 1 {
								continue o2
							}
						}
					}
				}
				Fprintln(out, string(ans))
				continue o
			}
			ans[i] = b
		}
		Fprintln(out, -1)
	}
}

package main

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

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

	ini := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	ins := func() string {
		sc.Scan()
		return sc.Text()
	}

	for t := ini(); t > 0; t-- {
		n, m := ini(), ini()

		a := make([]string, n)
		s := make([]byte, m)

		for i := range a {
			a[i] = ins()
		}

		b := make([]int, n)

		var i int64
		for i = 0; i < m; i++ {
			// fmt.Println(b)
			m := make(map[byte]int)
			var l byte

			for j := range a {
				if b[j] == 0 {
					m[a[j][i]]++
				} else {
					m[a[j][i]] += 2
				}
			}

			mx := -1
			for k, v := range m {
				if v > mx {
					l = k
					mx = v
				}
			}

			s[i] = l

			for j := range b {
				if a[j][i] != l {
					b[j]++
				}
			}

			// fmt.Println(b)
		}

		ok := true

		for i := range b {
			if b[i] > 1 {
				ok = false
				break
			}
		}

		if !ok {
			fmt.Println(-1)
			// fmt.Println(string(s))
			continue
		}

		fmt.Println(string(s))
	}
}

package main

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

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

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

	ins := func() string {
		sc.Scan()
		return sc.Text()
	}

	for t := ini(); t > 0; t-- {
		n, m := ini(), ini()

		a := make([]string, n)
		s := make([]byte, m)

		for i := range a {
			a[i] = ins()
		}

		b := make([]int, n)

		for i := 0; i < m; i++ {
			// fmt.Println(b)
			m := make(map[byte]int)
			var l byte

			for j := range a {
				if b[j] == 0 {
					m[a[j][i]]++
				} else {
					m[a[j][i]] += 2
				}
			}

			mx := -1
			for k, v := range m {
				if v > mx {
					l = k
					mx = v
				}
			}

			s[i] = l

			for j := range b {
				if a[j][i] != l {
					b[j]++
				}
			}

			// fmt.Println(b)
		}

		ok := true

		for i := range b {
			if b[i] > 1 {
				ok = false
				break
			}
		}

		if !ok {
			fmt.Println(-1)
			// fmt.Println(string(s))
			continue
		}

		fmt.Println(string(s))
	}
}

package main

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

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

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

	ins := func() string {
		sc.Scan()
		return sc.Text()
	}

	for t := ini(); t > 0; t-- {
		n, m := ini(), ini()

		a := make([]string, n)
		s := make([]byte, m)

		for i := range a {
			a[i] = ins()
		}

		b := make([]int, n)

		for i := 0; i < m; i++ {
			m := make(map[byte]int)
			found := false
			var l byte

			for j := range a {
				if b[j] == 0 {
					m[a[j][i]]++
				} else {
					found = true
					l = a[j][i]
					break
				}
			}

			if found == false {
				mx := 0
				for k, v := range m {
					if v > mx {
						l = k
						mx = v
					}
				}
			}

			s[i] = l

			for j := range b {
				if a[j][i] != l {
					b[j]++
				}
			}
		}

		ok := true

		for i := range b {
			if b[i] > 1 {
				ok = false
				break
			}
		}

		if !ok {
			fmt.Println(-1)
			continue
		}

		fmt.Println(string(s))
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
  	"strings"
  	"strconv"
  	"math"

)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n,m:= reader.NextInt(),reader.NextInt()
		aa := make([][]rune, n)
		for i:=range aa {
			aa[i]=make([]rune,m)
		}
		for i := 0; i <n; i++ {
			s:=reader.NextString()
			aa[i]=[]rune(s)
		}
		sol(aa)
	}
}

func sol(aa [][]rune) {
	ans:=aa[0]
	// fmt.Println(ans)
	if(isValid(ans,aa)){
		writer.Printf("%s \n",string(ans))
		return
	}else{
		for i:=0;i<len(aa[0]);i++ {
			prevLetter:=ans[i]
			for c:='a';c<='z';c++ {
				ans[i]=c
				if(isValid(ans,aa)){
					writer.Printf("%s\n",string(ans))
					return
				}
			}
			ans[i]=prevLetter
		}
	}
	writer.Printf("-1 \n")

}

func isValid(benchmark []rune, aa [][]rune) bool{
	for i:=1;i<len(aa);i++ {
		cnt:=0
		for j:=0;j<len(aa[0]);j++ {
			if(benchmark[j]!=aa[i][j]){
				cnt++
			}
		}
		if(cnt>1){
			return false
		}
	}
	return true
}

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

func LevenshteinDistance(a, b string) int {
	// Write your code here.
	rows:=len(a)
	cols:=len(b)
	dp:=make([][]int,rows+1)
	for i:=range dp{
		dp[i]=make([]int,cols+1)
	}
	for i:=0;i<=rows;i++{
		dp[i][0]=i
	}
	for i:=0;i<=cols;i++{
		dp[0][i]=i
	}
	for i:=1;i<=rows;i++{
		for j:=1;j<=cols;j++{
			if(a[i-1]==b[j-1]){
				dp[i][j]=dp[i-1][j-1]
			}else{
				dp[i][j] = 1 + min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
			}
		}
	}

	return dp[rows][cols]
}
func min(a,b,c int) int{
	if(a<=b && a<=c){
		return a
	}
	if(b<=a && b<=c){
		return b
	}else{
		return c
	}
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
  	"strings"
  	"strconv"
  	"math"

)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n,m:= reader.NextInt(),reader.NextInt()
		aa := make([][]rune, n)
		for i:=range aa {
			aa[i]=make([]rune,m)
		}
		for i := 0; i <n; i++ {
			s:=reader.NextString()
			aa[i]=[]rune(s)
		}
		sol(aa)
	}
}

func sol(aa [][]rune) {
	ans:=aa[0]
	// fmt.Println(ans)
	if(isValid(ans,aa)){
		writer.Printf("%s \n",string(ans))
		return
	}else{
		for i:=0;i<len(aa[0]);i++ {
			prevLetter:=ans[i]
			for c:='a';c<='z';c++ {
				ans[i]=c
				if(isValid(ans,aa)){
					writer.Printf("%s\n",string(ans))
					return
				}
			}
			ans[i]=prevLetter
		}
	}
	writer.Printf("-1\n")

}

func isValid(benchmark []rune, aa [][]rune) bool{
	for i:=1;i<len(aa);i++ {
		cnt:=0
		for j:=0;j<len(aa[0]);j++ {
			if(benchmark[j]!=aa[i][j]){
				cnt++
			}
		}
		if(cnt>1){
			return false
		}
	}
	return true
}

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

func LevenshteinDistance(a, b string) int {
	// Write your code here.
	rows:=len(a)
	cols:=len(b)
	dp:=make([][]int,rows+1)
	for i:=range dp{
		dp[i]=make([]int,cols+1)
	}
	for i:=0;i<=rows;i++{
		dp[i][0]=i
	}
	for i:=0;i<=cols;i++{
		dp[0][i]=i
	}
	for i:=1;i<=rows;i++{
		for j:=1;j<=cols;j++{
			if(a[i-1]==b[j-1]){
				dp[i][j]=dp[i-1][j-1]
			}else{
				dp[i][j] = 1 + min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
			}
		}
	}

	return dp[rows][cols]
}
func min(a,b,c int) int{
	if(a<=b && a<=c){
		return a
	}
	if(b<=a && b<=c){
		return b
	}else{
		return c
	}
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
  	"strings"
  	"strconv"
  	"math"

)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n,m:= reader.NextInt(),reader.NextInt()
		aa := make([][]rune, n)
		for i:=range aa {
			aa[i]=make([]rune,m)
		}
		for i := 0; i <n; i++ {
			s:=reader.NextString()
			aa[i]=[]rune(s)
		}
		sol(aa)
	}
}

func sol(aa [][]rune) {
	ans:=aa[0]
	// fmt.Println(ans)
	if(isValid(ans,aa)){
		writer.Printf("%s \n",string(ans))
		return
	}else{
		for i:=0;i<len(aa[0]);i++ {
			prevLetter:=ans[i]
			for c:='a';c<='z';c++ {
				ans[i]=c
				if(isValid(ans,aa)){
					writer.Printf("%s \n",string(ans))
					return
				}
			}
			ans[i]=prevLetter
		}
	}
	writer.Printf("-1 \n")

}

func isValid(benchmark []rune, aa [][]rune) bool{
	for i:=1;i<len(aa);i++ {
		cnt:=0
		for j:=0;j<len(aa[0]);j++ {
			if(benchmark[j]!=aa[i][j]){
				cnt++
			}
		}
		if(cnt>1){
			return false
		}
	}
	return true
}

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

func LevenshteinDistance(a, b string) int {
	// Write your code here.
	rows:=len(a)
	cols:=len(b)
	dp:=make([][]int,rows+1)
	for i:=range dp{
		dp[i]=make([]int,cols+1)
	}
	for i:=0;i<=rows;i++{
		dp[i][0]=i
	}
	for i:=0;i<=cols;i++{
		dp[0][i]=i
	}
	for i:=1;i<=rows;i++{
		for j:=1;j<=cols;j++{
			if(a[i-1]==b[j-1]){
				dp[i][j]=dp[i-1][j-1]
			}else{
				dp[i][j] = 1 + min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
			}
		}
	}

	return dp[rows][cols]
}
func min(a,b,c int) int{
	if(a<=b && a<=c){
		return a
	}
	if(b<=a && b<=c){
		return b
	}else{
		return c
	}
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		p int
		b byte
	}

	var t, n, m int
o:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a := make([][]byte, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		ans := make([]byte, m)

		if n == 2 {
			cnt:=0
			for i := 0; i < m; i++ {
				if a[0][i] == a[1][i] {
					ans[i] = a[0][i]
				} else {
					if cnt ==2 {
						Fprintln(out, -1)
						continue o
					}
					ans[i] = a[cnt][i]
					cnt++
				}
			}
			Fprintln(out, string(ans))
			continue
		}

		//if m == 1 {
		//	Fprintln(out, "a")
		//	continue
		//}
		pos3 := []int{}
		pos2 := []pair{}
		for j := 0; j < m; j++ {
			cntMp := map[byte]int{}
			for _, s := range a {
				cntMp[s[j]]++
			}
			if len(cntMp) > 2 {
				pos3 = append(pos3, j)
			} else if len(cntMp) == 2 {
				bs := make([]byte, 0, len(cntMp))
				for k := range cntMp {
					bs = append(bs, k)
				}
				if cntMp[bs[0]] == 1 {
					pos2 = append(pos2, pair{j, bs[1]})
				} else if cntMp[bs[1]] == 1 {
					pos2 = append(pos2, pair{j, bs[0]})
				} else {
					Fprintln(out, -1)
					continue o
				}
			} else {
				for b := range cntMp {
					ans[j] = b
				}
			}
		}
		if len(pos3) > 1 {
			Fprintln(out, -1)
			continue
		} else if len(pos3) == 1 {
			if len(pos2) > 0 {
				Fprintln(out, -1)
				continue
			}
			ans[pos3[0]] = 'z'
		} else {
			for _, p := range pos2 {
				ans[p.p] = p.b
			}
		}
		for _, s := range a {
			cnt:=0
			for j, b := range s {
				if ans[j] != b{
					cnt++
					if cnt > 1 {
						Fprintln(out, -1)
						continue o
					}
				}
			}
		}
		Fprintln(out, string(ans))
	}
}

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

package main

import (
	"fmt"
)

func is_good(txt []rune, G [][]rune, n int, m int) bool {
	for i := 0; i < n; i++ {
		dist := 0
		for j := 0; j < m; j++ {
			if txt[j] != G[i][j] {
				dist++
			}
			if dist > 1 {
				return false
			}
		}
	}
	return true
}

func main(){
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Scan(&n, &m)
		G := make([][]rune, n)
		for i := 0; i < n; i++ {
			var s string
			fmt.Scan(&s)
			G[i] = []rune(s)
		}
		ans := "-1"
		found := false
		for i := 0; i < n && !found; i++ {
			cand := make([]rune, m)
			copy(cand, G[i])
			for k := 0; k < m && !found; k++ {
				for c := 'a'; c <= 'z'; c++ {
					cand[k] = c
					if is_good(cand, G, n, m) {
						ans = string(cand)
						found = true
						break
					}
				}
			}
		}
		fmt.Println(ans)
	}

}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	Int64() int64
	UInt64() uint64
	String() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) Int64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) String() string {
	impl.Scan()
	return impl.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
}

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		n := reader.Int64()
		m := reader.Int64()
		a := make([]string, n)
		for i := range a {
			a[i] = reader.String()
		}
		writer.Printf("%s\n", sol(a, m))
	}
}

func sol(a []string, m int64) string {
	first := []byte(a[0])
	for i := range first {
		curr := first[i]
		for j := byte(0); j < 27; j++ {
			nxtPos := curr + j
			if nxtPos > 'z' {
				nxtPos -= 26
			}
			first[i] = nxtPos
			if isValid(string(first), a[1:]) {
				return string(first)
			}
		}
	}
	return "-1"
}

func isValid(res string, rem []string) bool {
	diff := false
	for i := range res {
		for _, s := range rem {
			if res[i] != s[i] {
				if diff {
					return false
				} else {
					diff = true
					break
				}
			}
		}
	}
	return true
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	Int64() int64
	UInt64() uint64
	String() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) Int64() int64 {
	impl.Scan()
	bb := impl.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 (impl *wordScannerImpl) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) String() string {
	impl.Scan()
	return impl.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
}

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		n := reader.Int64()
		m := reader.Int64()
		a := make([]string, n)
		for i := range a {
			a[i] = reader.String()
		}
		writer.Printf("%s\n", sol(a, m))
	}
}

func sol(a []string, m int64) string {
	first := []byte(a[0])
	for i := range first {
		curr := first[i]
		for j := byte(0); j < 26; j++ {
			nxtPos := curr + j
			if nxtPos > 'z' {
				nxtPos -= 26
			}
			first[i] = nxtPos
			if isValid(string(first), a[1:]) {
				return string(first)
			}
		}
	}
	return "-1"
}

func isValid(res string, rem []string) bool {
	diff := false
	for i := range res {
		for _, s := range rem {
			if res[i] != s[i] {
				if diff {
					return false
				} else {
					diff = true
				}
			}
		}
	}
	return true
}


package main

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

// github.com/EndlessCheng/codeforces-go
func CF982C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, w, ans int
	Fscan(in, &n)
	if n&1 == 1 {
		Fprint(_w, -1)
		return
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	var f func(int, int) int
	f = func(v, fa int) int {
		sz := 1
		for _, w := range g[v] {
			if w != fa {
				sz += f(w, v)
			}
		}
		if fa != -1 && sz&1 == 0 {
			ans++
		}
		return sz
	}
	f(0, -1)
	Fprint(_w, ans)
}

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

package main

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

type edge struct {
	v, e int
}

const nmax int = 100000
const mmax int = 200000

var edges [2*mmax + 1]edge
var adj [nmax + 1]int
var visited [nmax + 1]bool
var res int

func dfs(s int) int {
	nfii := 0

	for i := adj[s]; i != 0; i = edges[i].e {
		if !visited[edges[i].v] {
			nfii++
			visited[edges[i].v] = true
			nfii -= dfs(edges[i].v)
		}
	}

	if nfii&1 == 1 {
		res++
		return 1
	}

	return 0
}

func main() {
	n := 0

	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &n)

	if n&1 == 1 {
		fmt.Print("-1")
		return
	}

	for i := 1; i < n; i++ {
		var v, u int
		fmt.Fscan(in, &v)
		fmt.Fscan(in, &u)

		edges[2*i].v = u
		edges[2*i].e = adj[v]
		adj[v] = 2 * i

		edges[2*i+1].v = v
		edges[2*i+1].e = adj[u]
		adj[u] = 2*i + 1
	}

	visited[1] = true
	dfs(1)

	fmt.Print(res - 1)
}

package main

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

var n int
var tree [][]int
var used []int
var ans int

func dfs(v, p int) int {
	cnt := 0
	for _, next := range tree[v] {
		if next != p {
			cnt += dfs(next, v)
		}
	}
	cnt++
	used[v] = cnt
	for _, next := range tree[v] {
		if next != p && used[next] % 2 == 0 {
			ans++
		}
	}
	return cnt
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	if n % 2 == 1 {
		fmt.Println(-1)
		return
	}
	tree = make([][]int, n)
	used = make([]int, n)
	for i := 0; i < n - 1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		tree[v1-1] = append(tree[v1-1], v2-1)
		tree[v2-1] = append(tree[v2-1], v1-1)
	}
	dfs(0, -1)
	fmt.Println(ans)
}

package main

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

var n int
var m [][]int
var used []int
var ans int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	if n % 2 == 1 {
		fmt.Println(-1)
		return
	}
	m = make([][]int, n)
	used = make([]int, n)
	for i := 0; i < n - 1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		m[v1-1] = append(m[v1-1], v2-1)
		m[v2-1] = append(m[v2-1], v1-1)
	}
	getMaxEdges(0, -1)
	fmt.Println(ans)
	return
}

func getMaxEdges(x, y int) int {
	cnt := 0
	for _, v := range m[x] {
		if v != y {
			cnt += getMaxEdges(v, x)
		}
	}
	cnt++
	used[x] = cnt

	for _, v := range m[x] {
		if v != y && used[v]%2 == 0 {
			ans++
		}
	}

	return cnt
}


package main

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

type caseInput struct {
	nums []int16
}

func readCaseInput(scanner *bufio.Scanner) caseInput {
	size := readInt(scanner)
	nums := make([]int16, size)
	for i := range nums {
		nums[i] = int16(readInt(scanner))
	}
	in := caseInput{nums}
	return in
}

type caseOutput struct {
	equat int
}

func writeCaseOutput(writer *bufio.Writer, out caseOutput) {
	writef(writer, "%d\n", out.equat)
}

func solveCase(in caseInput) caseOutput {
	sum := 0
	for _, v := range in.nums {
		sum += int(v)
	}
	sum2 := int(0)
	for i, v := range in.nums {
		sum2 += int(v)
		if sum2 * 2 >= sum {
			return caseOutput{i + 1}
		}
	}
	return caseOutput{len(in.nums)}
}

//	everything below is reusable boilerplate
func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	writeCaseOutput(writer, solveCase(readCaseInput(scanner)))
}

func main() {
	var scanner *bufio.Scanner
	if len(os.Args) > 1 {
		reader, err := os.Open(os.Args[1])
		if err != nil {
			panic(err)
		}
		defer reader.Close()
		scanner = bufio.NewScanner(reader)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}
	scanner.Split(bufio.ScanWords)

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

	solveSequential(scanner, writer)
}

func readInt64(sc *bufio.Scanner) int64 {
	sc.Scan()
	res, err := strconv.ParseInt(sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func readInt(sc *bufio.Scanner) int {
	return int(readInt64(sc))
}

func writef(writer *bufio.Writer, formatStr string, values ...interface{}) {
	out := fmt.Sprintf(formatStr, values...)
	_, err := writer.WriteString(out)
	if err != nil {
		panic(err)
	}
}

package main

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

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

	var n, sum int
	fmt.Fscanf(f, "%d\n", &n)

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

	var prefixSum int
	for i := 0; i < n; i++ {
		prefixSum += problems[i]
		if prefixSum >= (sum+1)/2 {
			fmt.Println(i + 1)
			return
		}
	}
}

package main

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

func binSearch(arr []int, x int) int {
	var mid, lo, hi = 0, 0, len(arr) - 1

	for hi - lo > 1 {
		mid = (lo + hi) / 2
		switch {
			case x < arr[mid]: hi = mid
			case x > arr[mid]: lo = mid
			default: return mid
		}
	}

	if arr[hi] >= x {
		return hi
	} else {
		return lo
	}
}

func main() {
	var daysN, celebDay, halfDay int

	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d\n", &daysN)

	days := make([]int, daysN)

	celebDay = 0
	fmt.Fscanf(reader, "%d", &days[0])
	for i := 1; i < daysN; i++ {
		fmt.Fscanf(reader, "%d", &days[i])
		days[i] += days[i - 1]
		halfDay = int(math.Ceil(float64(days[i]) / 2))
		if days[celebDay] < halfDay {
			celebDay += binSearch(days[celebDay:i + 1], halfDay)
		}
	}

	fmt.Println(celebDay + 1)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func nextInt() int {
	in.Scan()
	r := 0
	for _, c := range in.Bytes() {
		r *= 10
		r += int(c - '0')
	}
	return r
}

func main() {
	in.Split(bufio.ScanWords)
	n, s := nextInt(), 0
	a := make([]int, n)
	for i := range a {
		a[i] = nextInt()
		s += a[i]
	}
	t := (s + 1) / 2
	for i, v := range a {
		t -= v
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

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

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

	var n, sum int
	fmt.Fscanf(f, "%d\n", &n)

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

	var prefixSum int
	for i := 0; i < n; i++ {
		prefixSum += problems[i]
		if prefixSum >= sum/2 {
			fmt.Println(i + 1)
			return
		}
	}
}

package main

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

func binSearch(arr []int, x int) int {
	var mid, lo, hi = 0, 0, len(arr) - 1

	for hi - lo > 1 {
		mid = (lo + hi) / 2
		switch {
			case x < arr[mid]: hi = mid
			case x > arr[mid]: lo = mid
			default: return mid
		}
	}

	if arr[hi] >= x {
		return hi
	} else {
		return lo
	}
}

func main() {
	var daysN, celebDay int

	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d\n", &daysN)

	days := make([]int, daysN)

	celebDay = 0
	fmt.Fscanf(reader, "%d ", &days[0])
	for i := 1; i < daysN; i++ {
		fmt.Fscanf(reader, "%d ", &days[i])
		days[i] += days[i - 1]
		if days[celebDay] < days[i] / 2 {
			celebDay += binSearch(days[celebDay:i + 1], days[i] / 2)
		}
	}

	fmt.Println(celebDay + 1)
}


package main

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

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

type entry struct {
	a, b int
}

type es []entry

var reme = [][2]int{}

func (e es) Less(i, j int) bool {
	if e[i].a == e[j].a {
		return e[i].b < e[j].b
	} else {
		return e[i].a < e[j].a
	}
}

func (e es) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
	reme = append(reme, [2]int{i, j})
}

func (e es) Len() int {
	return len(e)
}

func slove() {
	defer func() {
		reme = make([][2]int, 0)
	}()
	var n int
	fmt.Fscan(r, &n)
	nums1 := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums1[i])
	}
	nums2 := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums2[i])
	}
	e := make(es, n)
	for i := range e {
		e[i].a = nums1[i]
		e[i].b = nums2[i]
	}
	sort.Sort(e)
	for i:=1;i<n;i++ {
		if e[i].b < e[i-1].b {
			fmt.Fprintln(w, "-1")
			return
		}
	}
	fmt.Fprintln(w, len(reme))
	for i := range reme {
		fmt.Fprintln(w, reme[i][0]+1, reme[i][1]+1)
	}
}

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

package main

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

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

func CF1691C(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(r, &n)
		a, b := make([]int64, n), make([]int64, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(r, &a[i])
		}
		for i := int64(0); i < n; i++ {
			fmt.Fscan(r, &b[i])
		}

		ans, ok := CF1691CS(a, b, n)
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, len(ans))
			for _, v := range ans {
				fmt.Fprintln(w, v[0]+1, v[1]+1)
			}
		}

	}
}

func CF1691CS(a, b []int64, n int64) ([][]int64, bool) {
	ans := make([][]int64, 0)

	for i := int64(0); i < n; i++ {
		for j := i + 1; j < n; j++ {
			if a[i] >= a[j] && b[i] >= b[j] {
				a[i], a[j] = a[j], a[i]
				b[i], b[j] = b[j], b[i]
				ans = append(ans, []int64{i, j})
			} else if a[i] <= a[j] && b[i] <= b[j] {
				continue
			} else {
				return nil, false
			}
		}
	}

	return ans, true
}

package main

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

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

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

	var T int
	var n int
	var x int

loop:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a, b := []int{}, []int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a = append(a, x)
		}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			b = append(b, x)
		}

		ha, hb := map[int]bool{}, map[int]bool{}
		for _, v := range a {
			ha[v] = true
		}
		for _, v := range b {
			hb[v] = true
		}

		ra, rb := map[int]int{}, map[int]int{}
		d := 0
		for i := 1; i <= n; i++ {
			if ha[i] {
				d++
				ra[i] = d
			}
		}
		d = 0
		for i := 1; i <= n; i++ {
			if hb[i] {
				d++
				rb[i] = d
			}
		}

		ar, br := []int{}, []int{}
		for _, v := range a {
			ar = append(ar, ra[v])
		}
		for _, v := range b {
			br = append(br, rb[v])
		}

		do := [][]int{}
		for i, v := range ar {
			do = append(do, []int{v, br[i], i})
		}

		sort.Slice(do, func(i, j int) bool {
			return do[i][0] < do[j][0] || do[i][0] == do[j][0] && do[i][1] < do[j][1]
		})

		for i := 1; i < n; i++ {
			if do[i][1] < do[i-1][1] {
				Fprintln(out, -1)
				continue loop
			}
		}
		//Fprintln(out, do)

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

		t := [][]int{}
		for i, v := range do {
			if o[i] != v[2] {
				j := p[v[2]]
				p[o[i]], p[v[2]] = j, i
				o[j], o[i] = o[i], o[j]
				t = append(t, []int{i, j})
			}
			//Fprintln(out, i, o)
		}

		Fprintln(out, len(t))
		for _, v := range t {
			Fprintln(out, v[0]+1, v[1]+1)
		}

		//// chk
		//Fprintln(out, a)
		//Fprintln(out, b)
		//for _, v := range t {
		//	a[v[0]], a[v[1]] = a[v[1]], a[v[0]]
		//	b[v[0]], b[v[1]] = b[v[1]], b[v[0]]
		//}
		//Fprintln(out, a)
		//Fprintln(out, b)
	}
}

package main

import (
	"bufio"
	"container/heap"
	"container/list"
	. "fmt"
	"math"
	"os"
	"sort"
)

type Int int64

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

type pair struct {
	a, b  int
	index int
}

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		b := make([]int, n)

		p := make([]int, n)
		ind := make([]int, n)

		f := make([]pair, n)

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

		for i := 0; i < n; i++ {
			Fscan(in, &b[i])
			f[i] = pair{a[i], b[i], i}
		}

		sort.SliceStable(f, func(i, j int) bool {
			if f[i].a == f[j].a {
				return f[i].b < f[j].b
			}
			return f[i].a < f[j].a
		})

		flag := true
		for i := 1; i < n; i++ {
			if f[i-1].a > f[i].a || f[i-1].b > f[i].b {
				Fprintln(out, -1)
				flag = false
				break
			}
		}

		if flag {
			res := list.New()
			for i := 0; i < n; i++ {
				if i != p[f[i].index] {
					res.PushBack(pair{i, p[f[i].index], -1})

					ind[i], ind[p[f[i].index]] = ind[p[f[i].index]], ind[i]

					for j := 0; j < n; j++ {
						p[ind[j]] = j
					}
				}
			}

			Fprintln(out, res.Len())
			for it := res.Front(); it != nil; it = it.Next() {
				pa := it.Value.(pair)
				Fprintln(out, pa.a+1, pa.b+1)
			}
		}

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

type Void struct{}

var void Void

type HashSet struct {
	ma map[Int]Void
}

func NewHashSet() *HashSet {
	return &HashSet{
		ma: make(map[Int]Void),
	}
}

func (s *HashSet) add(x Int) bool {
	_, exists := s.ma[x]
	if !exists {
		s.ma[x] = void
		return true
	} else {
		return false
	}
}

func (s *HashSet) del(x Int) {
	delete(s.ma, x)
}

func (s *HashSet) exist(x Int) bool {
	_, exists := s.ma[x]
	return exists
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    Int // The value of the item; arbitrary.
	priority Int // The priority of the item in the queue.
	indices  Int
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func NewItem(value, priority Int) *Item {
	return &Item{
		value:    value,
		priority: priority,
		index:    -1,
	}

}

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

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

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

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Int, priority Int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

type SegTree struct {
	st []Segment
}

type Segment struct {
	l, r, val, add, minIndex Int
}

func newSegTree(n Int) SegTree {
	segTree := SegTree{make([]Segment, n<<2)}
	segTree.Build(1, 1, n)
	return segTree
}

func (segTree SegTree) Build(x, l, r Int) {
	segTree.st[x].l = l
	segTree.st[x].r = r
	if l == r {
		segTree.st[x].minIndex = l
		return
	}
	mid := (l + r) >> 1
	segTree.Build(x<<1, l, mid)
	segTree.Build(x<<1|1, mid+1, r)
}

func (segTree SegTree) push(x Int) {
	if segTree.st[x].add != 0 {
		segTree.modifyOne(x<<1, segTree.st[x].add)
		segTree.modifyOne(x<<1|1, segTree.st[x].add)
		segTree.st[x].add = 0
	}
}

func (segTree SegTree) update(x Int) {
	if segTree.st[x<<1].val < segTree.st[x<<1|1].val {
		segTree.st[x].val = segTree.st[x<<1].val
		segTree.st[x].minIndex = segTree.st[x<<1].minIndex
	} else {
		segTree.st[x].val = segTree.st[x<<1|1].val
		segTree.st[x].minIndex = segTree.st[x<<1|1].minIndex
	}
}

func (segTree SegTree) modifyOne(x, add Int) {
	segTree.st[x].val += add
	if segTree.st[x].l != segTree.st[x].r {
		segTree.st[x].add += add
	}
}

func (segTree SegTree) Modify(x, l, r, add Int) {
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		segTree.modifyOne(x, add)
		return
	}
	segTree.push(x)
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		segTree.Modify(x<<1, l, r, add)
	}
	if mid < r {
		segTree.Modify(x<<1|1, l, r, add)
	}
	segTree.update(x)
}

func updateRes(cRes, cIndex, res, index Int) (Int, Int) {
	if cRes > res {
		return res, index
	}
	return cRes, cIndex
}

func (segTree SegTree) Query(x, l, r Int) (Int, Int) {
	res := Int(math.MaxInt64)
	resInd := -L1
	if segTree.st[x].l >= l && segTree.st[x].r <= r {
		res, resInd = updateRes(res, resInd, segTree.st[x].val, segTree.st[x].minIndex)
		return res, resInd
	}
	mid := (segTree.st[x].l + segTree.st[x].r) >> 1
	if mid >= l {
		sonRes, sonInd := segTree.Query(x<<1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	if mid < r {
		sonRes, sonInd := segTree.Query(x<<1|1, l, r)
		res, resInd = updateRes(res, resInd, sonRes, sonInd)
	}
	return res, resInd
}

type Graph struct {
	nodeSize Int
	edges    []Edge
	head     []Int
	dis      []Int
	vis      []bool
	nCnt     Int
	ans      []Int
}

type Edge struct {
	u, v, c, indices, next Int
}

func NewGraph(nodeSize Int) *Graph {
	return &Graph{
		nodeSize: nodeSize,
		edges:    make([]Edge, (2e5+10)<<2),
		head:     make([]Int, nodeSize+100),
		dis:      make([]Int, nodeSize+100),
		vis:      make([]bool, nodeSize+100),
		nCnt:     L1,
		ans:      make([]Int, nodeSize),
	}
}

func (g *Graph) addEdge(u Int, v Int, cost Int, indices Int) {
	g.nCnt++
	g.edges[g.nCnt] = Edge{u, v, cost, indices, g.head[u]}
	g.head[u] = g.nCnt
}

func (g *Graph) Dijkstra(begin Int) {
	for i := L1; i <= g.nodeSize; i++ {
		g.dis[i] = math.MaxInt64
	}
	pq := make(PriorityQueue, 0)
	heap.Init(&pq)

	g.dis[begin] = 0
	heap.Push(&pq, &Item{begin, 0, -1, -1})

	id := L0

	for len(pq) > 0 {
		it := heap.Pop(&pq).(*Item)
		u := it.value
		if g.vis[u] {
			continue
		}
		g.vis[u] = true

		if it.indices != -1 && id < g.nodeSize-1 {
			g.ans[id] = it.indices
			id++
		}

		for i := g.head[u]; i != 0; i = g.edges[i].next {
			v := g.edges[i].v
			if g.dis[v] > g.dis[u]+g.edges[i].c {
				g.dis[v] = g.dis[u] + g.edges[i].c

				heap.Push(&pq, &Item{v, g.dis[v], g.edges[i].indices, -1})
			}
		}
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

type pair struct {
	first, second int
}

var ans []pair

type IntSlice []int

func (x IntSlice) Len() int           { return len(x) }
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
func (x IntSlice) Swap(i, j int) {
	x[i], x[j] = x[j], x[i]
	ans = append(ans, pair{first: i, second: j})
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	for t := io.ReadInt(); t > 0; t-- {
		ans = make([]pair, 0)

		n := io.ReadInt()
		a := io.ReadIntSlice(n)
		b := io.ReadIntSlice(n)

		c := make([]int, len(a))
		copy(c, a)

		sort.Stable(IntSlice(c))

		copy(c, b)
		for i := range ans {
			b[ans[i].first], b[ans[i].second] = b[ans[i].second], b[ans[i].first]
		}

		if sort.IntsAreSorted(b) {
			fmt.Println(len(ans))
			for i := 0; i < len(ans); i++ {
				fmt.Println(ans[i].first+1, ans[i].second+1)
			}
			continue
		}

		copy(c, b)

		sort.Stable(IntSlice(c))

		copy(c, a)
		for i := range ans {
			a[ans[i].first], a[ans[i].second] = a[ans[i].second], a[ans[i].first]
		}

		if sort.IntsAreSorted(a) {
			fmt.Println(len(ans))
			for i := 0; i < len(ans); i++ {
				fmt.Println(ans[i].first+1, ans[i].second+1)
			}
			continue
		}

		fmt.Println(-1)
	}
}

package main

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

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}

func solve(length int, array_a, array_b []int) int {
	return sort2(length, array_a, array_b)
}

func sort2(length int, a, b []int) int {
	moves := make([]Move, 0)
	for i := 0; i < length; i++ {
		for j := i + 1; j < length; j++ {

			if (a[i] > a[j] && b[i] >= b[j]) || (b[i] > b[j] && a[i] >= a[j]) {
				moves = append(moves, Move{
					i + 1, j + 1,
				})
				tmp1 := a[i]
				a[i] = a[j]
				a[j] = tmp1
				tmp1 = b[i]
				b[i] = b[j]
				b[j] = tmp1
			}
		}
	}
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] || b[i] > b[i+1] {
			fmt.Println(-1)
			return -1
		}
	}
	fmt.Println(len(moves))
	for _, m := range moves {
		fmt.Println(m)
	}
	return 0
}
func main() {
	var tc int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	tc, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < tc; i++ {
		var length int
		scanner.Scan()
		length, _ = strconv.Atoi(scanner.Text())
		a1 := make([]int, length)
		a2 := make([]int, length)
		scanner.Scan()
		la1 := strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a1[i1], _ = strconv.Atoi(v)
		}
		scanner.Scan()
		la1 = strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a2[i1], _ = strconv.Atoi(v)
		}
		solve(length, a1, a2)

	}

}

package main

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

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}

func solve(length int, array_a, array_b []int) int {
	// defer func() {
	// 	if err := recover(); err != nil {
	// 		fmt.Printf("array_a: %v\n", array_a)
	// 		fmt.Printf("array_b: %v\n", array_b)
	// 	}
	// }()
	sorted_a := make([]int, length)
	copy(sorted_a, array_a)
	sorted_b := make([]int, length)
	copy(sorted_b, array_b)
	sort.Ints(sorted_a)
	sort.Ints(sorted_b)
	index_a := make(map[int][]int)
	index_b := make(map[int][]int)
	for i, v := range array_a {
		if _, ok := index_a[v]; ok {
			index_a[v] = append(index_a[v], i)
		} else {
			index_a[v] = make([]int, 0)
			index_a[v] = append(index_a[v], i)
		}
	}
	for i, v := range array_b {
		if _, ok := index_b[v]; ok {
			index_b[v] = append(index_b[v], i)
		} else {
			index_b[v] = make([]int, 0)
			index_b[v] = append(index_b[v], i)
		}
	}
	canSort := false
	for i, v := range sorted_b {
		canSort = false
		for j, k1 := range index_b[v] {
			if sorted_a[i] == array_a[k1] {
				canSort = true
				index_b[v] = append(index_b[v][:j], index_b[v][j+1:]...)
				break

			}
		}
		if !canSort {
			return -1
		}
	}
	return 0
}

func sort2(length int, a, b []int) int {
	var move = 10e4
	moves := make([]Move, 0)
	for move >= 0 {
		if isSort(length, a) && isSort(length, b) {
			break
		}
		canSort := false
		for i := 0; i < length-1; i++ {
			for j := i + 1; j < length; j++ {
				if a[i] >= a[j] && b[i] >= b[j] {
					moves = append(moves, Move{
						i + 1, j + 1,
					})
					tmp1 := a[i]
					a[i] = a[j]
					a[j] = tmp1
					tmp1 = b[i]
					b[i] = b[j]
					b[j] = tmp1
					move--
					canSort = true
				}
			}
		}
		if !canSort {
			return -1
		}
	}
	if move <= 0 {
		return -1
	} else {
		fmt.Println(len(moves))
		for _, m := range moves {
			fmt.Println(m)
		}
		return 0
	}
}
func main() {
	var tc int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	tc, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < tc; i++ {
		var length int
		scanner.Scan()
		length, _ = strconv.Atoi(scanner.Text())
		a1 := make([]int, length)
		a2 := make([]int, length)
		scanner.Scan()
		la1 := strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a1[i1], _ = strconv.Atoi(v)
		}
		scanner.Scan()
		la1 = strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a2[i1], _ = strconv.Atoi(v)
		}
		c := solve(length, a1, a2)
		if c != -1 {
			sort2(length, a1, a2)
		} else {
			fmt.Println(c)
		}
	}

}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	var n int

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

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

		moves := solveC(a, b)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}
		Fprintln(out, -1)

	}
}

type pair struct {
	i, j int
}

func solveC(a, b []int) []pair {
	n := len(a)

	moves := make([]pair, 0)
	count := 0
	for i := 0; i < n; i++ {
		// fmt.Println("===========================")
		// fmt.Println(a)
		// fmt.Println(b)
		smallestIdxA := i
		smallestIdxB := i
		for j := i; j < n; j++ {
			if a[smallestIdxA] > a[j] {
				smallestIdxA = j
			}
			if b[smallestIdxB] > b[j] {
				smallestIdxB = j
			}
		}

		if i != smallestIdxA && b[i] >= b[smallestIdxA] {
			a[i], a[smallestIdxA] = a[smallestIdxA], a[i]
			b[i], b[smallestIdxA] = b[smallestIdxA], b[i]
			moves = append(moves, pair{i + 1, smallestIdxA + 1})
			i--
			count++
			if count > 10000 {
				return nil
			}
			continue
		}
		if i != smallestIdxB && a[i] >= a[smallestIdxB] {
			a[i], a[smallestIdxB] = a[smallestIdxB], a[i]
			b[i], b[smallestIdxB] = b[smallestIdxB], b[i]
			moves = append(moves, pair{i + 1, smallestIdxB + 1})
			i--
			count++
			if count > 10000 {
				return nil
			}
			continue
		}
	}

	if isSorted(a) && isSorted(b) {
		return moves
	}
	return nil
}

func isSorted(nums []int) bool {
	n := len(nums)
	for i := 0; i < n-1; i++ {
		if nums[i] > nums[i+1] {
			return false
		}
	}
	return true
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	var n int

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

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

		ao := make([]int, len(a))
		bo := make([]int, len(b))
		copy(ao, a)
		copy(bo, b)

		moves := solveC(a, b)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}

		moves = solveC(bo, ao)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}
		Fprintln(out, -1)

	}
}

type pair struct {
	i, j int
}

func solveC(a, b []int) []pair {
	n := len(a)

	moves := make([]pair, 0)
	count := 0
	for i := 0; i < n; i++ {
		// fmt.Println("===========================")
		// fmt.Println(a)
		// fmt.Println(b)
		smallestIdxA := i
		smallestIdxB := i
		for j := i; j < n; j++ {
			if a[smallestIdxA] > a[j] {
				smallestIdxA = j
			}
			if b[smallestIdxB] > b[j] {
				smallestIdxB = j
			}
		}

		if i != smallestIdxA && b[i] >= b[smallestIdxA] {
			a[i], a[smallestIdxA] = a[smallestIdxA], a[i]
			b[i], b[smallestIdxA] = b[smallestIdxA], b[i]
			moves = append(moves, pair{i + 1, smallestIdxA + 1})
			i--
			count++
			if count > 10000 {
				return nil
			}
			continue
		}
		if i != smallestIdxB && a[i] >= a[smallestIdxB] {
			a[i], a[smallestIdxB] = a[smallestIdxB], a[i]
			b[i], b[smallestIdxB] = b[smallestIdxB], b[i]
			moves = append(moves, pair{i + 1, smallestIdxB + 1})
			i--
			count++
			if count > 10000 {
				return nil
			}
			continue
		}
	}

	if isSorted(a) && isSorted(b) {
		return moves
	}
	return nil
}

func isSorted(nums []int) bool {
	n := len(nums)
	for i := 0; i < n-1; i++ {
		if nums[i] > nums[i+1] {
			return false
		}
	}
	return true
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var T,N,A,B,mina,minb int

	fmt.Fscan(in, &T)

	for i := 0;i < T;i++{
		fmt.Fscan(in, &N)
		listA := make([]int,N)
		listB := make([]int,N)
		mA := make([]int,N)
		mB := make([]int,N)
		for j := 0;j < N;j++{
			fmt.Fscan(in, &A)
			listA[j] = A
			mA[j] = A
			if mina > A{
				mina = A
			}
		}
		for j := 0;j < N;j++{
			fmt.Fscan(in, &B)
			listB[j] = B
			mB[j] = B
			if minb > B{
				minb = B
			}
		}
		sort.Ints(listA)
		sort.Ints(listB)
		list,judge := sortlist(listA,listB,mA,mB,N)
		if judge {
			ans,ans2 := swapsort(list,N)
			fmt.Println(ans)
			for i := 0;i < ans;i++{
				fmt.Println(ans2[i][0]+1,ans2[i][1]+1)
			}
		}else{
			fmt.Println(-1)
		}

	}
}

func sortlist(listA []int,listB []int,mA []int,mB []int,N int) ([]int, bool){
	list := make([]int,N)
	count := 0
	//judge := false
	for i := 0;i < N;i++ {
		judge := false
		for j := 0;j < N;j++ {
			if listA[i] == mA[j] && listB[i] == mB[j] {
				judge = true
				list[j] = count
				mA[j] = 101
				mB[j] = 101
				count++
				break
			}
		}
		if !judge{
			return list,judge
		}
	}
	return list,true
}

func swapsort(list []int,N int)(int,[][]int){
	listN := make([][]int,N)
	count := 0
	for i := 0;i < N;i++ {
		if list[i] != i{
			for j := 0;j < N;j++ {
				if list[j] == i{
					listN[count] = append(listN[count],i,j)
					list[i], list[j] = list[j], list[i]
					count++
				}
			}
		}
	}
	return count,listN
}

package main

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

type pair struct{ x, y int }

func sort(a []int, delta int) (p []pair) {
	n := len(a)
	for i := 0; i < n; i++ {
		for j := 0; j < n-i-1; j++ {
			if a[j] > a[j+1] {
				p = append(p, pair{j + delta, j + delta + 1})
				a[j], a[j+1] = a[j+1], a[j]
			}
		}
	}
	return p
}

func Solve() {
	n := ri()
	var a, b []int
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	for i := 0; i < n; i++ {
		b = append(b, ri())
	}
	p := sort(a, 0)
	for _, t := range p {
		b[t.x], b[t.y] = b[t.y], b[t.x]
	}
	for i := 0; i < n; i++ {
		j := i
		for j+1 < n && a[i] == a[j+1] {
			j++
		}
		t := sort(b[i:j+1], i)
		p = append(p, t...)
		i = j
	}
	if sort2.IntsAreSorted(b) {
		println(len(p))
		for _, t := range p {
			println(t.x+1, t.y+1)
		}
	} else {
		println(-1)
	}
}

func main() {
	T := 0 // 如果是单组样例，请讲 T := 0 改为 T := 1
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

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

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。注意，这个函数不会换行，多个变量之间有空格
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出。这个函数会换行，多个变量之间有空格
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

package main

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

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}

func solve(length int, array_a, array_b []int) int {
	sorted_a := make([]int, length)
	copy(sorted_a, array_a)
	sorted_b := make([]int, length)
	copy(sorted_b, array_b)
	sort.Ints(sorted_a)
	sort.Ints(sorted_b)
	index_a := make(map[int][]int)
	index_b := make(map[int][]int)
	for i, v := range array_a {
		if _, ok := index_a[v]; ok {
			index_a[v] = append(index_a[v], i)
		} else {
			index_a[v] = make([]int, 0)
			index_a[v] = append(index_a[v], i)
		}
	}
	for i, v := range array_b {
		if _, ok := index_b[v]; ok {
			index_b[v] = append(index_b[v], i)
		} else {
			index_b[v] = make([]int, 0)
			index_b[v] = append(index_b[v], i)
		}
	}
	canSort := false
	for i, v := range sorted_b {
		for _, k1 := range index_b[v] {
			if sorted_a[i] == array_a[k1] {
				canSort = true
				delete(index_b, v)

			}
		}
		if !canSort {
			return -1
		}
	}
	return 0
}

func sort2(length int, a, b []int) int {
	var move = 10e4
	moves := make([]Move, 0)
	for move >= 0 {
		if isSort(length, a) && isSort(length, b) {
			break
		}
		canSort := false
		for i := 0; i < length-1; i++ {
			for j := i + 1; j < length; j++ {
				if a[i] >= a[j] && b[i] >= b[j] {
					moves = append(moves, Move{
						i + 1, j + 1,
					})
					tmp1 := a[i]
					a[i] = a[j]
					a[j] = tmp1
					tmp1 = b[i]
					b[i] = b[j]
					b[j] = tmp1
					move--
					canSort = true
				}
			}
		}
		if !canSort {
			return -1
		}
	}
	if move <= 0 {
		return -1
	} else {
		fmt.Println(len(moves))
		for _, m := range moves {
			fmt.Println(m)
		}
		return 0
	}
}
func main() {
	var tc int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	tc, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < tc; i++ {
		var length int
		scanner.Scan()
		length, _ = strconv.Atoi(scanner.Text())
		a1 := make([]int, length)
		a2 := make([]int, length)
		scanner.Scan()
		la1 := strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a1[i1], _ = strconv.Atoi(v)
		}
		scanner.Scan()
		la1 = strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a2[i1], _ = strconv.Atoi(v)
		}
		c := solve(length, a1, a2)
		if c != -1 {
			sort2(length, a1, a2)
		} else {
			fmt.Println(c)
		}
	}

}

package main

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

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}

func solve(length int, array_a, array_b []int) int {
	sorted_a := make([]int, length)
	copy(sorted_a, array_a)
	sorted_b := make([]int, length)
	copy(sorted_b, array_b)
	sort.Ints(sorted_a)
	sort.Ints(sorted_b)
	index_a := make(map[int][]int)
	index_b := make(map[int][]int)
	for i, v := range array_a {
		if _, ok := index_a[v]; ok {
			index_a[v] = append(index_a[v], i)
		} else {
			index_a[v] = make([]int, 0)
			index_a[v] = append(index_a[v], i)
		}
	}
	for i, v := range array_b {
		if _, ok := index_b[v]; ok {
			index_b[v] = append(index_b[v], i)
		} else {
			index_b[v] = make([]int, 0)
			index_b[v] = append(index_b[v], i)
		}
	}
	canSort := false
	for i, v := range sorted_b {
		for _, k1 := range index_b[v] {
			if sorted_a[i] == array_a[k1] {
				canSort = true
			}
		}
		if !canSort {
			return -1
		}
	}
	return 0
}

func sort2(length int, a, b []int) int {
	var move = 10e4
	moves := make([]Move, 0)
	for move >= 0 {
		if isSort(length, a) && isSort(length, b) {
			break
		}
		canSort := false
		for i := 0; i < length-1; i++ {
			for j := i + 1; j < length; j++ {
				if a[i] >= a[j] && b[i] >= b[j] {
					moves = append(moves, Move{
						i + 1, j + 1,
					})
					tmp1 := a[i]
					a[i] = a[j]
					a[j] = tmp1
					tmp1 = b[i]
					b[i] = b[j]
					b[j] = tmp1
					move--
					canSort = true
				}
			}
		}
		if !canSort {
			return -1
		}
	}
	if move <= 0 {
		return -1
	} else {
		fmt.Println(len(moves))
		for _, m := range moves {
			fmt.Println(m)
		}
		return 0
	}
}
func main() {
	var tc int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	tc, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < tc; i++ {
		var length int
		scanner.Scan()
		length, _ = strconv.Atoi(scanner.Text())
		a1 := make([]int, length)
		a2 := make([]int, length)
		scanner.Scan()
		la1 := strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a1[i1], _ = strconv.Atoi(v)
		}
		scanner.Scan()
		la1 = strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a2[i1], _ = strconv.Atoi(v)
		}
		c := solve(length, a1, a2)
		if c != -1 {
			sort2(length, a1, a2)
		} else {
			fmt.Println(c)
		}
	}

}

package main

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

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}

func solve(length int, array_a, array_b []int) int {
	sorted_a := make([]int, length)
	copy(sorted_a, array_a)
	sorted_b := make([]int, length)
	copy(sorted_b, array_b)
	sort.Ints(sorted_a)
	sort.Ints(sorted_b)
	index_a := make(map[int][]int)
	index_b := make(map[int][]int)
	for i, v := range array_a {
		if _, ok := index_a[v]; ok {
			index_a[v] = append(index_a[v], i)
		} else {
			index_a[v] = make([]int, 0)
			index_a[v] = append(index_a[v], i)
		}
	}
	for i, v := range array_b {
		if _, ok := index_b[v]; ok {
			index_b[v] = append(index_b[v], i)
		} else {
			index_b[v] = make([]int, 0)
			index_b[v] = append(index_b[v], i)
		}
	}
	canSort := false
	for i, v := range sorted_b {
		for _, k1 := range index_b[v] {
			if sorted_a[i] == array_a[k1] {
				canSort = true
			}
		}
		if !canSort {
			return -1
		}
	}
	return 0
}

func sort2(length int, a, b []int) int {
	var move = 10e4
	moves := make([]Move, 0)
	for move >= 0 {
		if isSort(length, a) && isSort(length, b) {
			break
		}
		canSort := false
		for i := 0; i < length-1; i++ {
			for j := i + 1; j < length; j++ {
				if a[i] >= a[j] && b[i] >= b[j] {
					moves = append(moves, Move{
						i + 1, j + 1,
					})
					tmp1 := a[i]
					a[i] = a[j]
					a[j] = tmp1
					tmp1 = b[i]
					b[i] = b[j]
					b[j] = tmp1
					move--
					canSort = true
				}
			}
		}
		if !canSort {
			return -1
		}
	}
	if move <= 0 {
		return -1
	} else {
		fmt.Println(len(moves))
		for _, m := range moves {
			fmt.Println(m)
		}
		return 0
	}
}
func main() {
	var tc int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	tc, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < tc; i++ {
		var length int
		scanner.Scan()
		length, _ = strconv.Atoi(scanner.Text())
		a1 := make([]int, length)
		a2 := make([]int, length)
		scanner.Scan()
		la1 := strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a1[i1], _ = strconv.Atoi(v)
		}
		scanner.Scan()
		la1 = strings.Split(scanner.Text(), " ")
		for i1, v := range la1 {
			a2[i1], _ = strconv.Atoi(v)
		}
		c := solve(length, a1, a2)
		fmt.Println(c)
		if c != -1 {
			sort2(length, a1, a2)
		}
	}

}

package main

import (
	"fmt"
)

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}
func solve(length int, a, b []int) int {
	var move = 10e4
	moves := make([]Move, 0)
	for move >= 0 {
		if isSort(length, a) && isSort(length, b) {
			break
		}
		for i := 0; i < length-1; i++ {
			for j := i + 1; j < length; j++ {
				if a[i] >= a[j] && b[i] >= b[j] {
					moves = append(moves, Move{
						i + 1, j + 1,
					})
					tmp1 := a[i]
					a[i] = a[j]
					a[j] = tmp1
					tmp1 = b[i]
					b[i] = b[j]
					b[j] = tmp1
					move--
				}
			}
		}
		move--
	}
	if move <= 0 {
		fmt.Println(-1)
		return -1
	} else {
		fmt.Println(len(moves))
		for _, m := range moves {
			fmt.Println(m)
		}
		return 0
	}
}
func main() {
	var tc int
	fmt.Scanf("%d\n", &tc)
	for i := 0; i < tc; i++ {
		var length int
		fmt.Scanf("%d\n", &length)
		a1 := make([]int, length)
		a2 := make([]int, length)
		for j := 0; j < length; j++ {
			fmt.Scanf("%d", &a1[j])
		}
		for j := 0; j < length; j++ {
			fmt.Scanf("%d", &a2[j])
		}
		solve(length, a1, a2)
	}

}

package main

import (
	"fmt"
)

func isSort(length int, a []int) bool {
	for i := 0; i < length-1; i++ {
		if a[i] > a[i+1] {
			return false
		}
	}
	return true
}

type Move struct {
	i int
	j int
}

func (s Move) String() string {
	return fmt.Sprintf("%d %d", s.i, s.j)
}
func solve(length int, a, b []int) int {
	var move = 10e4
	moves := make([]Move, 0)
	for move >= 0 {
		if isSort(length, a) && isSort(length, b) {
			break
		}
		for i := 0; i < length-1; i++ {
			for j := i + 1; j < length; j++ {
				if a[i] >= a[j] && b[i] >= b[j] {
					moves = append(moves, Move{
						i + 1, j + 1,
					})
					tmp1 := a[i]
					a[i] = a[j]
					a[j] = tmp1
					tmp1 = b[i]
					b[i] = b[j]
					b[j] = tmp1
					move--
				}
			}
		}
		move--
	}
	if move <= 0 {
		return -1
	} else {
		fmt.Println(len(moves))
		for _, m := range moves {
			fmt.Println(m)
		}
		return 0
	}
}
func main() {
	var tc int
	fmt.Scanf("%d\n", &tc)
	for i := 0; i < tc; i++ {
		var length int
		fmt.Scanf("%d\n", &length)
		a1 := make([]int, length)
		a2 := make([]int, length)
		for j := 0; j < length; j++ {
			fmt.Scanf("%d", &a1[j])
		}
		for j := 0; j < length; j++ {
			fmt.Scanf("%d", &a2[j])
		}
		res := solve(length, a1, a2)
		fmt.Printf("%d", res)
	}

}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	var n int

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

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

		ao := make([]int, len(a))
		bo := make([]int, len(b))
		copy(ao, a)
		copy(bo, b)

		moves := solveC(a, b)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}

		moves = solveC(bo, ao)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}
		Fprintln(out, -1)

	}
}

type pair struct {
	i, j int
}

func solveC(a, b []int) []pair {
	n := len(a)

	moves := make([]pair, 0)
	count := 0
	for i := 0; i < n; i++ {
		// fmt.Println("===========================")
		// fmt.Println(a)
		// fmt.Println(b)
		smallestIdxA := i
		smallestIdxB := i
		for j := i; j < n; j++ {
			if a[smallestIdxA] > a[j] {
				smallestIdxA = j
			}
			if b[smallestIdxB] > b[j] {
				smallestIdxB = j
			}
		}

		if b[i] >= b[smallestIdxA] {
			a[i], a[smallestIdxA] = a[smallestIdxA], a[i]
			b[i], b[smallestIdxA] = b[smallestIdxA], b[i]
			moves = append(moves, pair{i + 1, smallestIdxA + 1})
			i--
			count++
			if count > 10000 {
				return nil
			}
			continue
		}
		if a[i] >= a[smallestIdxB] {
			a[i], a[smallestIdxB] = a[smallestIdxB], a[i]
			b[i], b[smallestIdxB] = b[smallestIdxB], b[i]
			moves = append(moves, pair{i + 1, smallestIdxB + 1})
			i--
			count++
			if count > 10000 {
				return nil
			}
			continue
		}
	}

	if isSorted(a) && isSorted(b) {
		return moves
	}
	return nil
}

func isSorted(nums []int) bool {
	n := len(nums)
	for i := 0; i < n-1; i++ {
		if nums[i] > nums[i+1] {
			return false
		}
	}
	return true
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	var n int

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

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

		ao := make([]int, len(a))
		bo := make([]int, len(b))
		copy(ao, a)
		copy(bo, b)

		moves := solveC(a, b)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}

		moves = solveC(bo, ao)
		if moves != nil {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
			continue
		}
		Fprintln(out, -1)

	}
}

type pair struct {
	i, j int
}

func solveC(a, b []int) []pair {
	n := len(a)

	moves := make([]pair, 0)
	for i := 0; i < n; i++ {
		// fmt.Println("===========================")
		// fmt.Println(a)
		// fmt.Println(b)
		smallestIdxA := i
		smallestIdxB := i
		for j := i; j < n; j++ {
			if a[smallestIdxA] > a[j] {
				smallestIdxA = j
			}
			if b[smallestIdxB] > a[j] {
				smallestIdxB = j
			}
		}

		if smallestIdxA != i && b[i] >= b[smallestIdxA] {
			a[i], a[smallestIdxA] = a[smallestIdxA], a[i]
			b[i], b[smallestIdxA] = b[smallestIdxA], b[i]
			moves = append(moves, pair{i + 1, smallestIdxA + 1})
			continue
		}
		if smallestIdxB != i && a[i] >= a[smallestIdxB] {
			a[i], a[smallestIdxB] = a[smallestIdxB], a[i]
			b[i], b[smallestIdxB] = b[smallestIdxB], b[i]
			moves = append(moves, pair{i + 1, smallestIdxB + 1})
			continue
		}
	}

	if isSorted(a) && isSorted(b) {
		return moves
	}
	return nil
}

func isSorted(nums []int) bool {
	n := len(nums)
	for i := 0; i < n-1; i++ {
		if nums[i] > nums[i+1] {
			return false
		}
	}
	return true
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	var n int

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

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

		moves := solveC(a, b)
		if moves == nil {
			Fprintln(out, -1)
		} else {
			Fprintln(out, len(moves))
			for i := range moves {
				Fprintln(out, moves[i].i, moves[i].j)
			}
		}

	}
}

type pair struct {
	i, j int
}

func solveC(a, b []int) []pair {
	n := len(a)

	moves := make([]pair, 0)
	for i := 0; i < n; i++ {
		smallestIdx := i
		for j := i; j < n; j++ {
			if a[smallestIdx] > a[j] {
				smallestIdx = j
			}
		}
		if smallestIdx != i {
			a[i], a[smallestIdx] = a[smallestIdx], a[i]
			b[i], b[smallestIdx] = b[smallestIdx], b[i]
			moves = append(moves, pair{i + 1, smallestIdx + 1})
		}
	}

	if !isSorted(b) {
		return nil
	}
	return moves
}

func isSorted(nums []int) bool {
	n := len(nums)
	for i := 0; i < n-1; i++ {
		if nums[i] > nums[i+1] {
			return false
		}
	}
	return true
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var T,N,A,B,mina,minb int

	fmt.Fscan(in, &T)

	for i := 0;i < T;i++{
		fmt.Fscan(in, &N)
		listA := make([]int,N)
		listB := make([]int,N)
		mA := make([]int,N)
		mB := make([]int,N)
		for j := 0;j < N;j++{
			fmt.Fscan(in, &A)
			listA[j] = A
			mA[j] = A
			if mina > A{
				mina = A
			}
		}
		for j := 0;j < N;j++{
			fmt.Fscan(in, &B)
			listB[j] = B
			mB[j] = B
			if minb > B{
				minb = B
			}
		}
		sort.Ints(listA)
		sort.Ints(listB)
		list,judge := sortlist(listA,listB,mA,mB,N)
		if judge {
			fmt.Println(swapsort(list,N))
		}else{
			fmt.Println(-1)
		}

	}
}

func sortlist(listA []int,listB []int,mA []int,mB []int,N int) ([]int, bool){
	list := make([]int,N)
	count := 0
	//judge := false
	for i := 0;i < N;i++ {
		judge := false
		for j := 0;j < N;j++ {
			if listA[i] == mA[j] && listB[i] == mB[j] {
				judge = true
				list[j] = count
				mA[j] = 101
				mB[j] = 101
				count++
				break
			}
		}
		if !judge{
			return list,judge
		}
	}
	return list,true
}

func swapsort(list []int,N int)int{
	count := 0
	for i := 0;i < N;i++ {
		if list[i] != i{
			for j := 0;j < N;j++ {
				if list[j] == i{
					list[i], list[j] = list[j], list[i]
					count++
				}
			}
		}
	}
	return count
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var T,N,A,B,mina,minb int

	fmt.Fscan(in, &T)

	for i := 0;i < T;i++{
		fmt.Fscan(in, &N)
		listA := make([]int,N)
		listB := make([]int,N)
		mA := make([]int,N)
		mB := make([]int,N)
		for j := 0;j < N;j++{
			fmt.Fscan(in, &A)
			listA[j] = A
			mA[j] = A
			if mina > A{
				mina = A
			}
		}
		for j := 0;j < N;j++{
			fmt.Fscan(in, &B)
			listB[j] = B
			mB[j] = B
			if minb > B{
				minb = B
			}
		}
		sort.Ints(listA)
		sort.Ints(listB)
		list,judge := sortlist(listA,listB,mA,mB,N)
		if judge {
			fmt.Println(swapsort(list,N))
		}else{
			fmt.Println(-1)
		}

	}
}

func sortlist(listA []int,listB []int,mA []int,mB []int,N int) ([]int, bool){
	list := make([]int,N)
	count := 0
	//judge := false
	for i := 0;i < N;i++ {
		judge := false
		for j := 0;j < N;j++ {
			if listA[i] == mA[j] && listB[i] == mB[j] {
				judge = true
				list[j] = count
				mA[j] = 101
				mB[j] = 101
				count++
			}
		}
		if !judge{
			return list,judge
		}
	}
	return list,true
}

func swapsort(list []int,N int)int{
	count := 0
	for i := 0;i < N;i++ {
		if list[i] != i{
			for j := 0;j < N;j++ {
				if list[j] == i{
					list[i], list[j] = list[j], list[i]
					count++
				}
			}
		}
	}
	return count
}

package main

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

type pair struct{ x, y int }

func sort(a []int) (p []pair) {
	n := len(a)
	for i := 0; i < n; i++ {
		for j := 0; j < n-i-1; j++ {
			if a[j] > a[j+1] {
				p = append(p, pair{j, j + 1})
				a[j], a[j+1] = a[j+1], a[j]
			}
		}
	}
	return p
}

func Solve() {
	n := ri()
	var a, b []int
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	for i := 0; i < n; i++ {
		b = append(b, ri())
	}
	p := sort(a)
	for _, t := range p {
		b[t.x], b[t.y] = b[t.y], b[t.x]
	}
	for i := 0; i < n; i++ {
		j := i
		for j+1 < n && a[i] == a[j+1] {
			j++
		}
		t := sort(b[i : j+1])
		p = append(p, t...)
		i = j
	}
	if sort2.IntsAreSorted(b) {
		println(len(p))
		for _, t := range p {
			println(t.x+1, t.y+1)
		}
	} else {
		println(-1)
	}
}

func main() {
	T := 0 // 如果是单组样例，请讲 T := 0 改为 T := 1
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

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

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。注意，这个函数不会换行，多个变量之间有空格
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出。这个函数会换行，多个变量之间有空格
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}