Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.

The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests. Next q lines contain the descriptions of the requests, one per line. Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20. The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.

In the first line output the integer n — the number of users that changed their handles at least once. In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order. Each user who changes the handle must occur exactly once in this description.

5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

package main

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

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0; i<q; i++{
		scanner.Scan()
		f := scanner.Text()
		scanner.Scan()
		s := scanner.Text()
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%s %s\n",v,k)
	}
}

package main

import "fmt"

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	for i:=0; i<q; i++{
		var f,s string
		fmt.Scan(&f,&s)
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%s %s\n",v,k)
	}
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scanf("%d\n", &q)

	names := make(map[string]string)
	for i := 0; i < q; i++ {
		var oldName, newName string
		fmt.Scanf("%s %s\n", &oldName, &newName)

		priorName, exists := names[oldName]
		if exists {
			delete(names, oldName)
			oldName = priorName
		}

		names[newName] = oldName
	}

	fmt.Printf("%d\n", len(names))
	for newName, oldName := range names {
		fmt.Printf("%s %s\n", oldName, newName)
	}
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	// scanner.Split(bufio.ScanWords)

	q := nextInt()
	nameMap := make(map[string]string)

	for ; q > 0; q-- {
		names := strings.Fields(next())
		if _, ok := nameMap[names[0]]; !ok {
			nameMap[names[1]] = names[0]
		} else {
			oldName := nameMap[names[0]]
			delete(nameMap, names[0])
			nameMap[names[1]] = oldName
		}
	}

	println(len(nameMap))
	for k, v := range nameMap {
		println(v, k)
	}
}

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

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

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

package main

import "fmt"

func main() {
	var q int
	fmt.Scan(&q)
	nameMap := make(map[string]string)
	for ; q > 0; q-- {
		var oldName, newName string
		fmt.Scan(&oldName, &newName)
		oldest, exists := nameMap[oldName]
		if exists {
			delete(nameMap, oldName)
			nameMap[newName] = oldest
		} else {
			nameMap[newName] = oldName
		}
	}
	fmt.Println(len(nameMap))
	for newName, oldName := range nameMap {
		fmt.Println(oldName + " " + newName)
	}
}

package main

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

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

func main() {
  scanner := bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()
  q := scanInt(scanner)
  currentToOriginal := map[string]string{}
  for i := 0; i < q; i++ {
    scanner.Scan()
    previous := scanner.Text()
    scanner.Scan()
    current := scanner.Text()
    original, found := currentToOriginal[previous]
    if found {
      delete(currentToOriginal, previous)
      currentToOriginal[current] = original
    } else {
      currentToOriginal[current] = previous
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", len(currentToOriginal)))
  for current, original := range currentToOriginal {
    writer.WriteString(fmt.Sprintf("%s %s\n", original, current))
  }
}

package main

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

func reader(a []string) func() string {
	var ptr int = -1
	return func () string {
		ptr += 1
		return a[ptr]
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	s, _ := in.ReadString('\n')
	next := reader(strings.Fields(s))
	q, _ := strconv.Atoi(next())

	map1, map2 := make(map[string]string), make(map[string]string)

	for i := 0; i < q; i++ {
		s, _ := in.ReadString('\n')
		next = reader(strings.Fields(s))
		a, b := next(), next()
		orig, ok := map2[a]
		if !ok {
			map1[a] = b
			map2[b] = a
		}else {
			map1[orig] = b
			delete(map2, a)
			map2[b] = orig
		}
	}

	fmt.Println(len(map1))
	for key, value := range map1 {
		fmt.Println(key, value)
	}
}

package main

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

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

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

	q := nextInt()
	map1, map2 := make(map[string]string), make(map[string]string)
	for i := 0; i < q; i++ {
		a, b := next(), next()
		orig, exists := map2[a]
		if !exists {
			map1[a] = b
			map2[b] = a
		}else {
			map1[orig] = b
			delete(map2, a)
			map2[b] = orig
		}
	}

	fmt.Fprintln(out, len(map1))
	for key, value := range map1 {
		fmt.Fprintln(out, key, value)
	}
}

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

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

func nextFloat() float64 {
	ret, _ := strconv.ParseFloat(next(), 64)
	return ret
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	used := make(map[string]bool)
	new_old := make(map[string]string)

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	for i := 0; i < n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err != io.EOF {
				return
			}
		} else {
			ss := strings.Split(re.ReplaceAllString(s, ""), " ")
			if len(ss) != 2 {
				return
			}
			oh := ss[0]
			nh := ss[1]

			used[oh] = true
			if !used[nh] {
				if ooh, ok := new_old[oh]; ok {
					new_old[nh] = ooh
					delete(new_old, oh)
				} else {
					new_old[nh] = oh
				}
				used[nh] = true
			}
		}
	}

	fmt.Println(len(new_old))
	for nh, oh := range new_old {
		fmt.Println(oh, nh)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, q int
	d := make(map[string]string)

	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		var oldH, newH string

		fmt.Scan(&oldH, &newH)

		origH, exists := d[oldH]

		if exists {
			d[newH] = origH
			delete(d, oldH)
		} else {
			d[newH] = oldH
			n++
		}
	}

	fmt.Println(n)

	for newH, oldH := range d {
		fmt.Println(oldH, newH)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	newUser := make(map[string]bool)
	pastUser := make(map[string]bool)
	history := make(map[string]string)
	for i := 0; i < n; i++ {
		var from, to string
		fmt.Scan(&from, &to)
		if !pastUser[from] {
			newUser[from] = true
		}
		history[from] = to
		pastUser[to] = true
	}

	currentName := func(s string) string {
		for history[s] != "" {
			s = history[s]
		}
		return s
	}

	fmt.Println(len(newUser))
	for k := range newUser {
		fmt.Println(k, currentName(k))
	}
}

package main

import "fmt"

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

	m := make(map[string]string)
	not_begin := make(map[string]int)
	var s, t string
	for i := 0; i < n; i++ {
		fmt.Scan(&s, &t)
		m[s] = t
		not_begin[t] = 1
	}

	ans := make(map[string]string)
	cnt := 0
	for k, _ := range m {
		if _, ok := not_begin[k]; ok {
			continue
		}

		cur := k
		next := ""
		ok := true
		for ok {
			next, ok = m[cur]
			if ok {
				cur = next
			}
		}

		ans[k] = cur
		cnt++
	}

	fmt.Println(cnt)
	for k, v := range ans {
		fmt.Println(k, v)
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)

	names := make(map[string]string)

	for i := 0; i < n; i++ {
		var oldName, newName string
		fmt.Scanf("%s%s\n", &oldName, &newName)

		storedName, ok := names[oldName]

		if ok {
			delete(names, oldName)
			names[newName] = storedName
		} else {
			names[newName] = oldName
		}
	}

	fmt.Println(len(names))

	for key, value := range names {
		fmt.Println(value, key)
	}
}

package main

import "fmt"

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	for i:=0; i<q; i++{
		var f,s string
		fmt.Scanf("%s %s",&f,&s)
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%v %v\n",v,k)
	}
}

package main

import "fmt"

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	for i:=0; i<q; i++{
		var f,s string
		fmt.Scanf("%s %s",&f,&s)
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%s %s\n",v,k)
	}
}

package main

import (
    "fmt"
)

func main() {
	var q int
    fmt.Scanf("%d", &q)

	names := make(map[string]string)
	for i := 0; i < q; i++ {
		var oldName, newName string
		fmt.Scanf("%s %s", &oldName, &newName)

		priorName, exists := names[oldName]
		if exists {
			delete(names, oldName)
			oldName = priorName
		}

		names[newName] = oldName
	}

	fmt.Printf("%d\n", len(names))
	for newName, oldName := range names {
		fmt.Printf("%s %s\n", oldName, newName)
	}
}


Frog Gorf is traveling through Swamp kingdom. Unfortunately, after a poor jump, he fell into a well of $$$n$$$ meters depth. Now Gorf is on the bottom of the well and has a long way up.The surface of the well's walls vary in quality: somewhere they are slippery, but somewhere have convenient ledges. In other words, if Gorf is on $$$x$$$ meters below ground level, then in one jump he can go up on any integer distance from $$$0$$$ to $$$a_x$$$ meters inclusive. (Note that Gorf can't jump down, only up).Unfortunately, Gorf has to take a break after each jump (including jump on $$$0$$$ meters). And after jumping up to position $$$x$$$ meters below ground level, he'll slip exactly $$$b_x$$$ meters down while resting.Calculate the minimum number of jumps Gorf needs to reach ground level.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 300\,000$$$) — the depth of the well. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le i$$$), where $$$a_i$$$ is the maximum height Gorf can jump from $$$i$$$ meters below ground level. The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i \le n - i$$$), where $$$b_i$$$ is the distance Gorf will slip down if he takes a break on $$$i$$$ meters below ground level.

If Gorf can't reach ground level, print $$$-1$$$. Otherwise, firstly print integer $$$k$$$ — the minimum possible number of jumps. Then print the sequence $$$d_1,\,d_2,\,\ldots,\,d_k$$$ where $$$d_j$$$ is the depth Gorf'll reach after the $$$j$$$-th jump, but before he'll slip down during the break. Ground level is equal to $$$0$$$. If there are multiple answers, print any of them.

NoteIn the first example, Gorf is on the bottom of the well and jump to the height $$$1$$$ meter below ground level. After that he slip down by meter and stays on height $$$2$$$ meters below ground level. Now, from here, he can reach ground level in one jump.In the second example, Gorf can jump to one meter below ground level, but will slip down back to the bottom of the well. That's why he can't reach ground level.In the third example, Gorf can reach ground level only from the height $$$5$$$ meters below the ground level. And Gorf can reach this height using a series of jumps $$$10 \Rightarrow 9 \dashrightarrow 9 \Rightarrow 4 \dashrightarrow 5$$$ where $$$\Rightarrow$$$ is the jump and $$$\dashrightarrow$$$ is slipping during breaks.

package main

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

const N = 300010

var (
	n     int
	a     []int
	b     []int
	fa    [N]int
	que   [N]int
	lst   [N]int
	from  [N]int
	reach [N][]int
	head  int
	tail  int
)

func find(x int) int {
	//fmt.Printf("find %v\n", x)
	if x != fa[x] {
		fa[x] = find(fa[x])
	}
	return fa[x]
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs)+1)
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i+1] = val
	}
	arr[0] = 0
	return arr
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n = readInt(in)
	a = readArrInt(in)
	b = readArrInt(in)
	for i := 1; i <= n; i++ {
		reach[i+b[i]] = append(reach[i+b[i]], i)
		fa[i] = i
	}
	que[tail] = n
	tail++
	for _, value := range reach[n] {
		fa[value] = find(value - 1)
	}
	for head < tail {

		v := que[head]
		//fmt.Printf("bfs %d\n", v)
		if v == 0 {
			break
		}
		head++
		for i := find(v); i >= v-a[v]; i = find(i - 1) {
			//fmt.Printf("i = %d\n", i)
			lst[i+b[i]] = v
			from[i+b[i]] = i
			que[tail] = i + b[i]
			tail++
			for _, value := range reach[i+b[i]] {
				fa[find(value)] = find(value - 1)
			}
			if i <= 0 {
				tail = 0
				head = 0
				for head != n {
					que[tail] = from[head]
					tail++
					head = lst[head]
				}
				fmt.Println(tail)
				for tail > 0 {
					tail--
					fmt.Printf("%d ", que[tail])
				}
				return
			}
		}
	}
	fmt.Print(-1)
}

package main

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

var n int

var a []int
var b []int

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Fscan(r, &n)

	a = make([]int, n + 1)
	b = make([]int, n + 1)

	for j := 1; j <= n; j++ {
		fmt.Fscan(r, &a[j])
	}

	for j := 1; j <= n; j++ {
		fmt.Fscan(r, &b[j])
	}

	solve()

	fmt.Println()

}

var dp []int

var noSlip []int
var slip []int

func solve() {
	dp = make([]int, n + 1)
	noSlip = make([]int, n + 1)
	slip = make([]int, n + 1)

	for i := 0; i < n + 1; i++ {
		dp[i] = 1e9
	}

	if n - a[n] <= 0 {
		fmt.Println(1)
		fmt.Println(0)
		return
	}

	var arr []int

	for p := n; p >= n - a[n]; p-- {
		noSlip[p + b[p]] = p

		dp[p + b[p]] = 1

		arr = append(arr, p + b[p])
	}

	lo := n - a[n]

	for ;len(arr) > 0; {
		p := arr[0]
		arr = arr[1 :]

		x := a[p]

		if p - x <= 0 {

			fmt.Println(dp[p] + 1)

			k := dp[p]

			var stack []int

			stack = append(stack, 0)

			for ;k > 0; k--{
				pos := noSlip[p]
				s := slip[pos]

				stack = append(stack, pos)

				p = s
			}

			for j := len(stack) - 1; j >= 0; j-- {
				fmt.Print(stack[j])
				fmt.Print(" ")
			}

			return
		}

		if p - x < lo {

			for k := lo - 1; k >= p - x; k-- {
				to := k + b[k]

				if dp[to] == 1e9 {
					dp[to] = dp[p] + 1

					noSlip[to] = k
					slip[k] = p

					arr = append(arr, to)
				}
			}

			lo = p - x
		}
	}

	fmt.Println(-1)
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	dec := make([]int, n)
	for i := range dec {
		Fscan(in, &dec[i])
	}
	inc := make([]int, n)
	for i := range inc {
		Fscan(in, &inc[i])
	}

	vis := make([]bool, n)
	vis[n-1] = true
	type pair struct{ h, fa int }
	from := make([]pair, n)
	q := []int{n - 1}
	for step, min := 1, n-1; q != nil; step++ {
		tmp := q
		q = nil
		for _, v := range tmp {
			st := v - dec[v]
			if st < 0 {
				Fprintln(out, step)
				path := make([]int, 1, step)
				for ; v != n-1; v = from[v].fa {
					path = append(path, from[v].h+1)
				}
				for i := len(path) - 1; i >= 0; i-- {
					Fprint(out, path[i], " ")
				}
				return
			}
			if st >= min {
				continue
			}
			for h := st; h < min; h++ {
				if w := h + inc[h]; !vis[w] {
					vis[w] = true
					from[w] = pair{h, v}
					q = append(q, w)
				}
			}
			min = st
		}
	}
	Fprint(out, -1)
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	b := make([]int, n)
	for i := range b {
		Fscan(in, &b[i])
	}

	vis := make([]bool, n)
	vis[n-1] = true
	type pair struct{ h, v int }
	from := make([]pair, n)
	for i := range from {
		from[i].v = -1
	}
	min := n - 1
	q := []int{n - 1}
	for step := 1; q != nil; step++ {
		tmp := q
		q = nil
		for _, v := range tmp {
			if v < a[v] {
				Fprintln(out, step)
				path := []int{}
				for x := v; x != n-1; x = from[x].v {
					path = append(path, from[x].h)
				}
				for i := len(path) - 1; i >= 0; i-- {
					Fprint(out, path[i]+1, " ")
				}
				Fprint(out, 0)
				return
			}
			if v-a[v] >= min {
				continue
			}
			for i := v - a[v]; i < min; i++ {
				if w := i + b[i]; !vis[w] {
					vis[w] = true
					from[w] = pair{i, v}
					q = append(q, w)
				}
			}
			min = v - a[v]
		}
	}
	Fprint(out, -1)
}

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

package main

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

const N = 300010

var (
	n     int
	a     []int
	b     []int
	fa    [N]int
	que   [N]int
	lst   [N]int
	from  [N]int
	reach [N][]int
	head  int
	tail  int
)

func find(x int) int {
	//fmt.Printf("find %v\n", x)
	if x != fa[x] {
		fa[x] = find(fa[x])
	}
	return fa[x]
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs)+1)
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i+1] = val
	}
	arr[0] = 0
	return arr
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n = readInt(in)
	a = readArrInt(in)
	b = readArrInt(in)
	for i := 1; i <= n; i++ {
		reach[i+b[i]] = append(reach[i+b[i]], i)
		fa[i] = i
	}
	que[tail] = n
	tail++
	for _, value := range reach[n] {
		fa[value] = find(value - 1)
	}
	for head < tail {

		v := que[head]
		//fmt.Printf("bfs %d\n", v)
		if v == 0 {
			break
		}
		head++
		for i := find(v); i >= v-a[v]; i = find(i - 1) {
			//fmt.Printf("i = %d\n", i)
			lst[i+b[i]] = v
			from[i+b[i]] = i
			que[tail] = i + b[i]
			tail++
			for _, value := range reach[i+b[i]] {
				fa[find(value)] = find(value - 1)
			}
			if i <= 0 {
				break
			}
		}
	}
	if lst[0] == 0 {
		fmt.Print("-1")
	} else {
		tail = 0
		head = 0
		for head != n {
			que[tail] = from[head]
			tail++
			head = lst[head]
		}
		fmt.Println(tail)
		for tail > 0 {
			tail--
			fmt.Printf("%d ", que[tail])
		}
	}
}


Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order.Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer.When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.Your task is to calculate the total time it takes for Ayush to process all the orders.You can assume that the market has endless stock.

The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.

Print the only integer t — the total time needed for Ayush to process all the orders.

NoteCustomer 1 wants the items 1 and 5.pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].Time taken for the first customer is 3 + 5 = 8.Customer 2 wants the items 3 and 1.pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2].Time taken for the second customer is 3 + 3 = 6.Total time is 8 + 6 = 14.Formally pos(x) is the index of x in the current row.

package main

import "fmt"

func main() {
	var n, m, k int
	fmt.Scan(&n, &m, &k)
	p := make([]int, k)
	for i := range p {
		fmt.Scan(&p[i])
	}
	res := 0
	for i := 0; i < n*m; i++ {
		var a int
		fmt.Scan(&a)
		i := 0
		for i = range p {
			if p[i] == a {
				break
			}
		}
		res += i + 1
		for j := i; j > 0; j-- {
			p[j] = p[j-1]
		}
		p[0] = a
	}
	fmt.Println(res)
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m, k := nextInt(), nextInt(), nextInt()
	items := make([]int, k)
	totalTime := 0

	for i := range items {
		items[i] = nextInt()
	}

	for i := 0; i < n*m; i++ {
		totalTime += findPos(nextInt(), items)
	}

	println(totalTime)
}

func findPos(order int, items []int) int {
	i := 0
	pos := -1

	for ; i < len(items); i++ {
		if items[i] == order {
			pos = i + 1
			break
		}
	}

	temp := items[i]

	for ; i > 0; i-- {
		items[i] = items[i-1]
	}

	items[0] = temp

	return pos
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }


Black is gifted with a Divine array $$$a$$$ consisting of $$$n$$$ ($$$1 \le n \le 2000$$$) integers. Each position in $$$a$$$ has an initial value. After shouting a curse over the array, it becomes angry and starts an unstoppable transformation.The transformation consists of infinite steps. Array $$$a$$$ changes at the $$$i$$$-th step in the following way: for every position $$$j$$$, $$$a_j$$$ becomes equal to the number of occurrences of $$$a_j$$$ in $$$a$$$ before starting this step.Here is an example to help you understand the process better:  Initial array:$$$2$$$ $$$1$$$ $$$1$$$ $$$4$$$ $$$3$$$ $$$1$$$ $$$2$$$After the $$$1$$$-st step:$$$2$$$ $$$3$$$ $$$3$$$ $$$1$$$ $$$1$$$ $$$3$$$ $$$2$$$After the $$$2$$$-nd step:$$$2$$$ $$$3$$$ $$$3$$$ $$$2$$$ $$$2$$$ $$$3$$$ $$$2$$$After the $$$3$$$-rd step:$$$4$$$ $$$3$$$ $$$3$$$ $$$4$$$ $$$4$$$ $$$3$$$ $$$4$$$...... In the initial array, we had two $$$2$$$-s, three $$$1$$$-s, only one $$$4$$$ and only one $$$3$$$, so after the first step, each element became equal to the number of its occurrences in the initial array: all twos changed to $$$2$$$, all ones changed to $$$3$$$, four changed to $$$1$$$ and three changed to $$$1$$$.The transformation steps continue forever.You have to process $$$q$$$ queries: in each query, Black is curious to know the value of $$$a_x$$$ after the $$$k$$$-th step of transformation.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial values of array $$$a$$$. The third line of each test case contains a single integer $$$q$$$ ($$$1 \le q \le 100\,000$$$) — the number of queries. Next $$$q$$$ lines contain the information about queries — one query per line. The $$$i$$$-th line contains two integers $$$x_i$$$ and $$$k_i$$$ ($$$1 \le x_i \le n$$$; $$$0 \le k_i \le 10^9$$$), meaning that Black is asking for the value of $$$a_{x_i}$$$ after the $$$k_i$$$-th step of transformation. $$$k_i = 0$$$ means that Black is interested in values of the initial array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2000$$$ and the sum of $$$q$$$ over all test cases doesn't exceed $$$100\,000$$$.

For each test case, print $$$q$$$ answers. The $$$i$$$-th of them should be the value of $$$a_{x_i}$$$ after the $$$k_i$$$-th step of transformation. It can be shown that the answer to each query is unique.

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in [][]int) {
	values := make(map[int]int, len(in[0]))

	for iterCount := 1; iterCount < len(in); iterCount++ {
		for key := range values {
			values[key] = 0
		}

		for _, v := range in[iterCount-1] {
			values[v]++
		}

		in[iterCount] = make([]int, len(in[0]))

		//equal := true

		for i, v := range in[iterCount-1] {
			in[iterCount][i] = values[v]

			//if values[v] != v {
			//	equal = false
			//}
		}

		//if equal {
		//	break
		//}
	}
}

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

	defer out.Flush()

	n := readInt(in)

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := make([][]int, 1000)

		initialArr[0] = readArrInt(in)

		nQueries := readInt(in)

		Solve(initialArr)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)

			k := query[1]

			if query[1] >= len(initialArr) {
				k = len(initialArr) - 1
			}

			//fmt.Fprintf(out, "%+v\n", initialArr[k])
			fmt.Fprintf(out, "%d\n", initialArr[k][query[0]-1])
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

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

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

		z := [][]int{}
		z = append(z, arr)

		// build up transformations.
		for {
			t := make([]int, n)
			var modified bool
			for i := 0; i < n; i++ {
				var c int
				for k := 0; k < n; k++ {
					if z[len(z)-1][k] == z[len(z)-1][i] {
						c++
					}
				}

				t[i] = c
				if t[i] != z[len(z)-1][i] {
					modified = true
				}
			}

			if !modified {
				break
			}
			z = append(z, t)
		}

		var q int
		fmt.Fscan(in, &q)
		for i := 0; i < q; i++ {
			var x, k int
			fmt.Fscan(in, &x, &k)

			if k > len(z)-1 {
				k = len(z) - 1
			}
			fmt.Fprintln(out, z[k][x-1])
		}
	}
}

package main

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

var n, q int

var a []int

var qu[][2]int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &a[j])
		}

		fmt.Fscan(r, &q)

		qu = make([][2]int, q)

		for j := 0; j < q; j++ {
			fmt.Fscan(r, &qu[j][0], &qu[j][1])
		}

		solve()

		fmt.Println()
	}
}

const MAX = 2101

var mat [MAX][MAX]int

func solve()  {
	//2 1 1 4 3 1 1 1 2
	//2 5 5 1 1 5 5 5 2
	//2 5 5 2 2 5 5 5 2

	arr := make([]int, n + 1)

	for i :=0 ;i < n; i++ {
		arr[a[i]]++
	}

	for i := 0; i < n; i++ {
		mat[0][i] = a[i]
	}

	for k := 1; k < MAX; k++ {

		for i := 0; i < n; i++ {
			mat[k][i] = arr[mat[k - 1][i]]
		}

		arr = make([]int, n + 1)

		for i := 0; i < n; i++ {
			arr[mat[k][i]]++
		}
	}

	for i := 0; i < q; i++ {
		x, k := qu[i][0], qu[i][1]
		x--

		if k > MAX {
			k = MAX - 1
		}

		fmt.Println(mat[k][x])
	}

}

package main

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

/*
2
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
2
1 1
2
1 0
2 1000000000
---
1
2
3
3
1
2
======
1
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
---
1
2
3
3
========
1
4
5 5 1 2
8
1 2
2 2
3 2
4 2
1 20
2 20
3 20
4 20

*/

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

	var t int
	scanf("%d\n", &t)

	bld := strings.Builder{}

	for l := 0; l < t; l++ {
		var n int
		scanf("%d\n", &n)

		cnts := make(map[int]int)

		a := make([][]int, 1000)
		a[0] = make([]int, n)

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

		for i := 1; i < 1000; i++ {
			a[i] = make([]int, n)

			isChanged := false

			newCnts := make(map[int]int)
			for j := 0; j < n; j++ {
				a[i][j] = cnts[a[i-1][j]]
				newCnts[a[i][j]]++

				if a[i][j] != a[i-1][j] {
					isChanged = true
				}
			}

			if !isChanged {
				a = a[:i]
				break
			}

			cnts = newCnts
		}

		var q int
		scanf("%d\n", &q)

		for i := 0; i < q; i++ {
			var x, k int
			scanf("%d %d\n", &x, &k)

			layer := k
			if k >= len(a) {
				layer = len(a) - 1
			}

			ans := a[layer][x-1]

			fmt.Fprintf(&bld, "%d\n", ans)
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst rune
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

package main

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

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

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

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

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

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

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

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

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

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

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

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

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

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

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

	return
}

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

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

	return num
}

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

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

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

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

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

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

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

// for o := 0; o < t; o++ {
// 	var a, b, c int
// 	fmt.Fscan(input, &a, &b, &c)

// 	A := a % 2
// 	B := b % 2
// 	C := c % 2
// 	if A+B+C == 0 {
// 		fmt.Println(0)
// 	}
// 	if A+B+C == 1 {
// 		if A == 1 {
// 			fmt.Println(1)
// 		}
// 		if B == 1 {
// 			if a >= 2 {
// 				fmt.Println(0)
// 			} else {
// 				fmt.Println(2)
// 			}
// 		}
// 		if C == 1 {
// 			if a > 0
// 			fmt.Println(1)
// 		}
// 	}
// 	if A+B+C == 2 {

// 	}
// 	if A+B+C == 3 {
// 		fmt.Println(0)
// 	}

// }

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

func Ans(ARR []int) {
	if len(ARR) < 2 {
		return
	}
	if len(ARR) == 2 {
		fmt.Println(ARR[1])
		fmt.Println(-ARR[0])
	} else {
		if len(ARR) == 3 {
			first := ARR[0]
			Midd := ARR[1]
			Last := ARR[2]
			var F, M, L int
			Pos := 0
			Neg := 0
			if first > 0 {
				Pos++
			} else {
				Neg++
			}
			if Midd > 0 {
				Pos++
			} else {
				Neg++
			}
			if Last > 0 {
				Pos++
			} else {
				Neg++
			}
			if Pos*Neg != 0 {
				if ABS(first) == ABS(Midd) && ABS(Midd) == ABS(Last) {
					if first == Midd {
						if Midd == Last {
							F = -1
							M = -1
							L = 2
						} else {
							F = 1
							M = 1
							L = 2
						}
					} else {
						if first == Last {
							F = 1
							L = 1
							M = 2
						} else {
							M = 1
							L = 1
							F = 2
						}
					}
				} else {
					One := first + Midd
					Two := Midd + Last
					Thr := first + Last
					l := 0
					if One != 0 && ABS(One) <= 10000 {
						F = Last
						M = Last
						L = -One
						l = 1
					}

					if Two != 0 && ABS(Two) <= 10000 && l == 0 {
						F = -Two
						M = first
						L = first
						l = 1
					}
					if Thr != 0 && ABS(Thr) <= 10000 && l == 0 {
						F = Midd
						M = -Thr
						L = Midd
					}

				}
			} else {
				if ABS(first) == ABS(Midd) && ABS(Midd) == ABS(Last) {
					F = -1
					M = -1
					L = 2
				} else {
					One := first - Midd
					Two := Midd - Last
					Thr := first - Last
					l := 0
					if One != 0 && ABS(One) <= 10000 {
						F = -Last
						M = Last
						L = One
						l = 1
					}

					if Two != 0 && ABS(Two) <= 10000 && l == 0 {
						F = Two
						M = -first
						L = first
						l = 1
					}
					if Thr != 0 && ABS(Thr) <= 10000 && l == 0 {
						F = -Midd
						M = Thr
						L = Midd
					}

				}
			}

			fmt.Println(F)
			fmt.Println(M)
			fmt.Println(L)
		} else {
			Q := make([]int, 0)
			P := make([]int, 0)
			P = append(P, ARR[0], ARR[1])
			p := len(ARR)
			for i := 2; i < p; i++ {
				Q = append(Q, ARR[i])
			}
			Ans(P)
			Ans(Q)
		}
	}
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)
		DDD := make([]int, 2001)

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

		B := make([][]int, 0)
		B = append(B, ARR)
		for j := 1; j < 100; j++ {
			C := make([]int, 0)
			NewD := make([]int, 2001)
			for i := 0; i < n; i++ {
				q := DDD[ARR[i]]
				C = append(C, q)
				NewD[q]++

			}
			DDD = NewD
			B = append(B, C)
			ARR = C

		}

		var q int
		fmt.Fscan(input, &q)
		for i := 0; i < q; i++ {
			var a, b int64
			fmt.Fscan(input, &a, &b)
			if b >= 100 {
				fmt.Println(B[99][a-1])
			} else {
				fmt.Println(B[b][a-1])

			}
		}

	}

}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in []int, q []int) int {
	var iterCount int

	input := make([]int, len(in))
	copy(input, in)

	if q[1] == 0 {
		return input[q[0]-1]
	}

	values := make(map[int]int, len(input))

	for iterCount < q[1] {
		for key := range values {
			values[key] = 0
		}

		for _, v := range input {
			values[v]++
		}

		equal := true

		for i, v := range input {
			input[i] = values[v]

			if values[v] != v {
				equal = false
			}
		}

		if equal {
			return input[q[0]-1]
		}

		iterCount++
		fmt.Printf("iter: %d values: %+v", iterCount, input)
	}

	return input[q[0]-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n := readInt(in)

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := readArrInt(in)

		nQueries := readInt(in)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)
			res := Solve(initialArr, query)

			fmt.Printf("%d\n", res)
		}
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in []int, q []int) int {
	var iterCount int

	if q[1] == 0 {
		return in[q[0]-1]
	}

	values := make(map[int]int, len(in))

	for iterCount < q[1] {
		for key := range values {
			values[key] = 0
		}

		for _, v := range in {
			values[v]++
		}

		equal := true

		for i, v := range in {
			in[i] = values[v]

			if values[v] != v {
				equal = false
			}
		}

		if equal {
			return in[q[0]-1]
		}

		iterCount++
	}

	return in[q[0]-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n := readInt(in)

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := readArrInt(in)

		nQueries := readInt(in)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)
			res := Solve(initialArr, query)

			fmt.Printf("%d\n", res)
		}
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in []int, q []int) int {
	var iterCount int

	if q[1] == 0 {
		return in[q[0]-1]
	}

	for iterCount < q[1] {
		values := make(map[int]int, len(in))

		for _, v := range in {
			values[v]++
		}

		equal := true

		for i, v := range in {
			in[i] = values[v]

			if values[v] != v {
				equal = false
			}
		}

		if equal {
			return in[q[0]-1]
		}

		iterCount++
	}

	return in[q[0]-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n := readInt(in)

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := readArrInt(in)

		nQueries := readInt(in)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)
			res := Solve(initialArr, query)

			fmt.Printf("%d\n", res)
		}
	}
}


You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays:   $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$;  $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$;  $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$;  $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$;  and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j < 0$$$. If there are no $$$j$$$ such that $$$p_j < 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$) — the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.

Print $$$n$$$ integers — the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j < 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.

NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j < 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k < 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.

package main

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

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

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

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

var err error

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

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

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

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

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

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

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

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

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

func Gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return Gcd(b, a%b)
}

const mod = 1000000007

func Add(a, b int64) int64 {
	return (a + b) % mod
}

func Mult(a, b int64) int64 {
	return (a * b) % mod
}

func Sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

func solve() {
	// x, y, k := nextLong()-1, nextLong(), nextLong()
	// printf("%d\n", k+((y+1)*k+x-2)/x)
	n := nextInt()
	arr := make([]int, n)
	for i := range arr {
		arr[i] = nextInt()
	}
	lock := make([]int, n)
	tmp := []int{}
	for i, v := range arr {
		lock[i] = nextInt()
		if lock[i] == 0 {
			tmp = append(tmp, v)
		}
	}
	sort.Ints(tmp)
	ind := n - 1
	for _, v := range tmp {
		for lock[ind] == 1 {
			ind--
		}
		arr[ind] = v
		ind--
	}

	for _, v := range arr {
		printf("%d ", v)
	}
	printf("\n")
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "foreign"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create(name + ".out")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// 	printf("Case #%d:\n", i)
		solve()
	}

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

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		L := readNNums(reader, n)
		solve(n, A, L)
		for i := 0; i < n; i++ {
			buf.WriteString(fmt.Sprintf("%d ", A[i]))
		}
		buf.WriteByte('\n')
	}

	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int, L []int) {
	ps := make([]Pair, 0, n)
	for i := 0; i < n; i++ {
		if L[i] == 0 {
			ps = append(ps, Pair{A[i], i})
		}
	}

	sort.Sort(Pairs(ps))
	var j int
	for i := 0; i < n; i++ {
		if L[i] == 0 {
			A[i] = ps[j].first
			j++
		}
	}
}

type Pair struct {
	first, second int
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].first > ps[j].first
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}

package main

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

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

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

func main() {
	defer writer.Flush()

	var t int
	var n int
	var arr []int
	var b []int
	var un []int

	var ans []int
	scanf("%d\n", &t)
	for t > 0 {
		scanf("%d\n", &n)
		arr = make([]int, n)
		b = make([]int, n)
		un = make([]int, 0)
		ans = make([]int, n)
		for i := 0; i < n; i++ {
			scanf("%d", &arr[i])
		}
		scanf("\n")
		for i := 0; i < n; i++ {
			scanf("%d", &b[i])
			if b[i] == 0 {
				un = append(un, arr[i])
			}
		}
		scanf("\n")
		sort.Sort(sort.Reverse(sort.IntSlice(un)))
		// printf("%v\n", arr)
		// printf("%v\n", un)
		j := 0
		for i := 0; i < n || j < len(un); i++ {
			if b[i] == 1 {
				ans[i] = arr[i]
			} else {
				ans[i] = un[j]
				j++
			}
		}
		for _, v := range ans {
			printf("%v ", v)
		}
		printf("\n")
		t--
	}
}

/*
URL:
https://codeforces.com/contest/1418/problem/B
*/

package main

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

var (
	t int

	n int
	A []int
	L []int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n = readi()
		A = readis(n)
		L = readis(n)

		solve()
	}
}

func solve() {
	S := Sum(A...)

	if S < 0 {
		fmt.Println(PrintIntsLine(A...))
		return
	}

	B := []int{}
	for i := 0; i < n; i++ {
		if L[i] == 0 {
			B = append(B, A[i])
		}
	}

	sort.Sort(sort.Reverse(sort.IntSlice(B)))

	idx := 0
	for i := 0; i < n; i++ {
		if L[i] == 0 {
			A[i] = B[idx]
			idx++
		}
	}

	fmt.Println(PrintIntsLine(A...))
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
)

// modi can calculate a right residual whether value is positive or negative.
func modi(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// modll can calculate a right residual whether value is positive or negative.
func modll(val, m int64) int64 {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

/********** bufio setting **********/

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

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

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

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

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

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

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	tt := rw.NextInt()
	for i := 0; i < tt; i++ {
		solveCase()
	}
}
func solveCase() {
	n := rw.NextInt()
	a := rw.NextInts(n)
	b := rw.NextInts(n)
	c := make([]int, 0)
	for i, v := range a {
		if b[i] == 0 {
			c = append(c, -v)
		}
	}
	sort.Ints(c)
	j := 0
	for i := 0; i < len(a); i++ {
		if b[i] == 0 {
			a[i] = -c[j]
			j++
		}
	}
	rw.PrintlnInts(a...)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		n := readInt()
		a := readIntArray(n)
		b := readIntArray(n)
		c := make([]int, 0)
		for i, v := range a {
			if b[i] == 0 {
				c = append(c, -v)
			}
		}
		sort.Ints(c)
		j := 0
		for i := 0; i < len(a); i++ {
			if b[i] == 0 {
				a[i] = -c[j]
				j++
			}
		}
		for i := 0; i < len(a); i++ {
			if i > 0 {
				fmt.Print(" ")
			}
			fmt.Print(a[i])
		}
		fmt.Println()
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

// 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 {
		v    int
		lock bool
	}
	f := func(a []int) int {
		ans, s := -1, 0
		for i, v := range a {
			s += v
			if s < 0 {
				ans = i
			}
		}
		return ans + 1
	}

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]pair, n)
		for i := range a {
			Fscan(in, &a[i].v)
		}
		var b, ans1, ans2 []int
		for i, p := range a {
			Fscan(in, &a[i].lock)
			if !a[i].lock {
				b = append(b, p.v)
			}
		}
		sort.Ints(b)
		l, r := 0, len(b)-1
		for _, p := range a {
			if p.lock {
				ans1 = append(ans1, p.v)
				ans2 = append(ans2, p.v)
			} else {
				ans1 = append(ans1, b[l])
				l++
				ans2 = append(ans2, b[r])
				r--
			}
		}
		if f(ans1) < f(ans2) {
			for _, v := range ans1 {
				Fprint(out, v, " ")
			}
		} else {
			for _, v := range ans2 {
				Fprint(out, v, " ")
			}
		}
		Fprintln(out)
	}
}

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

package main

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

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

func sort(a, b []int, n int) []int {
	var ind = 0
	for j := 0; j < n-1; j++ {
		for ;j < n-1; j++ {
			if b[j] == 0 {
				ind = j
				break
			}
		}
		for i := j+1; i < n; i++ {
			if a[i] > a[ind] && b[i] == 0{
				ind = i;
			}
		}
		if b[ind] + b[j] == 0 && a[ind] > a[j] && ind > j {
			a[ind], a[j] = a[j], a[ind]
		}
	}
	return a
}

func main() {
defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var a = make([]int, n)
		var b = make([]int, n)
		for i:=0; i< n; i++ {
			scan(&a[i])
		}
		for i:=0; i< n; i++ {
			scan(&b[i])
		}

		a = sort(a,b,n)
		for i:=0; i < n ; i++ {
			print(a[i], " ")
		}
		print("\n")
	}
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	tt := rw.NextInt()
	for i := 0; i < tt; i++ {
		solveCase()
	}
}

func solveCase() {
	n := rw.NextInt()
	a := rw.NextInts(n)
	b := rw.NextInts(n)
	var c []int
	for i := 0; i < n; i++ {
		if b[i] == 0 {
			c = append(c, -a[i])
		}
	}
	sort.Ints(c)
	for i, j := 0, 0; i < n; i++ {
		if b[i] == 0 {
			a[i] = -c[j]
			j++
		}
	}
	rw.PrintlnInts(a...)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

/*
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1

1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
*/

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

	var T, N, target int64
	for Fscan(in, &T); T > int64(0); T-- {
		var A []int64
		var AUnlock []int64
		var B []int64
		Fscan(in, &N)

		for i := 0; i < int(N); i++ {
			Fscan(in, &target)
			A = append(A, target)
		}

		for i := 0; i < int(N); i++ {
			Fscan(in, &target)
			B = append(B, target)
			if target == 0 {
				AUnlock = append(AUnlock, A[i])
			}
		}

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

		i, j := 0, 0
		for ; i < len(B); i++ {
			if B[i] == 1 {
				Fprintf(out, "%d ", A[i])
			}else {
				Fprintf(out, "%d ", AUnlock[j])
				j++
			}
		}
		Fprintf(out, "\n")
	}

}

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

package main

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

func solve(r *bufio.Reader, w *bufio.Writer) {
	var n int
	fmt.Fscanln(r, &n)
	a := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(r, "%d ", &a[i])
	}
	fmt.Fscanf(r, "%d\n", &a[n-1])
	lock := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(r, "%d ", &lock[i])
	}
	fmt.Fscanf(r, "%d\n", &lock[n-1])
	shouldSort := make([]int, 0, n)
	for i := 0; i < n; i++ {
		if lock[i] == 0 {
			shouldSort = append(shouldSort, a[i])
		}
	}
	sort.Ints(shouldSort)
	k := len(shouldSort) - 1
	for i := 0; i < n; i++ {
		if lock[i] == 1 {
			continue
		}
		a[i] = shouldSort[k]
		k -= 1
	}
	for i := 0; i < n-1; i++ {
		fmt.Fprintf(w, "%d ", a[i])
	}
	fmt.Fprintf(w, "%d\n", a[n-1])
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscanln(reader, &t)
	for ; t > 0; t-- {
		solve(reader, writer)
	}
}

package main

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

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		n := readInt()
		a := readIntArray(n)
		b := readIntArray(n)
		c := make([]int, 0)
		for i, v := range a {
			if b[i] == 0 {
				c = append(c, v)
			}
		}
		sort.Ints(c)
		j := 0
		for i := 0; i < len(a); i++ {
			if b[i] == 0 {
				a[i] = c[j]
				j++
			}
		}
		for i := 0; i < len(a); i++ {
			if i > 0 {
				fmt.Print(" ")
			}
			fmt.Print(a[i])
		}
		fmt.Println()
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

func sort(a, b []int, n int) []int {
	var ind = 0
	for j := 0; j < n-1; j++ {
		for ;j < n-1; j++ {
			if b[j] == 0 {
				ind = j
				break
			}
		}
		for i := j+1; i < n; i++ {
			if a[i] > a[ind] && b[i] == 0{
				ind = i;
			}
		}
		if b[ind] + b[j] == 0 && a[ind] > a[j] {
			a[ind], a[j] = a[j], a[ind]
		}
	}
	return a
}

func main() {
defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var a = make([]int, n)
		var b = make([]int, n)
		for i:=0; i< n; i++ {
			scan(&a[i])
		}
		for i:=0; i< n; i++ {
			scan(&b[i])
		}

		a = sort(a,b,n)
		for i:=0; i < n ; i++ {
			print(a[i], " ")
		}
		print("\n")
	}
}

package main

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

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

func sort(a, b []int, n int) []int {
	var ind = 0
	for j := 0; j < n-1; j++ {
		for ;j < n-1; j++ {
			if b[j] == 0 {
				ind = j
				break
			}
		}
		for i := j+1; i < n; i++ {
			if a[i] > a[ind] && b[i] == 0{
				ind = i;
			}
		}
		if b[ind] + b[j] == 0 {
			a[ind], a[j] = a[j], a[ind]
		}
	}
	return a
}

func main() {
defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var a = make([]int, n)
		var b = make([]int, n)
		for i:=0; i< n; i++ {
			scan(&a[i])
		}
		for i:=0; i< n; i++ {
			scan(&b[i])
		}

		a = sort(a,b,n)
		for i:=0; i < n ; i++ {
			print(a[i], " ")
		}
		print("\n")
	}
}

package main

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

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

func sort(a, b []int, n int) []int {
	var ind = -1
	var pind = ind
	for j := 0; j < n-1; j++ {
		for ;j < n-1; j++ {
			if b[j] == 0 {
				ind = j
				break
			}
		}
		if ind > pind {
			for i := j+1; i < n; i++ {
				if a[i] > a[ind] && b[i] == 0{
					ind = i;
				}
			}
			a[ind], a[j] = a[j], a[ind]
		}
		pind = ind
	}
	return a
}

func main() {
defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var a = make([]int, n)
		var b = make([]int, n)
		for i:=0; i< n; i++ {
			scan(&a[i])
		}
		for i:=0; i< n; i++ {
			scan(&b[i])
		}

		a = sort(a,b,n)
		for i:=0; i < n ; i++ {
			print(a[i], " ")
		}
		print("\n")
	}
}

package main

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

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

func sort(a, b []int, n int) []int {
	var ind = -1
	var pind = ind
	for j := 0; j < n-1; j++ {
		for ;j < n-1; j++ {
			if b[j] == 0 {
				ind = j
				break
			}
		}
		if ind > pind {
			for i := j+1; i < n; i++ {
				if a[i] < a[ind] && b[i] == 0{
					ind = i;
				}
			}
			a[ind], a[j] = a[j], a[ind]
		}
		pind = ind
	}
	return a
}

func main() {
defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var a = make([]int, n)
		var b = make([]int, n)
		for i:=0; i< n; i++ {
			scan(&a[i])
		}
		for i:=0; i< n; i++ {
			scan(&b[i])
		}

		a = sort(a,b,n)
		for i:=0; i < n ; i++ {
			print(a[i], " ")
		}
		print("\n")
	}
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	tt := rw.NextInt()
	for i := 0; i < tt; i++ {
		solveCase()
	}
}

func solveCase() {
	n := rw.NextInt()
	a := rw.NextInts(n)
	b := rw.NextInts(n)
	var c []int
	for i := 0; i < n; i++ {
		if b[i] == 0 {
			c = append(c, -a[i])
		}
	}
	sort.Ints(c)
	for i, j := 0, 0; i < n; i++ {
		if b[i] == 0 {
			a[i] = c[j]
			j++
		}
	}
	rw.PrintlnInts(a...)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

/*
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1

1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
*/

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

	var T, N, target int64
	var A []int64
	var AUnlock []int64
	var B []int64
	for Fscan(in, &T); T > int64(0); T-- {
		Fscan(in, &N)

		for i := 0; i < int(N); i++ {
			Fscan(in, &target)
			A = append(A, target)
		}

		for i := 0; i < int(N); i++ {
			Fscan(in, &target)
			B = append(B, target)
			if target == 0 {
				AUnlock = append(AUnlock, target)
			}
		}

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

		i, j := 0, 0
		for ; i < len(B); i++ {
			if B[i] == 1 {
				Fprintf(out, "%d ", A[i])
			}else {
				Fprintf(out, "%d ", AUnlock[j])
				j++
			}
		}
		Fprintf(out, "\n")
	}

}

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

package main

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

func solve(r *bufio.Reader, w *bufio.Writer) {
	var n int
	fmt.Fscanln(r, &n)
	a := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(r, "%d ", &a[i])
	}
	fmt.Fscanf(r, "%d\n", &a[n-1])
	lock := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(r, "%d ", &lock[i])
	}
	fmt.Fscanf(r, "%d\n", &lock[n-1])
	shouldSort := make([]int, 0, n)
	for i := 0; i < n; i++ {
		if lock[i] == 0 {
			shouldSort = append(shouldSort, a[i])
		}
	}
	sort.Ints(shouldSort)
	k := 0
	for i := 0; i < n; i++ {
		if lock[i] == 1 {
			continue
		}
		a[i] = shouldSort[k]
		k += 1
	}
	for i := 0; i < n-1; i++ {
		fmt.Fprintf(w, "%d ", a[i])
	}
	fmt.Fprintf(w, "%d\n", a[n-1])
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscanln(reader, &t)
	for ; t > 0; t-- {
		solve(reader, writer)
	}
}


Inaka has a disc, the circumference of which is $$$n$$$ units. The circumference is equally divided by $$$n$$$ points numbered clockwise from $$$1$$$ to $$$n$$$, such that points $$$i$$$ and $$$i + 1$$$ ($$$1 \leq i < n$$$) are adjacent, and so are points $$$n$$$ and $$$1$$$.There are $$$m$$$ straight segments on the disc, the endpoints of which are all among the aforementioned $$$n$$$ points.Inaka wants to know if her image is rotationally symmetrical, i.e. if there is an integer $$$k$$$ ($$$1 \leq k < n$$$), such that if all segments are rotated clockwise around the center of the circle by $$$k$$$ units, the new image will be the same as the original one.

The first line contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 100\,000$$$, $$$1 \leq m \leq 200\,000$$$) — the number of points and the number of segments, respectively. The $$$i$$$-th of the following $$$m$$$ lines contains two space-separated integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) that describe a segment connecting points $$$a_i$$$ and $$$b_i$$$. It is guaranteed that no segments coincide.

Output one line — "Yes" if the image is rotationally symmetrical, and "No" otherwise (both excluding quotation marks). You can output each letter in any case (upper or lower).

NoteThe first two examples are illustrated below. Both images become the same as their respective original ones after a clockwise rotation of $$$120$$$ degrees around the center.

package main

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

type Integers2 []int

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

func Newton(a []int, x int) int {
	n2 := len(a) - 1
	if n2 == -1 {
		return -1
	}
	n1 := 0
	var n int
	if x < a[n1] || x > a[n2] {
		return -1
	}
	if x == a[n1] {
		return n1
	}
	if x == a[n2] {
		return n2
	}
	for {
		if n2-n1 < 3 {
			for i := n1 + 1; i < n2; i++ {
				if x == a[i] {
					return i
				}
			}
			return -1
		}
		n = (n1 + n2) / 2
		if x == a[n] {
			return n
		}
		if x > a[n] {
			n1 = n
		} else {
			n2 = n
		}

	}
}

func main() {
	read := bufio.NewReader(os.Stdin)
	write := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscan(read, &n, &m)
	ti := make([]bool, n)
	thord := make([][]int, n)
	ver := make([]int, 2*m)
	a := make([][2]int, m)

	var i int
	for i = 0; i < m; i++ {
		var x, y int
		fmt.Fscan(read, &x, &y)
		var h [2]int

		if x < y {
			h = [2]int{x, y}
		} else {
			h = [2]int{y, x}
		}

		a[i] = h
		ti[x-1] = true
		ti[y-1] = true
		thord[h[0]-1] = append(thord[h[0]-1], h[1])
		ver[2*i] = x
		ver[2*i+1] = y
	}

	//sort.Sort(Integers2(a))
	for i = 0; i < m; i++ {
		sort.Sort(Integers2(thord[a[i][0]-1]))
	}

	sort.Sort(Integers2(ver))
	//resb := false
	res := "NO"
	//var i int32
	startx := ver[0]

	//nrutin := 0
	//intCh := make(chan int, nx) //Канал
	ver2 := make([]int, n)
	tx := -1
	nx := 0
	for _, val := range ver {
		if val == tx {
			continue
		}
		ver2[nx] = val
		tx = val
		nx++
	}

	nx2 := nx/2 + 1

	for i := 1; i < nx2; i++ {
		/*
			if resb {
				break
			}
		*/
		/*
			if ver[i] == tx {
				continue
			}
		*/
		tx = ver2[i]
		h := tx - startx

		//nrutin++
		//go func(h int) {
		r := true
		var j int
		for j = 0; j < m; j++ {
			/*
				if resb {
					intCh <- 5
					return
				}
			*/

			x := a[j][0] + h
			if x > n {
				x = x - n
			}
			y := a[j][1] + h
			if y > n {
				y = y - n
			}

			if !ti[x-1] || !ti[y-1] {
				r = false
				break
			}

			amin := x
			amax := y
			if amax < amin {
				amax = x
				amin = y
			}

			ch := Newton(thord[amin-1], amax)

			if ch == -1 {
				r = false
				break
			}

		}

		if r {
			//resb = true
			res = "YES"
			break
		}
		//intCh <- 5
		//}(hh)
	}
	/*
		for i := 0; i < nrutin; i++ {
			_ = <-intCh

		}
	*/
	fmt.Fprint(write, res)
	write.Flush()

}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1147B(reader io.Reader, writer io.Writer) {
	calcMinPeriod := func(pattern []int) int {
		n := len(pattern)
		maxMatchLengths := make([]int, n)
		maxLength := 0
		for i := 1; i < n; i++ {
			c := pattern[i]
			for maxLength > 0 && pattern[maxLength] != c {
				maxLength = maxMatchLengths[maxLength-1]
			}
			if pattern[maxLength] == c {
				maxLength++
			}
			maxMatchLengths[i] = maxLength
		}
		if val := maxMatchLengths[n-1]; val > 0 {
			if n%(n-val) == 0 {
				return n / (n - val)
			}
		}
		return 0
	}
	calcGCD := func(a, b int64) int64 {
		for b > 0 {
			a, b = b, a%b
		}
		return a
	}
	calcLCM := func(a, b int64) int64 {
		return a / calcGCD(a, b) * b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	lenPosMat := make([][]int, n/2+1)
	for ; m > 0; m-- {
		var a, b int
		Fscan(in, &a, &b)
		if a > b {
			a, b = b, a
		}
		segLen := b - a
		pos := a
		if segLen > n-segLen {
			segLen = n - segLen
			pos = b
		}
		lenPosMat[segLen] = append(lenPosMat[segLen], pos)
	}

	k := int64(-1)
	for segLen, posList := range lenPosMat {
		if len(posList) == 0 {
			continue
		}
		var p int
		if segLen*2 < n {
			sort.Ints(posList)
			posList = append(posList, posList[0]+n)
			diffList := make([]int, len(posList)-1)
			for i := range diffList {
				diffList[i] = posList[i+1] - posList[i]
			}
			p = calcMinPeriod(diffList)
			if p == 0 {
				Fprint(out, "No")
				return
			}
		} else if segLen*2 == n {
			p = 2
		} else {
			break
		}
		if n%p > 0 {
			Fprint(out, "No")
			return
		}
		p = n / p
		if k == -1 {
			k = int64(p)
		} else {
			k = calcLCM(k, int64(p))
		}
		if k >= int64(n) {
			Fprint(out, "No")
			return
		}
	}
	Fprint(out, "Yes")
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1147B(reader io.Reader, writer io.Writer) {
	calcMinPeriod := func(pattern []int) int {
		n := len(pattern)
		maxMatchLengths := make([]int, n)
		maxLength := 0
		for i := 1; i < n; i++ {
			c := pattern[i]
			for maxLength > 0 && pattern[maxLength] != c {
				maxLength = maxMatchLengths[maxLength-1]
			}
			if pattern[maxLength] == c {
				maxLength++
			}
			maxMatchLengths[i] = maxLength
		}
		if val := maxMatchLengths[n-1]; val > 0 {
			if n%(n-val) == 0 {
				return n / (n - val)
			}
		}
		return 0
	}
	calcGCD := func(a, b int64) int64 {
		for b > 0 {
			a, b = b, a%b
		}
		return a
	}
	calcLCM := func(a, b int64) int64 {
		return a / calcGCD(a, b) * b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	lenPosMat := make([][]int, n/2+5)
	for ; m > 0; m-- {
		var a, b int
		Fscan(in, &a, &b)
		if a > b {
			a, b = b, a
		}
		segLen := b - a
		pos := a
		if segLen > n-segLen {
			segLen = n - segLen
			pos = b
		}
		lenPosMat[segLen] = append(lenPosMat[segLen], pos)
	}

	k := int64(-1)
	for segLen, posList := range lenPosMat {
		if len(posList) == 0 {
			continue
		}
		var p int
		if segLen*2 < n {
			sort.Ints(posList)
			posList = append(posList, posList[0]+n)
			diffList := make([]int, len(posList)-1)
			for i := range diffList {
				diffList[i] = posList[i+1] - posList[i]
			}
			p = calcMinPeriod(diffList)
			if p == 0 {
				Fprint(out, "No")
				return
			}
		} else if segLen*2 == n {
			p = 2
		} else {
			break
		}
		if n%p > 0 {
			Fprint(out, "No")
			return
		}
		p = n / p
		if k == -1 {
			k = int64(p)
		} else {
			k = calcLCM(k, int64(p))
		}
		if k >= int64(n) {
			Fprint(out, "No")
			return
		}
	}
	Fprint(out, "Yes")
}

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

package main

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

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

func map_equals(a, b map[int]bool) bool {
	for k, v := range a {
		val, ok := b[k]
		if ok == false || val != v {
			return false
		}
	}
	return true
}

func z_algorithm(vals []map[int]bool) []int {
	z := make([]int, len(vals))
	var left, right int
	for i := 1; i < len(vals); i++ {
		if i <= right {
			z[i] = min(z[i-left], right-i+1)
		}
		for i+z[i] < len(vals) && map_equals(vals[z[i]], vals[i+z[i]]) {
			z[i]++
		}
		if i+z[i]-1 > right {
			left = i
			right = i + z[i] - 1
		}
	}
	return z
}

func main() {

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	vals := make([]map[int]bool, n)
	for i := 0; i < n; i++ {
		vals[i] = make(map[int]bool)
	}
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Fscanf(reader, "%d %d\n", &a, &b)
		a--
		b--
		vals[a][(a-b+n)%n] = true
		vals[b][(b-a+n)%n] = true
	}
	for i := 0; i < n; i++ {
		vals = append(vals, vals[i])
	}
	z := z_algorithm(vals)
	//fmt.Printf("%v ", z)
	var res bool
	for i := 0; i < 2*n; i++ {
		if i != n && z[i] >= n {
			res = true
		}
	}
	if res {
		fmt.Print("Yes\n")
	} else {
		fmt.Print("No\n")
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	segs := make([][]int, n+1)
	for i := 0; i < n; i++ {
		segs[i] = make([]int, 0)
	}
	for i := 0; i < m; i++ {
		l, r := 0, 0
		fmt.Fscanf(in, "%d %d\n", &l, &r)
		diff1 := ((r + n) - l) % n
		diff2 := ((l + n) - r) % n
		if diff2 > diff1 {
			segs[diff1] = append(segs[diff1], l)
		} else {
			segs[diff2] = append(segs[diff2], r)
		}
	}
	ans := n
	for i := 0; i < n+1; i++ {
		if len(segs[i]) == 1 && i != n/2 {
			fmt.Fprintf(out, "No\n")
			return
		}
	}
	for i := 0; i < n+1; i++ {
		if len(segs[i]) > 0 {
			q := findPattern(segs[i], n)
			if i == n/2 {
				if q < 2 {
					q = 2
				}
			}
			ans = gcd(ans, q)
			//fmt.Println(ans)
			if ans < 2 {
				fmt.Fprintf(out, "No\n")
				return
			}
		}
	}
	fmt.Fprintf(out, "Yes\n")
	return
}

func findPattern(a []int, m int) int {
	sort.Ints(a)
	for i := 1; i <= len(a)/2; i++ {
		if len(a)%i == 0 {
			if work(a, i, m) {
				return len(a) / i
			}
		}
	}
	return 1
}

func work(a []int, l int, m int) bool {
	//fmt.Println(a, l, m)
	n := len(a)
	numOfSeg := n / l
	for i := 1; i < numOfSeg; i++ {
		if a[i*l]-a[(i-1)*l] != (a[0] + m - a[(numOfSeg-1)*l]) {
			//fmt.Println(a, l, "debug")
			return false
		}
	}
	for j := 1; j < l; j++ {
		for i := 1; i < n/l; i++ {
			if a[i*l+j]-a[i*l+j-1] != a[j]-a[j-1] {
				//fmt.Println(a, l, "debug2")
				return false
			}
		}
	}
	return true
}

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

package main

import "fmt"

/*
type Integers []int

func (s Integers) Len() int {
	return len(s)
}
func (s Integers) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func (s Integers) Less(i, j int) bool {
	return s[i] < s[j]
}
*/

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	var ti = make([]int, n+1)
	var a []int
	for i := 0; i < m; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		a = append(a, x)
		a = append(a, y)
		ti[x] = 1
		ti[y] = 1
	}

	//sort.Sort(Integers(a))

	m = m * 2
	res := "NO"
	for i := 1; i < n; i++ {
		b := make([]int, m)
		copy(b, a)
		r := true
		for j := 0; j < m; j++ {
			b[j] = b[j] + i
			if b[j] > n {
				b[j] = b[j] - n
			}
			if ti[b[j]] != 1 {
				r = false
				break
			}
		}

		if r {
			res = "YES"
			break
		}
	}
	fmt.Print(res)
}

package main

import "fmt"

/*
type Integers []int

func (s Integers) Len() int {
	return len(s)
}
func (s Integers) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func (s Integers) Less(i, j int) bool {
	return s[i] < s[j]
}
*/

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	var ti = make([]bool, n+1)
	/*
		var thord [][]bool
		for i := 0; i <= n; i++ {
			line := make([]bool, n+1)
			thord = append(thord, line)
		}
	*/

	var a [][]int
	for i := 0; i < m; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		h := []int{x, y}
		a = append(a, h)
		ti[x] = true
		ti[y] = true
	}

	//sort.Sort(Integers(a))

	res := "NO"
	for i := 1; i < n; i++ {

		r := true
		for j := 0; j < m; j++ {
			x := a[j][0] + i
			if x > n {
				x = x - n
			}
			y := a[j][1] + i
			if y > n {
				y = y - n
			}
			if !ti[x] || !ti[y] {
				r = false
				break
			}
			for k := 0; k < m; k++ {
				if (a[k][0] == x && a[k][1] == y) ||
					(a[k][0] == y && a[k][1] == x) {
					break
				}

			}

		}

		if r {
			res = "YES"
			break
		}
	}
	fmt.Print(res)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1147B(reader io.Reader, writer io.Writer) {
	calcMinPeriod := func(pattern []int) int {
		n := len(pattern)
		maxMatchLengths := make([]int, n)
		maxLength := 0
		for i := 1; i < n; i++ {
			c := pattern[i]
			for maxLength > 0 && pattern[maxLength] != c {
				maxLength = maxMatchLengths[maxLength-1]
			}
			if pattern[maxLength] == c {
				maxLength++
			}
			maxMatchLengths[i] = maxLength
		}
		if val := maxMatchLengths[n-1]; val > 0 {
			if n%(n-val) == 0 {
				return n / (n - val)
			}
		}
		return 0
	}
	calcGCD := func(a, b int64) int64 {
		for b > 0 {
			a, b = b, a%b
		}
		return a
	}
	calcLCM := func(a, b int64) int64 {
		return a / calcGCD(a, b) * b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	lenPosMat := make([][]int, n/2+5)
	for ; m > 0; m-- {
		var a, b int
		Fscan(in, &a, &b)
		if a > b {
			a, b = b, a
		}
		segLen := b - a
		pos := a
		if segLen > n-segLen {
			segLen = n - segLen
			pos = b
		}
		lenPosMat[segLen] = append(lenPosMat[segLen], pos)
	}

	k := int64(-1)
	for segLen, posList := range lenPosMat {
		if len(posList) == 0 {
			continue
		}
		var p int64
		if segLen*2 < n {
			sort.Ints(posList)
			posList = append(posList, posList[0]+n)
			diffList := make([]int, len(posList)-1)
			for i := range diffList {
				diffList[i] = posList[i+1] - posList[i] - segLen
			}
			p = int64(calcMinPeriod(diffList))
			if p == 0 {
				Fprint(out, "No")
				return
			}
		} else if segLen*2 == n {
			p = int64(segLen)
		} else {
			break
		}
		if k == -1 {
			k = p
		} else {
			k = calcLCM(k, p)
		}
		if k >= int64(n) {
			Fprint(out, "No")
			return
		}
	}
	Fprint(out, "Yes")
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1147B(reader io.Reader, writer io.Writer) {
	calcMinPeriod := func(pattern []int) int {
		n := len(pattern)
		maxMatchLengths := make([]int, n)
		maxLength := 0
		for i := 1; i < n; i++ {
			c := pattern[i]
			for maxLength > 0 && pattern[maxLength] != c {
				maxLength = maxMatchLengths[maxLength-1]
			}
			if pattern[maxLength] == c {
				maxLength++
			}
			maxMatchLengths[i] = maxLength
		}
		if val := maxMatchLengths[n-1]; val > 0 {
			if val%(n-val) == 0 {
				return val/(n-val) + 1
			}
		}
		return 0
	}
	calcGCD := func(a, b int64) int64 {
		for b > 0 {
			a, b = b, a%b
		}
		return a
	}
	calcLCM := func(a, b int64) int64 {
		return a / calcGCD(a, b) * b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	lenPosMat := make([][]int, n/2+1)
	for ; m > 0; m-- {
		var a, b int
		Fscan(in, &a, &b)
		if a > b {
			a, b = b, a
		}
		segLen := b - a
		pos := a
		if segLen > n-segLen {
			segLen = n - segLen
			pos = b
		}
		lenPosMat[segLen] = append(lenPosMat[segLen], pos)
	}

	k := int64(-1)
	for segLen, posList := range lenPosMat {
		if len(posList) == 0 {
			continue
		}
		var p int64
		if segLen*2 < n {
			sort.Ints(posList)
			posList = append(posList, posList[0]+n)
			diffList := make([]int, len(posList)-1)
			for i := range diffList {
				diffList[i] = posList[i+1] - posList[i] - segLen
			}
			p = int64(calcMinPeriod(diffList))
			if p == 0 {
				Fprint(out, "No")
				return
			}
		} else if segLen*2 == n {
			p = int64(segLen)
		} else {
			break
		}
		if k == -1 {
			k = p
		} else {
			k = calcLCM(k, p)
		}
		if k >= int64(n) {
			Fprint(out, "No")
			return
		}
	}
	Fprint(out, "Yes")
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	segs := make([][]int, n+1)
	for i := 0; i < n; i++ {
		segs[i] = make([]int, 0)
	}
	for i := 0; i < m; i++ {
		l, r := 0, 0
		fmt.Fscanf(in, "%d %d\n", &l, &r)
		diff := ((r + n) - l) % n
		segs[diff] = append(segs[diff], l)
	}
	ans := n
	for i := 0; i < n+1; i++ {
		if len(segs[i]) == 1 {
			fmt.Fprintf(out, "No\n")
			return
		}
	}
	for i := 0; i < n+1; i++ {
		if len(segs[i]) > 0 {
			q := findPattern(segs[i], n)
			if i == n/2 {
				if q < 2 {
					q = 2
				}
			}
			ans = gcd(ans, q)
			//fmt.Println(ans)
			if ans < 2 {
				fmt.Fprintf(out, "No\n")
				return
			}
		}
	}
	fmt.Fprintf(out, "Yes\n")
	return
}

func findPattern(a []int, m int) int {
	sort.Ints(a)
	for i := 1; i <= len(a)/2; i++ {
		if len(a)%i == 0 {
			if work(a, i, m) {
				return len(a) / i
			}
		}
	}
	return 1
}

func work(a []int, l int, m int) bool {
	//fmt.Println(a, l, m)
	n := len(a)
	numOfSeg := n / l
	for i := 1; i < numOfSeg; i++ {
		if a[i*l]-a[(i-1)*l] != (a[0] + m - a[(numOfSeg-1)*l]) {
			//fmt.Println(a, l, "debug")
			return false
		}
	}
	for j := 1; j < l; j++ {
		for i := 1; i < n/l; i++ {
			if a[i*l+j]-a[i*l+j-1] != a[j]-a[j-1] {
				//fmt.Println(a, l, "debug2")
				return false
			}
		}
	}
	return true
}

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	segs := make([][]int, n+1)
	for i := 0; i < n; i++ {
		segs[i] = make([]int, 0)
	}
	for i := 0; i < m; i++ {
		l, r := 0, 0
		fmt.Fscanf(in, "%d %d\n", &l, &r)
		diff1 := ((r + n) - l) % n
		diff2 := ((l + n) - r) % n
		if diff2 > diff1 {
			segs[diff1] = append(segs[diff1], l)
		} else {
			segs[diff2] = append(segs[diff2], r)
		}
	}
	ans := n
	for i := 0; i < n+1; i++ {
		if len(segs[i]) == 1 {
			fmt.Fprintf(out, "No\n")
			return
		}
	}
	for i := 0; i < n+1; i++ {
		if len(segs[i]) > 0 {
			q := findPattern(segs[i], n)
			if i == n/2 {
				if q < 2 {
					q = 2
				}
			}
			ans = gcd(ans, q)
			//fmt.Println(ans)
			if ans < 2 {
				fmt.Fprintf(out, "No\n")
				return
			}
		}
	}
	fmt.Fprintf(out, "Yes\n")
	return
}

func findPattern(a []int, m int) int {
	sort.Ints(a)
	for i := 1; i <= len(a)/2; i++ {
		if len(a)%i == 0 {
			if work(a, i, m) {
				return len(a) / i
			}
		}
	}
	return 1
}

func work(a []int, l int, m int) bool {
	//fmt.Println(a, l, m)
	n := len(a)
	numOfSeg := n / l
	for i := 1; i < numOfSeg; i++ {
		if a[i*l]-a[(i-1)*l] != (a[0] + m - a[(numOfSeg-1)*l]) {
			//fmt.Println(a, l, "debug")
			return false
		}
	}
	for j := 1; j < l; j++ {
		for i := 1; i < n/l; i++ {
			if a[i*l+j]-a[i*l+j-1] != a[j]-a[j-1] {
				//fmt.Println(a, l, "debug2")
				return false
			}
		}
	}
	return true
}

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	segs := make([][]int, n+1)
	for i := 0; i < n; i++ {
		segs[i] = make([]int, 0)
	}
	for i := 0; i < m; i++ {
		l, r := 0, 0
		fmt.Fscanf(in, "%d %d\n", &l, &r)
		diff := ((r + n) - l) % n
		segs[diff] = append(segs[diff], l)
	}
	ans := n
	for i := 0; i < n+1; i++ {
		if len(segs[i]) == 1 {
			fmt.Fprintf(out, "No\n")
			return
		}
	}
	for i := 0; i < n+1; i++ {
		if len(segs[i]) > 0 {
			q := findPattern(segs[i])
			ans = gcd(ans, q)
			if n < 2 {
				fmt.Fprintf(out, "No\n")
				return
			}
		}
	}
	fmt.Fprintf(out, "Yes\n")
	return
}

func findPattern(a []int) int {
	sort.Ints(a)
	for i := 1; i <= len(a)/2; i++ {
		if len(a)%i == 0 {
			if work(a, i) {
				return i
			}
		}
	}
	return 1
}

func work(a []int, l int) bool {
	n := len(a)
	for j := 1; j < l; j++ {
		for i := 1; i < n/l; i++ {
			if a[i*l+j]-a[i*l+j-1] != a[j]-a[j-1] {
				return false
			}
		}
	}
	return true
}

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}