Monocarp is a little boy who lives in Byteland and he loves programming.Recently, he found a permutation of length $$$n$$$. He has to come up with a mystic permutation. It has to be a new permutation such that it differs from the old one in each position.More formally, if the old permutation is $$$p_1,p_2,\ldots,p_n$$$ and the new one is $$$q_1,q_2,\ldots,q_n$$$ it must hold that $$$$$$p_1\neq q_1, p_2\neq q_2, \ldots ,p_n\neq q_n.$$$$$$Monocarp is afraid of lexicographically large permutations. Can you please help him to find the lexicographically minimal mystic permutation?

There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 200$$$) — the number of test cases. This is followed by the test cases description. The first line of each test case contains a positive integer $$$n$$$ ($$$1\leq n\leq 1000$$$) — the length of the permutation. The second line of each test case contains $$$n$$$ distinct positive integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$). It's guaranteed that $$$p$$$ is a permutation, i. e. $$$p_i \neq p_j$$$ for all $$$i \neq j$$$.  It is guaranteed that the sum of $$$n$$$ does not exceed $$$1000$$$ over all test cases.

For each test case, output $$$n$$$ positive integers — the lexicographically minimal mystic permutations. If such a permutation does not exist, output $$$-1$$$ instead.

NoteIn the first test case possible permutations that are mystic are $$$[2,3,1]$$$ and $$$[3,1,2]$$$. Lexicographically smaller of the two is $$$[2,3,1]$$$.In the second test case, $$$[1,2,3,4,5]$$$ is the lexicographically minimal permutation and it is also mystic.In third test case possible mystic permutations are $$$[1,2,4,3]$$$, $$$[1,4,2,3]$$$, $$$[1,4,3,2]$$$, $$$[3,1,4,2]$$$, $$$[3,2,4,1]$$$, $$$[3,4,2,1]$$$, $$$[4,1,2,3]$$$, $$$[4,1,3,2]$$$ and $$$[4,3,2,1]$$$. The smallest one is $$$[1,2,4,3]$$$.

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

		nums := make([]int, n)

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

		if n == 1 {
			Fprintln(out, -1)
			continue
		}
		ans := make([]int, n)
		sort.Ints(nums)

		for i := range ans {
			// fmt.Println(nums, ans)
			sid := findSmallestIdx(nums, mp, i)
			// fmt.Println(sid)
			ans[i] = nums[sid]
			nums[sid] = -1
			if mp[ans[i]] == i {
				ans[i-1], ans[i] = ans[i], ans[i-1]
			}
		}
		Fprintln(out, strings.Trim(Sprint(ans), "[]"))

	}
}

func findSmallestIdx(nums []int, mp map[int]int, idx int) int {
	// fmt.Println("============================================")
	for i := range nums {
		// fmt.Println(nums[i], mp[nums[i]])
		if nums[i] != -1 && mp[nums[i]] != idx {
			return i
		}
	}
	return idx
}

package main

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

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

	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		var buf bytes.Buffer
		var n int
		fmt.Scan(&n)

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

		if n > 1 {
			sort.Ints(q)

			for i := 0; i < n; i++ {
				if p[i] == q[i] {
					if i != n-1 {
						q[i], q[i+1] = q[i+1], q[i]
					} else {
						q[i], q[i-1] = q[i-1], q[i]
					}
				}

			}

			for i := 0; i < n; i++ {
				buf.WriteString(strconv.Itoa(q[i]))
				buf.WriteRune(' ')
			}

		} else {
			buf.WriteString("-1")
		}
		fmt.Fprintln(out, buf.String())
	}
}

package main

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

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

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

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

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

		for i := 0; i < n; i++ {
			if arr[i] == b[i] {
				b[i], b[i+1] = b[i+1], b[i]
			}
		}
		if b[n] == n {
			b[n-2], b[n-1] = n, b[n-2]
		}
		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d ", b[i])
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

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

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

		if n == 1 {
			fmt.Fprintln(out, -1)
		} else {
			g := make([]int, n)
			x := 1
			m := make([]bool, n+2)
			m[0] = true
			for i, v := range arr {
				if v == x {
					if !m[x-1] {
						g[i] = x - 1
						m[x-1] = true
					} else {
						if x >= n {
							g[i], g[i-1] = g[i-1], n
						} else {
							g[i] = x + 1
							m[x+1] = true
						}
					}
				} else {
					g[i] = x
					m[x] = true
					x++
					if m[x] {
						x++
					}
				}
			}

			for _, v := range g {
				fmt.Fprintf(out, "%d ", v)
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
outer:
	for iter := 0; iter < ntests; iter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slice := make([]int, n)
		tmp := make([]int, n)
		res := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		if n == 1 {
			fmt.Fprintln(writer, "-1")
			continue outer
		}
		copy(tmp, slice)
		sort.Slice(tmp, func(i, j int) bool { return tmp[i] < tmp[j] })
		for i := 0; i < n; i++ {
			min := tmp[0]
			if min != slice[0] {
				tmp = tmp[1:]
				slice = slice[1:]
				res[i] = min
			} else {
				if i == n-1 {
					// special case
					// have to swap something; n >= 3
					res[n-1] = res[n-2]
					res[n-2] = min
				} else {
					min = tmp[1]
					tmp = append(tmp[:1], tmp[2:]...)
					slice = slice[1:]
					res[i] = min
				}
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fprintf(writer, "%d ", res[i])
		}
		fmt.Fprintln(writer)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }

type rbtreesetnode struct { left, right, up int32; red bool; key int }
type rbtreeset struct {
	lessthan func(a, b int) bool; tree []rbtreesetnode; root int32; recycler []int32; sz int; minidx int32
	maxidx int32
}
type rbtreesetIterator interface { Next() (ok bool); Prev() (ok bool); Key() int }
type rbtreesetiter struct { cur int32; key int; rbtree *rbtreeset }
func (i *rbtreesetiter) Key() int { return i.key }
func (i *rbtreesetiter) Next() bool {
	rbtree := i.rbtree; v := rbtree.nextidx(i.cur); if v == 0 { return false }; i.cur, i.key = v, rbtree.tree[v].key
	return true
}
func (i *rbtreesetiter) Prev() bool {
	rbtree := i.rbtree; v := rbtree.previdx(i.cur); if v == 0 { return false }; i.cur, i.key = v, rbtree.tree[v].key
	return true
}
func Newrbtreeset(lessthan func(a, b int) bool) *rbtreeset {
	q := &rbtreeset{lessthan, make([]rbtreesetnode, 2), int32(0), make([]int32, 0), 0, 0, 0}
	q.tree[0].left, q.tree[0].right, q.tree[0].up, q.tree[0].red = 0, 0, 0, false; q.recycler = append(q.recycler, 1)
	return q
}
func (q *rbtreeset) Add(k int) {
	if q.sz == 0 {
		z := q.getNewNodenum(); tree := q.tree; q.minidx, q.maxidx, q.sz, q.root = z, z, q.sz+1, z
		tree[z].key, tree[z].up, tree[z].left, tree[z].right, tree[z].red = k, 0, 0, 0, false; return
	}
	y, cmp := q.findInsertionPoint(k); if cmp == 0 { return }; z := q.getNewNodenum(); q.sz += 1; tree := q.tree
	tree[z].key, tree[z].up, tree[z].left, tree[z].right, tree[z].red = k, y, 0, 0, true
	if cmp < 0 { tree[y].left = z } else { tree[y].right = z }
	if q.sz == 0 || q.lessthan(k, tree[q.minidx].key) { q.minidx = z }
	if q.sz == 0 || q.lessthan(tree[q.maxidx].key, k) { q.maxidx = z }; var p, g, u int32
	for p = tree[z].up; tree[p].red; p = tree[z].up {
		g = tree[p].up ; if g == 0 { break }
		if p == tree[g].left {
			u = tree[g].right
			if tree[u].red { tree[p].red, tree[u].red, tree[g].red, z = false, false, true, g; continue }
			if z == tree[p].right { z = p; q.rotleft(z); p = tree[z].up }; q.rotright(g)
			tree[g].red, tree[p].red = true, false
		} else {
			u = tree[g].left
			if tree[u].red { tree[p].red, tree[u].red, tree[g].red, z = false, false, true, g; continue }
			if z == tree[p].left { z = p; q.rotright(z); p = tree[z].up }; q.rotleft(g)
			tree[g].red, tree[p].red = true, false
		}
	}
	tree[q.root].red = false
}
func (q *rbtreeset) Delete(k int) bool {
	if q.sz == 0 { return false }; z, cmp := q.findInsertionPoint(k); if cmp != 0 { return false }; q.sz--
	q.recycler = append(q.recycler, z)
	if q.sz > 0 && !q.lessthan(q.tree[q.minidx].key, k) { q.minidx = q.nextidx(q.minidx) }
	if q.sz > 0 && !q.lessthan(k, q.tree[q.maxidx].key) { q.maxidx = q.previdx(q.maxidx) }
	if q.sz == 0 { q.root = 0; return true }; tree := q.tree; var x int32; y, y_orig_red := z, tree[z].red
	if tree[z].left == 0 {
		x = tree[z].right; q.rbTransplant(z, x)
	} else if tree[z].right == 0 {
		x = tree[z].left; q.rbTransplant(z, x)
	} else {
		y = q.findminidx(tree[z].right); y_orig_red = tree[y].red; x = tree[y].right
		if tree[y].up == z {
			tree[x].up = y
		} else {
			q.rbTransplant(y, x); tree[y].right = tree[z].right; tree[tree[y].right].up = y
		}
		q.rbTransplant(z, y); tree[y].left = tree[z].left; tree[tree[y].left].up = y; tree[y].red = tree[z].red
	}
	if !y_orig_red {
		for q.root != x && !tree[x].red {
			p := tree[x].up
			if tree[p].left == x {
				s := tree[p].right
				if tree[s].red { tree[s].red = false; tree[p].red = true; q.rotleft(p); s = tree[p].right }
				c := tree[s].left; d := tree[s].right
				if !tree[c].red && !tree[d].red {
					tree[s].red = true; x = p
				} else {
					if !tree[d].red { tree[c].red = false; tree[s].red = true; q.rotright(s); s = tree[p].right }
					tree[s].red = tree[p].red; tree[p].red = false; tree[tree[s].right].red = false; q.rotleft(p)
					x = q.root
				}
			} else {
				s := tree[p].left
				if tree[s].red { tree[s].red = false; tree[p].red = true; q.rotright(p); s = tree[p].left }
				c := tree[s].right; d := tree[s].left
				if !tree[c].red && !tree[d].red {
					tree[s].red = true; x = p
				} else {
					if !tree[d].red { tree[c].red = false; tree[s].red = true; q.rotleft(s); s = tree[p].left }
					tree[s].red = tree[p].red; tree[p].red = false; tree[tree[s].left].red = false; q.rotright(p)
					x = q.root
				}
			}
		}
		tree[x].red = false
	}
	return true
}
func (q *rbtreeset) Clear() {
	q.tree, q.root, q.recycler, q.sz = q.tree[:2], 0, q.recycler[:0], 0; q.recycler = append(q.recycler, int32(1))
}
func (q *rbtreeset) IsEmpty() bool { return q.sz == 0 }
func (q *rbtreeset) Contains(k int) bool { _, cmp := q.findInsertionPoint(k); return cmp == 0 }
func (q *rbtreeset) Count(k int) int { _, cmp := q.findInsertionPoint(k); if cmp == 0 { return 1 }; return 0 }
func (q *rbtreeset) Len() int { return q.sz }
func (q *rbtreeset) MinKey() (k int) {
	if q.sz == 0 { panic("Called MinKey on an empty rbtreeset") }; return q.tree[q.minidx].key
}
func (q *rbtreeset) MaxKey() (k int) {
	if q.sz == 0 { panic("Called MaxKey on an empty rbtreeset") }; return q.tree[q.maxidx].key
}
func (q *rbtreeset) findLtIdx(k int) (int32, bool) {
	if q.sz == 0 || !q.lessthan(q.tree[q.minidx].key, k) { return 0, false }; idx, pos := q.findInsertionPoint(k)
	if pos != 1 { idx = q.previdx(idx) }; return idx, true
}
func (q *rbtreeset) findLeIdx(k int) (int32, bool) {
	if q.sz == 0 || q.lessthan(k, q.tree[q.minidx].key) { return 0, false }; idx, pos := q.findInsertionPoint(k)
	if pos == -1 { idx = q.previdx(idx) }; return idx, true
}
func (q *rbtreeset) findGtIdx(k int) (int32, bool) {
	if q.sz == 0 || !q.lessthan(k, q.tree[q.maxidx].key) { return 0, false }; idx, pos := q.findInsertionPoint(k)
	if pos != -1 { idx = q.nextidx(idx) }; return idx, true
}
func (q *rbtreeset) findGeIdx(k int) (int32, bool) {
	if q.sz == 0 || q.lessthan(q.tree[q.maxidx].key, k) { return 0, false }; idx, pos := q.findInsertionPoint(k)
	if pos == 1 { idx = q.nextidx(idx) }; return idx, true
}
func (q *rbtreeset) FindLt(k int) (int, bool) {
	var ans int; idx, ok := q.findLtIdx(k); if ok { ans = q.tree[idx].key }; return ans, ok
}
func (q *rbtreeset) FindLe(k int) (int, bool) {
	var ans int; idx, ok := q.findLeIdx(k); if ok { ans = q.tree[idx].key }; return ans, ok
}
func (q *rbtreeset) FindGt(k int) (int, bool) {
	var ans int; idx, ok := q.findGtIdx(k); if ok { ans = q.tree[idx].key }; return ans, ok
}
func (q *rbtreeset) FindGe(k int) (int, bool) {
	var ans int; idx, ok := q.findGeIdx(k); if ok { ans = q.tree[idx].key }; return ans, ok
}
func (q *rbtreeset) FindLtIter(k int) (rbtreesetIterator, bool) {
	var ans *rbtreesetiter; idx, ok := q.findLtIdx(k); if ok { ans = &rbtreesetiter{idx, q.tree[idx].key, q} }
	return ans, ok
}
func (q *rbtreeset) FindLeIter(k int) (rbtreesetIterator, bool) {
	var ans *rbtreesetiter; idx, ok := q.findLeIdx(k); if ok { ans = &rbtreesetiter{idx, q.tree[idx].key, q} }
	return ans, ok
}
func (q *rbtreeset) FindGtIter(k int) (rbtreesetIterator, bool) {
	var ans *rbtreesetiter; idx, ok := q.findGtIdx(k); if ok { ans = &rbtreesetiter{idx, q.tree[idx].key, q} }
	return ans, ok
}
func (q *rbtreeset) FindGeIter(k int) (rbtreesetIterator, bool) {
	var ans *rbtreesetiter; idx, ok := q.findGeIdx(k); if ok { ans = &rbtreesetiter{idx, q.tree[idx].key, q} }
	return ans, ok
}
func (q *rbtreeset) FindIter(k int) (rbtreesetIterator, bool) {
	if q.sz == 0 { return nil, false }; idx, pos := q.findInsertionPoint(k); if pos != 0 { return nil, false }
	return &rbtreesetiter{idx, q.tree[idx].key, q}, true
}
func (q *rbtreeset) MinIter() (rbtreesetIterator, bool) {
	if q.sz == 0 { return nil, false }; idx := q.findminidx(q.root)
	return &rbtreesetiter{idx, q.tree[idx].key, q}, true
}
func (q *rbtreeset) MaxIter() (rbtreesetIterator, bool) {
	if q.sz == 0 { return nil, false }; idx := q.findmaxidx(q.root)
	return &rbtreesetiter{idx, q.tree[idx].key, q}, true
}
func (q *rbtreeset) rbTransplant(u, v int32) {
	tree := q.tree
	if tree[u].up == 0 {
		q.root = v
	} else {
		p := tree[u].up; if u == tree[p].left { tree[p].left = v } else { tree[p].right = v }
	}
	tree[v].up = tree[u].up
}
func (q *rbtreeset) findInsertionPoint(k int) (int32, int8) {
	n, lt, tree := q.root, q.lessthan, q.tree
	for {
		nkey := tree[n].key
		if lt(nkey, k) {
			r := tree[n].right; if r == 0 { return n, 1 }; n = r
		} else if lt(k, nkey) {
			l := tree[n].left; if l == 0 { return n, -1 }; n = l
		} else {
			return n, 0
		}
	}
}
func (q *rbtreeset) findmaxidx(n1 int32) int32 {
	tree := q.tree; for { xx := tree[n1].right; if xx == 0 { break }; n1 = xx }; return n1
}
func (q *rbtreeset) findminidx(n1 int32) int32 {
	tree := q.tree; for { xx := tree[n1].left; if xx == 0 { break }; n1 = xx }; return n1
}
func (q *rbtreeset) nextidx(cur int32) int32 {
	last := int32(-2); tree := q.tree; rr := tree[cur].right; if rr > 0 { return q.findminidx(rr) }
	for { last, cur = cur, tree[cur].up; if cur == 0 || tree[cur].left == last { break } }; return cur
}
func (q *rbtreeset) previdx(cur int32) int32 {
	last := int32(0); tree := q.tree; ll := tree[cur].left; if ll > 0 { return q.findmaxidx(ll) }
	for { last, cur = cur, tree[cur].up; if cur == 0 || tree[cur].right == last { break } }; return cur
}
func (q *rbtreeset) rotleft(x int32) {
	tree := q.tree; y := tree[x].right; p := tree[x].up; tree[x].right = tree[y].left
	if tree[y].left != 0 { tree[tree[y].left].up = x }; tree[y].up = p
	if p == 0 {
		q.root = y
	} else if x == tree[p].left {
		tree[p].left = y
	} else {
		tree[p].right = y
	}
	tree[y].left = x; tree[x].up = y
}
func (q *rbtreeset) rotright(x int32) {
	tree := q.tree; y := tree[x].left; p := tree[x].up; tree[x].left = tree[y].right
	if tree[y].right != 0 { tree[tree[y].right].up = x }; tree[y].up = p
	if p == 0 {
		q.root = y
	} else if x == tree[p].right {
		tree[p].right = y
	} else {
		tree[p].left = y
	}
	tree[y].right = x; tree[x].up = y
}
func (q *rbtreeset) getNewNodenum() int32 {
	l := len(q.recycler); newnode := q.recycler[l-1]; q.recycler = q.recycler[:l-1]
	if l == 1 { q.tree = append(q.tree, rbtreesetnode{}); q.recycler = append(q.recycler, int32(len(q.tree)-1)) }
	return newnode
}

func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	ss := Newrbtreeset(func (a,b int) bool { return a < b } )
	for tt:=1;tt<=T;tt++ {
		N := gi(); P := gis(N)
		if N == 1 { fmt.Fprintln(wrtr,-1); continue }
		ss.Clear(); for i:=1;i<=N;i++ { ss.Add(i) }
		ansarr := ia(N)
		for i:=0;i<N-1;i++ {
			p := P[i]
			m := ss.MinKey()
			if m == p { m,_ = ss.FindGt(m) }
			ansarr[i] = m; ss.Delete(m)
		}
		m,_ := ss.FindGt(0); ansarr[N-1] = m
		if P[N-1] == ansarr[N-1] { ansarr[N-2],ansarr[N-1] = ansarr[N-1],ansarr[N-2] }
		fmt.Fprintln(wrtr,vecintstring(ansarr))
	}
}

package main

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

/*
1 2 3 -> 2 3 1

2 3 4 5 1
1 2 3 4 5

2 3 1 4
1 2 4 3

2
4
1 3 2 4
5
1 3 2 4 5
===
2 1 4 3

2 1 3 5 4

*/

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

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

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

	sort.Ints(q)

	for i, num := range q {
		if num == p[i] {
			if i != len(q)-1 {
				q[i], q[i+1] = q[i+1], q[i]
			} else if i != 0 {
				q[i], q[i-1] = q[i-1], q[i]
			} else {
				fmt.Fprintln(writer, -1)
				return
			}
		}
	}

	for _, num := range q {
		fmt.Fprintf(writer, "%d ", num)
	}
	fmt.Fprintln(writer)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

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

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

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

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

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

	return factors
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		sort.Ints(nums)
		cnt := 0
		for !isMystic(p, nums) && cnt < 10005 {
			nextPermutation(nums)
			cnt++
		}

		if cnt >= 10005 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, strings.Trim(Sprint(nums), "[]"))
		}

	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {

	numsLen := len(nums)
	first := -1
	second := -1

	for i, j := numsLen-2, numsLen-1; i >= 0; {
		if nums[i] < nums[j] {
			first = i
			second = j
			break
		} else {
			i--
			j--
		}
	}

	if !(first == -1) {
		smallestGreaterIndex := second
		for i := second + 1; i < numsLen; i++ {
			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
				smallestGreaterIndex = i
			}
		}
		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

		sort.Slice(nums[second:numsLen], func(i, j int) bool {
			return nums[second+i] < nums[second+j]
		})
	}
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		sort.Ints(nums)
		cnt := 0
		for !isMystic(p, nums) && cnt < 2005 {
			nextPermutation(nums)
			cnt++
		}

		if cnt >= 2005 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, strings.Trim(Sprint(nums), "[]"))
		}

	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {

	numsLen := len(nums)
	first := -1
	second := -1

	for i, j := numsLen-2, numsLen-1; i >= 0; {
		if nums[i] < nums[j] {
			first = i
			second = j
			break
		} else {
			i--
			j--
		}
	}

	if !(first == -1) {
		smallestGreaterIndex := second
		for i := second + 1; i < numsLen; i++ {
			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
				smallestGreaterIndex = i
			}
		}
		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

		sort.Slice(nums[second:numsLen], func(i, j int) bool {
			return nums[second+i] < nums[second+j]
		})
	}
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		sort.Ints(nums)
		cnt := 0
		for !isMystic(p, nums) && cnt < 1005 {
			nextPermutation(nums)
			cnt++
		}

		if cnt >= 1005 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, strings.Trim(Sprint(nums), "[]"))
		}

	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {

	numsLen := len(nums)
	first := -1
	second := -1

	for i, j := numsLen-2, numsLen-1; i >= 0; {
		if nums[i] < nums[j] {
			first = i
			second = j
			break
		} else {
			i--
			j--
		}
	}

	if !(first == -1) {
		smallestGreaterIndex := second
		for i := second + 1; i < numsLen; i++ {
			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
				smallestGreaterIndex = i
			}
		}
		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

		sort.Slice(nums[second:numsLen], func(i, j int) bool {
			return nums[second+i] < nums[second+j]
		})
	}
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		sort.Ints(nums)
		cnt := 0
		for !isMystic(p, nums) && cnt < 2*n {
			nextPermutation(nums)
			cnt++
		}

		if cnt >= 2*n {
			Fprintln(out, -1)
		} else {
			Fprintln(out, strings.Trim(Sprint(nums), "[]"))
		}

	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {

	numsLen := len(nums)
	first := -1
	second := -1

	for i, j := numsLen-2, numsLen-1; i >= 0; {
		if nums[i] < nums[j] {
			first = i
			second = j
			break
		} else {
			i--
			j--
		}
	}

	if !(first == -1) {
		smallestGreaterIndex := second
		for i := second + 1; i < numsLen; i++ {
			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
				smallestGreaterIndex = i
			}
		}
		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

		sort.Slice(nums[second:numsLen], func(i, j int) bool {
			return nums[second+i] < nums[second+j]
		})
	}
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		sort.Ints(nums)
		cnt := 0
		for !isMystic(p, nums) && cnt < 2*n {
			nextPermutation(nums)
			cnt++
		}

		if cnt >= 2*n {
			Fprintln(out, -1)
			continue
		}

		Fprintln(out, strings.Trim(Sprint(nums), "[]"))
	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {
	length := len(nums)
	if length == 1 {
		return
	}
	if nums[length-1] > nums[length-2] {
		nums[length-1], nums[length-2] = nums[length-2], nums[length-1]
		return
	}

	idx := length - 1
	for idx > 0 {
		if nums[idx] > nums[idx-1] {
			break
		}
		idx--
	}
	if idx == 0 {
		sort.Sort(sort.IntSlice(nums))
	} else {
		minNum, minIdx := nums[idx], idx

		for i := idx + 1; i < length; i++ {
			if nums[i] < minNum && nums[i] > nums[idx-1] {
				minNum = nums[i]
				minIdx = i
			}
		}

		nums[minIdx], nums[idx-1] = nums[idx-1], nums[minIdx]
		sort.Sort(sort.IntSlice(nums[idx:]))
	}
}

// func nextPermutation(nums []int) {

// 	numsLen := len(nums)
// 	first := -1
// 	second := -1

// 	for i, j := numsLen-2, numsLen-1; i >= 0; {
// 		if nums[i] < nums[j] {
// 			first = i
// 			second = j
// 			break
// 		} else {
// 			i--
// 			j--
// 		}
// 	}

// 	if !(first == -1) {
// 		smallestGreaterIndex := second
// 		for i := second + 1; i < numsLen; i++ {
// 			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
// 				smallestGreaterIndex = i
// 			}
// 		}
// 		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

// 		sort.Slice(nums[second:numsLen], func(i, j int) bool {
// 			return nums[second+i] < nums[second+j]
// 		})
// 	}
// }

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		sort.Ints(nums)
		cnt := 0
		for !isMystic(p, nums) && cnt < n+5 {
			nextPermutation(nums)
			cnt++
		}

		if len(nums) == 1 || cnt > n {
			Fprintln(out, -1)
			continue
		}

		Fprintln(out, strings.Trim(Sprint(nums), "[]"))
	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {
	length := len(nums)
	if length == 1 {
		return
	}
	if nums[length-1] > nums[length-2] {
		nums[length-1], nums[length-2] = nums[length-2], nums[length-1]
		return
	}

	idx := length - 1
	for idx > 0 {
		if nums[idx] > nums[idx-1] {
			break
		}
		idx--
	}
	if idx == 0 {
		sort.Sort(sort.IntSlice(nums))
	} else {
		minNum, minIdx := nums[idx], idx

		for i := idx + 1; i < length; i++ {
			if nums[i] < minNum && nums[i] > nums[idx-1] {
				minNum = nums[i]
				minIdx = i
			}
		}

		nums[minIdx], nums[idx-1] = nums[idx-1], nums[minIdx]
		sort.Sort(sort.IntSlice(nums[idx:]))
	}
}

// func nextPermutation(nums []int) {

// 	numsLen := len(nums)
// 	first := -1
// 	second := -1

// 	for i, j := numsLen-2, numsLen-1; i >= 0; {
// 		if nums[i] < nums[j] {
// 			first = i
// 			second = j
// 			break
// 		} else {
// 			i--
// 			j--
// 		}
// 	}

// 	if !(first == -1) {
// 		smallestGreaterIndex := second
// 		for i := second + 1; i < numsLen; i++ {
// 			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
// 				smallestGreaterIndex = i
// 			}
// 		}
// 		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

// 		sort.Slice(nums[second:numsLen], func(i, j int) bool {
// 			return nums[second+i] < nums[second+j]
// 		})
// 	}
// }

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		copy(p, nums)
		found := false
		sort.Ints(nums)
		for i := 0; i <= 2*n; i++ {
			if isMystic(p, nums) {
				found = true
				break
			}
			nextPermutation(nums)
		}

		if !found {
			Fprintln(out, -1)
		} else {
			Fprintln(out, strings.Trim(Sprint(nums), "[]"))
		}
	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {

	numsLen := len(nums)
	first := -1
	second := -1

	for i, j := numsLen-2, numsLen-1; i >= 0; {
		if nums[i] < nums[j] {
			first = i
			second = j
			break
		} else {
			i--
			j--
		}
	}

	if !(first == -1) {
		smallestGreaterIndex := second
		for i := second + 1; i < numsLen; i++ {
			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
				smallestGreaterIndex = i
			}
		}
		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

		sort.Slice(nums[second:numsLen], func(i, j int) bool {
			return nums[second+i] < nums[second+j]
		})
	}
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

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

		copy(p, nums)
		found := false
		sort.Ints(nums)
		for i := 0; i <= n; i++ {
			if isMystic(p, nums) {
				found = true
				break
			}
			nextPermutation(nums)
		}

		if !found {
			Fprintln(out, -1)
		} else {
			Fprintln(out, strings.Trim(Sprint(nums), "[]"))
		}
	}
}

func isMystic(p, q []int) bool {
	for i := range p {
		if p[i] == q[i] {
			return false
		}
	}
	return true
}

func nextPermutation(nums []int) {

	numsLen := len(nums)
	first := -1
	second := -1

	for i, j := numsLen-2, numsLen-1; i >= 0; {
		if nums[i] < nums[j] {
			first = i
			second = j
			break
		} else {
			i--
			j--
		}
	}

	if !(first == -1) {
		smallestGreaterIndex := second
		for i := second + 1; i < numsLen; i++ {
			if nums[i] > nums[first] && nums[i] < nums[smallestGreaterIndex] {
				smallestGreaterIndex = i
			}
		}
		nums[first], nums[smallestGreaterIndex] = nums[smallestGreaterIndex], nums[first]

		sort.Slice(nums[second:numsLen], func(i, j int) bool {
			return nums[second+i] < nums[second+j]
		})
	}
}

package main

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

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

	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		var buf bytes.Buffer
		var n int
		fmt.Scan(&n)

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

		if n > 1 {
			sort.Ints(q)

			if q[0] == p[0] {
				q[0], q[1] = q[1], q[0]
			}
			i := n - 1
			for ; i > 0; i-- {
				if p[i] == q[i] {
					q[i], q[i-1] = q[i-1], q[i]
				}
			}
			for ; i < n; i++ {
				buf.WriteString(strconv.Itoa(q[i]))
				buf.WriteRune(' ')
			}

		} else {
			buf.WriteString("-1")
		}
		fmt.Fprintln(out, buf.String())
	}
}


Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either "M" or from $$$0$$$ to $$$3$$$ "X" followed by "S" or "L". For example, sizes "M", "XXS", "L", "XXXL" are valid and "XM", "Z", "XXXXL" are not.There are $$$n$$$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of T-shirts. The $$$i$$$-th of the next $$$n$$$ lines contains $$$a_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the previous year. The $$$i$$$-th of the next $$$n$$$ lines contains $$$b_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the current year. It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $$$b$$$ from the list $$$a$$$.

Print the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0.

NoteIn the first example Ksenia can replace "M" with "S" and "S" in one of the occurrences of "XS" with "L".In the second example Ksenia should replace "L" in "XXXL" with "S".In the third example lists are equal.

package main

import "fmt"

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

   m := make(map[string]int)
   for i:=1; i <= n;i++{
   		var sze string
   		fmt.Scanln(&sze)
   		m[sze]++
   }
   for i:=1; i<= n;i++{
   		var sze string
   		fmt.Scanln(&sze)
   		if m[sze] > 0{
   			m[sze]--
   			if m[sze] == 0{
   				delete(m,sze)
			}
		}
   }
   ans := 0
   for _, count := range m{
   	    ans += count
   }
   fmt.Println(ans)
}

package main

import (
	"fmt"
)

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

	sizes := make(map[string]int)
	for i := 0; i < n; i++ {
		var size string
		fmt.Scanln(&size)
		sizes[size]++
	}

	for i := 0; i < n; i++ {
		var size string
		fmt.Scanln(&size)
		if sizes[size] > 0 {
			sizes[size]--
			if sizes[size] == 0 {
				delete(sizes, size)
			}
		}
	}

	ans := 0
	for _, count := range sizes {
		ans += count
	}

	fmt.Println(ans)
}

package main

import (
	"fmt"
)

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

	sizes := make(map[string]int)
	for i := 0; i < n; i++ {
		var size string
		fmt.Scanln(&size)
		sizes[size]++
	}

	for i := 0; i < n; i++ {
		var size string
		fmt.Scanln(&size)
		if sizes[size] > 0 {
			sizes[size]--
			if sizes[size] == 0 {
				delete(sizes, size)
			}
		}
	}

	fmt.Println(len(sizes))
}


Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, j. Because large values require more space to store them, the maximum value in a' should be as small as possible.Petya is good in theory, however, he needs your help to implement the algorithm.

The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively. Each of the following n rows contain m integers ai, j (1 ≤ ai, j ≤ 109) that are the values in the table.

Output the compressed table in form of n lines each containing m integers. If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.

NoteIn the first sample test, despite the fact a1, 2 ≠ a21, they are not located in the same row or column so they may become equal after the compression.

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

type cell struct {
	r, c, a        int
	rl, rh, cl, ch *cell
	v              int
}

type byA []*cell

func (s byA) Len() int           { return len(s) }
func (s byA) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s byA) Less(i, j int) bool { return s[i].a < s[j].a }

func main() {
	out := bufio.NewWriterSize(os.Stdout, 2<<16)
	defer out.Flush()

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

	byRow := make([][]*cell, n)
	byCol := make([][]*cell, m)

	for r := range byRow {
		for c := range byCol {
			cell := &cell{r: r, c: c}
			fmt.Fscan(in, &cell.a)
			byRow[r] = append(byRow[r], cell)
			byCol[c] = append(byCol[c], cell)
		}
	}

	for _, s := range byRow {
		sort.Sort(byA(s))
		for i := range s {
			if i > 0 {
				s[i].rl = s[i-1]
			}
			if i+1 < m {
				s[i].rh = s[i+1]
			}
		}
	}
	for _, s := range byCol {
		sort.Sort(byA(s))
		for i := range s {
			if i > 0 {
				s[i].cl = s[i-1]
			}
			if i+1 < n {
				s[i].ch = s[i+1]
			}
		}
	}

	numVertices := 0
	for _, row := range byRow {
		for _, cell := range row {
			if cell.v == 0 {
				numVertices++
				fill(cell, numVertices)
			}
		}
	}

	edges := make([][]int, numVertices+1)
	for _, row := range byRow {
		for i := range row[1:] {
			if row[i].a < row[i+1].a {
				v := row[i+1].v
				edges[v] = append(edges[v], row[i].v)
			}
		}
	}
	for _, col := range byCol {
		for i := range col[1:] {
			if col[i].a < col[i+1].a {
				v := col[i+1].v
				edges[v] = append(edges[v], col[i].v)
			}
		}
	}

	vertices := make([]int, numVertices+1)
	var val func(int) int
	val = func(v int) int {
		if vertices[v] != 0 {
			return vertices[v]
		}
		ans := 1
		for _, u := range edges[v] {
			if ch := val(u); ans <= ch {
				ans = ch + 1
			}
		}
		vertices[v] = ans
		return ans
	}

	res := make([][]int, n)
	for i := range res {
		res[i] = make([]int, m)
	}
	for _, row := range byRow {
		for _, cell := range row {
			res[cell.r][cell.c] = val(cell.v)
		}
	}

	for _, row := range res {
		for i, a := range row {
			if i == 0 {
				fmt.Fprint(out, a)
			} else {
				fmt.Fprint(out, " ", a)
			}
		}
		fmt.Fprintln(out)
	}
}

func fill(c *cell, v int) {
	if c.v != 0 {
		return
	}
	c.v = v
	for _, try := range []*cell{c.rl, c.rh, c.cl, c.ch} {
		if try != nil && try.a == c.a {
			fill(try, v)
		}
	}
}


Iahub accidentally discovered a secret lab. He found there n devices ordered in a line, numbered from 1 to n from left to right. Each device i (1 ≤ i ≤ n) can create either ai units of matter or ai units of antimatter. Iahub wants to choose some contiguous subarray of devices in the lab, specify the production mode for each of them (produce matter or antimatter) and finally take a photo of it. However he will be successful only if the amounts of matter and antimatter produced in the selected subarray will be the same (otherwise there would be overflowing matter or antimatter in the photo). You are requested to compute the number of different ways Iahub can successful take a photo. A photo is different than another if it represents another subarray, or if at least one device of the subarray is set to produce matter in one of the photos and antimatter in the other one.

The first line contains an integer n (1 ≤ n ≤ 1000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000). The sum a1 + a2 + ... + an will be less than or equal to 10000.

Output a single integer, the number of ways Iahub can take a photo, modulo 1000000007 (109 + 7).

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
)

func main() {
	const mod = 1000000007

	var (
		n   int
		ans int64
	)

	fmt.Fscan(reader, &n)

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

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

	for i := 0; i < n; i++ {
		if i > 0 {
			for j := a[i]; j <= 10000; j++ {
				f1[i][j] += f1[i-1][j-a[i]]
				f1[i][j] %= mod
			}
			for j := 10000 - a[i]; j >= 0; j-- {
				f1[i][j] += f1[i-1][j+a[i]]
				f1[i][j] %= mod
			}
		}

		if o := 5000 + a[i]; o <= 10000 {
			f1[i][o]++
		}
		if o := 5000 - a[i]; o >= 0 {
			f1[i][o]++
		}
		ans += int64(f1[i][5000])
	}

	fmt.Print(ans % mod)
}