A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$:  $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$.  $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$.  $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$.  $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.

For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.

// 00
package main

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

// not exact
func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, n, "\n")

		for i := 1; i <= n; i++ {
			write(f, i)
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, i, " ")
				}
			}
			write(f, k, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, strconv.Itoa(n), "\n")

		for i := 1; i <= n; i++ {
			write(f, strconv.Itoa(i))
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, strconv.Itoa(i), " ")
				}
			}
			write(f, strconv.Itoa(k), "\n")
		}
	}
}

// 00
package main

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

// func write(f *bufio.Writer, a ...interface{}) {
// 	f.Write([]byte(fmt.Sprint(a...)))
// }
func write(f *bufio.Writer, a ...string) {
	for i := 0; i < len(a); i++ {
		f.Write([]byte(a[i]))
	}
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, strconv.Itoa(n), "\n")

		for i := 1; i <= n; i++ {
			write(f, strconv.Itoa(i))
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, strconv.Itoa(i), " ")
				}
			}
			write(f, strconv.Itoa(k), "\n")
		}
	}
}

// 00
package main

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

// func write(f *bufio.Writer, a ...interface{}) {
// 	f.Write([]byte(fmt.Sprint(a...)))
// }
func write(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, strconv.Itoa(n))
		write(f, "\n")

		for i := 1; i <= n; i++ {
			write(f, strconv.Itoa(i))
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, strconv.Itoa(i))
					write(f, " ")
				}
			}
			write(f, strconv.Itoa(k))
			write(f, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, n, "\n")

		for i := 1; i <= n; i++ {
			write(f, strconv.Itoa(i))
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, strconv.Itoa(i), " ")
				}
			}
			write(f, k, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, n, "\n")

		for i := 1; i <= n; i++ {
			write(f, i)
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, i, " ")
				}
			}
			write(f, k, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		write(f, n, "\n")

		for i := 1; i <= n; i++ {
			f.Write([]byte(strconv.Itoa(i)))
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					write(f, i, " ")
				}
			}
			write(f, k, "\n")
		}
	}
}

// 00
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		f.Write([]byte(strconv.Itoa(n)))
		f.Write([]byte("\n"))
		for i := 1; i <= n; i++ {
			f.Write([]byte(strconv.Itoa(i)))
			if i != n {
				f.Write([]byte(" "))
			}
		}
		f.Write([]byte("\n"))
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					f.Write([]byte(strconv.Itoa(i)))
					f.Write([]byte(" "))
				}
			}
			f.Write([]byte(strconv.Itoa(k)))
			f.Write([]byte("\n"))
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

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

func printSlice(s []int) {
	fmt.Println(strings.Trim(fmt.Sprintf("%v", s), "[]"))
}

package main

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

type CF1716B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1716B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1716B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1716B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1716B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1716B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1716B
Date: 8/7/2022
User: pepradere
URL: https://codeforces.com/problemset/problem/1716/B
Problem: CF1716B
**/
func (in *CF1716B) Run() {
	var ans strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		pos := n
		ans.WriteString(fmt.Sprintf("%d\n", n))
		for j := 0; j < n; j++ {
			val := 1
			for i := 1; i <= n; i++ {
				if i == pos {
					ans.WriteString(fmt.Sprintf("%d ", n))
					pos--
				} else {
					ans.WriteString(fmt.Sprintf("%d ", val))
					val++
				}
			}
			ans.WriteString("\n")
		}
	}
	fmt.Print(ans.String())
}

func NewCF1716B(r *bufio.Reader) *CF1716B {
	return &CF1716B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1716B(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1716B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1716B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1716B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1716B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1716B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1716B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1716B
Date: 8/7/2022
User: pepradere
URL: https://codeforces.com/problemset/problem/1716/B
Problem: CF1716B
**/
func (in *CF1716B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		var ans strings.Builder
		pos := n
		ans.WriteString(fmt.Sprintf("%d\n", n))
		for j := 0; j < n; j++ {
			val := 1
			for i := 1; i <= n; i++ {
				if i == pos {
					ans.WriteString(fmt.Sprintf("%d ", n))
					pos--
				} else {
					ans.WriteString(fmt.Sprintf("%d ", val))
					val++
				}
			}
			ans.WriteString("\n")
		}
		fmt.Print(ans.String())
	}
}

func NewCF1716B(r *bufio.Reader) *CF1716B {
	return &CF1716B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1716B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var n, d int
	fmt.Scanf("%d\n", &n)
	for n > 0 {
		fmt.Scanf("%d\n", &d)
		temp := make([]string, d)
		for i := 0; i < d; i++ {
			temp[i] = strconv.Itoa(i + 1)
		}
		fmt.Println(d)
		fmt.Println(strings.Join(temp, " "))
		for i := 0; i < d-1; i++ {
			temp[i], temp[i+1] = temp[i+1], temp[i]
			fmt.Println(strings.Join(temp, " "))
		}
		n--
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, i, j int
	var ch byte
	a := make([]int, 105)
	fmt.Fscan(in, &t)
	for t != 0 {
		fmt.Fscan(in, &n)
		for i = 1; i <= n; i++ {
			a[i] = i
		}
		fmt.Fprintln(out, n)
		for i = n; i > 0; i-- {
			if i != n {
				a[i], a[i+1] = a[i+1], a[i]
			}
			for j = 1; j <= n; j++ {
				if j == n {
					ch = '\n'
				} else {
					ch = ' '
				}
				fmt.Fprintf(out, "%d%c", a[j], ch)
			}
		}
		t--
	}
}

package main

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

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var ntests int
	fmt.Fscan(in, &ntests)
	for iii := 0; iii < ntests; iii++ {
		var n int
		fmt.Fscan(in, &n)
		res := n - 2 + 1
		// if n != 2 {
		// 	res++
		// }
		vals := make([]int, n)
		for i := 0; i < n; i++ {
			vals[i] = i + 1
		}
		fmt.Fprintln(out, res+1)
		for ip := 0; ip < n; ip++ {
			fmt.Fprintf(out, "%d ", vals[ip])
		}
		fmt.Fprintln(out)
		for i := 1; i <= res; i++ {
			vals[i-1], vals[i] = vals[i], vals[i-1]
			for ip := 0; ip < n; ip++ {
				fmt.Fprintf(out, "%d ", vals[ip])
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

func readLine() string {
	s, err := in.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readLines(n int) []string {
	res := make([]string, n)
	for i := 0; i < n; i++ {
		res[i] = readLine()
	}
	return res
}

func readInt() int {
	n, err := strconv.ParseInt(readLine(), 10, 32)
	if err != nil {
		panic(err)
	}
	return int(n)
}

func readInt2() (int, int) {
	as := readInts(2)
	return as[0], as[1]
}

func readInt3() (int, int, int) {
	as := readInts(3)
	return as[0], as[1], as[2]
}

func readUint() uint64 {
	n, err := strconv.ParseUint(readLine(), 10, 64)
	if err != nil {
		panic(err)
	}
	return n
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

func readStrings(n int) []string {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	return as
}

var in = bufio.NewReader(os.Stdin)

func main() {
	t := readInt()

	for i := 0; i < t; i++ {
		n := readInt()
		a := make([]string, n)
		for j := 0; j < n; j++ {
			a[j] = fmt.Sprint(j + 1)
		}
		fmt.Println(n)
		for j := 0; j < n; j++ {
			fmt.Println(strings.Join(a, " "))
			if j < n-1 {
				a[j], a[j+1] = a[j+1], a[j]
			}
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		fmt.Fprintln(out,n)
		list:= make([]int,n)
		for j:=1;j<=n;j++{
			list[j-1]=j
			fmt.Fprintf(out,"%v ",j)
		}
		fmt.Fprintln(out,)
		for j:=0;j<n-1;j++{
			for k:=j;k<=j;k++{
				list[j],list[n-1] = list[n-1],list[j]
			}
			for l:=0;l<len(list);l++{
				fmt.Fprintf(out,"%v ",list[l])
			}
			fmt.Fprintln(out,)
		}
	}
}

package main

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

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

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

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

		fmt.Fprintln(out, n)
		for i := 0; i < n; i++ {
			for _, v := range aa {
				fmt.Fprintf(out, "%d ", v)
			}
			fmt.Fprintln(out)

			if i < n-1 {
				aa[i], aa[i+1] = aa[i+1], aa[i]
			}
		}
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	var n int
	scan(&n)
	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		a[i] = i
	}
	printf("%d\n", n)
	for t := 1; t <= n; t++ {
		for i := 1; i <= n; i++ {
			printf("%d ", a[i])
		}
		printf("\n")
		//swap
		id := t + 1
		if id > n {
			id = n - 1
		}
		tmp := a[t]
		a[t] = a[id]
		a[id] = tmp
	}
}

func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

// https://codeforces.com/problemset/problem/1716/B

// 1716B - Permutation Chain

// A permutation of length n is a sequence of integers from 1 to n such that each integer appears in it exactly once.
// Let the fixedness of a permutation p be the number of fixed points in it — the number of positions j such that pj=j, where pj is the j-th element of the permutation p.
// You are asked to build a sequence of permutations a1,a2,…, starting from the identity permutation (permutation a1=[1,2,…,n]). Let's call it a permutation chain. Thus, ai is the i-th permutation of length n.
// For every i from 2 onwards, the permutation ai should be obtained from the permutation ai−1 by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation ai should be strictly lower than the fixedness of the permutation ai−1.
// Consider some chains for n=3:
//  • a1=[1,2,3], a2=[1,3,2] — that is a valid chain of length 2. From a1 to a2, the elements on positions 2 and 3 get swapped, the fixedness decrease from 3 to 1.
//  • a1=[2,1,3], a2=[3,1,2] — that is not a valid chain. The first permutation should always be [1,2,3] for n=3.
//  • a1=[1,2,3], a2=[1,3,2], a3=[1,2,3] — that is not a valid chain. From a2 to a3, the elements on positions 2 and 3 get swapped but the fixedness increase from 1 to 3.
//  • a1=[1,2,3], a2=[3,2,1], a3=[3,1,2] — that is a valid chain of length 3. From a1 to a2, the elements on positions 1 and 3 get swapped, the fixedness decrease from 3 to 1. From a2 to a3, the elements on positions 2 and 3 get swapped, the fixedness decrease from 1 to 0.
// Find the longest permutation chain. If there are multiple longest answers, print any of them.

// Input
// The first line contains a single integer t (1≤t≤99) — the number of testcases.
// The only line of each testcase contains a single integer n (2≤n≤100) — the required length of permutations in the chain.

// Output
// For each testcase, first, print the length of a permutation chain k.
// Then print k permutations a1,a2,…,ak. a1 should be an identity permutation of length n ([1,2,…,n]). For each i from 2 to k, ai should be obtained by swapping two elements in ai−1. It should also have a strictly lower fixedness than ai−1.

// Example
// input
// 2
// 2
// 3
// output
// 2
// 1 2
// 2 1
// 3
// 1 2 3
// 3 2 1
// 3 1 2

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var c, t, n, i, j int
	var a []int

	for c, _ = fmt.Fscan(reader, &t); c == 1; c, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &n)
			a = make([]int, n)

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

			fmt.Fprintln(writer, n)

			for i = 0; i < n-1; i++ {
				for j = 0; j < n-1; j++ {
					fmt.Fprint(writer, a[j], " ")
				}

				fmt.Fprintln(writer, a[n-1])

				a[i], a[i+1] = a[i+1], a[i]
			}

			for i = 0; i < n-1; i++ {
				fmt.Fprint(writer, a[i], " ")
			}

			fmt.Fprintln(writer, a[n-1])
		}
	}

	writer.Flush()
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		w1 := do(n)
		fmt.Fprintln(w, len(w1))
		for _, v := range w1 {
			for _, v := range v {
				fmt.Fprint(w, v, " ")
			}
			fmt.Fprintln(w)
		}
	}
}

func do(n int) [][]int {
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		j, k := 1, 0
		for j <= i {
			a[i][k] = j + 1
			j++
			k++
		}
		a[i][k] = 1
		k++
		for j < n {
			a[i][k] = j + 1
			j++
			k++
		}
	}
	return a
}

package main

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

func solve() {
	n := get()
	a := make([]int, n)
	for i := range a {
		a[i] = i + 1
	}
	putln(n)
	putaln(a)
	for i := 1; i < n; i++ {
		a[i], a[i-1] = a[i-1], a[i]
		putaln(a)
	}
}

const (
	once = false
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

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

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func putaln(a []int) {
	for _, v := range a {
		put(v)
		putc(' ')
	}
	putc('\n')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		arr[i] = i + 1
	}
	fmt.Fprintln(out, n)
	for i := range arr {
		fmt.Fprint(out, arr[i], " ")
	}
	fmt.Fprintln(out)
	for i := 1; i < n; i++ {
		arr[i-1], arr[i] = arr[i], arr[i-1]
		for i := range arr {
			fmt.Fprint(out, arr[i], " ")
		}
		fmt.Fprintln(out)
	}
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func printArray(a []int) string {
	return strings.Trim(strings.Replace(fmt.Sprint(a), ",", "", -1), "[]")
}

func solve(n int) string {
	var rs []string
	rs = append(rs, fmt.Sprint(n))
	a := make([]int, n)
	for i := range a {
		a[i] = i + 1
	}
	rs = append(rs, printArray(a))
	for i := 1; i < n; i++ {
		a[i], a[i-1] = a[i-1], a[i]
		rs = append(rs, printArray(a))
	}
	return strings.Join(rs, "\n")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var t, n int

	fmt.Scanf("%d\n", &t)

	for test := 0; test < t; test++ {
		fmt.Scanf("%d\n", &n)
		fmt.Println(n)
		a := make([]int, n)

		for i := 0; i < len(a); i++ {
			a[i] = i + 1
		}

		printSlice(a)

		// First swap
		a[len(a)-1], a[len(a)-2] = a[len(a)-2], a[len(a)-1]
		printSlice(a)

		for i := len(a) - 2; i > 0; i-- {
			a[i-1], a[i] = a[i], a[i-1]
			printSlice(a)
		}

	}
}

func printSlice(s []int) {
	fmt.Println(strings.Trim(fmt.Sprintf("%v", s), "[]"))
}

package main

import (

	"bufio"
	"os"
	"fmt"
)

var (

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

func main() {

	defer out.Flush()
	var T int
	fmt.Fscan(in, &T)

	for i := 0; i < T; i++ {

		var (
			n int
			a []int
		)
		fmt.Fscan(in, &n)
		fmt.Fprintln(out, n)
		for j := 1; j <= n; j++ {

			a = append(a, j)
		}

		for j := 1; j <= n; j++ {

			for k := 0; k < n; k++ {

				fmt.Fprint(out, a[k], " ")
			}
			fmt.Fprintln(out)
			if j == n {

				continue
			}
			var x = a[j]
			a[j] = a[j - 1]
			a[j - 1] = x
		}
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	fmt.Fprintln(ww, n)
	ans := make([]int, n)
	for i := range ans {
		ans[i] = i + 1
	}
	for i:=0;i<len(ans);i++ {
		fmt.Fprintf(ww, "%d ", ans[i])
	}
	fmt.Fprintln(ww)
	for i:=1;i<n;i++ {
		ans[i-1], ans[i] = ans[i], ans[i-1]
		for i:=0;i<len(ans);i++ {
			fmt.Fprintf(ww, "%d ", ans[i])
		}
		fmt.Fprintln(ww)
	}
}

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

// 00
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		for i := 1; i <= n; i++ {
			fmt.Print(i)
			if i != n {
				fmt.Print(" ")
			}
		}
		fmt.Println()
		for k := n - 1; k >= 1; k-- {
			for i := 1; i <= n; i++ {
				if i != k {
					fmt.Print(i, " ")
				}
			}
			fmt.Println(k)
		}
	}
}

package main

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

type CF1716B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1716B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1716B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1716B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1716B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1716B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1716B
Date: 8/7/2022
User: pepradere
URL: https://codeforces.com/problemset/problem/1716/B
Problem: CF1716B
**/
func (in *CF1716B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		var ans strings.Builder
		pos := n
		for j := 0; j < n; j++ {
			val := 1
			for i := 1; i <= n; i++ {
				if i == pos {
					ans.WriteString(fmt.Sprintf("%d ", n))
					pos--
				} else {
					ans.WriteString(fmt.Sprintf("%d ", val))
					val++
				}
			}
			ans.WriteString("\n")
		}
		fmt.Print(ans.String())
	}
}

func NewCF1716B(r *bufio.Reader) *CF1716B {
	return &CF1716B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1716B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var n, d int
	fmt.Scanf("%d\n", &n)
	for n > 0 {
		fmt.Scanf("%d\n", &d)
		temp := make([]string, d)
		for i := 0; i < d; i++ {
			temp[i] = strconv.Itoa(i + 1)
		}
		fmt.Println(strings.Join(temp, " "))
		for i := 0; i < d-1; i++ {
			temp[i], temp[i+1] = temp[i+1], temp[i]
			fmt.Println(strings.Join(temp, " "))
		}
		n--
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, i, j int
	var ch byte
	a := make([]int, 105)
	fmt.Fscan(in, &t)
	for t != 0 {
		fmt.Fscan(in, &n)
		for i = 1; i <= n; i++ {
			a[i] = i
		}
		for i = n; i > 0; i-- {
			if i != n {
				a[i], a[i+1] = a[i+1], a[i]
			}
			for j = 1; j <= n; j++ {
				if j == n {
					ch = '\n'
				} else {
					ch = ' '
				}
				fmt.Fprintf(out, "%d%c", a[j], ch)
			}
		}
		t--
	}
}

package main

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

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var ntests int
	fmt.Fscan(in, &ntests)
	for iii := 0; iii < ntests; iii++ {
		var n int
		fmt.Fscan(in, &n)
		res := n - 2 + 1
		// if n != 2 {
		// 	res++
		// }
		vals := make([]int, n)
		for i := 0; i < n; i++ {
			vals[i] = i + 1
		}
		fmt.Fprintln(out, res)
		for ip := 0; ip < n; ip++ {
			fmt.Fprintf(out, "%d ", vals[ip])
		}
		fmt.Fprintln(out)
		for i := 1; i <= res; i++ {
			vals[i-1], vals[i] = vals[i], vals[i-1]
			for ip := 0; ip < n; ip++ {
				fmt.Fprintf(out, "%d ", vals[ip])
			}
			fmt.Fprintln(out)
		}
	}
}


Adieu l'ami.Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.The space is represented by a rectangular grid of n × m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 r1 c1 r2 c2" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, "2 r1 c1 r2 c2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n × m area.Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 r1 c1 r2 c2" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.And you're here to tell Koyomi the feasibility of each of his attempts.

The first line of input contains three space-separated integers n, m and q (1 ≤ n, m ≤ 2 500, 1 ≤ q ≤ 100 000) — the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively. The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 ≤ t ≤ 3, 1 ≤ r1, r2 ≤ n, 1 ≤ c1, c2 ≤ m) — the type and two coordinates of an action. Additionally, the following holds depending on the value of t:    If t = 1: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1;  If t = 2: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1, the specified group of barriers exist on the ground before the removal.  If t = 3: no extra restrictions.

For each of Koyomi's attempts (actions with t = 3), output one line — containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.

NoteFor the first example, the situations of Koyomi's actions are illustrated below.

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	source := rand.NewSource(time.Now().UnixNano())
	r := rand.New(source)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			x := r.Int63n(1 << 20) + 1
			y := r.Int63n(1 << 20) + 1
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			if idx == 0 {
				for i:=1;i<=1; {}
			}
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005]pair

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 30)
			y := r.Int63n(1 << 30)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q] = pair{x,y}
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			x := hash[idx].first
			y := hash[idx].second
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 25)
			y := r.Int63n(1 << 25)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			if idx == 0 {
				for i:=1;i<=1; {}
			}
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 20)
			y := r.Int63n(1 << 20)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			if idx == 0 {
				for i:=1;i<=1; {}
			}
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 20) + 1
			y := r.Int63n(1 << 20) + 1
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			if idx == 0 {
				for i:=1;i<=1; {}
			}
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 27)
			y := r.Int63n(1 << 27)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			if idx == 0 {
				for i:=1;i<=1; {}
			}
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 62)
			y := r.Int63n(1 << 62)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005]pair

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 62)
			y := r.Int63n(1 << 62)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q] = pair{x,y}
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			x := hash[idx].first
			y := hash[idx].second
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 27)
			y := r.Int63n(1 << 27)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005][2]int64

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 20) + 1
			y := r.Int63n(1 << 20) + 1
			if x == y {
				panic("err")
			}
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q][0] = x
			hash[q][1] = y
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			if idx == 0 {
				for i:=1;i<=1; {}
			}
			x := hash[idx][0]
			y := hash[idx][1]
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

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 toInt(buf []byte) (o int) {
    for _, v := range buf {
        o = o*10 + int(v-'0')
    }
    return
}

type pair struct {
	first int64
	second int64
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

type rect struct{
	a,b,c,d int
}

var mp map[rect]int
var tree [2503][2503][2]int64
var hash [100005]pair

func update(r int,c int, p int64, q int64) {
	tr := r
	tc := c
	for r = tr; r <=2501; r += (r & (-r)) {
		for c = tc; c<=2501; c += (c & (-c)) {
			tree[r][c][0] += p
			tree[r][c][1] += q
			//fmt.Println(tree[r][c].first, tree[r][c].second)
		}
	}
}

func read(r int,c int) (int64,int64) {
	var x,y int64
	tr := r
	tc := c
	for r = tr; r > 0; r -= (r & (-r)) {
		for c = tc; c > 0; c -= (c & (-c)) {
			x += tree[r][c][0]
			y += tree[r][c][1]
			//fmt.Println(tree[r][c].first,tree[r][c].second)
		}
	}
	return x,y
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n := toInt(scanner.Bytes())
	scanner.Scan()
	m := toInt(scanner.Bytes())
	scanner.Scan()
	n,m = m,n
	q := toInt(scanner.Bytes())
	mp = make(map[rect]int)
	for ;q > 0; q-- {
		scanner.Scan()
		t := toInt(scanner.Bytes())
		scanner.Scan()
		r1 := toInt(scanner.Bytes())
		scanner.Scan()
		c1 := toInt(scanner.Bytes())
		scanner.Scan()
		r2 := toInt(scanner.Bytes())
		scanner.Scan()
		c2 := toInt(scanner.Bytes())
		//fmt.Println(t,r1,c1,r2,c2)
		if  t == 1 {
			source := rand.NewSource(time.Now().UnixNano())
			r := rand.New(source)
			x := r.Int63n(1 << 27)
			y := r.Int63n(1 << 27)
			update(r2 + 1,c2 + 1,x,y)
			update(r1, c2 + 1,-x,-y)
			update(r2 + 1, c1,-x,-y)
			update(r1, c1,x,y)
			mp[rect{r1,c1,r2,c2}] = q
			hash[q] = pair{x,y}
		}
		if t == 2 {
			idx := mp[rect{r1,c1,r2,c2}]
			x := hash[idx].first
			y := hash[idx].second
			update(r2 + 1,c2 + 1,-x,-y)
			update(r1, c2 + 1,x,y)
			update(r2 + 1, c1,x,y)
			update(r1, c1,-x,-y)
		}
		if t == 3 {
			x,y := read(r1,c1)
			z,t := read(r2,c2)
			//fmt.Println(x,y,z,t)
			if (x == z) && (y == t) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}


We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.

The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$

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)

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

		f := true
		var sum int64
		for i := 0; i < n; i++ {
			if i > 0 && sum == 0 && a[i] > 0 || sum < 0 {
				f = false
				break
			}
			sum += a[i]
		}

		if sum != 0 {
			f = false
		}
		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

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)

		a := make([]int64, n+1)
		g := make([]int64, n+1)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
			g[i] = a[i] + g[i-1]
		}

		if g[n] != 0 {
			fmt.Fprintln(out, "NO")
			continue
		}

		f := true
		for i := 1; i <= n; i++ {
			if g[i] < 0 {
				f = false
				break
			}
		}

		var vis bool
		for i := 1; i <= n; i++ {
			if g[i] == 0 {
				vis = true
			} else if vis {
				f = false
				break
			}

		}
		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		incarr,decarr := make([]int64,N),make([]int64,N)
		ans := "Yes"
		incarr[0] = int64(A[0])
		for i:=1;i<N;i++ {
			decarr[i] = incarr[i-1]
			incarr[i] = int64(A[i]) + decarr[i]
		}
		if incarr[N-1] != 0 { ans = "No" }
		for _,a := range incarr { if a < 0 { ans = "No" } }
		for _,a := range decarr { if a < 0 { ans = "No" } }
		foundZero := false
		for _,a := range incarr { if a == 0 { foundZero = true } else if foundZero { ans = "No" } }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
outer:
	for iter := 0; iter < ntests; iter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slice := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		last := n - 1
		for ; last > 0 && slice[last] == 0; last-- {
		}
		if last == 0 {
			if slice[last] == 0 {
				fmt.Fprintln(writer, "Yes")
				continue outer
			} else {
				fmt.Fprintln(writer, "No")
				continue outer
			}
		} else {
			tmp := slice[last]
			if tmp >= 0 {
				fmt.Fprintln(writer, "No")
				continue outer
			}
			for i := last - 1; i >= 0; i-- {
				tmp = slice[i] + tmp
				if i == 0 {
					if tmp != 0 {
						fmt.Fprintln(writer, "No")
						continue outer
					} else {
						fmt.Fprintln(writer, "Yes")
						continue outer
					}
				} else {
					if tmp >= 0 {
						fmt.Fprintln(writer, "No")
						continue outer
					}
				}
			}
		}
	}
}

package main

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

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

const No = "No"
const Yes = "Yes"

func solve() {
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	for i := range nums {
		fmt.Fscan(r, &nums[i])
	}
	var cur int64 = 0
	flag := false
	for _, v := range nums {
		if flag && v != 0 {
			fmt.Fprintln(w, No)
			return
		}
		cur += int64(v)
		if cur < 0 {
			fmt.Fprintln(w, No)
			return
		} else if cur == 0 {
			flag = true
		}
	}
	if cur != 0 {
		fmt.Fprintln(w, No)
	} else {
		fmt.Fprintln(w, Yes)
	}

}
func main() {
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

var a = [200010]int64{}
var n, m, k int64

var scanner = bufio.NewScanner(os.Stdin)
const maxCapacity = 1024*1024*4
var buf= make([]byte, maxCapacity)

func init()  {
	scanner.Buffer(buf, maxCapacity)
}

func min(a ...int) int{
	ans := a[0]
	for _, val := range a {
		if ans > val {
			ans = val
		}
	}
	return ans
}

func scan(arr []int64) {
	scanner.Scan()
	str := strings.Split(scanner.Text(), " ")
	for i:= 0; i < min(len(str), len(arr)); i++ {
		arr[i], _ = strconv.ParseInt(str[i], 10, 64)
	}
}

func scanf(arr ...*int64) {
	scanner.Scan()
	str := strings.Split(scanner.Text(), " ")
	for i:= 0; i < min(len(str), len(arr)); i++ {
		*arr[i], _ = strconv.ParseInt(str[i], 10, 64)
	}
}

func check() bool{
	flg, cnt := true, int64(0)
	flag := false
	for i := n-1; i >= 0; i-- {
		cnt += a[i]
		if cnt > 0 {
			return false
		}
		if cnt == 0 && flag{
			if flg {
				flg = false
			} else {
				return false
			}
		}
		if cnt != 0 {
			flag = true
		}
		//fmt.Println(cnt)
	}
	return cnt == 0
}

func solve()  {
	//fmt.Scanln(&n)
	scanf(&n)
	scan(a[:n])

	if check() {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func main() {
	var t int64
	scanf(&t)
	//fmt.Scanln(&t)
	//fmt.Println(t)
	for i := 0; int64(i) < t; i++ {
		solve()
	}
}

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n int
	Fscan(r, &t)
	for t > 0 {
		t--
		Fscan(r, &n)
		a := make([]int, n)
		for i := range a{
			Fscan(r, &a[i])
		}
		s := 0
		k := 0
		for i := n - 1; i >= 0; i--{
			if a[i] != 0{
				k = i
				break
			}
		}
		for i := k; i >= 0; i--{
			s += a[i]
			if s >= 0 && i != 0|| s != 0 && i == 0{
				Fprintln(w, "NO")
				s = 999999999
				break
			}
		}
		if s != 999999999{
			Fprintln(w, "YES")
		}
	}
}

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)

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

		if g[n] != 0 {
			fmt.Fprintln(out, "NO")
			continue
		}

		f := true
		for i := 1; i <= n; i++ {
			if g[i] < 0 {
				f = false
				break
			}
		}

		var vis bool
		for i := 1; i <= n; i++ {
			if g[i] == 0 {
				vis = true
			} else if vis {
				f = false
				break
			}

		}
		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

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)

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

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

		if g[n] != 0 {
			fmt.Fprintln(out, "NO")
			continue
		}

		f := true
		var vis bool
		for i := 1; i < n+1; i++ {
			if g[i] == 0 {
				vis = true
			} else if vis || g[i] < 0 {
				f = false
				break
			}

		}
		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		incarr,decarr := ia(N),ia(N)
		ans := "Yes"
		incarr[0] = A[0]
		for i:=1;i<N;i++ {
			decarr[i] = incarr[i-1]
			incarr[i] = A[i] + decarr[i]
		}
		if incarr[N-1] != 0 { ans = "NO" }
		for _,a := range incarr { if a < 0 { ans = "No" } }
		for _,a := range decarr { if a < 0 { ans = "No" } }
		foundZero := false
		for _,a := range incarr { if a == 0 { foundZero = true } else if foundZero { ans = "No" } }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

var a = [100010]int64{}
var n, m, k int64

var scanner = bufio.NewScanner(os.Stdin)
const maxCapacity = 1024*1024*4
var buf= make([]byte, maxCapacity)

func init()  {
	scanner.Buffer(buf, maxCapacity)
}

func min(a ...int) int{
	ans := a[0]
	for _, val := range a {
		if ans > val {
			ans = val
		}
	}
	return ans
}

func scan(arr []int64) {
	scanner.Scan()
	str := strings.Split(scanner.Text(), " ")
	for i:= 0; i < min(len(str), len(arr)); i++ {
		arr[i], _ = strconv.ParseInt(str[i], 10, 64)
	}
}

func scanf(arr ...*int64) {
	scanner.Scan()
	str := strings.Split(scanner.Text(), " ")
	for i:= 0; i < min(len(str), len(arr)); i++ {
		*arr[i], _ = strconv.ParseInt(str[i], 10, 64)
	}
}

func check() bool{
	flg, cnt := true, int64(0)
	flag := false
	for i := n-1; i >= 0; i-- {
		cnt += a[i]
		if cnt > 0 {
			return false
		}
		if cnt == 0 && flag{
			if flg {
				flg = false
			} else {
				return false
			}
		}
		if cnt != 0 {
			flag = true
		}
		//fmt.Println(cnt)
	}
	return cnt == 0
}

func solve()  {
	//fmt.Scanln(&n)
	scanf(&n)
	scan(a[:n])

	if check() {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func main() {
	var t int64
	scanf(&t)
	//fmt.Scanln(&t)
	fmt.Println(t)
	for i := 0; int64(i) < t; i++ {
		solve()
	}
}

package main

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

var a = [100010]int64{}
var n, m, k int

var scanner = bufio.NewScanner(os.Stdin)
const maxCapacity = 1024*1024*4
var buf= make([]byte, maxCapacity)

func init()  {
	scanner.Buffer(buf, maxCapacity)
}

func min(a ...int) int{
	ans := a[0]
	for _, val := range a {
		if ans > val {
			ans = val
		}
	}
	return ans
}

func scan(arr []int64) {
	scanner.Scan()
	str := strings.Split(scanner.Text(), " ")
	for i:= 0; i < min(len(str), len(arr)); i++ {
		arr[i], _ = strconv.ParseInt(str[i], 10, 64)
	}
}

func check() bool{
	flg, cnt := true, int64(0)
	flag := false
	//fmt.Println(a[:n], n)
	for i := n-1; i >= 0; i-- {
		cnt += a[i]
		if cnt > 0 {
			return false
		}
		if cnt == 0 && flag{
			if flg {
				flg = false
			} else {
				return false
			}
		} else if cnt != 0 {
			flag = true
		}
		//fmt.Println(cnt)
	}
	return cnt == 0
}

func solve()  {
	fmt.Scanln(&n)
	scan(a[:n])

	if check() {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}