This is an interactive problem.This is a hard version of the problem. The difference from the easy version is that in the hard version $$$1 \le t \le \min(n, 10^4)$$$ and the total number of queries is limited to $$$6 \cdot 10^4$$$.Polycarp is playing a computer game. In this game, an array consisting of zeros and ones is hidden. Polycarp wins if he guesses the position of the $$$k$$$-th zero from the left $$$t$$$ times.Polycarp can make no more than $$$6 \cdot 10^4$$$ requests totally of the following type:   ? $$$l$$$ $$$r$$$ — find out the sum of all elements in positions from $$$l$$$ to $$$r$$$ ($$$1 \le l \le r \le n$$$) inclusive. To make the game more interesting, each guessed zero turns into one and the game continues on the changed array. More formally, if the position of the $$$k$$$-th zero was $$$x$$$, then after Polycarp guesses this position, the $$$x$$$-th element of the array will be replaced from $$$0$$$ to $$$1$$$.Help Polycarp win the game.

NoteIn the first test, the array $$$[1, 0, 1, 1, 0, 1]$$$ is hidden. After answering the query $$$k=2$$$, the array changed to $$$[1, 0, 1, 1, 1, 1]$$$.

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

type BIT []int

func NewBit(l int) BIT {
	return make(BIT, l)
}

func (bit BIT) Add(p int, inc int) {
	if p == 0 {
		bit[p] += inc
		return
	}
	for p < len(bit) {
		bit[p] += inc
		p += (p & -p)
	}
}

func (bit BIT) Get(s, e int) int {
	return bit.getPrefix(e) - bit.getPrefix(s-1)
}

func (bit BIT) getPrefix(p int) int {
	if p < 0 {
		return 0
	}
	var total int = 0
	for p > 0 {
		total += bit[p]
		p -= (p & -p)
	}
	total += bit[p]
	return total
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		oldStart := 0
		oldEnd := n + 1
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if oldStart == start && oldEnd == end {
				os.Exit(2)
			}
			oldStart = start
			oldEnd = end
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	// var point float64 = float64(num) / float64(total+1)
	var point float64 = 0.5
	ret := int(point*float64(end-start+1)) + start
	if ret <= start {
		ret = start + 1
	}
	if ret > end {
		ret = end
	}
	return ret
}

type sel struct {
	start int
	end   int
}

var sols = map[sel]int{}
var bit = NewBit(200002)

func removeSol(p int) {
	bit.Add(p, -1)
}

func get(s, e int) int {
	adj := bit.Get(s, e)
	if r, ok := sols[sel{s, e}]; ok {
		return r + adj
	}
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	ret := e - s + 1 - k
	sols[sel{s, e}] = ret
	return ret + adj
}

func found(p int) {
	fmt.Printf("! %d\n", p)
	removeSol(p)
}

package main
import(."fmt";."sort")

//github.com/EndlessCheng/codeforces-go
func main(){
	var n,m,k int
	Scan(&n,&m)
	t:=make([]int,n+1)
	s:=make([]int,n+1)
	for i:=range s{s[i]=-1}
	for;m>0;m--{
		Scan(&k)
		p:=1+Search(n-1,func(r int)bool{
			r++
			c:=0
			for i:=r;i>0;i&=i-1{c+=t[i]}
			if s[r]<0{
				Println("?",1,r)
				Scan(&s[r])
				s[r]-=c
			}
			return s[r]+c+k<=r
		})
		Println("!",p)
		for;p<=n;p+=p&-p{t[p]++}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
type fenwick struct {
	tree []int64
}

func newFenwickTree(n int) fenwick {
	return fenwick{make([]int64, n)}
}
func (f fenwick) add(i int, val int) {
	for ; i < len(f.tree); i += i & -i {
		f.tree[i] += int64(val)
	}
}
func (f fenwick) sum(i int) (res int64) {
	for ; i > 0; i &= i - 1 {
		res += f.tree[i]
	}
	return
}

func run(_r io.Reader, _w io.Writer, Fscan func(r io.Reader, a ...interface{}) (n int, err error), Fprintln func(w io.Writer, a ...interface{}) (n int, err error)) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	var n, t, k int
	Fscan(in, &n, &t)

	q := func(l, r int) (resp int) {
		Fprintln(out, "?", l, r)
		out.Flush()
		Fscan(in, &resp)
		if resp == -1 {
			panic(r)
		}
		return
	}

	f := newFenwickTree(n + 10)
	prefix := newFenwickTree(n + 10)
	vis := make([]bool, n+1)

	for ti := 0; ti < t; ti++ {
		Fscan(in, &k)
		if k == -1 {
			panic(ti)
		}
		ans := sort.Search(n-1, func(r int) bool {
			r++
			var c int
			if vis[r] {
				c = int(f.sum(r))
			} else {
				c = q(1, r)
				pre := int(prefix.sum(r))
				f.add(r, c-pre)
				f.add(r+1, -(c - pre))
				vis[r] = true
			}
			return c+k <= r
		})
		ans++
		Fprintln(out, "!", ans)
		out.Flush()
		vis[ans] = true
		f.add(ans, 1)
		prefix.add(ans, 1)
	}
}

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

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	// var point float64 = float64(num) / float64(total+1)
	var point float64 = 0.5
	ret := int(point*float64(end-start+1)) + start
	if ret <= start {
		ret = start + 1
	}
	if ret > end {
		ret = end
	}
	return ret
}

type sel struct {
	start int
	end   int
}

type BIT struct {
    vals [200002]int
}

var tree = &BIT{};

func (b* BIT) Add(pos int){
    for pos < 200002 {
        b.vals[pos] += 1
        pos += (pos & -pos)
    }
}

func (b* BIT) getPrefix(pos int) int {
    res := 0
    for pos > 0 {
        res += b.vals[pos]
        pos -= (pos & -pos)
    }
    return res
}

func (b* BIT) Get(l, r int) int {
    return b.getPrefix(r) - b.getPrefix(l - 1)
}

var sols = map[sel]int{}

func removeSol(p int) {
    tree.Add(p)
    return
	for k, v := range sols {
		if k.start <= p && k.end >= p {
			sols[k] = v - 1
		}
	}
}

func get(s, e int) int {
    sub := tree.Get(s, e)
	if r, ok := sols[sel{s, e}]; ok {
		return r - sub
	}
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	ret := e - s + 1 - k
	sols[sel{s, e}] = ret + sub
	return ret - sub
}

func found(p int) {
	fmt.Printf("! %d\n", p)
	removeSol(p)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
    var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	// var point float64 = float64(num) / float64(total+1)
	var point float64 = 0.5
	ret := int(point*float64(end-start+1)) + start
	if ret <= start {
		ret = start + 1
	}
	if ret > end {
		ret = end
	}
	return ret
}

type sel struct {
	start int
	end   int
}

type BIT struct {
    vals [200002]int
}

func (b* BIT) Add(pos int){
    pos += 1
    for pos < 200001 {
        b.vals[pos] += 1
        pos += (pos & -pos)
    }
}

func (b* BIT) getPrefix(pos int) int {
    pos += 1
    res := 0
    for pos > 0 {
        res += b.vals[pos]
        pos -= (pos & -pos)
    }
    return res
}

var tree = &BIT{};

func (b* BIT) Get(l, r int) int {
    res := b.getPrefix(r)
    if l != 0 {
        res -= b.getPrefix(l - 1)
    }
    return res
}

var sols = map[sel]int{}

func removeSol(p int) {
    tree.Add(p)
    return
	for k, v := range sols {
		if k.start <= p && k.end >= p {
			sols[k] = v - 1
		}
	}
}

func get(s, e int) int {
    sub := tree.Get(s, e)
	if r, ok := sols[sel{s, e}]; ok {
		return r - sub
	}
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	ret := e - s + 1 - k
	sols[sel{s, e}] = ret + sub
	return ret - sub
}

func found(p int) {
	fmt.Printf("! %d\n", p)
	removeSol(p)
}

package main

import (
    "fmt"
)

type segment struct {
    left int
    right int
}

var strlen int
var cache = map[segment]int{}

func decrease(ind, left, right int) {
    cache[segment{left, right}]--

    if (left != right) {
        var mid = (left + right) / 2
        if (ind <= mid) {
            decrease(ind, left, mid)
        } else {
            decrease(ind, mid + 1, right)
        }
    }
}

func solve_task() {
    var k int
    fmt.Scan(&k)

    var l_bord = 0
    var r_bord = strlen - 1

    for r_bord - l_bord > 0 {
        var m_bord = (l_bord + r_bord) / 2
        var sg = segment{l_bord, m_bord}
        cnt, is_cached := cache[sg]
        if !is_cached {
            fmt.Println("?", l_bord + 1, m_bord + 1)
            fmt.Scan(&cnt)
            cnt = sg.right - sg.left + 1 - cnt
            cache[sg] = cnt
        }

        if k <= cnt {
            r_bord = m_bord
        } else {
            k -= cnt
            l_bord = m_bord + 1
        }
    }
    fmt.Printf("! %d\n", l_bord + 1)
    decrease(l_bord, 0, strlen - 1)
}

func main() {
    var n_tasks int
    fmt.Scan(&strlen, &n_tasks)
    for i_task := 0; i_task < n_tasks; i_task++ {
        solve_task()
    }
}

package main

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

var cache map[string]int

func requests(l, r int, reader *bufio.Reader) (sum int) {
	// var arr []int = []int{100, 1, 0, 1, 1, 0, 1}
	// for i := l; i <= r; i++ {
	// 	sum += arr[i]
	// }
	key := strconv.Itoa(l) + "-" + strconv.Itoa(r)
	var ok bool
	sum, ok = cache[key]
	if ok {
		return
	}
	fmt.Printf("? %d %d\n", l, r)
	os.Stdout.Sync()
	fmt.Fscanf(reader, "%d\n", &sum)

	cache[key] = sum
	return
}

func updateCache(l, r, index int) {
	var mid, sum int
	var key string
	var ok bool
	for l < r {
		mid = l + (r-l)/2

		if mid < index {
			l = mid + 1
		} else {
			r = mid
		}

		key = strconv.Itoa(l) + "-" + strconv.Itoa(r)
		sum, ok = cache[key]
		if ok {
			cache[key] = sum + 1
		}
	}
}

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

	var n, t, k int

	fmt.Fscanf(reader, "%d %d\n", &n, &t)
	cache = make(map[string]int, n)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d\n", &k)
		l, r := 1, n
		var mid, had int
		for l < r {
			mid = l + (r-l)/2
			had = mid - l + 1 - requests(l, mid, reader)
			if had < k {
				k = k - had
				l = mid + 1
			} else {
				r = mid
			}
		}

		updateCache(1, n, l)

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

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	// var point float64 = float64(num) / float64(total+1)
	var point float64 = 0.5
	ret := int(point*float64(end-start+1)) + start
	if ret == start {
		ret = start + 1
	}
	return ret
}

type sel struct {
	start int
	end   int
}

var sols = map[sel]int{}

func removeSol(p int) {
	for k, v := range sols {
		if k.start <= p && k.end >= p {
			sols[k] = v - 1
		}
	}
}

func get(s, e int) int {
	if r, ok := sols[sel{s, e}]; ok {
		return r
	}
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 - k
}

func found(p int) {
	fmt.Printf("! %d\n", p)
	removeSol(p)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	var point float64 = float64(num) / float64(total+1)
	ret := int(point*float64(end-start+1)) + start
	if ret == start {
		ret = start + 1
	}
	return ret
}

type sel struct {
	start int
	end   int
}

var sols = map[sel]int{}

func removeSol(p int) {
	for k, v := range sols {
		if k.start <= p && k.end >= p {
			sols[k] = v - 1
		}
	}
}

func get(s, e int) int {
	if r, ok := sols[sel{s, e}]; ok {
		return r
	}
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 - k
}

func found(p int) {
	fmt.Printf("! %d\n", p)
	removeSol(p)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	var point float64 = float64(num) / float64(total+1)
	ret := int(point*float64(end-start+1)) + start
	if ret == start {
		ret = start + 1
	}
	return ret
}

func get(s, e int) int {
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 - k
}

func found(p int) {
	fmt.Printf("! %d\n", p)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := int(float64(1)/float64(2)*float64(end-start+1)) + start
		// if k > total/2 {
		// 	mid = int(float64(total-k)/float64(total)*float64(end-start+1)) + start
		// }
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = int(float64(1)/float64(2)*float64(end-start+1)) + start
			// if k > total/2 {
			// 	mid = int(float64(total-k)/float64(total)*float64(end-start+1)) + start
			// }
		}
	}
}

func get(s, e int) int {
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 - k
}

func found(p int) {
	fmt.Printf("! %d\n", p)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := k/total*(end-start) + start
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid)
			if f >= k {
				end = mid
				total = f
			} else {
				start = mid + 1
				k -= f
				total = total - f
			}
			mid = k/total*(end-start) + start
		}
	}
}

func get(s, e int) int {
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 + k
}

func found(p int) {
	fmt.Printf("! %d\n", 5)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		get(1, n)
		get(1, n/2)
		mid := k/total*(end-start) + start
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid)
			if f >= k {
				end = mid
				total = f
			} else {
				start = mid + 1
				k -= f
				total = total - f
			}
			mid = k/total*(end-start) + start
		}
	}
}

func get(s, e int) int {
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 + k
}

func found(p int) {
	fmt.Printf("! %d\n", p)
}

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := k/total*(end-start) + start
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid)
			if f >= k {
				end = mid
				total = f
			} else {
				start = mid + 1
				k -= f
				total = total - f
			}
			mid = k/total*(end-start) + start
		}
	}
}

func get(s, e int) int {
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	return e - s + 1 + k
}

func found(p int) {
	fmt.Printf("! %d\n", p)
}

package main
import(."fmt";."sort")

// github.com/EndlessCheng/codeforces-go
func main(){
	var n,m,k int
	Scan(&n,&m)
	t:=make([]int,n+1)
	s:=make([]int,n+1)
	for i:=range s{s[i]=-1}
	for;m>0;m--{
		Scan(&k)
		p:=1+Search(n-1,func(r int)bool{
			if r++;s[r]<0{
				Println("?",1,r)
				Scan(&s[r])
			}
			c:=s[r]
			for i:=r;i>0;i&=i-1{c-=t[i]}
			return c+k<=r
		})
		Println("!",p)
		for;p<=n;p+=p&-p{t[p]++}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
type fenwick struct {
	tree []int64
}

func newFenwickTree(n int) fenwick {
	return fenwick{make([]int64, n)}
}
func (f fenwick) add(i int, val int) {
	for ; i < len(f.tree); i += i & -i {
		f.tree[i] += int64(val)
	}
}
func (f fenwick) sum(i int) (res int64) {
	for ; i > 0; i &= i - 1 {
		res += f.tree[i]
	}
	return
}

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

	var n, t, k int
	Fscan(in, &n, &t)

	q := func(l, r int) (resp int) {
		Fprintln(out, "?", l, r)
		out.Flush()
		Fscan(in, &resp)
		if resp == -1 {
			panic(r)
		}
		return
	}

	f := newFenwickTree(n + 10)
	vis := make([]bool, n+1)

	for ti := 0; ti < t; ti++ {
		Fscan(in, &k)
		if k == -1 {
			panic(ti)
		}
		ans := sort.Search(n-1, func(r int) bool {
			r++
			var c int
			if vis[r] {
				c = int(f.sum(r))
			} else {
				c = q(1, r)
				f.add(r, c)
				f.add(r+1, -c)
				vis[r] = true
			}
			return c+k <= r
		})
		ans++
		Fprintln(out, "!", ans)
		out.Flush()
		vis[ans] = true
		f.add(ans, 1)

	}
}

func main() { ioq() }

package main

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

// github.com/EndlessCheng/codeforces-go
type fenwick struct {
	tree []int
}

func newFenwickTree(n int) fenwick {
	return fenwick{make([]int, n)}
}
func (f fenwick) add(i int, val int) {
	for ; i < len(f.tree); i += i & -i {
		f.tree[i] += val
	}
}
func (f fenwick) sum(i int) (res int) {
	for ; i > 0; i &= i - 1 {
		res += f.tree[i]
	}
	return
}
func (f fenwick) query(l, r int) (res int) { // [l,r]
	return f.sum(r) - f.sum(l-1)
}

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

	var n, t, k int
	Fscan(in, &n, &t)

	q := func(l, r int) (resp int) {
		Fprintln(out, "?", l, r)
		out.Flush()
		Fscan(in, &resp)
		return
	}

	f := newFenwickTree(n + 10)
	vis := make([]bool, n+1)

	for ti := 0; ti < t; ti++ {
		Fscan(in, &k)
		ans := sort.Search(n-1, func(r int) bool {
			r++
			var c int
			if vis[r] {
				c = f.sum(r)
			} else {
				c = q(1, r)
				f.add(r, c)
				f.add(r+1, -c)
				vis[r] = true
			}
			return c+k <= r
		})
		ans++
		Fprintln(out, "!", ans)
		out.Flush()
		vis[ans] = true
		f.add(ans, 1)

	}
}

func main() { ioq() }

package main

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

// github.com/EndlessCheng/codeforces-go
type fenwick struct {
	tree []int
}

func newFenwickTree(n int) fenwick {
	return fenwick{make([]int, n)}
}
func (f fenwick) add(i int, val int) {
	for ; i < len(f.tree); i += i & -i {
		f.tree[i] += val
	}
}
func (f fenwick) sum(i int) (res int) {
	for ; i > 0; i &= i - 1 {
		res += f.tree[i]
	}
	return
}
func (f fenwick) query(l, r int) (res int) { // [l,r]
	return f.sum(r) - f.sum(l-1)
}

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

	var n, t, k int
	Fscan(in, &n, &t)

	q := func(l, r int) (resp int) {
		Fprintln(out, "?", l, r)
		out.Flush()
		Fscan(in, &resp)
		return
	}

	f := newFenwickTree(n + 10)
	vis := make([]bool, n+1)

	for ti := 0; ti < t; ti++ {
		Fscan(in, &k)
		ans := sort.Search(n-1, func(r int) bool {
			r++
			var c int
			if vis[r] {
				c = f.sum(r)
			} else {
				c = q(1, r)
				f.add(r, c)
				f.add(r+1, -c)
				vis[r] = true
			}
			return c+k <= r
		})
		ans++
		Fprintln(out, "!", ans)
		out.Flush()

		f.add(ans, 1)

	}
}

func main() { ioq() }

package main

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

func ReadByte() byte {
	return ReadBytes(1)[0]
}

func ReadNL() byte {
	var nl byte
	for nl != 0x0a {
		nl = ReadByte()
	}
	return nl
}

func ReadBytes(n int) []byte {
	p := make([]byte, n)
	_, err := io.ReadFull(os.Stdin, p)
	if err != nil {
		panic(err)
	}
	return p
}

func main() {
	var n int
	var t int
	_, err := fmt.Scanf("%d %d", &n, &t)
	if err != nil {
		panic(err)
	}
	for i := 0; i < t; i++ {
		ReadNL()
		var k int
		_, err := fmt.Scanf("%d", &k)
		if err != nil {
			panic(err)
		}
		if k == -1 {
			os.Exit(0)
		}
		start := 1
		end := n
		total := get(1, n)
		mid := getMid(start, end, total, k)
		for {
			if end == start {
				found(start)
				break
			}
			f := get(start, mid-1)
			if f >= k {
				end = mid - 1
				total = f
			} else {
				start = mid
				k -= f
				total -= f
			}
			mid = getMid(start, end, total, k)
		}
	}
}

func getMid(start, end, total, num int) int {
	// var point float64 = float64(num) / float64(total+1)
	var point float64 = 0.5
	ret := int(point*float64(end-start+1)) + start
	if ret <= start {
		ret = start + 1
	}
	if ret > end {
		ret = end
	}
	return ret
}

type sel struct {
	start int
	end   int
}

type BIT struct {
    vals [200002]int
}

func (b* BIT) Add(pos int){
    pos += 1
    for pos > 0 {
        b.vals[pos] += 1
        pos -= (pos & -pos)
    }
}

func (b* BIT) getPrefix(pos int) int {
    pos += 1
    res := 0
    for pos < 200002 {
        res += b.vals[pos]
        pos += (pos & -pos)
    }
    return res
}

var tree = &BIT{};

func (b* BIT) Get(l, r int) int {
    res := b.getPrefix(r)
    if l != 0 {
        res -= b.getPrefix(l - 1)
    }
    return res
}

var sols = map[sel]int{}

func removeSol(p int) {
    tree.Add(p)
    return
	for k, v := range sols {
		if k.start <= p && k.end >= p {
			sols[k] = v - 1
		}
	}
}

func get(s, e int) int {
    sub := tree.Get(s, e)
	if r, ok := sols[sel{s, e}]; ok {
		return r - sub
	}
	fmt.Printf("? %d %d\n", s, e)
	ReadNL()
	var k int
	_, err := fmt.Scanf("%d", &k)
	if err != nil {
		panic(err)
	}
	if k == -1 {
		os.Exit(0)
	}
	ret := e - s + 1 - k
	sols[sel{s, e}] = ret
	return ret - sub
}

func found(p int) {
	fmt.Printf("! %d\n", p)
	removeSol(p)
}

package main

import (
    "fmt"
)

type segment struct {
    left int
    right int
}

var strlen int
var cache = map[segment]int{}

func decrease(ind, left, right int) {
    cache[segment{left, right}]--

    if (left != right) {
        var mid = (left + right) / 2
        if (ind <= mid) {
            decrease(ind, left, mid)
        } else {
            decrease(ind, mid + 1, right)
        }
    }
}

func solve_task() {
    var k int
    fmt.Scanln(&k)

    var l_bord = 0
    var r_bord = strlen - 1

    for r_bord - l_bord > 0 {
        var m_bord = (l_bord + r_bord) / 2
        var sg = segment{l_bord, m_bord}
        cnt, is_cached := cache[sg]
        if !is_cached {
            fmt.Println("?", l_bord + 1, m_bord + 1)
            fmt.Scan(&cnt)
            cnt = sg.right - sg.left + 1 - cnt
            cache[sg] = cnt
        }

        if k <= cnt {
            r_bord = m_bord
        } else {
            k -= cnt
            l_bord = m_bord + 1
        }
    }
    fmt.Printf("! %d\n", l_bord + 1)
    decrease(l_bord, 0, strlen - 1)
}

func main() {
    var n_tasks int
    fmt.Scanln(&strlen, &n_tasks)
    for i_task := 0; i_task < n_tasks; i_task++ {
        solve_task()
    }
}

package main

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

func requests(l, r int, reader *bufio.Reader) (sum int) {
	// var arr []int = []int{100, 1, 0, 1, 1, 0, 1}
	// for i := l; i <= r; i++ {
	// 	sum += arr[i]
	// }
	fmt.Printf("? %d %d\n", l, r)
	os.Stdout.Sync()
	fmt.Fscanf(reader, "%d\n", &sum)

	return
}

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

	var n, t, k int

	fmt.Fscanf(reader, "%d %d\n", &n, &t)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d\n", &k)
		l, r := 1, n
		var mid, had int
		for l < r {
			mid = l + (r-l)/2
			had = mid - l + 1 - requests(l, mid, reader)
			if had < k {
				k = k - had
				l = mid + 1
			} else {
				r = mid
			}
		}

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


You are given a graph consisting of $$$n$$$ vertices and $$$m$$$ edges. It is not guaranteed that the given graph is connected. Some edges are already directed and you can't change their direction. Other edges are undirected and you have to choose some direction for all these edges.You have to direct undirected edges in such a way that the resulting graph is directed and acyclic (i.e. the graph with all edges directed and having no directed cycles). Note that you have to direct all undirected edges.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$) — the number of vertices and the number of edges in the graph, respectively. The next $$$m$$$ lines describe edges of the graph. The $$$i$$$-th edge is described with three integers $$$t_i$$$, $$$x_i$$$ and $$$y_i$$$ ($$$t_i \in [0; 1]$$$, $$$1 \le x_i, y_i \le n$$$) — the type of the edge ($$$t_i = 0$$$ if the edge is undirected and $$$t_i = 1$$$ if the edge is directed) and vertices this edge connects (the undirected edge connects vertices $$$x_i$$$ and $$$y_i$$$ and directed edge is going from the vertex $$$x_i$$$ to the vertex $$$y_i$$$). It is guaranteed that the graph do not contain self-loops (i.e. edges from the vertex to itself) and multiple edges (i.e. for each pair ($$$x_i, y_i$$$) there are no other pairs ($$$x_i, y_i$$$) or ($$$y_i, x_i$$$)). It is guaranteed that both sum $$$n$$$ and sum $$$m$$$ do not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$; $$$\sum m \le 2 \cdot 10^5$$$).

For each test case print the answer — "NO" if it is impossible to direct undirected edges in such a way that the resulting graph is directed and acyclic, otherwise print "YES" on the first line and $$$m$$$ lines describing edges of the resulted directed acyclic graph (in any order). Note that you cannot change the direction of the already directed edges. If there are several answers, you can print any.

NoteExplanation of the second test case of the example:Explanation of the third test case of the example:

package main

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

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

	type pair struct{ u, v int}

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		n,m :=0,0
		Fscan(in, &n, &m)
		undirected := make([] pair,0)
		e := make([][]int, n)
		deg := make([]int, n)
		for i:=0;i<m;i++{
			t,u,v :=0,0,0
			Fscan(in, &t, &u , &v)
			// Fprintln(out,t,u,v)
			u,v=u-1,v-1
			// Fprintln(out,t,u,v)
			if t==1 {
				e[u]=append(e[u],v)
				deg[v]++
				// Fprintln(out,"dr",u,v)
			} else {
				undirected = append(undirected,pair{u,v})
			}
		}

		topo := make([]int,0,n)
		pos := make([]int,n)
		for i:=0;i<n;i++{
			if deg[i]==0{
				topo=append(topo,i)
			}
		}

		fl:=true

		for i:=0;i<n;i++{
			if len(topo) == i{
				fl=false;
				break
			}
			u:=topo[i]
			pos[u]=i
			for _,v := range(e[u]){
				deg[v]--
				if deg[v]==0{
					topo=append(topo,v)
				}
			}
		}

		// Fprintln(out,deg)
		// Fprintln(out,undirected)
		// Fprintln(out,e)
		if !fl {
			Fprintln(out,"NO")
			continue
		}

		Fprintln(out,"YES")
		for u:=0;u<n;u++{
			for _,v := range(e[u]){
				Fprintln(out,u+1,v+1)
			}
		}
		// Fprintln(out,topo)
		for _,d := range(undirected){
			u,v := d.u,d.v
			if pos[u]<pos[v]{
				Fprintln(out,u+1,v+1)
			} else {
				Fprintln(out,v+1,u+1)
			}
		}
	}
}

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

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type edge struct {
		t, v int
	}

	for t := io.nextInt(); t > 0; t-- {
		n, m := io.nextInt(), io.nextInt()
		g := make([][]edge, n+1)
		in := make([]int, n+1)
		for i := 0; i < m; i++ {
			typ, u, v := io.nextInt(), io.nextInt(), io.nextInt()
			g[u] = append(g[u], edge{typ, v})
			if typ == 1 {
				in[v]++
			} else {
				g[v] = append(g[v], edge{typ, u})
			}
		}

		var q []int
		for u := 1; u <= n; u++ {
			if in[u] == 0 {
				q = append(q, u)
			}
		}

		var topo []int
		for len(q) > 0 {
			u := q[0]
			q = q[1:]
			topo = append(topo, u)
			for _, e := range g[u] {
				if e.t == 1 {
					in[e.v]--
					if in[e.v] == 0 {
						q = append(q, e.v)
					}
				}
			}
		}

		if len(topo) < n {
			io.println("NO")
		} else {
			io.println("YES")
			vis := make([]bool, n+1)
			for _, u := range topo {
				vis[u] = true
				for _, e := range g[u] {
					if !vis[e.v] {
						io.println(u, e.v)
					}
				}
			}
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p []byte
}

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p)
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p)
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p)
	for f.p[0] <= ' ' {
		f.Read(f.p)
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p)
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

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

type Queue struct {
    size int
    data []int
}

func QueueNew() *Queue {
    return &Queue{
        size: 0,
        data: make([]int, 0),
    }
}

func (q *Queue) Push(e int) {
    q.size++
    q.data = append(q.data, e)
}

func (q *Queue) Pop() int {
    rv := q.data[0]
    q.size--
    q.data = q.data[1:]
    return rv
}

func (q *Queue) Empty() bool {
    return q.size == 0
}

type IntVector struct {
    size int
    data []int
}

func IntVectorNew(size int) *IntVector {
    rv := &IntVector{
        size: size,
        data: make([]int, size),
    }
    for i := 0; i < rv.size; i++ {
        rv.data[i] = 0
    }
    return rv
}

func (v *IntVector) Push(e int) {
    v.size++
    v.data = append(v.data, e)
}

func (v *IntVector) Size() int {
    return v.size
}

func (v *IntVector) At(idx int) *int {
    return &v.data[idx]
}

type Pair struct {
    first int
    second int
}

type PairVector struct {
    size int
    data []Pair
}

func PairVectorNew(size int) *PairVector {
    rv := &PairVector{
        size: size,
        data: make([]Pair, size),
    }
    for i := 0; i < rv.size; i++ {
        rv.data[i] = Pair{first: 0, second: 0}
    }
    return rv
}

func (v *PairVector) Push(e Pair) {
    v.size++
    v.data = append(v.data, e)
}

func (v *PairVector) Size() int {
    return v.size
}

func (v *PairVector) At(idx int) *Pair {
    return &v.data[idx]
}

type VectorVector struct {
    size int
    data []*IntVector
}

func VectorVectorNew(size int) *VectorVector {
    rv := &VectorVector{
        size: size,
        data: make([]*IntVector, size),
    }
    for i := 0; i < rv.size; i++ {
        rv.data[i] = IntVectorNew(0)
    }
    return rv
}

func (v *VectorVector) Push(e *IntVector) {
    v.size++
    v.data = append(v.data, e)
}

func (v *VectorVector) Size() int {
    return v.size
}

func (v *VectorVector) At(idx int) *IntVector {
    return v.data[idx]
}

type Task struct {
    nedges int
    nvertices int
    directed *VectorVector
    undirected *PairVector
    position *IntVector
}

func TaskNew() *Task {
    return &Task{
        nedges: 0,
        nvertices: 0,
        directed: nil,
        undirected: PairVectorNew(0),
        position: nil,
    }
}

func (task *Task) ReadInput(stdin *bufio.Scanner) {
    stdin.Scan()
    fmt.Sscanf(stdin.Text(), "%d %d\n", &task.nvertices, &task.nedges)
    task.directed = VectorVectorNew(task.nvertices + 1)
    task.position = IntVectorNew(task.nvertices + 1)

    for i := 0; i < task.nedges; i++ {
        var t, from, to int
        stdin.Scan()
        fmt.Sscanf(stdin.Text(), "%d %d %d\n", &t, &from, &to)

        if 1 == t {
            task.directed.At(from).Push(to)
        } else {
            task.undirected.Push(Pair{first: from, second: to})
        }
    }
}

func (task *Task) WriteOutput() {
    if task.Topsort() == false {
        fmt.Println("NO")
        return
    }

    for u := 1; u <= task.nvertices; u++ {
        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            if *task.position.At(u) >= *task.position.At(v) {
                fmt.Println("NO")
                return
            }
        }
    }

    fmt.Println("YES")

    for u := 1; u <= task.nvertices; u++ {
        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            fmt.Println(u, v)
        }
    }

    for i := 0; i < task.undirected.Size(); i++ {
        u := task.undirected.At(i).first
        v := task.undirected.At(i).second
        if *task.position.At(u) < *task.position.At(v) {
            fmt.Println(u, v)
        } else {
            fmt.Println(v, u)
        }
    }
}

func (task *Task) Topsort() bool {
    queue := QueueNew()
    ideg := IntVectorNew(task.nvertices + 1)
    position := 1

    for u := 1; u <= task.nvertices; u++ {
        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            *ideg.At(v)++
        }
    }

    for u := 1; u <= task.nvertices; u++ {
        if *ideg.At(u) == 0 {
            queue.Push(u)
        }
    }

    if queue.Empty() {
        return false
    }

    for queue.Empty() == false {
        u := queue.Pop()
        *task.position.At(u) = position
        position++

        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            *ideg.At(v)--
            if *ideg.At(v) == 0 {
                queue.Push(v)
            }
        }
    }

    return true
}

func main() {
    stdin := bufio.NewScanner(os.Stdin)
    stdin.Scan()

    var ntests int
    fmt.Sscanf(stdin.Text(), "%d\n", &ntests)

    for n := 0; n < ntests; n++ {
        task := TaskNew()
        task.ReadInput(stdin)
        task.WriteOutput()
    }
}

package main

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

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

	var t, n, m, tp, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		g := make([][]pair, n)
		deg := make([]int, n)
		for ; m > 0; m-- {
			Fscan(in, &tp, &v, &w)
			v--
			w--
			g[v] = append(g[v], pair{w, tp})
			if tp == 0 {
				g[w] = append(g[w], pair{v, tp})
			} else {
				deg[w]++
			}
		}
		vis := make([]bool, n)
		used := make([]bool, n)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				vis[i] = true
				q = append(q, i)
			}
		}
		c := 0
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			c++
			for i, e := range g[v] {
				w := e.to
				if e.tp == 0 {
					if !used[w] {
						g[v][i].tp = 1
					}
				} else {
					deg[w]--
				}
				if deg[w] == 0 && !vis[w] {
					vis[w] = true
					q = append(q, w)
				}
			}
			used[v] = true //
		}
		if c < n {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for v, es := range g {
			for _, e := range es {
				if e.tp == 1 {
					Fprintln(out, v+1, e.to+1)
				}
			}
		}
	}
}

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

package main

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

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

	var t, n, m, tp, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		g := make([][]pair, n)
		deg := make([]int, n)
		for ; m > 0; m-- {
			Fscan(in, &tp, &v, &w)
			v--
			w--
			g[v] = append(g[v], pair{w, tp})
			if tp == 0 {
				g[w] = append(g[w], pair{v, tp})
			} else {
				deg[w]++
			}
		}
		vis := make([]int8, n)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				vis[i] = 1
				q = append(q, i)
			}
		}
		c := 0
		for len(q) > 0 {
			v, q = q[0], q[1:]
			c++
			for i, e := range g[v] {
				w := e.to
				if e.tp == 0 {
					if vis[w] < 2 {
						g[v][i].tp = 1
					}
				} else {
					deg[w]--
				}
				if deg[w] == 0 && vis[w] == 0 {
					vis[w] = 1
					q = append(q, w)
				}
			}
			vis[v] = 2
		}
		if c < n {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for v, es := range g {
			for _, e := range es {
				if e.tp == 1 {
					Fprintln(out, v+1, e.to+1)
				}
			}
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1385E(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	type pair struct{ to, tp int }

	for t := r(); t > 0; t-- {
		n, m := r(), r()
		g := make([][]pair, n)
		deg := make([]int, n)
		for ; m > 0; m-- {
			tp, v, w := r(), r()-1, r()-1
			g[v] = append(g[v], pair{w, tp})
			if tp == 0 {
				g[w] = append(g[w], pair{v, tp})
			} else {
				deg[w]++
			}
		}
		vis := make([]int8, n)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				vis[i] = 1
				q = append(q, i)
			}
		}
		c := 0
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			c++
			for i, e := range g[v] {
				w := e.to
				if e.tp == 0 {
					if vis[w] < 2 {
						g[v][i].tp = 1
					}
				} else {
					deg[w]--
				}
				if deg[w] == 0 && vis[w] == 0 {
					vis[w] = 1
					q = append(q, w)
				}
			}
			vis[v] = 2
		}
		if c < n {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for v, es := range g {
			for _, e := range es {
				if e.tp != 0 {
					Fprintln(out, v+1, e.to+1)
				}
			}
		}
	}
}

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

package main

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

func init() {
	debug.SetGCPercent(-1)
}

// github.com/EndlessCheng/codeforces-go
func CF1385E(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := [4096]byte{}
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			_r.Read(buf[:])
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	type pair struct{ to, tp int }

	for t := r(); t > 0; t-- {
		n, m := r(), r()
		g := make([][]pair, n)
		deg := make([]int, n)
		for ; m > 0; m-- {
			tp, v, w := r(), r()-1, r()-1
			g[v] = append(g[v], pair{w, tp})
			if tp == 0 {
				g[w] = append(g[w], pair{v, tp})
			} else {
				deg[w]++
			}
		}
		vis := make([]int8, n)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				vis[i] = 1
				q = append(q, i)
			}
		}
		c := 0
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			c++
			for i, e := range g[v] {
				w := e.to
				if e.tp == 0 {
					if vis[w] < 2 {
						g[v][i].tp = 1
					}
				} else {
					deg[w]--
				}
				if deg[w] == 0 && vis[w] == 0 {
					vis[w] = 1
					q = append(q, w)
				}
			}
			vis[v] = 2
		}
		if c < n {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for v, es := range g {
			for _, e := range es {
				if e.tp != 0 {
					Fprintln(out, v+1, e.to+1)
				}
			}
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1385E(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := [4096]byte{}
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			_r.Read(buf[:])
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	type pair struct{ to, tp int }

	for t := r(); t > 0; t-- {
		n, m := r(), r()
		g := make([][]pair, n)
		deg := make([]int, n)
		for ; m > 0; m-- {
			tp, v, w := r(), r()-1, r()-1
			g[v] = append(g[v], pair{w, tp})
			if tp == 0 {
				g[w] = append(g[w], pair{v, tp})
			} else {
				deg[w]++
			}
		}
		vis := make([]int8, n)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				vis[i] = 1
				q = append(q, i)
			}
		}
		c := 0
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			c++
			for i, e := range g[v] {
				w := e.to
				if e.tp == 0 {
					if vis[w] < 2 {
						g[v][i].tp = 1
					}
				} else {
					deg[w]--
				}
				if deg[w] == 0 && vis[w] == 0 {
					vis[w] = 1
					q = append(q, w)
				}
			}
			vis[v] = 2
		}
		if c < n {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for v, es := range g {
			for _, e := range es {
				if e.tp != 0 {
					Fprintln(out, v+1, e.to+1)
				}
			}
		}
	}
}

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

package main

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

type Age struct {
	x, y int
}

var color []int
var g [][]int
var gOr [][]int

var hasCycle bool
var ans []Age
var used []bool

func dfs(v int) {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs(to)
		} else if color[to] == 1 {
			hasCycle = true
			break
		}
	}

	if hasCycle {
		return
	}

	for _, to := range gOr[v] {
		if !used[to] {
			g[to] = append(g[to], v)
			ans = append(ans, Age{to, v})
		}
	}

	used[v] = true
	color[v] = 2
}

func dfs2(v int)  {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs2(to)
		} else if color[to] == 1 {
			hasCycle = true
			break
		}
	}
	color[v] = 2
}

func main() {
	defer writer.Flush()
	tests := NextInt()
	for tests > 0 {
		tests--
		n, m := NextInt(), NextInt()
		g = make([][]int, n+1)
		gOr = make([][]int, n+1)
		for i := 1; i <= n; i++ {
			g[i] = make([]int, 0)
			gOr[i] = make([]int, 0)
		}

		ans = make([]Age, 0)
		for i := 0; i < m; i++ {
			t, x, y := NextInt(), NextInt(), NextInt()
			if t == 1 {
				ans = append(ans, Age{x, y})
				g[x] = append(g[x], y)
			} else {
				gOr[x] = append(gOr[x], y)
				gOr[y] = append(gOr[y], x)
			}
		}

		color = make([]int, n+1)
		hasCycle = false
		used = make([]bool, n+1)

		for i := 1; i <= n; i++ {
			if color[i] == 0 {
				dfs(i)
			}
		}

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

		if hasCycle {
			Println("NO")
		} else {
			Println("YES")
			for _, age := range ans {
				Print(age.x)
				Print(" ")
				Println(age.y)
			}
		}
	}
}

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

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type edge struct {
		t, v int
	}

	for t := io.nextInt(); t > 0; t-- {
		n, m := io.nextInt(), io.nextInt()
		g := make([][]edge, n+1)
		in := make([]int, n+1)
		for i := 0; i < m; i++ {
			typ, u, v := io.nextInt(), io.nextInt(), io.nextInt()
			g[u] = append(g[u], edge{typ, v})
			if typ == 1 {
				in[v]++
			} else {
				g[v] = append(g[v], edge{typ, u})
			}
		}

		var q []int
		for u := 1; u <= n; u++ {
			if in[u] == 0 {
				q = append(q, u)
			}
		}

		var topo []int
		for len(q) > 0 {
			u := q[0]
			q = q[1:]
			topo = append(topo, u)
			for _, e := range g[u] {
				if e.t == 1 {
					in[e.v]--
					if in[e.v] == 0 {
						fmt.Println("push", e.v)
						q = append(q, e.v)
					}
				}
			}
		}

		if len(topo) < n {
			io.println("NO")
		} else {
			io.println("YES")
			vis := make([]bool, n+1)
			for _, u := range topo {
				vis[u] = true
				for _, e := range g[u] {
					if !vis[e.v] {
						io.println(u, e.v)
					}
				}
			}
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p []byte
}

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p)
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p)
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p)
	for f.p[0] <= ' ' {
		f.Read(f.p)
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p)
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
    "fmt"
)

type Queue struct {
    size int
    data []int
}

func QueueNew() *Queue {
    return &Queue{
        size: 0,
        data: make([]int, 0),
    }
}

func (q *Queue) Push(e int) {
    q.size++
    q.data = append(q.data, e)
}

func (q *Queue) Pop() int {
    rv := q.data[0]
    q.size--
    q.data = q.data[1:]
    return rv
}

func (q *Queue) Empty() bool {
    return q.size == 0
}

type IntVector struct {
    size int
    data []int
}

func IntVectorNew(size int) *IntVector {
    rv := &IntVector{
        size: size,
        data: make([]int, size),
    }
    for i := 0; i < rv.size; i++ {
        rv.data[i] = 0
    }
    return rv
}

func (v *IntVector) Push(e int) {
    v.size++
    v.data = append(v.data, e)
}

func (v *IntVector) Size() int {
    return v.size
}

func (v *IntVector) At(idx int) *int {
    return &v.data[idx]
}

type Pair struct {
    first int
    second int
}

type PairVector struct {
    size int
    data []Pair
}

func PairVectorNew(size int) *PairVector {
    rv := &PairVector{
        size: size,
        data: make([]Pair, size),
    }
    for i := 0; i < rv.size; i++ {
        rv.data[i] = Pair{first: 0, second: 0}
    }
    return rv
}

func (v *PairVector) Push(e Pair) {
    v.size++
    v.data = append(v.data, e)
}

func (v *PairVector) Size() int {
    return v.size
}

func (v *PairVector) At(idx int) *Pair {
    return &v.data[idx]
}

type VectorVector struct {
    size int
    data []*IntVector
}

func VectorVectorNew(size int) *VectorVector {
    rv := &VectorVector{
        size: size,
        data: make([]*IntVector, size),
    }
    for i := 0; i < rv.size; i++ {
        rv.data[i] = IntVectorNew(0)
    }
    return rv
}

func (v *VectorVector) Push(e *IntVector) {
    v.size++
    v.data = append(v.data, e)
}

func (v *VectorVector) Size() int {
    return v.size
}

func (v *VectorVector) At(idx int) *IntVector {
    return v.data[idx]
}

type Task struct {
    nedges int
    nvertices int
    directed *VectorVector
    undirected *PairVector
    position *IntVector
}

func TaskNew() *Task {
    return &Task{
        nedges: 0,
        nvertices: 0,
        directed: nil,
        undirected: PairVectorNew(0),
        position: nil,
    }
}

func (task *Task) ReadInput() {
    fmt.Scanf("%d %d\n", &task.nvertices, &task.nedges)
    task.directed = VectorVectorNew(task.nvertices + 1)
    task.position = IntVectorNew(task.nvertices + 1)

    for i := 0; i < task.nedges; i++ {
        var t, from, to int
        fmt.Scanf("%d %d %d\n", &t, &from, &to)

        if 1 == t {
            task.directed.At(from).Push(to)
        } else {
            task.undirected.Push(Pair{first: from, second: to})
        }
    }
}

func (task *Task) WriteOutput() {
    if task.Topsort() == false {
        fmt.Println("NO")
        return
    }

    for u := 1; u <= task.nvertices; u++ {
        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            if *task.position.At(u) >= *task.position.At(v) {
                fmt.Println("NO")
                return
            }
        }
    }

    fmt.Println("YES")

    for u := 1; u <= task.nvertices; u++ {
        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            if *task.position.At(u) >= *task.position.At(v) {
                fmt.Println(u, v)
            } else {
                fmt.Println(v, u)
            }
        }
    }

    for i := 0; i < task.undirected.Size(); i++ {
        u := task.undirected.At(i).first
        v := task.undirected.At(i).second
        if *task.position.At(u) < *task.position.At(v) {
            fmt.Println(u, v)
        } else {
            fmt.Println(v, u)
        }
    }
}

func (task *Task) Topsort() bool {
    queue := QueueNew()
    ideg := IntVectorNew(task.nvertices + 1)
    position := 1

    for u := 1; u <= task.nvertices; u++ {
        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            *ideg.At(v)++
        }
    }

    for u := 1; u <= task.nvertices; u++ {
        if *ideg.At(u) == 0 {
            queue.Push(u)
        }
    }

    if queue.Empty() {
        return false
    }

    for queue.Empty() == false {
        u := queue.Pop()
        *task.position.At(u) = position
        position++

        for vidx := 0; vidx < task.directed.At(u).Size(); vidx++ {
            v := *task.directed.At(u).At(vidx)
            *ideg.At(v)--
            if *ideg.At(v) == 0 {
                queue.Push(v)
            }
        }
    }

    return true
}

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

    for n := 0; n < ntests; n++ {
        task := TaskNew()
        task.ReadInput()
        task.WriteOutput()
    }
}

package main

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

type Age struct {
	x, y int
}

var color []int
var g [][]int
var gOr [][]int

var hasCycle bool
var ans []Age
var used []bool

func dfs(v int) {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs(to)
		} else if color[to] == 1 {
			hasCycle = true
		}
	}
	for _, to := range gOr[v] {
		if !used[to] {
			ans = append(ans, Age{to, v})
		}
	}

	used[v] = true
	color[v] = 2
}

func main() {
	defer writer.Flush()
	tests := NextInt()
	for tests > 0 {
		tests--
		n, m := NextInt(), NextInt()
		g = make([][]int, n+1)
		gOr = make([][]int, n+1)
		for i := 1; i <= n; i++ {
			g[i] = make([]int, 0)
			gOr[i] = make([]int, 0)
		}

		ans = make([]Age, 0)
		for i := 0; i < m; i++ {
			t, x, y := NextInt(), NextInt(), NextInt()
			if t == 1 {
				ans = append(ans, Age{x, y})
				g[x] = append(g[x], y)
			} else {
				gOr[x] = append(g[x], y)
				gOr[y] = append(g[y], x)
			}
		}

		color = make([]int, n+1)
		hasCycle = false

		used = make([]bool, n+1)
		for i := 1; i <= n; i++ {
			if color[i] == 0 {
				dfs(i)
			}
		}
		if hasCycle {
			Println("NO")
		} else {
			Println("YES")
			for _, age := range ans {
				Print(age.x)
				Print(" ")
				Println(age.y)
			}
		}
	}
}

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

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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

package main

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

type Age struct {
	x, y int
}

var color []int
var g [][]int
var gOr [][]int

var hasCycle bool
var ans []Age
var used []bool

func dfs(v int) {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs(to)
		} else if color[to] == 1 {
			hasCycle = true
			break
		}
	}

	if hasCycle {
		return
	}

	for _, to := range gOr[v] {
		if !used[to] {
			g[to] = append(g[to], v)
			ans = append(ans, Age{to, v})
		}
	}

	used[v] = true
	color[v] = 2
}

func dfs2(v int)  {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs2(to)
		} else if color[to] == 1 {
			hasCycle = true
			break
		}
	}
	color[v] = 2
}
func main() {
	defer writer.Flush()
	tests := NextInt()
	for tests > 0 {
		tests--
		n, m := NextInt(), NextInt()
		g = make([][]int, n+1)
		gOr = make([][]int, n+1)
		for i := 1; i <= n; i++ {
			g[i] = make([]int, 0)
			gOr[i] = make([]int, 0)
		}

		ans = make([]Age, 0)
		for i := 0; i < m; i++ {
			t, x, y := NextInt(), NextInt(), NextInt()
			if t == 1 {
				ans = append(ans, Age{x, y})
				g[x] = append(g[x], y)
			} else {
				gOr[x] = append(g[x], y)
				gOr[y] = append(g[y], x)
			}
		}

		color = make([]int, n+1)
		hasCycle = false
		used = make([]bool, n+1)

		for i := 1; i <= n; i++ {
			if color[i] == 0 {
				dfs(i)
			}
		}

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

		if hasCycle {
			Println("NO")
		} else {
			Println("YES")
			for _, age := range ans {
				Print(age.x)
				Print(" ")
				Println(age.y)
			}
		}
	}
}

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

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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

package main

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

type Age struct {
	x, y int
}

var color []int
var g [][]int
var gOr [][]int

var hasCycle bool
var ans []Age
var used []bool

func dfs(v int) {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs(to)
		} else if color[to] == 1 {
			hasCycle = true
			break
		}
	}

	if hasCycle {
		return
	}

	for _, to := range gOr[v] {
		if !used[to] {
			g[to] = append(g[to], v)
			ans = append(ans, Age{to, v})
		}
	}

	used[v] = true
	color[v] = 2
}

func dfs2(v int)  {
	color[v] = 1
	for _, to := range g[v] {
		if color[to] == 0 {
			dfs(to)
		} else if color[to] == 1 {
			hasCycle = true
			break
		}
	}
	color[v] = 2
}
func main() {
	defer writer.Flush()
	tests := NextInt()
	for tests > 0 {
		tests--
		n, m := NextInt(), NextInt()
		g = make([][]int, n+1)
		gOr = make([][]int, n+1)
		for i := 1; i <= n; i++ {
			g[i] = make([]int, 0)
			gOr[i] = make([]int, 0)
		}

		ans = make([]Age, 0)
		for i := 0; i < m; i++ {
			t, x, y := NextInt(), NextInt(), NextInt()
			if t == 1 {
				ans = append(ans, Age{x, y})
				g[x] = append(g[x], y)
			} else {
				gOr[x] = append(g[x], y)
				gOr[y] = append(g[y], x)
			}
		}

		color = make([]int, n+1)
		hasCycle = false
		used = make([]bool, n+1)

		for i := 1; i <= n; i++ {
			if color[i] == 0 {
				dfs(i)
			}
		}

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

		if hasCycle {
			Println("NO")
		} else {
			Println("YES")
			for _, age := range ans {
				Print(age.x)
				Print(" ")
				Println(age.y)
			}
		}
	}
}

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

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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


Vasya has a grid with $$$2$$$ rows and $$$n$$$ columns. He colours each cell red, green, or blue.Vasya is colourblind and can't distinguish green from blue. Determine if Vasya will consider the two rows of the grid to be coloured the same.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of columns of the grid. The following two lines each contain a string consisting of $$$n$$$ characters, each of which is either R, G, or B, representing a red, green, or blue cell, respectively — the description of the grid.

For each test case, output "YES" if Vasya considers the grid's two rows to be identical, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

6

2

RG

RB

4

GRBG

GBGB

5

GGGGG

BBBBB

7

BBBBBBB

RRRRRRR

8

RGBRRGBR

RGGRRBGR

1

G

G

NoteIn the first test case, Vasya sees the second cell of each row as the same because the second cell of the first row is green and the second cell of the second row is blue, so he can't distinguish these two cells. The rest of the rows are equal in colour. Therefore, Vasya will say that the two rows are coloured the same, even though they aren't.In the second test case, Vasya can see that the two rows are different.In the third test case, every cell is green or blue, so Vasya will think they are the same.

package main

import "fmt"

func findAns(input1, input2 string, n int) {
	// fmt.Println(input1, "   ", input2)
	for i := 0; i < n; i++ {
		if input1[i] != input2[i] && !((string(input1[i]) == "G" && string(input2[i]) == "B") || (string(input1[i]) == "B" && string(input2[i]) == "G")) {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
}

func main() {
	t, n := 0, 0
	input1, input2 := "", ""
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&n)
		fmt.Scan(&input1)
		fmt.Scan(&input2)
		findAns(input1, input2, n)
		t--
	}
}

package main

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

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

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

func main() {

	defer writer.Flush()

	var t int
	scanws(&t)

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

		var n int
		scanws(&n)

		var s1 string
		var s2 string

		scanws(&s1)
		scanws(&s2)

		gb := "GB"

		f := 0

		for j := 0; j < len(s1); j++ {

			if (s1[j] == gb[0] || s1[j] == gb[1]) && (s2[j] == gb[0] || s2[j] == gb[1]) {

				continue
			} else {
				if (s1[j] != s2[j]) {
					printf("NO\n")
					f = 1
					break
				}
			}

		}

		if f == 0 {
			printf("YES\n")
		}

	}
}

package main

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

func main() {
	var (
		t      int
		n      int
		str1   string
		str2   string
		strsl1 []string
		strsl2 []string
		c      int
	)
	fmt.Scanf("%d\n", &t)
	scanner := bufio.NewScanner(os.Stdin)
	for i := 0; i < t; i++ {
		strsl1, strsl2, c = nil, nil, 0
		scanner.Scan()
		n, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		str1 = scanner.Text()
		scanner.Scan()
		str2 = scanner.Text()
		strsl1, strsl2 = strings.Split(str1, ""), strings.Split(str2, "")
		for j := 0; j < n; j++ {
			if strsl1[j] == strsl2[j] {
				c++
			} else if (strsl1[j] == "B" || strsl1[j] == "G") && (strsl2[j] == "B" || strsl2[j] == "G") {
				c++
			}
		}
		if c == n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		var a,b string
		fmt.Scan(&a,&b)
		for j:=0;j<n;j++{
			if string(a[j]) == "R" {
				if string(b[j]) != "R" {
					fmt.Println("NO")
					goto out
				}
			} else {
				if string(b[j]) == "R" {
					fmt.Println("NO")
					goto out
				}
			}
		}
		fmt.Println("YES")
		out:
	}
}

package main

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

func main() {
	cin := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(cin, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(cin, &n)
		var first_row, second_row string
		fmt.Fscan(cin, &first_row, &second_row)
		same := true
		for j := 0; j < n; j++ {
			if first_row[j] != second_row[j] {
				if first_row[j] == 'R' || second_row[j] == 'R' {
					same = false
					break
				}
			}
		}
		if same {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var T int
	var N int
	var flag bool
	var S string
	var S2 string
	fmt.Scan(&T)
	for T > 0 {
		fmt.Scan(&N)
		fmt.Scan(&S)
		fmt.Scan(&S2)
		flag = true
		for i := 0; i < N; i++ {
			if S[i] != S2[i] && (S[i] == 'R' || S2[i] == 'R') {
				flag = false
				break
			}
		}
		if flag {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		T--
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	nrTests, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < nrTests; i++ {
		scanner.Scan()
		nrSym, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		line1 := scanner.Text()
		scanner.Scan()
		line2 := scanner.Text()
		same := true
		for j := 0; j < nrSym; j++ {
			if line1[j] != line2[j] {
				if (string(line1[j]) == "G" && string(line2[j]) == "B") || (string(line1[j]) == "B" && string(line2[j]) == "G") {
				} else {
					same = false
					break
				}
			}
		}
		if same {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func RGB(A, B string, n int) bool {

	ret := true
	for i := 0; i < n; i++ {
		if A[i] == 'R' {
			if B[i] != 'R' {
				ret = false

			}
		}
		if A[i] != 'R' {
			if B[i] == 'R' {
				ret = false
			}
		}
	}

	return ret
}

func main() {

	var tc int

	fmt.Scanln(&tc)
	for t := 1; t <= tc; t++ {
		var n int
		fmt.Scanln(&n)
		var A, B string
		fmt.Scanln(&A)
		fmt.Scanln(&B)

		check := RGB(A, B, n)
		if check == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

func inArray(x byte, arr []byte) bool {
	for i := range arr {
		if x == arr[i] {
			return true
		}
	}
	return false
}

func equal(x, y byte) bool {
	return (x == 'R' && y == 'R') || (inArray(x, []byte{'G', 'B'}) && inArray(y, []byte{'G', 'B'}))
}

func solve(n int, p, q string) string {
	for i := 0; i < n; i++ {
		if !equal(p[i], q[i]) {
			return "NO"
		}
	}
	return "YES"
}

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

package main

import (
	"fmt"
	"regexp"
	"sort"
)

type runes []rune

func (s runes) Less(i, j int) bool {
	return s[i] < s[j]
}

func (s runes) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s runes) Len() int {
	return len(s)
}

func SortString(s string) string {
	r := []rune(s)
	sort.Sort(runes(r))
	return string(r)
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	r := regexp.MustCompile(`([G|B])`)
	for t > 0 {
		t--
		var n int
		var r1, r2 string
		fmt.Scanf("%d\n", &n)
		fmt.Scanf("%s\n %s\n", &r1, &r2)
		r1 = r.ReplaceAllString(r1, "X")
		r2 = r.ReplaceAllString(r2, "X")
		ok := true
		for i := range r1 {
			if r1[i] != r2[i] {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

// https://codeforces.com/contest/1722/problem/B
// 将第一行字符中的所有'B'全部替换为字符'C'，然后比较两个字符是否一致

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		//fmt.Scanf("%d\n", &n)
		fmt.Scan(&n)
		var str1, str2 string
		//fmt.Scanf("%s\n", &str1)
		fmt.Scan(&str1)
		b1 := []byte(str1)
		//fmt.Scanf("%s\n", &str2)
		fmt.Scan(&str2)
		b2 := []byte(str2)
		for j := 0; j < n; j++ {
			if b1[j] == 'B' {
				b1[j] = 'G'
			}
			if b2[j] == 'B' {
				b2[j] = 'G'
			}
		}
		str1 = string(b1)
		str2 = string(b2)
		//fmt.Println(str)
		//fmt.Println(str2)
		if str2 == str1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
)

func Solve_1722B() {
	var t, n int
	fmt.Scan(&t)
	for t > 0 {
		t--
		fmt.Scan(&n)
		flag := true
		var s1, s2 string
		fmt.Scan(&s1)
		fmt.Scan(&s2)
		ss1 := []byte(s1)
		ss2 := []byte(s2)
		for i, _ := range ss1 {
			if ss1[i] == ss2[i] || ss1[i] == 'G' && ss2[i] == 'B' || ss1[i] == 'B' && ss2[i] == 'G' {
				continue
			}
			flag = false
			break
		}
		if flag {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func main() {
  Solve_1722B()
}

package main

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

func main() {
	r := readerStdin()
	cases := readInt(r)

	for cases != 0 {
		lenField := readInt(r)
		first, second := readString(r), readString(r)

		for pointer := 0; pointer < lenField; pointer++ {
			if first[pointer] == 'R' && second[pointer] != 'R' || first[pointer] != 'R' && second[pointer] == 'R' {
				fmt.Println("NO")
				goto Next
			}
		}

		fmt.Println("YES")
	Next:
		cases--
	}
}

func readerStdin() *bufio.Reader {
	return bufio.NewReader(os.Stdin)
}

func readString(r *bufio.Reader) (s string) {
	s, _ = r.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return
}
func readInt(r *bufio.Reader) (i int) {
	str := readString(r)
	i, _ = strconv.Atoi(str)
	return
}

package main

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

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

func (in *CF1722B) 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 *CF1722B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1722B
Date: 31/08/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1722/B
Problem: CF1722B
**/
func (in *CF1722B) Run() {
	mapa := make(map[uint8]uint8)
	mapa['B'] = 'G'
	mapa['G'] = 'B'
	mapa['R'] = 'R'
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := in.NextString()
		b := in.NextString()

		equals := true
		for i := 0; i < n; i++ {
			if a[i] == b[i] || (a[i] == 'G' && b[i] == 'B') || (a[i] == 'B' && b[i] == 'G') {
				continue
			} else {
				equals = false
			}
		}

		if equals {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

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

package main
import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	defer writer.Flush()
	var t, n uint32
	var colors1, colors2 string

	scanws(&t)

	for ; t > 0; t-- {
		scanws(&n)
		scanws(&colors1)
		scanws(&colors2)
		printf("%s\n", yesNoMap(compColorStrings(colors1, colors2)))
	}
}

func compColorStrings(s1, s2 string) bool {
	for i := range s1 {
		if s1[i] == 'R' && s2[i] != 'R' || s1[i] != 'R' && s2[i] == 'R' {
			return false
		}
	}
	return true
}

func yesNoMap(tf bool) string {
	return map[bool]string{false: "NO", true: "YES"}[tf]
}

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanws(a ...interface{}) { fmt.Fscan(reader, a...) }

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	res := true
	var rs1, rs2 []rune

	n := make([]int, t)
	s1 := make([]string, t)
	s2 := make([]string, t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n[i])
		fmt.Scan(&s1[i])
		fmt.Scan(&s2[i])
	}
	for i := 0; i < t; i++ {
		rs1 = []rune(s1[i])
		rs2 = []rune(s2[i])
		for j := 0; j < n[i]; j++ {
			if rs1[j] == rs2[j] {
				continue
			} else {
				if rs1[j] == 'G' && rs2[j] == 'B' || rs1[j] == 'B' && rs2[j] == 'G' {
					continue
				} else {
					res = false
				}
			}
		}
		if !res {
			res = true
			fmt.Println("NO")
		} else {
			res = true
			fmt.Println("YES")
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

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

out:
	for i := 0; i < n; i++ {
		var length int
		var str1 string
		var str2 string

		fmt.Scanln(&length)
		fmt.Scanln(&str1)
		fmt.Scanln(&str2)

		for j := 0; j < length; j++ {
			str1 = strings.Replace(str1, "G", "B", -1)
			str2 = strings.Replace(str2, "G", "B", -1)

			if string(str1[j]) != string(str2[j]) {
				fmt.Println("No")
				continue out
			}
		}
		fmt.Println("Yes")

	}

}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for j := 0; j < t; j++ {
		var n int
		fmt.Scan(&n)
		var s1, s2 string
		fmt.Scan(&s1)
		fmt.Scan(&s2)
		//fmt.Printf("%v %v \n",s1,s2)
		flag := 1
		for i := 0; i < n; i++ {
			if s1[i] != s2[i] {
				if (s1[i] == 'B' && s2[i] == 'G') || (s1[i] == 'G' && s2[i] == 'B') {
					continue
				} else {
					flag = 0
					break
				}
			}
		}
		if flag == 1 {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

package main

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

var (
	in     *bufio.Reader = bufio.NewReader(os.Stdin)
	t, n   int
	s1, s2 string
)

func main() {
	fmt.Fscan(in, &t)
	for t > 0 {
		fmt.Fscan(in, &n, &s1, &s2)
		solve()
		t--
	}
}

func solve() {
	for i := 0; i < n; i++ {
		if s1[i] == s2[i] || s1[i] == 'G' && s2[i] == 'B' || s1[i] == 'B' && s2[i] == 'G' {
			continue
		}
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
}

package main

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

type float = float32
type double = float64
type long = int64
type ulong = uint64

type Readable interface {
	bool | int | uint64 | int64 | double | float | byte
}

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

func print(arg ...interface{})            { fmt.Fprint(out, arg...) }
func println(arg ...interface{})          { fmt.Fprintln(out, arg...) }
func printf(f string, arg ...interface{}) { fmt.Fprintf(out, f, arg...) }
func scanf(f string, arg ...interface{})  { fmt.Fscanf(in, f, arg...) }

func readLn() string {
	ln, err := in.ReadString('\n')
	if err != nil {
		panic(err)
	}

	return strings.Trim(ln, " \n\r")
}

func readString() string {
	return readLn()
}

func readStrings() []string {
	return strings.Split(readLn(), " ")
}

func read[T Readable](f func(string) (T, error), input ...string) T {
	if len(input) == 0 {
		input = []string{readLn()}
	}

	res, err := f(input[0])
	if err != nil {
		panic(err)
	}

	return res
}

func readMany[T Readable](f func(...string) T) []T {
	input := readStrings()
	arr := make([]T, len(input))
	for i := 0; i < len(arr); i++ {
		arr[i] = f(input[i])
	}

	return arr
}

func readBool(input ...string) bool {
	return read(strconv.ParseBool, input...)
}

func readInt(input ...string) int {
	return read(strconv.Atoi, input...)
}

func readByte(input ...string) byte {
	return byte(read(func(s string) (uint64, error) {
		return strconv.ParseUint(s, 10, 8)
	}, input...))
}

func readDouble(input ...string) double {
	return read(func(s string) (float64, error) {
		return strconv.ParseFloat(s, 64)
	}, input...)
}

func readFloat(input ...string) float {
	return float32(read(func(s string) (float64, error) {
		return strconv.ParseFloat(s, 32)
	}, input...))
}

func readLong(input ...string) long {
	return read(func(s string) (int64, error) {
		return strconv.ParseInt(s, 10, 64)
	}, input...)
}

func readULong(input ...string) ulong {
	return read(func(s string) (uint64, error) {
		return strconv.ParseUint(s, 10, 64)
	}, input...)
}

func readBinary(input ...string) long {
	return read(func(s string) (int64, error) {
		return strconv.ParseInt(s, 2, 64)
	}, input...)
}

func readBools() []bool {
	return readMany(readBool)
}

func readInts() []int {
	return readMany(readInt)
}

func readBytes() []byte {
	return readMany(readByte)
}

func readDoubles() []double {
	return readMany(readDouble)
}

func readFloats() []float {
	return readMany(readFloat)
}

func readLongs() []long {
	return readMany(readLong)
}

func readULongs() []ulong {
	return readMany(readULong)
}

func main() {
	defer out.Flush()

	// solution
	t := readInt()
	ans := make([]string, t)
	for i := 0; i < t; i++ {
		n := readInt()
		s1 := readString()
		s2 := readString()
		f := true
		for j := 0; j < n; j++ {
			if s1[j] == s2[j] || s1[j] == 'G' && s2[j] == 'B' || s1[j] == 'B' && s2[j] == 'G' {
				continue
			}
			f = false
			break
		}
		if f {
			ans[i] = "YES"
		} else {
			ans[i] = "NO"
		}
	}
	for i := 0; i < t; i++ {
		println(ans[i])
	}
}

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

	return x
}

func max(x, y long) long {
	if x > y {
		return x
	}

	return y
}

func min(x, y long) long {
	if x > y {
		return y
	}

	return x
}

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
		var s1, s2 string
		fmt.Fscan(in, &n, &s1, &s2)

		f := true
		for i := range s1 {
			if s1[i] == 'R' && s2[i] != 'R' {
				f = false
				break
			} else if (s1[i] == 'G' || s1[i] == 'B') && s2[i] == 'R' {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}

	}
}

package main

import "fmt"

func findAns(input1, input2 string, n int) {
	// fmt.Println(input1, "   ", input2)
	for i := 0; i < n; i++ {
		if (input1[i] != input2[i]) || (string(input1[i]) != "G" && string(input2[i]) != "B") && (string(input1[i]) != "B" && string(input2[i]) != "G") {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
}

func main() {
	t, n := 0, 0
	input1, input2 := "", ""
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&n)
		fmt.Scan(&input1)
		fmt.Scan(&input2)
		findAns(input1, input2, n)
		t--
	}
}

package main

import "fmt"

func findAns(input1, input2 string, n int) {
	// fmt.Println(input1, "   ", input2)
	for i := 0; i < n; i++ {
		// fmt.Println(string(input1[i]), "   ", string(input2[i]))
		if input1[i] != input2[i] {
			if (string(input1[i]) != "G" && string(input2[i]) != "B") && (string(input1[i]) != "B" && string(input2[i]) != "G") {
				fmt.Println("NO")
				return
			}
		}
	}
	fmt.Println("YES")
}

func main() {
	t, n := 0, 0
	input1, input2 := "", ""
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&n)
		fmt.Scan(&input1)
		fmt.Scan(&input2)
		findAns(input1, input2, n)
		t--
	}
}

package main

import "fmt"

func findAns(input1, input2 string, n int) {
	// fmt.Println(input1, "   ", input2)
	for i := 0; i < n; i++ {
		fmt.Println(string(input1[i]), "   ", string(input2[i]))
		if input1[i] != input2[i] {
			if (string(input1[i]) != "G" && string(input2[i]) != "B") && (string(input1[i]) != "B" && string(input2[i]) != "G") {
				fmt.Println("NO")
				return
			}
		}
	}
	fmt.Println("YES")
}

func main() {
	t, n := 0, 0
	input1, input2 := "", ""
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&n)
		fmt.Scan(&input1)
		fmt.Scan(&input2)
		findAns(input1, input2, n)
		t--
	}
}

package main

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

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

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

func main() {

	defer writer.Flush()

	var t int
	scanws(&t)

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

		var n int
		scanws(&n)

		var s1 string
		var s2 string

		scanws(&s1)
		scanws(&s2)

		gb := "GB"

		f := 0

		for j := 0; j < len(s1); j++ {

			fmt.Println(s1[j], s2[j])

			if (s1[j] == gb[0] || s1[j] == gb[1]) && (s2[j] == gb[0] || s2[j] == gb[1]) {

				continue
			} else {
				if (s1[j] != s2[j]) {
					printf("NO\n")
					f = 1
					break
				}
			}

		}

		if f == 0 {
			printf("YES\n")
		}

	}
}


William really likes the cellular automaton called "Game of Life" so he decided to make his own version. For simplicity, William decided to define his cellular automaton on an array containing $$$n$$$ cells, with each cell either being alive or dead.Evolution of the array in William's cellular automaton occurs iteratively in the following way:  If the element is dead and it has exactly $$$1$$$ alive neighbor in the current state of the array, then on the next iteration it will become alive. For an element at index $$$i$$$ the neighbors would be elements with indices $$$i - 1$$$ and $$$i + 1$$$. If there is no element at that index, it is considered to be a dead neighbor.  William is a humane person so all alive elements stay alive. Check the note section for examples of the evolution.You are given some initial state of all elements and you need to help William find the state of the array after $$$m$$$ iterations of evolution.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 10^3, 1 \le m \le 10^9$$$), which are the total number of cells in the array and the number of iterations. The second line of each test case contains a string of length $$$n$$$ made up of characters "0" and "1" and defines the initial state of the array. "1" means a cell is alive and "0" means it is dead. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

In each test case output a string of length $$$n$$$, made up of characters "0" and "1"  — the state of the array after $$$m$$$ iterations of evolution.

NoteSequence of iterations of evolution for the first test case   01000000001  — initial state  11100000011  — first iteration of evolution  11110000111  — second iteration of evolution  11111001111  — third iteration of evolution Sequence of iterations of evolution for the second test case   0110100101  — initial state  1110111101  — first iteration of evolution  1110111101  — second iteration of evolution

package main

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

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

func (in *CF1523A) 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 *CF1523A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1523A
Date: 30/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1523/A
Problem: CF1523A
**/
func (in *CF1523A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		str := "0" + in.NextString() + "0"
		for m > 0 {
			var aux strings.Builder
			aux.WriteString("0")
			change := false
			for i := 1; i <= n; i++ {
				if str[i-1] == '0' && str[i] == '0' && str[i+1] == '1' {
					aux.WriteString("1")
					change = true
				} else if str[i-1] == '1' && str[i] == '0' && str[i+1] == '0' {
					aux.WriteString("1")
					change = true
				} else {
					aux.WriteString(string(str[i]))
				}
			}
			aux.WriteString("0")
			if change {
				str = aux.String()
			} else {
				break
			}

			m--
		}
		fmt.Println(str[1 : len(str)-1])
	}
}

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

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