package main

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

func main() {
	CF1709C(os.Stdin, os.Stdout)
}
func CF1709C(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var n int
	fmt.Fscan(r, &n)
	for  ; n > 0; n-- {
		var s string
		fmt.Fscan(r, &s)

		num, p := 0, 0
		for i := 0; i < len(s); i++ {
			switch s[i] {
			case '(':
				num++
			case ')':
				num--
			case '?':
				p++
			}
			if num+p == 1 {
				num = 1
				p = 0
			}
		}

		if num == p {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func sol() bool {
	s := myio.ReadBytes()
	l, r, q := 0, 0, 0
	// myio.Println(string(s))
	for _, c := range s {
		if c == '(' {
			l++
		} else if c == ')' {
			r++
		} else {
			q++
		}
	}
	nl := (l+r+q)/2 - l
	nr := (l+r+q)/2 - r
	u, ct := 0, 0
	// myio.Println(l, r, q, nl, nr)
	if nl == 0 || nr == 0 {
		return true
	}

	for _, c := range s {
		if c == '(' {
			u++
		} else if c == ')' {
			u--
		} else {
			ct++
			if ct < nl {
				u++
			} else if ct == nl {
				u--
			} else if ct == nl+1 {
				u++
			} else {
				u--
			}
		}
		if u < 0 {
			return true
		}
	}
	return false
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		if sol() {
			myio.Println("YES")
		} else {
			myio.Println("NO")
		}
	}
}

package main

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

func check(a []rune) bool {
    	check := 0
    	other := 0
    	for i := range a {
    		switch {
    		case a[i] == '(':
    			check++
    		case a[i] == ')':
    			check--
    		default:
    			other++
    		}
    	}
    	//Thx YT.
    	open  := (other - check) / 2
    	close := (other + check) / 2
    	if open == 0 || close == 0 {
    		return true
    	}
    	new_replace := []rune(new_str(open, close))
	i := 0
	for j, _ := range a {
		switch a[j] {
		case '?':
			a[j] = new_replace[i]
			i++
		}
	}
    	check = 0
    	for _, j := range a {
    		switch j {
    		case '(':
    			check++
    		default:
    			check--
    		}
    		if check < 0 { return true }
    	}
    	return false
}

func new_str(open, close int) string {
    	var new_string string
    	for i := 1; i < open; i++ {
    		new_string += "("
    	}
    	new_string += ")("
    	for i := 1; i < close; i++ {
    		new_string += ")"
    	}
    	return new_string
}

func main() {
    	testcase := 0
    	reader := bufio.NewReader(os.Stdin)
    	writer := bufio.NewWriter(os.Stdout)
    	defer writer.Flush()

    	fmt.Fscan(reader, &testcase)
    	for testcase > 0 {
    		var a string
    		fmt.Fscan(reader, &a)
    		if check([]rune(a)) {
    			fmt.Fprintln(writer, "YES")
    		} else {
    			fmt.Fprintln(writer, "NO")
    		}
    		testcase--
    	}
}

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(in io.Reader, out io.Writer) {
	var t int
	fmt.Fscan(in, &t)
	for ti := 0; ti < t; ti++ {
		var s string
		fmt.Fscan(in, &s)
		w1 := do(s)
		fmt.Fprintln(out, w1)
	}
}

func do(s string) string {
	b := []byte(s)
	l := len(b) / 2
	for _, c := range b {
		if c == '(' {
			l--
		}
	}
	var x, y int
	for i, c := range b {
		if c == '?' {
			l--
			b[i] = '('
			if l < 0 {
				b[i] = ')'
			}
			if l == 0 {
				x = i
			}
			if l == -1 {
				y = i
			}
		}
	}
	if y <= x {
		return "YES"
	}
	b[x], b[y] = b[y], b[x]
	l = 0
	for _, c := range b {
		if c == '(' {
			l++
		} else {
			l--
		}
		if l < 0 {
			return "YES"
		}
	}
	return "NO"
}

package main

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

func solve() {
	k, x := 0, 0
	for _, c := range gets() {
		switch c {
		case '(':
			k++
		case ')':
			k--
		case '?':
			x++
		}
		if k+x == 1 {
			k, x = 1, 0
		}
	}
	yn(k == x)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanLines
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

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

	if once {
		solve()
		return
	}

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

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

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

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

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

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

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

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

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

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

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

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

type IntType interface {
	int | int64
}

type Type interface {
	IntType | float64
}

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

func fabs(x float64) float64 {
	return math.Abs(x)
}

func max1[T Type](x T, y T) T {
	if x > y {
		return x
	}
	return y
}

func max[T Type](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = max1(r, x)
	}
	return r
}

func maxa[T Type](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] <= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func min1[T Type](x T, y T) T {
	if x < y {
		return x
	}
	return y
}

func min[T Type](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = min1(r, x)
	}
	return r
}

func mina[T Type](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] >= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func L(i int) int64 {
	return int64(i)
}

func F(i int) float64 {
	return float64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

const heapBase = 4

func children(i int) (int, int) {
	start := heapBase * i
	return start + 1, start + (1 + heapBase)
}

func parent(i int) int {
	return (i - 1) / heapBase
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s := readString(reader)
		res := solve(s)
		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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(s string) bool {
	n := len(s)
	if n%2 == 1 {
		return false
	}
	cnt := make([]int, 3)

	for i := 0; i < n; i++ {
		if s[i] == '(' {
			cnt[0]++
		} else if s[i] == ')' {
			cnt[1]++
		} else {
			cnt[2]++
		}
	}

	// open = close = n / 2
	if cnt[0] > n/2 || cnt[1] > n/2 {
		return false
	}

	if cnt[2] == 0 {
		return cnt[0] == cnt[1]
	}

	// cnt[0] + x = n / 2
	x := n/2 - cnt[0]

	buf := []byte(s)
	a, b := -1, -1
	for i := 0; i < n; i++ {
		if buf[i] == '?' {
			if x > 0 {
				buf[i] = '('
				x--
				a = i
			} else {
				if b < 0 {
					b = i
				}
				buf[i] = ')'
			}
		}
	}
	var open int
	for i := 0; i < n; i++ {
		if buf[i] == '(' {
			open++
		} else {
			open--
		}
		if open < 0 {
			return false
		}
	}

	if open != 0 {
		return false
	}
	// a, b
	if a < 0 || b < 0 {
		// can't change
		return true
	}

	// a < b
	buf[a], buf[b] = buf[b], buf[a]

	for i := 0; i < n; i++ {
		if buf[i] == '(' {
			open++
		} else {
			open--
		}
		if open < 0 {
			return true
		}
	}

	return open != 0
}

package main

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

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

func slove() {
	var s string
	fmt.Fscan(rr, &s)
	l, r, q := 0, 0, 0
	for i := range s {
		if s[i] == '(' {
			l++
		} else if s[i] == ')' {
			r++
		} else {
			q++
		}
	}
	nl, nr := 0, 0
	if l - r > 0 {
		nr = l - r
		nl += (q-nr)/2
		nr += (q-nr)/2
	} else {
		nl = r - l
		nr += (q-nl)/2
		nl += (q-nl)/2
	}
	ori_nr := nr
	if nl == 0 || nr == 0 {
		fmt.Fprintln(ww, "YES")
		return
	}
	cnt := 0
	for i := range s {
		if s[i] == '(' {
			cnt++
		} else if s[i] == ')' {
			cnt--
			if cnt <= 1 && nl == 0 && nr == ori_nr {
				fmt.Fprintln(ww, "YES")
				return
			}
		} else if s[i] == '?' {
			if nl > 0 {
				nl--
				if nl == 0 && cnt == 0 {
					fmt.Fprintln(ww, "YES")
					return
				}
				cnt++

			} else {
				nr--
				cnt--
			}
		}
	}
	fmt.Fprintln(ww, "NO")
}

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

package main

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

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

func test(s string) bool {
	n := len(s)
	prefix := make([]int, n+1)
	for i:=n-1;i>=0;i-- {
		prefix[i] = prefix[i+1]
		if s[i] == ')' {
			prefix[i]++
		} else if s[i] == '(' {
			prefix[i]--
		} else {
			prefix[i]--
		}
	}
	base := 0
	cnt := 0
	bias := 0
	ans := false
	for i := range s {
		//fmt.Fprintln(ww, cnt, bias, base, string(s[i:i+1]))
		if s[i] == '(' {
			base++
		} else if s[i] == ')' {
			base--
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i+1] < 0 {
				bias += cnt
				cnt = 0
				ans = false
			}
		} else {
			cnt++
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i+1] < 0 {
				bias += cnt
				cnt = 0
				ans = false
			} else if cnt >= 2 {
				ans = true
			}
		}
	}
	return ans
}

func slove() {
	var s string
	fmt.Fscan(rr, &s)
	n := len(s)
	ss := make([]byte, n)
	for i := range s {
		if s[i] == '(' {
			ss[n-i-1] = ')'
		} else if s[i] == ')' {
			ss[n-i-1] = '('
		} else {
			ss[n-i-1] = '?'
		}
	}
	if test(s) && test(string(ss)) {
		fmt.Fprintln(ww, "NO")
	} else {
		fmt.Fprintln(ww, "YES")
	}
}

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

package main

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

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

func test(s string) bool {
	n := len(s)
	prefix := make([]int, n+1)
	for i:=n-1;i>=0;i-- {
		prefix[i] = prefix[i+1]
		if s[i] == ')' {
			prefix[i]++
		} else if s[i] == '(' {
			prefix[i]--
		} else {
			prefix[i]--
		}
	}
	base := 0
	cnt := 0
	bias := 0
	for i := range s {
		//fmt.Fprintln(ww, cnt, bias, base, string(s[i:i+1]))
		if s[i] == '(' {
			base++
		} else if s[i] == ')' {
			base--
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i+1] < 0 {
				bias += cnt
				cnt = 0
			}
		} else {
			cnt++
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i+1] < 0 {
				bias += cnt
				cnt = 0
			} else if cnt >= 2 {
				return true
			}
		}
	}
	return false
}

func slove() {
	var s string
	fmt.Fscan(rr, &s)
	n := len(s)
	ss := make([]byte, n)
	for i := range s {
		if s[i] == '(' {
			ss[n-i-1] = ')'
		} else if s[i] == ')' {
			ss[n-i-1] = '('
		} else {
			ss[n-i-1] = '?'
		}
	}
	if test(s) && test(string(ss)) {
		fmt.Fprintln(ww, "NO")
	} else {
		fmt.Fprintln(ww, "YES")
	}
}

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

package main

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

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

func slove() {
	var s string
	fmt.Fscan(rr, &s)
	n := len(s)
	prefix := make([]int, n+1)
	for i:=n-1;i>=0;i-- {
		prefix[i] = prefix[i+1]
		if s[i] == ')' {
			prefix[i]++
		} else if s[i] == '(' {
			prefix[i]--
		} else {
			prefix[i]--
		}
	}
	base := 0
	cnt := 0
	bias := 0
	for i := range s {
		//fmt.Fprintln(ww, cnt, bias, base, string(s[i:i+1]))
		if s[i] == '(' {
			base++
		} else if s[i] == ')' {
			base--
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i+1] < 0 {
				bias += cnt
				cnt = 0
			}
		} else {
			cnt++
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i+1] < 0 {
				bias += cnt
				cnt = 0
			} else if cnt >= 2 {
				fmt.Fprintln(ww, "NO")
				return
			}
		}
	}
	fmt.Fprintln(ww, "YES")
}

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

package main

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

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

func slove() {
	var s string
	fmt.Fscan(rr, &s)
	n := len(s)
	prefix := make([]int, n+1)
	for i := range s {
		prefix[i+1] = prefix[i]
		if s[i] == ')' {
			prefix[i+1]++
		} else if s[i] == '(' {
			prefix[i+1]--
		} else {
			prefix[i+1]--
		}
	}
	base := 0
	cnt := 0
	bias := 0
	for i := range s {
		if s[i] == '(' {
			base++
		} else if s[i] == ')' {
			base--
		} else {
			cnt++
			if cnt - 2 + bias + base < 0 || cnt - 2 + bias + base - prefix[i] < 0 {
				bias += cnt
				cnt = 0
			} else if cnt >= 2 {
				fmt.Fprintln(ww, "NO")
				return
			}
		}
	}
	fmt.Fprintln(ww, "YES")
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func sol() bool {
	s := myio.ReadBytes()
	l, r, q := 0, 0, 0
	// myio.Println(string(s))
	for _, c := range s {
		if c == '(' {
			l++
		} else if c == ')' {
			r++
		} else {
			q++
		}
	}
	nl := (l+r+q)/2 - l
	nr := (l+r+q)/2 - r
	u, ct := 0, 0
	// myio.Println(l, r, q, nl, nr)
	if nl == 0 || nr == 0 {
		return true
	}

	for _, c := range s {
		if c == '(' {
			u++
		} else if c == ')' {
			u--
		} else {
			ct++
			if ct < nl {
				u++
			} else if ct == nl {
				if u == 0 {
					return true
				} else {
					return false
				}
			}
		}
	}
	return false
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		if sol() {
			myio.Println("YES")
		} else {
			myio.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func sol() bool {
	s := myio.ReadBytes()
	l, r, q := 0, 0, 0
	// myio.Println(string(s))
	for _, c := range s {
		if c == '(' {
			l++
		} else if c == ')' {
			r++
		} else {
			q++
		}
	}
	nl := (l+r+q)/2 - l
	nr := (l+r+q)/2 - r
	u, ct := 0, 0
	// myio.Println(l, r, q, nl, nr)
	if nl == 0 || nr == 0 {
		return true
	}

	for _, c := range s {
		if c == '{' {
			u++
		} else if c == '}' {
			u--
		} else {
			ct++
			if ct < nl {
				u++
			} else if ct == nl {
				if u == 0 {
					return true
				} else {
					return false
				}
			}
		}
	}
	return false
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		if sol() {
			myio.Println("YES")
		} else {
			myio.Println("NO")
		}
	}
}

package main

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

func solve() {
	k, x := 0, 0
	for c := range gets() {
		switch c {
		case '(':
			k++
		case ')':
			k--
		case '?':
			x++
		}
		if 1-k == x {
			x = 0
			k = 1
		}
	}
	yn(k == x)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanLines
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

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

	if once {
		solve()
		return
	}

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

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

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

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

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

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

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

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

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

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

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

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

type IntTp interface {
	int | int64
}

type Tp interface {
	IntTp | float64
}

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

func fabs(x float64) float64 {
	return math.Abs(x)
}

func max1[T Tp](x T, y T) T {
	if x > y {
		return x
	}
	return y
}

func max[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = max1(r, x)
	}
	return r
}

func maxa[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] <= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func min1[T Tp](x T, y T) T {
	if x < y {
		return x
	}
	return y
}

func min[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = min1(r, x)
	}
	return r
}

func mina[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] >= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func L(i int) int64 {
	return int64(i)
}

func F(i int) float64 {
	return float64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

const heapBase = 4

func children(i int) (int, int) {
	start := heapBase * i
	return start + 1, start + (1 + heapBase)
}

func parent(i int) int {
	return (i - 1) / heapBase
}


package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		var x0, n int64
		fmt.Scan(&x0, &n)
		if x0%2 == 0 {
			switch n % 4 {
			case 0:
				fmt.Println(x0)
			case 1:
				fmt.Println(x0 - n)
			case 2:
				fmt.Println(x0 + 1)
			default:
				fmt.Println(x0 + n + 1)
			}
		} else {
			switch n % 4 {
			case 0:
				fmt.Println(x0)
			case 1:
				fmt.Println(x0 + n)
			case 2:
				fmt.Println(x0 - 1)
			default:
				fmt.Println(x0 - n - 1)
			}
		}
	}
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var x, n int64
    fmt.Fscan(input, &x, &n)
    if n == 0 {
        fmt.Fprintln(output, x)
        return
    }
    ans := int64(0)
    if x & 1 == 1 {
        c := (n - 1) / 4
        ans = 1 + c * 4
        rem := (n - 1) % 4
        if rem == 3 {
            ans += n - (n - 1) - (n - 2)
        }
        if rem == 2 {
            ans += - n - (n - 1)
        }
        if rem == 1 {
            ans -= n
        }
    } else {
        c := (n - 1) / 4
        ans = -1 - c * 4
        rem := (n - 1) % 4
        if rem == 3 {
            ans += -n + n - 1 + n - 2
        }
        if rem == 2 {
            ans += n + n - 1
        }
        if rem == 1 {
            ans += n
        }
    }
    fmt.Fprintln(output, x + ans)
}

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

func lcm(a, b int64) int64 {
    return a * b / gcd(a, b)
}

func abs(x int) int {
    if x >= 0 { return x}
    return -x
}

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

package main
import (
    "bufio"
    "fmt"
    "os"
)
//+1 -2 -3 +4
type ll int64
var scan = fmt.Fscan
var println = fmt.Fprintln
//var print = fmt.Fprint

var in *bufio.Reader
var out *bufio.Writer
func init(){
    in = bufio.NewReader(os.Stdin)
    out = bufio.NewWriter(os.Stdout)
}

func main(){
	defer out.Flush()
	var t int; scan(in,&t)
	for ;t>0;t-- {
		var a, b ll
		scan(in,&a,&b)
		c := ((b>>2)<<2)
		p := -1
		if(a%2==1 || a%2 == -1) {p = -p}
		for i,j:=c+1,1;i<=b;i,j=i+1,j+1{
			if(j%2==0) {p = -p}
			a += ll(i) * ll(p)
		}

		println(out,a)
	}
}

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 x, n int64
		fmt.Fscan(in, &x, &n)

		var d int64
		switch n % 4 {
		case 0:
			d = 0
		case 1:
			d = -n
		case 2:
			d = 1
		case 3:
			d = n + 1
		}

		if x%2 == 0 {
			fmt.Fprintln(out, x+d)
		} else {
			fmt.Fprintln(out, x-d)
		}
	}
}

package main

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

/*
9
100 0
100 1
100 2
100 3
100 4
100 5
100 6
100 7
100 8
---
100
99
101
104
100
95
101
108
100
======
1
0 2
---
1
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var x0, n int64
		scanf("%d %d\n", &x0, &n)

		start := n/4*4 + 1
		x := x0

		for i := start; i <= n; i++ {
			if x%2 != 0 {
				x += i
			} else {
				x -= i
			}
		}

		fmt.Fprintln(&bld, x)
	}

	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 {
	x int64
	y int64
}

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
}

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

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

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

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

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

package main

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

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

func solve(x, n int64) int64 {
	d := n + 1
	if n%4 == 0 {
		d = 0
	} else if n%4 == 1 {
		d = -n
	} else if n%4 == 2 {
		d = 1
	}
	if x%2 == 0 {
		return x + d
	}
	return x - d
}

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

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)
	for t > 0 {
		t--
		var x,n int64
		fmt.Scanln(&x,&n)
		if n%4==0{
			fmt.Println(x)
			continue
		}else{
			var i int64 = (n%4)
			n-=n%4
			n++
			for i>0{
				i--
				if x%2==0{
					x-=n
				}else{
					x+=n
				}
				n++
			}
		}
		fmt.Println(x)
	}
}

package main

import (
	"fmt"
)

func scan(p ...interface{}) {
	fmt.Scan(p...)
}

func proc() int64 {
	var x, n int64
	scan(&x, &n)
	i := n
	if n == 0 {
		return x
	}
	if x&1 == 0 {
		x--
	} else {
		x++
	}
	n--
	if n == 0 {
		return x
	}
	b := n / 4
	n %= 4
	if x&1 == 0 {
		x += b * 4
	} else {
		x -= b * 4
	}
	for n = i - n + 1; n <= i; n++ {
		if x&1 == 0 {
			x -= n
		} else {
			x += n
		}
	}
	return x
}

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

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		x0, n := readTwoInt64(reader)

		s := sum(1, (n+3)/4) + sum(0, n/4+1) - sum(2, (n+2)/4) - sum(3, (n+1)/4)

		if x0 % 2 == 0 {
			x0 -= s
		} else {
			x0 += s
		}

		fmt.Fprintln(writer, x0)
		t--
	}

	writer.Flush()
}

func sum(a1 int64, n int64) int64 {
	return (2*a1 + (n-1)*4)*n/2
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

// 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 x, n int64
		fmt.Fscan(reader, &x, &n)
		if n%4 == 0 {
			fmt.Println(x)
		} else {
			ex := ((n - 1) / 4) * 4
			ex++
			if (n-1)%4 == 1 {
				ex -= n
			} else if (n-1)%4 == 2 {
				ex -= n
				ex -= n - 1
			} else if (n-1)%4 == 3 {
				ex -= n - 2
			}
			if x%2 == 0 {
				ex = x - ex
			} else {
				ex += x
			}
			fmt.Println(ex)
		}

	}
}

package main

import "fmt"

const (
	T_MIN = 1
	T_MAX = int64(1e4)
	X_MIN = int64(-1e14)
	X_MAX = int64(1e14)
	N_MIN = 0
	N_MAX = int64(1e14)
)

func evenCase(x, n int64) int64 {
	n4 := n % 4
	switch n4 {
	case 0:
		return x
	case 1:
		return x - n
	case 2:
		return x + 1
	case 3:
		return x + n + 1
	}
	return 0
}

func oddCase(x, n int64) int64 {
	n4 := n % 4
	switch n4 {
	case 0:
		return x
	case 1:
		return x + n
	case 2:
		return x - 1
	case 3:
		return x - n - 1
	}
	return 0
}

func main() {
	var t int
	fmt.Scanf("%v\n", &t)
	for i := 0; i < t; i++ {
		var x, n int64
		fmt.Scanf("%v %v\n", &x, &n)
		if x&1 == 0 {
			fmt.Println(evenCase(x, n))
		} else {
			fmt.Println(oddCase(x, n))
		}
	}
}

// 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 x, n int64
		fmt.Fscan(reader, &x, &n)
		if n == 0 || n == 4 {
			fmt.Println(x)
		} else {
			ex := ((n - 1) / 4) * 4
			ex++
			if (n-1)%4 == 1 {
				ex -= n
			} else if (n-1)%4 == 2 {
				ex -= n
				ex -= n - 1
			} else if (n-1)%4 == 3 {
				ex -= n - 2
			}
			if x%2 == 0 {
				ex = x - ex
			} else {
				ex += x
			}
			fmt.Println(ex)
		}

	}
}

// 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 x, n int64
		fmt.Fscan(reader, &x, &n)
		if n == 0 {
			fmt.Println(x)
		} else {
			ex := ((n - 1) / 4) * 4
			ex++
			if (n-1)%4 == 1 {
				ex -= n
			} else if (n-1)%4 == 2 {
				ex -= n
				ex -= n - 1
			} else if (n-1)%4 == 3 {
				ex -= n - 2
			}
			if x%2 == 0 {
				ex = x - ex
			} else {
				ex += x
			}
			fmt.Println(ex)
		}

	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		var x0, n int
		fmt.Scan(&x0, &n)
		if x0%2 == 0 {
			switch n % 4 {
			case 0:
				fmt.Println(x0)
			case 1:
				fmt.Println(x0 - n)
			case 2:
				fmt.Println(x0 + 1)
			default:
				fmt.Println(x0 + n + 1)
			}
		} else {
			switch n % 4 {
			case 0:
				fmt.Println(x0)
			case 1:
				fmt.Println(x0 + n)
			case 2:
				fmt.Println(x0 - 1)
			default:
				fmt.Println(x0 - n - 1)
			}
		}
	}
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var x, n int64
    fmt.Fscan(input, &x, &n)
    if n == 0 {
        fmt.Fprintln(output, x)
        return
    }
    ans := int64(0)
    if x & 1 == 1 {
        c := (n - 1) / 4
        ans = 1 + c * 4
        rem := (n - 1) % 4
        if rem == 3 {
            ans = n - (n - 1) - (n - 2)
        }
        if rem == 2 {
            ans += - n - (n - 1)
        }
        if rem == 1 {
            ans -= n
        }
    } else {
        c := (n - 1) / 4
        ans = -1 - c * 4
        rem := (n - 1) % 4
        if rem == 3 {
            ans += -n + n - 1 + n - 2
        }
        if rem == 2 {
            ans += n + n - 1
        }
        if rem == 1 {
            ans += n
        }
    }
    fmt.Fprintln(output, x + ans)
}

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

func lcm(a, b int64) int64 {
    return a * b / gcd(a, b)
}

func abs(x int) int {
    if x >= 0 { return x}
    return -x
}

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


package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

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

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

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

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

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// 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)
}

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

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

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		n := ReadInt()
		P := ReadIntSlice(n)
		for j := 0; j < n; j++ {
			P[j] /= 100
		}
		x, a := ReadInt2()
		y, b := ReadInt2()
		k := ReadInt64()

		sort.Sort(sort.Reverse(sort.IntSlice(P)))
		percents := make([]int, n)
		for j := 0; j < n; j++ {
			if (j+1)%a == 0 && (j+1)%b == 0 {
				percents[j] = x + y
			} else if (j+1)%a == 0 {
				percents[j] = x
			} else if (j+1)%b == 0 {
				percents[j] = y
			}
		}
		// fmt.Println(percents)

		// m は中央を意味する何らかの値
		isOK := func(m int) bool {
			tmp := make([]int, m)
			for h := 0; h < m; h++ {
				tmp[h] = percents[h]
			}
			sort.Sort(sort.Reverse(sort.IntSlice(tmp)))

			val := int64(0)
			for h := 0; h < m; h++ {
				val += int64(tmp[h]) * int64(P[h])
			}

			if val >= k {
				return true
			}
			return false
		}

		ng, ok := -1, n
		for int(math.Abs(float64(ok-ng))) > 1 {
			mid := (ok + ng) / 2
			if isOK(mid) {
				ok = mid
			} else {
				ng = mid
			}
		}

		if isOK(ok) {
			fmt.Println(ok)
		} else {
			fmt.Println(-1)
		}
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

const debug = false

var in = bufio.NewReader(os.Stdin)
var n, x, a, y, b int
var k int64

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

func can(p []int32, m int) bool {
	nab := int(int64(m) / (int64(a / gcd(a, b)) * int64(b)))
	na := m/a - nab
	nb := m/b - nab
	if debug {
		log.Println(m, nab, na, nb)
	}
	var kk int64
	i := 0
	for ; nab > 0 && i < m; nab-- {
		kk += int64(p[i]) * int64(x+y)
		i++
	}
	for ; na > 0 && i < m; na-- {
		kk += int64(p[i]) * int64(x)
		i++
	}
	for ; nb > 0 && i < m; nb-- {
		kk += int64(p[i]) * int64(y)
		i++
	}
	return kk >= k
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	var q int
	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &n)
		p := make([]int32, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &p[i])
			p[i] /= 100
		}
		fmt.Fscan(in, &x, &a, &y, &b, &k)
		sort.Slice(p, func(i, j int) bool { return p[i] > p[j] })
		if x < y {
			x, y = y, x
			a, b = b, a
		}
		lo := 1
		hi := n
		for lo < hi {
			mid := (lo + hi) / 2
			if can(p, mid) {
				hi = mid
			} else {
				lo = mid + 1
			}
		}
		if !can(p, lo) {
			lo = -1
		}
		fmt.Println(lo)
	}
}

package main

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

const debug = false

var in = bufio.NewReader(os.Stdin)
var n, x, a, y, b int
var k int64

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

func can(p []int32, m int) bool {
	nab := m / ((a * b) / gcd(a, b))
	na := m/a - nab
	nb := m/b - nab
	if debug {
		log.Println(m, nab, na, nb)
	}
	var kk int64
	i := 0
	for ; nab > 0 && i < m; nab-- {
		kk += int64(p[i]) * int64(x+y)
		i++
	}
	for ; na > 0 && i < m; na-- {
		kk += int64(p[i]) * int64(x)
		i++
	}
	for ; nb > 0 && i < m; nb-- {
		kk += int64(p[i]) * int64(y)
		i++
	}
	return kk >= k
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	var q int
	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &n)
		p := make([]int32, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &p[i])
			p[i] /= 100
		}
		fmt.Fscan(in, &x, &a, &y, &b, &k)
		sort.Slice(p, func(i, j int) bool { return p[i] > p[j] })
		if x < y {
			x, y = y, x
			a, b = b, a
		}
		lo := 1
		hi := n
		for lo < hi {
			mid := (lo + hi) / 2
			if can(p, mid) {
				hi = mid
			} else {
				lo = mid + 1
			}
		}
		if !can(p, lo) {
			lo = -1
		}
		fmt.Println(lo)
	}
}

package main

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

const debug = false

var in = bufio.NewReader(os.Stdin)
var n, x, a, y, b int
var k int64

func can(p []int32, m int) bool {
	nab := m / (a * b)
	na := m/a - nab
	nb := m/b - nab
	if debug {
		log.Println(m, nab, na, nb)
	}
	var kk int64 = 0
	i := 0
	for ; nab > 0 && i < m; nab-- {
		kk += int64(p[i]) * int64(x+y)
		i++
	}
	for ; na > 0 && i < m; na-- {
		kk += int64(p[i]) * int64(x)
		i++
	}
	for ; nb > 0 && i < m; nb-- {
		kk += int64(p[i]) * int64(y)
		i++
	}
	return kk >= k
}

func main() {
	if debug {
		log.SetFlags(log.LstdFlags | log.Lshortfile)
	}
	var q int
	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &n)
		p := make([]int32, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &p[i])
			p[i] /= 100
		}
		fmt.Fscan(in, &x, &a, &y, &b, &k)
		sort.Slice(p, func(i, j int) bool { return p[i] > p[j] })
		if x < y {
			x, y = y, x
			a, b = b, a
		}
		lo := 1
		hi := n
		for lo < hi {
			mid := (lo + hi) / 2
			if can(p, mid) {
				hi = mid
			} else {
				lo = mid + 1
			}
		}
		if !can(p, lo) {
			lo = -1
		}
		fmt.Println(lo)
	}
}


package main

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

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

func qwq() {
	var n, m int64
	fmt.Fscan(in, &n, &m)
	a := make([]int64, n+1)
	for i := int64(1); i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	var sum int64 = (1 + n) * n / 2
	for i := int64(1); i+1 <= n; i++ {
		if a[i] != a[i+1] {
			sum += i * (n - i)
		}
	}
	for ; m > 0; m-- {
		var p, x int64
		fmt.Fscan(in, &p, &x)
		if p > 1 {
			if a[p] == a[p-1] && a[p-1] != x {
				sum += (p - 1) * (n - p + 1)
			}
			if a[p] != a[p-1] && a[p-1] == x {
				sum -= (p - 1) * (n - p + 1)
			}
		}
		if p+1 <= n {
			if a[p] == a[p+1] && a[p+1] != x {
				sum += p * (n - p)

			}
			if a[p] != a[p+1] && a[p+1] == x {
				sum -= p * (n - p)
			}
		}
		a[p] = x
		fmt.Fprintln(out, sum)
	}
}

func main() {
	defer out.Flush()
	q := 1
	// fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

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 = 1
	// fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int64
		var m int
		fmt.Fscan(r, &n, &m)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([][2]int, m)
		for i := range b {
			fmt.Fscan(r, &b[i][0], &b[i][1])
		}
		w1 := do(n, m, a, b)
		for _, v := range w1 {
			fmt.Fprintln(w, v)
		}
	}
}

func getSum(n int64, a []int) int64 {
	s := int64(1)
	c := int64(1)
	for i := int64(1); i < n; i++ {
		if a[i] != a[i-1] {
			c += i + 1
		} else {
			c++
		}
		s += c
	}
	return s
}

func do(n int64, m int, a []int, b [][2]int) []int64 {
	s := getSum(n, a)
	r := make([]int64, m)
	same := func(i int64) (bool, bool) {
		return i > 0 && a[i] == a[i-1], i < n-1 && a[i] == a[i+1]
	}
	for j, b := range b {
		i, v := int64(b[0]-1), b[1]
		p0, q0 := same(i)
		a[i] = v
		p1, q1 := same(i)
		switch {
		case p0 && !q0 && !p1 && !q1:
			r[j] = s + i*(n-i)
		case !p0 && !q0 && p1 && !q1:
			r[j] = s - i*(n-i)
		case p0 && q0 && !p1 && !q1:
			r[j] = s + i + (1+2*i)*(n-(i+1))
		case !p0 && !q0 && p1 && q1:
			r[j] = s - i - (1+2*i)*(n-(i+1))
		case !p0 && q0 && !p1 && !q1:
			r[j] = s + (i+1)*(n-(i+1))
		case !p0 && !q0 && !p1 && q1:
			r[j] = s - (i+1)*(n-(i+1))
		case !p0 && q0 && p1 && !q1:
			r[j] = s - i + (n - (i + 1))
		case p0 && !q0 && !p1 && q1:
			r[j] = s + i - (n - (i + 1))
		default:
			r[j] = s
		}
		s = r[j]
	}
	return r
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	a := make([]int, n+5)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	pre := make([]int, n+5)
	for i := 1; i <= n; i++ {
		if i == 1 {
			pre[i] = 1
			continue
		}
		if a[i] == a[i-1] {
			pre[i] = pre[i-1] + 1
		} else {
			pre[i] = 1 + pre[i-1] + (i - 1)
		}
	}
	ans := 0
	for i := 1; i <= n; i++ {
		ans += pre[i]
	}
	for i := 1; i <= m; i++ {
		var p, x int
		fmt.Fscan(in, &p, &x)
		if p-1 >= 1 && a[p-1] == x && a[p] != a[p-1] {

			ans -= (p - 1) * (n - p + 1)
		}
		if p+1 <= n && a[p+1] == x && a[p] != a[p+1] {

			ans -= (n - p) * p
		}
		if p-1 >= 1 && a[p-1] != x && a[p] == a[p-1] {

			ans += (p - 1) * (n - p + 1)
		}
		if p+1 <= n && a[p+1] != x && a[p] == a[p+1] {
			ans += (n - p) * p
		}
		fmt.Fprintln(out, ans)
		a[p] = x
	}
}

package main

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

var n, m, pos, x int
var a []int
var dp, sum []int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &n, &m)
	a = make([]int, n+2)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	dp = make([]int64, n+1)
	sum = make([]int64, n+1)
	dp[1], sum[1] = 0, 1
	ans := int64(1)
	for i := 2; i <= n; i++ {
		sum[i] = int64(i)
		if a[i] == a[i-1] {
			dp[i] = dp[i-1]
			sum[i] += dp[i-1]
		} else {
			sum[i] += sum[i-1]
			dp[i] = sum[i-1]
		}
		ans += sum[i]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &pos, &x)
		if a[pos] == x {
			fmt.Fprintln(out, ans)
			continue
		}
		if a[pos-1] == a[pos] {
			ans += int64(pos-1) * int64(n-pos+1)
		} else if a[pos-1] != a[pos] && a[pos-1] == x {
			ans -= int64(pos-1) * int64(n-pos+1)
		}
		if a[pos+1] == a[pos] {
			ans += int64(pos) * int64(n-pos)
		} else if a[pos+1] != a[pos] && a[pos+1] == x {
			ans -= int64(pos) * int64(n-pos)
		}
		a[pos] = x
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

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

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

	// number of segments n*(n+1)/2
	// score: n*(n+1)*(n+2)/6

	var tot int64
	for i := int64(1); i <= n; i++ {
		if aa[i] != aa[i+1] {
			tot += (n - (i + 1) + 1) * i
		}
	}

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

		if aa[k] != aa[k-1] {
			tot -= int64((n - k + 1) * (k - 1))
		}
		if aa[k] != aa[k+1] {
			tot -= int64((n - (k + 1) + 1) * k)
		}
		aa[k] = x
		if aa[k] != aa[k-1] {
			tot += int64((n - k + 1) * (k - 1))
		}
		if aa[k] != aa[k+1] {
			tot += int64((n - (k + 1) + 1) * k)
		}
		fmt.Fprintln(out, tot+int64(n*(n+1)/2))
	}
}

package main

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

type pair struct {
	val int
	start int
	end int
}

func readArray(reader io.Reader, n int) []int {
	var items = make([]int, n)
	for i := 0; i < n; i ++ {
		fmt.Fscan(reader, &items[i])
	}

	return items
}

func insert(a []pair, index int, value pair) []pair {
    if len(a) == index { // nil or empty slice or after last element
        return append(a, value)
    }
    a = append(a[:index+1], a[index:]...) // index < len(a)
    a[index] = value
    return a
}

func remove(slice []pair, s int) []pair {
    return append(slice[:s], slice[s+1:]...)
}

func getSum(items []int) int64 {
	var sum int64 = 0
	for i, val := range items {
		if i > 0 && items[i - 1] == val {
			sum += int64(len(items) - i)
		} else {
			sum += int64(i) * int64(len(items) - i) + int64(len(items) - i)
		}
	}

	return sum
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)
	var n, m int
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &m)
	var items = readArray(reader, n)
	var sum = getSum(items)
	for i := 0; i < m; i++ {
		var i, x int
		fmt.Fscan(reader, &i)
		fmt.Fscan(reader, &x)
		i--
		if(items[i] == x) {
			fmt.Println(sum)
			continue
		}

		if i > 0 {
			if items[i - 1] == items[i] {
				sum += int64(i) * int64(len(items) - i)
			} else {
				if items[i - 1] == x {
					sum -= int64(i) * int64(len(items) - i)
				}
			}
		}
		if i < n - 1 {
			if items[i] == items[i + 1] {
				sum += int64(i + 1) * int64(len(items) - i - 1)
			} else {
				if items[i + 1] == x {
					sum -= int64(i + 1) * int64(len(items) - i - 1)
				}
			}
		}
		items[i] = x

		fmt.Println(sum)
	}
}

package main

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

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 Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		arr[i], _ = strconv.Atoi(scanner.Text())
	}
	tmpasm := make([]int, n)
	tmpSumasm := make([]int, n)
	tmpasm[0] = 1
	sumAsm := 1
	tmpSumasm[0] = 1
	last := arr[0]
	for i := 1; i < n; i++ {
		if last != arr[i] {
			tmpasm[i] = tmpasm[i-1] + 1
		} else {
			tmpasm[i] = tmpasm[i-1]
		}
		last = arr[i]
		sumAsm += tmpasm[i]
		tmpSumasm[i] = sumAsm
	}
	asm := sumAsm
	for i := 0; i < n-1; i++ {
		asm -= (n - 1 - i) * tmpasm[i]
		asm -= tmpSumasm[i]
		if arr[i] == arr[i+1] {
			asm += (n - 1 - i)
		}
		asm += sumAsm
	}

	for i_ := 0; i_ < m; i_++ {
		scanner.Scan()
		i, _ := strconv.Atoi(scanner.Text())
		i--
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		if arr[i] == x {
			wr.WriteString(strconv.Itoa(asm) + "\n")
			continue
		}

		diffLeft := 0
		if i > 0 && arr[i-1] != arr[i] && arr[i-1] == x {
			diffLeft = -1
		} else if i > 0 && arr[i-1] == arr[i] && arr[i-1] != x {
			diffLeft = 1
		}
		diffRight := 0
		if i < n-1 && arr[i+1] != arr[i] && arr[i+1] == x {
			diffRight = -1
		} else if i < n-1 && arr[i+1] == arr[i] && arr[i+1] != x {
			diffRight = 1
		}

		arr[i] = x

		leftCnt := (n - i) * i
		rightCnt := (i + 1) * (n - i - 1)
		asm += leftCnt * diffLeft
		asm += rightCnt * diffRight
		wr.WriteString(strconv.Itoa(asm) + "\n")
	}
}

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

package main

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

var n, m, pos, x int
var a []int
var dp, sum []int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &n, &m)
	a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	dp = make([]int64, n+1)
	sum = make([]int64, n+1)
	dp[1], sum[1] = 0, 1
	ans := int64(1)
	for i := 2; i <= n; i++ {
		sum[i] = int64(i)
		if a[i] == a[i-1] {
			dp[i] = dp[i-1]
			sum[i] += dp[i-1]
		} else {
			sum[i] += sum[i-1]
			dp[i] = sum[i-1]
		}
		ans += sum[i]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &pos, &x)
		if a[pos] == x {
			fmt.Fprintln(out, ans)
			continue
		}
		if pos > 1 {
			if a[pos-1] == a[pos] {
				ans += int64((pos - 1) * (n - pos + 1))
			} else if a[pos-1] != a[pos] && a[pos-1] == x {
				ans -= int64((pos - 1) * (n - pos + 1))
			}
		}
		if pos < n {
			if a[pos+1] == a[pos] {
				ans += int64(pos * (n - pos))
			} else if a[pos+1] != a[pos] && a[pos+1] == x {
				ans -= int64(pos * (n - pos))
			}
		}
		a[pos] = x
		fmt.Fprintln(out, ans)
	}
}

package main

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

var n, m, pos, x int
var a []int
var dp, sum []int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &n, &m)
	a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	dp = make([]int64, n+1)
	sum = make([]int64, n+1)
	dp[1], sum[1] = 0, 1
	ans := int64(1)
	for i := 2; i <= n; i++ {
		sum[i] = int64(i)
		if a[i] == a[i-1] {
			dp[i] = dp[i-1]
			sum[i] += dp[i-2]
		} else {
			dp[i] = sum[i-1]
			sum[i] += sum[i-1]
		}
		ans += sum[i]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &pos, &x)
		if a[pos] == x {
			fmt.Fprintln(out, ans)
			continue
		}
		if pos > 1 {
			if a[pos-1] == a[pos] {
				ans += int64((pos - 1) * (n - pos + 1))
			} else if a[pos-1] != a[pos] && a[pos-1] == x {
				ans -= int64((pos - 1) * (n - pos + 1))
			}
		}
		if pos < n {
			if a[pos+1] == a[pos] {
				ans += int64(pos * (n - pos))
			} else if a[pos+1] != a[pos] && a[pos+1] == x {
				ans -= int64(pos * (n - pos))
			}
		}
		a[pos] = x
		fmt.Fprintln(out, ans)
	}
}

package main

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

var n, m, pos, x int
var a []int
var dp []int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &n, &m)
	a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	dp = make([]int64, n+1)
	dp[1] = 1
	ans := int64(1)
	for i := 2; i <= n; i++ {
		dp[i] = int64(i)
		if a[i] == a[i-1] {
			dp[i] += dp[i-2]
		} else {
			dp[i] += dp[i-1]
		}
		ans += dp[i]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &pos, &x)
		if a[pos] == x {
			fmt.Fprintln(out, ans)
			continue
		}
		if pos > 1 {
			if a[pos-1] == a[pos] {
				ans += int64((pos - 1) * (n - pos + 1))
			} else if a[pos-1] != a[pos] && a[pos-1] == x {
				ans -= int64((pos - 1) * (n - pos + 1))
			}
		}
		if pos < n {
			if a[pos+1] == a[pos] {
				ans += int64(pos * (n - pos))
			} else if a[pos+1] != a[pos] && a[pos+1] == x {
				ans -= int64(pos * (n - pos))
			}
		}
		a[pos] = x
		fmt.Fprintln(out, ans)
	}
}

package main

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

var n, m, pos, x int
var a []int
var dp []int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &n, &m)
	a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	dp = make([]int64, n+1)
	dp[1] = 1
	ans := int64(1)
	for i := 2; i <= n; i++ {
		dp[i] = int64(i)
		if a[i] == a[i-1] {
			dp[i] += dp[i-2]
		} else {
			dp[i] += dp[i-1]
		}
		ans += dp[i]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &pos, &x)
		if a[pos] == x {
			continue
		}
		if pos > 1 {
			if a[pos-1] == a[pos] {
				ans += int64((pos - 1) * (n - pos + 1))
			} else if a[pos-1] != a[pos] && a[pos-1] == x {
				ans -= int64((pos - 1) * (n - pos + 1))
			}
		}
		if pos < n {
			if a[pos+1] == a[pos] {
				ans += int64(pos * (n - pos))
			} else if a[pos+1] != a[pos] && a[pos+1] == x {
				ans -= int64(pos * (n - pos))
			}
		}
		a[pos] = x
		fmt.Fprintln(out, ans)
	}
}

/*
    1 2 2 4 2
    (1) * 1          1
    (1) * 2 + 1      3
	(1) * 3 + 1      4
	(1) * 4 + 4      8
    (1) * 5 + 8     13

   2 2 3
   1
   2
   3 + 2      8

   1 2 3
   1 + 2 + 3 + 1 + 2 + 1 = 10

   1 2 1  = 10
   1 1 1  = 10 - 2 - 2

   1 2 3 3 4 5
*/

package main

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

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

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

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

	// number of segments n*(n+1)/2
	// score: n*(n+1)*(n+2)/6

	var tot int64
	for i := 1; i <= n; i++ {
		if aa[i] != aa[i+1] {
			tot += int64((n - (i + 1) + 1) * i)
		}
	}

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

		if aa[k] != aa[k-1] {
			tot -= int64((n - k + 1) * (k - 1))
		}
		if aa[k] != aa[k+1] {
			tot -= int64((n - (k + 1) + 1) * k)
		}
		aa[k] = x
		if aa[k] != aa[k-1] {
			tot += int64((n - k + 1) * (k - 1))
		}
		if aa[k] != aa[k+1] {
			tot += int64((n - (k + 1) + 1) * k)
		}
		fmt.Fprintln(out, tot+int64(n*(n+1)/2))
	}
}

package main

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

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

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

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

	// number of segments n*(n+1)/2
	// score: n*(n+1)*(n+2)/6

	var tot int
	for i := 1; i <= n; i++ {
		if aa[i] != aa[i+1] {
			tot += (n - (i + 1) + 1) * i
		}
	}

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

		if aa[k] != aa[k-1] {
			tot -= (n - k + 1) * (k - 1)
		}
		if aa[k] != aa[k+1] {
			tot -= (n - (k + 1) + 1) * k
		}
		aa[k] = x
		if aa[k] != aa[k-1] {
			tot += (n - k + 1) * (k - 1)
		}
		if aa[k] != aa[k+1] {
			tot += (n - (k + 1) + 1) * k
		}
		fmt.Fprintln(out, tot+n*(n+1)/2)
	}
}

package main

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

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

func qwq() {
	var n, m int64
	fmt.Fscan(in, &n, &m)
	a := make([]int64, n+1)
	for i := int64(1); i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	var sum int64 = (1 + n) * n / 2
	for i := int64(1); i+1 <= n; i++ {
		if a[i] != a[i+1] {
			sum += i * (n - i)
		}
	}
	for ; m > 0; m-- {
		var p, x int64
		fmt.Fscan(in, &p, &x)
		if p > 1 {
			if a[p] == a[p-1] && a[p] != x {
				sum += (p - 1) * (n - p + 1)
			}
			if a[p] != a[p-1] && a[p-1] == x {
				sum -= (p - 1) * (n - p + 1)
			}
		}
		if p+1 <= n {
			if a[p] == a[p+1] && a[p] != x {
				sum += p * (n - p)

			}
			if a[p] != a[p-1] && a[p+1] == x {
				sum -= p * (n - p)
			}
		}
		a[p] = x
		fmt.Fprintln(out, sum)
	}
}

func main() {
	defer out.Flush()
	q := 1
	// fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

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 = 1
	// fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([][2]int, m)
		for i := range b {
			fmt.Fscan(r, &b[i][0], &b[i][1])
		}
		w1 := do(n, m, a, b)
		for _, v := range w1 {
			fmt.Fprintln(w, v)
		}
	}
}

func do(n, m int, a []int, b [][2]int) []int {
	s := 1
	c := 1
	for i := 1; i < n; i++ {
		if a[i] != a[i-1] {
			c += i + 1
		} else {
			c++
		}
		s += c
	}
	r := make([]int, m)
	same := func(i int) (bool, bool) {
		return i > 0 && a[i] == a[i-1], i < n-1 && a[i] == a[i+1]
	}
	for j, b := range b {
		i, v := b[0]-1, b[1]
		p0, q0 := same(i)
		a[i] = v
		p1, q1 := same(i)
		switch {
		case p0 && !q0 && !p1 && !q1:
			r[j] = s + i*(n-i)
		case !p0 && !q0 && p1 && !q1:
			r[j] = s - i*(n-i)
		case p0 && q0 && !p1 && !q1:
			r[j] = s + i + (1+2*i)*(n-(i+1))
		case !p0 && !q0 && p1 && q1:
			r[j] = s - i - (1+2*i)*(n-(i+1))
		case !p0 && q0 && !p1 && !q1:
			r[j] = s + (i+1)*(n-(i+1))
		case !p0 && !q0 && !p1 && q1:
			r[j] = s - (i+1)*(n-(i+1))
		case !p0 && q0 && p1 && !q1:
			r[j] = s - i + (n - (i + 1))
		case p0 && !q0 && !p1 && q1:
			r[j] = s + i - (n - (i + 1))
		default:
			r[j] = s
		}
		s = r[j]
	}
	return r
}


package main

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

// github.com/EndlessCheng/codeforces-go
func CF1545B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return
	}

	var T, n int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		x, y := strings.Count(s, "11"), strings.Count(s, "0")
		x += y
		num, den := int64(1), int64(1)
		for i := 0; i < y; i++ {
			num = num * int64(x-i) % mod
			den = den * int64(i+1) % mod
		}
		Fprintln(out, num*pow(den, mod-2)%mod)
	}
}

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

package main

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

const (
	MAXN = 100005
	MOD  = 998244353
)

func pow(a int64, b int) int64 {
	if b == 0 {
		return 1
	} else if b%2 == 1 {
		return a * pow(a, b-1) % MOD
	} else {
		tmp := pow(a, b/2)
		return tmp * tmp % MOD
	}
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	fact := make([]int64, MAXN)
	fact[0] = 1
	for i := int64(1); i < MAXN; i++ {
		fact[i] = (i * fact[i-1]) % MOD
	}

	TC := sc.NextInt()
	for tc := 0; tc < TC; tc++ {
		_ = sc.NextInt()
		chess := sc.Next()

		cntG1 := 0
		cntSeq1 := 0
		cnt0 := 0
		for _, c := range chess {
			if c == '0' {
				cnt0++
				cntSeq1 = 0
			} else {
				cntSeq1++
				if cntSeq1 == 2 {
					cntG1++
					cntSeq1 = 0
				}
			}
		}

		ans := fact[cntG1+cnt0] * pow(fact[cntG1], MOD-2) % MOD * pow(fact[cnt0], MOD-2) % MOD
		fmt.Fprintln(wr, ans)
	}

}

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(num int, nums ...int) int {
	ans := num
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(num int, nums ...int) int {
	ans := num
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func InitInts1D(n int, val int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = val
	}
	return res
}
func InitInts2D(n, m int, val int) [][]int {
	res := make([][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts1D(m, val)
	}
	return res
}
func InitInts3D(n, m, k int, val int) [][][]int {
	res := make([][][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts2D(m, k, val)
	}
	return res
}

type StepState struct {
	step, dist int
}
type PriorityQueue []*StepState

func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
	if pq[i].step != pq[j].step {
		return pq[i].step < pq[j].step
	}
	return pq[i].dist < pq[j].dist
}
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
	item := x.(*StepState)
	*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}


package main

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

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

	var n, v, w, q, d int
	var x int64
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	type info struct{ tin, tout, d int }
	is := make([]info, n)
	depT := make([][]int, n)
	t := 0
	var f func(v, fa, d int)
	f = func(v, fa, d int) {
		t++
		is[v].tin = t
		is[v].d = d
		depT[d] = append(depT[d], t)
		for _, w := range g[v] {
			if w != fa {
				f(w, v, d+1)
			}
		}
		is[v].tout = t
	}
	f(0, -1, 0)
	add := make([]int64, n)
	diff := make([][]int64, n)
	for i, row := range depT {
		diff[i] = make([]int64, len(row)+1)
	}
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &v, &d, &x)
		v--
		add[v] += x
		info := is[v]
		if d += info.d + 1; d < n {
			l := sort.SearchInts(depT[d], info.tin)
			r := sort.SearchInts(depT[d], info.tout+1)
			diff[d][l] += x
			diff[d][r] -= x
		}
	}

	ans := make([]interface{}, n)
	var f2 func(v, fa, d int, s int64)
	f2 = func(v, fa, d int, s int64) {
		s += add[v] - diff[d][0]
		diff[d][1] += diff[d][0]
		diff[d] = diff[d][1:]
		ans[v] = s
		for _, w := range g[v] {
			if w != fa {
				f2(w, v, d+1, s)
			}
		}
	}
	f2(0, -1, 0, 0)
	Fprintln(out, ans...)
}

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

package main

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

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

	var n, v, w, q, d int
	var x int64
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	tin := make([]int, n)
	tout := make([]int, n)
	depT := make([][]int, n)
	dep := make([]int, n)
	t := 0
	var f func(v, fa, d int)
	f = func(v, fa, d int) {
		t++
		tin[v] = t
		depT[d] = append(depT[d], t)
		dep[v] = d
		for _, w := range g[v] {
			if w != fa {
				f(w, v, d+1)
			}
		}
		tout[v] = t
	}
	f(0, -1, 0)
	add := make([]int64, n)
	diff := make([][]int64, n)
	for i, row := range depT {
		diff[i] = make([]int64, len(row)+1)
	}
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &v, &d, &x)
		v--
		add[v] += x
		if d += dep[v] + 1; d < n {
			l := sort.SearchInts(depT[d], tin[v])
			r := sort.SearchInts(depT[d], tout[v]+1)
			diff[d][l] += x
			diff[d][r] -= x
		}
	}

	ans := make([]interface{}, n)
	var f2 func(v, fa, d int, s int64)
	f2 = func(v, fa, d int, s int64) {
		s += add[v] - diff[d][0]
		diff[d][1] += diff[d][0]
		diff[d] = diff[d][1:]
		ans[v] = s
		for _, w := range g[v] {
			if w != fa {
				f2(w, v, d+1, s)
			}
		}
	}
	f2(0, -1, 0, 0)
	Fprintln(out, ans...)
}

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

package main

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

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

	var n, v, w, q, d, x int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	tin := make([]int, n)
	tout := make([]int, n)
	depT := make([][]int, n)
	dep := make([]int, n)
	t := 0
	var f func(v, fa, d int)
	f = func(v, fa, d int) {
		t++
		tin[v] = t
		depT[d] = append(depT[d], t)
		dep[v] = d
		for _, w := range g[v] {
			if w != fa {
				f(w, v, d+1)
			}
		}
		tout[v] = t
	}
	f(0, -1, 0)
	add := make([]int, n)
	diff := make([][]int, n)
	for i, row := range depT {
		diff[i] = make([]int, len(row)+1)
	}
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &v, &d, &x)
		v--
		add[v] += x
		if d += dep[v] + 1; d < n {
			l := sort.SearchInts(depT[d], tin[v])
			r := sort.SearchInts(depT[d], tout[v]+1)
			diff[d][l] += x
			diff[d][r] -= x
		}
	}

	ans := make([]interface{}, n)
	var f2 func(v, fa, d, s int)
	f2 = func(v, fa, d, s int) {
		s += add[v] - diff[d][0]
		diff[d][1] += diff[d][0]
		diff[d] = diff[d][1:]
		ans[v] = s
		for _, w := range g[v] {
			if w != fa {
				f2(w, v, d+1, s)
			}
		}
	}
	f2(0, -1, 0, 0)
	Fprintln(out, ans...)
}

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


package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	A := sc.NextIntArray()

	current := N
	stack := make([]bool, N+1)
	for i := 0; i < N; i++ {
		stack[A[i]] = true
		if A[i] != current {
			fmt.Println("")
			continue
		}

		line := make([]int, 0)
		for l := A[i]; stack[l]; l-- {
			line = append(line, l)
			current--
		}
		PrintI(line...)
	}
}

func mapToString(arr []int) []string {
	ret := make([]string, len(arr))
	for i := 0; i < len(arr); i++ {
		ret[i] = strconv.Itoa(arr[i])
	}
	return ret
}
func PrintI(args ...int) {
	fmt.Println(strings.Join(mapToString(args), " "))
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
  sc.Scan()
  i, e := strconv.Atoi(sc.Text())
  if e != nil {
    panic(e)
  }

  return i
}

func main() {
  var a [100001]bool

  sc.Split(bufio.ScanWords)
  n := nextInt()
  p := n

  for i := 0; i < n; i++ {
    x  := nextInt()
    a[x] = true
    for p > 0 && a[p] {
      fmt.Print(p, " ")
      p -= 1
    }
    fmt.Println("")
  }
}

package main

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

func main() {
	var n int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	numsArr := make([]int, n)
	availableNums := make([]bool, n)
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &numsArr[index])
	}
	max := n
	for index := 0; index < n; index++ {
		availableNums[numsArr[index]-1] = true
		for availableNums[max-1] {
			fmt.Print(max, " ")
			max--
			if max < 1 {
				break
			}
		}
		fmt.Println("")
	}

}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
  sc.Scan()
  i, e := strconv.Atoi(sc.Text())
  if e != nil {
    panic(e)
  }

  return i
}

func main() {
  sc.Split(bufio.ScanWords)
  var st []int
  var x int
  st = make([]int, 0, 100001)

  n := nextInt()
  x = nextInt()
  st = append(st, x)

  for i := 0; i < n - 1; i++ {
    x = nextInt()
    if st[len(st) - 1] > x {
      for j := len(st) - 1; j >= 0; j-- {
        if (j == 0) {
          fmt.Println(st[j])
        } else {
          fmt.Print(st[j], " ")
        }
      }
      st = make([]int, 0, 100001)
    } else {
      fmt.Println("")
    }
    st = append(st, x)
  }
  for j := len(st) - 1; j >= 0; j-- {
    if (j == 0) {
      fmt.Println(st[j])
    } else {
      fmt.Print(st[j], " ")
    }
  }
}

package main

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

func contains(availableNums []int, number int) int {
	for index := 0; index < len(availableNums); index++ {
		if availableNums[index] == number {
			fmt.Print(number, " ")
			number--
			contains(availableNums, number)
			break
		}
	}
	return number
}

func main() {
	var n, max int
	var availableNums []int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	numsArr := make([]int, n)
	max = 0
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &numsArr[index])
		if numsArr[index] > max {
			max = numsArr[index]
		}
	}
	for index := 0; index < n; index++ {
		if numsArr[index] == max {
			fmt.Print(max, " ")
			max--
			max = contains(availableNums, max)
		} else {
			availableNums = append(availableNums, numsArr[index])
		}
		fmt.Println("")
	}

}

package main

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

func contains(availableNums []int, number int) int {
	for index := 0; index < len(availableNums); index++ {
		if availableNums[index] == number {
			fmt.Print(number, " ")
			number--
			contains(availableNums, number)
		}
	}
	return number
}

func main() {
	var n, max int
	var availableNums []int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	numsArr := make([]int, n)
	max = 0
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &numsArr[index])
		if numsArr[index] > max {
			max = numsArr[index]
		}
	}
	for index := 0; index < n; index++ {
		if numsArr[index] == max {
			fmt.Print(max, " ")
			max--
			max = contains(availableNums, max)
		} else {
			availableNums = append(availableNums, numsArr[index])
		}
		fmt.Println("")
	}

}

package main

import (
	"fmt"
)

func contains(availableNums []int, number int) int {
	for index := 0; index < len(availableNums); index++ {
		if availableNums[index] == number {
			fmt.Print(number, " ")
			number--
			contains(availableNums, number)
			break
		}
	}
	return number
}

func main() {
	var n, max int
	var availableNums []int
	// reader := bufio.NewReader(os.Stdin)
	// fmt.Fscan(reader, &n)
	n = 10
	// numsArr := make([]int, n)
	numsArr := [...]int{5, 1, 6, 2, 8, 3, 4, 10, 9, 7}

	max = 10
	for index := 0; index < n; index++ {
		// fmt.Fscan(reader, &numsArr[index])
		if numsArr[index] > max {
			max = numsArr[index]
		}
	}
	for index := 0; index < n; index++ {
		if numsArr[index] == max {
			fmt.Print(max, " ")
			max--
			max = contains(availableNums, max)
		} else {
			availableNums = append(availableNums, numsArr[index])
		}
		fmt.Println("")
	}

}