Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.Chilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.A number's length is the number of digits in its decimal representation without leading zeros.

A single input line contains a single integer n (1 ≤ n ≤ 105).

Print a single integer — the answer to the problem without leading zeroes, or "-1" (without the quotes), if the number that meet the problem condition does not exist.

package main
import (
	"fmt"
	"bufio"
	"os"
	//"strconv"
	//"sort"
)

func main() {
	var a [7]string
	a[0] = "050"
	a[1] = "080"
	a[2] = "170"
	a[3] = "020"
	a[4] = "200"
	a[5] = "110"
	a[6] = "00"
	var n int
	fmt.Scan(&n)
	out := bufio.NewWriter(os.Stdout)
	if n==1 || n==2{
	        out.WriteString("-1\n")
	} else if n==3 {
		out.WriteString("210\n")
	} else {
		out.WriteString("1")
		for i:=1;i<=n-4;i++ {
			out.WriteString("0")
		}
		out.WriteString(a[(n-4)%6])
	}
	out.Flush()
}

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	if n < 4 {
		if n == 3 {
			writer.WriteString("210\n")
		} else {
			writer.WriteString("-1\n")
		}
		return
	}
	mod7 := []int{1, 3, 2, 6, 4, 5}
	x := 7-mod7[(n-1)%6]
	mod2, mod3, mod5 := x%2, (x+1)%3, x%5
	for ; mod2 != 0 || mod3 != 0 || mod5 != 0; x += 7 {
		mod2 = (mod2+7)%2
		mod3 = (mod3+7)%3
		mod5 = (mod5+7)%5
	}
	writer.WriteString("1")
	zeros := n - int(math.Floor(math.Log10(float64(x)))) - 2
	for i := 0; i < zeros; i++ {
		writer.WriteString("0")
	}
	writer.WriteString(fmt.Sprintf("%d\n", x))
}

package main

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

func main() {
    writer := bufio.NewWriter(os.Stdout)
    var n int
    fmt.Scan(&n)
    if n < 3 {
        writer.WriteString("-1\n")
    } else if n == 3 {
        writer.WriteString("210\n")
    } else {
        s := [6]string{"170","020","200","110","050","080"}
        writer.WriteString("1")
        for i := 4; i < n; i++ { writer.WriteString("0") }
        writer.WriteString(s[n%6])
        writer.WriteRune('\n')
    }
    writer.Flush()
}

//248B
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	if n<=2 {
		fmt.Println("-1")
	} else if n==3 {
		fmt.Println("210")
	} else {
		if (n-3)%6==0 {
			fmt.Print("1")
			for i:=2; i<=n-3; i++ {
				fmt.Print("0")
			}
			fmt.Print("110")
		} else if (n-3)%6==1 {
			fmt.Print("1")
			for i:=2; i<=n-2; i++ {
				fmt.Print("0")
			}
			fmt.Print("50")
		} else if (n-3)%6==2 {
			fmt.Print("1")
			for i:=2; i<=n-2; i++ {
				fmt.Print("0")
			}
			fmt.Print("80")
		} else if (n-3)%6==3 {
			fmt.Print("1")
			for i:=2; i<=n-3; i++ {
				fmt.Print("0")
			}
			fmt.Print("170")
		} else if (n-3)%6==4 {
			fmt.Print("1")
			for i:=2; i<=n-2; i++ {
				fmt.Print("0")
			}
			fmt.Print("20")
		} else if (n-3)%6==5 {
			fmt.Print("1")
			for i:=2; i<=n-3; i++ {
				fmt.Print("0")
			}
			fmt.Print("200")
		}
		fmt.Println()
	}
}

package main

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

func main() {
    writer := bufio.NewWriter(os.Stdout)
    var n int
    fmt.Scan(&n)
    if n < 3 {
        writer.WriteString("-1\n")
    } else if n == 3 {
        writer.WriteString("210\n")
    } else {
        s := [6]string{"050","080","170","020","200","110"}
        writer.WriteString("1")
        for i := 4; i < n; i++ { writer.WriteString("0") }
        writer.WriteString(s[(n-4)%6])
        writer.WriteRune('\n')
    }
    writer.Flush()
}

package main

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

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

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

type intPair struct {
	a int64
	b int64
}

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
}

func main() {
	defer writer.Flush()
	var n int
	scanf("%d\n", &n)

	if(n<3) {
		printf("-1\n")
		return
	}

	if(n == 3) {
		printf("210\n")
		return
	}

	mod := []int64{5,8,17,2,20,11}

	val := mod[(n-4)%6]

	if(val >= 10) {
		printf("1")
		for i:=0;i<n-4;i++ {
			printf("0")
		}
		printf("%d", val)
		printf("0\n")
 	} else {
		printf("1")
		for i:=0;i<n-3;i++ {
			printf("0")
		}
		printf("%d", val)
		printf("0\n")
	}
}

// 248B
package main

import (
	"fmt"
)

func main() {
	var i, n int
	fmt.Scan(&n)
	if n == 1 || n == 2 {
		fmt.Println(-1)
		return
	}
	if n == 3 {
		fmt.Println(210)
		return
	}
	t := 1
	for i = 1; i < n; i++ {
		t = t * 10 % 210
	}
	fmt.Print(1)
	for i = 1; i < n-3; i++ {
		fmt.Print(0)
	}
	fmt.Printf("%03d\n", 210-t)

	return
}

// Codeforces 248 B big Integers, train
package main

import (
	"bufio"
	"fmt"
	"bytes"
	"strings"
	"strconv"
	"os"
	"math/big"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n int
var ans string

// readData read data for out problem.
func readData() {
 	in = bufio.NewReader(os.Stdin)
	// reading n
    n,_,_,_,_ = readFive()
}

// solve func solves our problem.
func solve() {
	if n<3 {
		ans = "-1"
		return
	} else if n==3 {
		ans = "210"
		return
	}
	// make big int 10 power n
	var buffer bytes.Buffer
	buffer.WriteByte('1')
	for i:=0; i < n-1; i++ {
		buffer.WriteByte('0')
	}
	s := buffer.String()
	bigi := new(big.Int)
	bigi.SetString(s, 10)
	mult := new(big.Int)
	mult.SetInt64(210)
	res := new(big.Int)
	res.Set(bigi)
	rem := mult.Sub(mult, bigi.Mod(bigi, mult))
	//fmt.Println("rem: ", rem)
	//fmt.Println("res: ", res)
	res.Add(res, rem)
	// remainder
	ans = res.String()
}

// printRes print problem solution in expected format.
func printRes() {
	fmt.Println(ans)
}

func main() {
	readData()
	solve()
	printRes()
}

// 248B-mic
package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	ans := 1
	if n == 1 || n == 2 {
		fmt.Println("-1")
		return
	}
	if n == 3 {
		fmt.Println("210")
		return
	}
	for i := 0; i < n-2; i++ {
		ans %= 21
		ans *= 10
	}
	ans %= 21
	ans = 21 - ans
	if ans >= 10 {
		fmt.Print("1")
		for i := 0; i < n-4; i++ {
			fmt.Print("0")
		}
		fmt.Print(ans)
		fmt.Println("0")
	} else {
		fmt.Print("1")
		for i := 0; i < n-3; i++ {
			fmt.Print("0")
		}
		fmt.Print(ans)
		fmt.Println("0")
	}
}

package main

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

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

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

type intPair struct {
	a int64
	b int64
}

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
}

func main() {
	defer writer.Flush()
}

package main

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

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

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

type intPair struct {
	a int64
	b int64
}

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
}

func main() {
	defer writer.Flush()
	var n int
	scanf("%d\n", &n)

	if(n<3) {
		printf("-1\n")
		return
	}
	printf("210")
	for i:=0;i<n-3;i++ {
		printf("0")
	}
	printf("\n")
}

// Codeforces 248 B big Integers, train
package main

import (
	"bufio"
	"fmt"
	"bytes"
	"strings"
	"strconv"
	"os"
	"math/big"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n int
var ans string

// readData read data for out problem.
func readData() {
 	in = bufio.NewReader(os.Stdin)
	// reading n
    n,_,_,_,_ = readFive()
}

// solve func solves our problem.
func solve() {
	if n<3 {
		ans = "-1"
		return
	} else if n==3 {
		ans = "210"
		return
	}
	// make big int 10 power n
	var buffer bytes.Buffer
	buffer.WriteByte('1')
	for i:=0; i < n; i++ {
		buffer.WriteByte('0')
	}
	s := buffer.String()
	bigi := new(big.Int)
	bigi.SetString(s, 10)
	mult := new(big.Int)
	mult.SetInt64(210)
	res := new(big.Int)
	res.Set(bigi)
	rem := mult.Sub(mult, bigi.Mod(bigi, mult))
	//fmt.Println("rem: ", rem)
	//fmt.Println("res: ", res)
	res.Add(res, rem)
	// remainder
	ans = res.String()
}

// printRes print problem solution in expected format.
func printRes() {
	fmt.Println(ans)
}

func main() {
	readData()
	solve()
	printRes()
}

// Codeforces 248 B big Integers, train
package main

import (
	"bufio"
	"fmt"
	"bytes"
	"strings"
	"strconv"
	"os"
	"math/big"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n int
var ans string

// readData read data for out problem.
func readData() {
 	in = bufio.NewReader(os.Stdin)
	// reading n
    n,_,_,_,_ = readFive()
}

// solve func solves our problem.
func solve() {
	if n<3 {
		ans = "-1"
		return
	} else if n==3 {
		ans = "210"
		return
	}
	// make big int 10 power n
	var buffer bytes.Buffer
	buffer.WriteByte('1')
	for i:=0; i < n; i++ {
		buffer.WriteByte('0')
	}
	s := buffer.String()
	bigi := new(big.Int)
	bigi.SetString(s, 10)
	mult := new(big.Int)
	mult.SetInt64(210)
	res := new(big.Int)
	res.Set(bigi)
	rem := mult.Sub(mult, bigi.Mod(bigi, mult))
	fmt.Println("rem: ", rem)
	fmt.Println("res: ", res)
	res.Add(res, rem)
	// remainder
	ans = res.String()
}

// printRes print problem solution in expected format.
func printRes() {
	fmt.Println(ans)
}

func main() {
	readData()
	solve()
	printRes()
}


Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.

The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r".

Output n lines — on the i-th line you should print the i-th stone's number from the left.

NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1.

//TIMELIMIT
package main
import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

func main() {
	var n,l,r,i int
	var s string
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&s)
	n=len(s)
	a:=make([]int,1000000)
	l=0
	r=n-1
	for i=0;i<n;i++{
		if s[i]=='l' {
			a[r]=i+1
			r--
		} else {
			a[l]=i+1
			l++
		}
	}
	for i=0;i<n;i++ {
		out.WriteString(strconv.Itoa(a[i])+"\n")
	}
	out.Flush()
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    var s string
    fmt.Fscan(reader,&s)
    ans, l, r := make([]int,len(s)), 0, len(s)-1
    for in, v := range s {
        if v == 'l' {
            ans[r] = in+1
            r--
        } else {
            ans[l] = in+1
            l++
        }
    }
    for _, v := range ans {
        writer.WriteString(strconv.Itoa(v)+"\n")
    }
    writer.Flush()
}

package main

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

func main() {
    writer := bufio.NewWriter(os.Stdout)
    var s string
    fmt.Scan(&s)
    ans, l, r := make([]int,len(s)), 0, len(s)-1
    for in, v := range s {
        if v == 'l' {
            ans[r] = in+1
            r--
        } else {
            ans[l] = in+1
            l++
        }
    }
    for _, v := range ans {
        writer.WriteString(strconv.Itoa(v)+"\n")
    }
    writer.Flush()
}

// 264A-mic
package main

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

func main() {
	var s string
	fmt.Scan(&s)
	o := bufio.NewWriter(os.Stdout)
	for i := 0; i < len(s); i++ {
		if s[i] == 'r' {
			o.WriteString(strconv.Itoa(i+1) + "\n")
		}
	}
	o.Flush()
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == 'l' {
			o.WriteString(strconv.Itoa(i+1) + "\n")
		}
	}
	o.Flush()
}

package main

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

var writer *bufio.Writer
var reader *bufio.Reader

type Node struct {
  Num int
  Left, Right *Node
}

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

  line, _ := reader.ReadString('\n')
  s := strings.TrimSpace(line)
  x := 1
  curr := &Node{ x, nil, nil }
  leftmost := curr
  for i := 0; i < len(s)-1; i++ {
    x++
    if s[i] == 'l' {
      left := curr.Left
      curr.Left = &Node{ x, left, curr }
      curr = curr.Left
      if left != nil {
        left.Right = curr
      } else {
        leftmost = curr
      }
    } else {
      right := curr.Right
      curr.Right = &Node{ x, curr, right }
      curr = curr.Right
      if right != nil {
        right.Left = curr
      }
    }
  }
  curr = leftmost
  for curr != nil {
    writer.WriteString(fmt.Sprintf("%d\n", curr.Num))
    curr = curr.Right
  }
}

package main
import (
	//"fmt"
	"bufio"
	"os"
	"strconv"
	"unicode/utf8"
)

func main() {
	var n,l,r int
	var s string
	out := bufio.NewWriter(os.Stdout)
	input:= bufio.NewScanner(os.Stdin)
	input.Scan()
	s=input.Text()
	//fmt.Scan(&s)
	n=utf8.RuneCountInString(s)
	a:=make([]int,100100)
	l=0
	r=n-1
	for i:=0;i<n;i++{
		if s[i]=='l' {
			a[r]=i+1
			r--
		} else {
			a[l]=i+1
			l++
		}
	}
	for i:=0;i<n;i++ {
		out.WriteString(strconv.Itoa(a[i])+"\n")
		out.Flush()
	}
}

package main
import (
	//"fmt"
	"bufio"
	"os"
	"strconv"
	"unicode/utf8"
)

func main() {
	var n,l,r int
	var s string
	out := bufio.NewWriter(os.Stdout)
	input:= bufio.NewScanner(os.Stdin)
	input.Scan()
	s=input.Text()
	//fmt.Scan(&s)
	len:=utf8.RuneCountInString(s)
	a:=make([]int,1000100)
	n=len
	l=0
	r=n-1
	for i:=0;i<n;i++{
		if s[i]=='l' {
			a[r]=i+1
			r--
		} else {
			a[l]=i+1
			l++
		}
	}
	for i:=0;i<n;i++ {
		//out.WriteRune(a[i])
		out.WriteString(strconv.Itoa(a[i])+"\n")
	}
	out.Flush()
}

package main
import (
	//"fmt"
	"bufio"
	"os"
	"strconv"
	"unicode/utf8"
)

func main() {
	var n,l,r int
	var s string
	out := bufio.NewWriter(os.Stdout)
	input:= bufio.NewScanner(os.Stdin)
	input.Scan()
	s=input.Text()
	//fmt.Scan(&s)
	len:=utf8.RuneCountInString(s)
	a:=make([]int,1000100)
	n=len
	l=0
	r=n-1
	for i:=0;i<n;i++{
		if s[i]=='l' {
			a[r]=i+1
			r--
		} else {
			a[l]=i+1
			l++
		}
	}
	for i:=0;i<n;i++ {
		//out.WriteRune(a[i])
		out.WriteString(strconv.Itoa(a[i])+"\n")
		out.Flush()
	}
	//out.Flush()
}


There are $$$n$$$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $$$s$$$, where the $$$i$$$-th candle is lit if and only if $$$s_i=1$$$.  Initially, the candle lights are described by a string $$$a$$$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit).You would like to make the candles look the same as string $$$b$$$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required.

The first line contains an integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 10^5$$$) — the number of candles. The second line contains a string $$$a$$$ of length $$$n$$$ consisting of symbols 0 and 1 — the initial pattern of lights. The third line contains a string $$$b$$$ of length $$$n$$$ consisting of symbols 0 and 1 — the desired pattern of lights. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$.

For each test case, output the minimum number of operations required to transform $$$a$$$ to $$$b$$$, or $$$-1$$$ if it's impossible.

NoteIn the first test case, the two strings are already equal, so we don't have to perform any operations.In the second test case, we can perform a single operation selecting the second candle to transform $$$01$$$ into $$$11$$$.In the third test case, it's impossible to perform any operations because there are no lit candles to select.In the fourth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$:   Select the $$$7$$$-th candle: $$$100010{\color{red}1}11\to 011101{\color{red} 1}00$$$.  Select the $$$2$$$-nd candle: $$$0{\color{red} 1}1101100\to 1{\color{red} 1}0010011$$$.  Select the $$$1$$$-st candle: $$${\color{red}1}10010011\to {\color{red}1}01101100$$$. In the fifth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$:   Select the $$$6$$$-th candle: $$$00101{\color{red}1}011\to 11010{\color{red}1}100$$$  Select the $$$2$$$-nd candle: $$$1{\color{red}1}0101100\to 0{\color{red}1}1010011$$$  Select the $$$8$$$-th candle: $$$0110100{\color{red}1}1\to 1001011{\color{red}1}0$$$  Select the $$$7$$$-th candle: $$$100101{\color{red}1}10\to 011010{\color{red}1}01$$$

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, nl int
	var s1, s2 string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&nl)
		fmt.Scan(&s1)
		fmt.Scan(&s2)
		a, b, c, d := 0,0,0,0

		for j, _ := range s1 {
			if s1[j] == '0' {
				if s2[j] == '0' {
					a++
				} else {
					b++
				}
			} else {
				if s2[j] == '0' {
					c++
				} else {
					d++
				}
			}
		}

		res := math.MaxInt
		if b == c {
			res = min(res, b+c)
		}
		if (d - a == 1) {
			res = min(res, d + a)
		}

		if res == math.MaxInt {
			fmt.Println(-1)
		} else {
			fmt.Println(res)
		}
	}
}

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

	return b
}

package main

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

const MOD int64 = 1000000007

var n int

var a, b string

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

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

		fmt.Fscan(read, &n)

		fmt.Fscan(read, &a, &b)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {

	EvenLitOnes := 0
	EvenLitZeros := 0

	oddLitOnes := 0
	oddLitZeroes := 0
	ev := 0
	odd := 0

	for i := 0; i < n; i++ {
		if a[i] == b[i] {
			ev++
			if a[i] == '1' {
				EvenLitOnes++
			} else {
				EvenLitZeros++
			}
		} else {
			odd++
			if a[i] == '1' {
				oddLitOnes++
			} else {
				oddLitZeroes++
			}
		}
	}

	var a int = 1e9
	var b int = 1e9

	if ev % 2 == 1 && EvenLitOnes - EvenLitZeros == 1 {
		a = ev
	}

	if odd % 2 == 0 && oddLitOnes == oddLitZeroes {
		b = odd
	}

	c := min(a, b)

	if c == 1e9 {
		return -1
	}

	return c
}

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

	return b
}

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

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var (
		n    int
		a, b string
	)
	scan(&n, &a, &b)

	var cnt [4]int
	for i := range a {
		cnt[((a[i]-'0')<<1)|(b[i]-'0')]++
	}

	var (
		q = list.New()
		d = make(map[[4]int]int)
	)
	q.PushBack(cnt)
	d[cnt] = 0
	for q.Len() > 0 {
		curr := q.Remove(q.Front()).([4]int)
		nexts := make([][4]int, 0, 2)
		if curr[0b11] > 0 {
			next := curr
			next[0b11], next[0b01] = next[0b01]+1, next[0b11]-1
			next[0b10], next[0b00] = next[0b00], next[0b10]
			nexts = append(nexts, next)
		}
		if curr[0b10] > 0 {
			next := curr
			next[0b10], next[0b00] = next[0b00]+1, next[0b10]-1
			next[0b11], next[0b01] = next[0b01], next[0b11]
			nexts = append(nexts, next)
		}
		for _, next := range nexts {
			if _, ok := d[next]; ok {
				continue
			}
			d[next] = d[curr] + 1
			q.PushBack(next)
		}
	}
	var ans = 1 << 30
	for k, v := range d {
		if k[0b01] == 0 && k[0b10] == 0 && ans > v {
			ans = v
		}
	}
	if ans == 1<<30 {
		ans = -1
	}
	printf("%v\n", ans)
}

package main

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

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

func lit(mode int, a []int) ([]int, error) {
	var res []int
	if mode == 0 {
		res = []int{a[2] - 1, a[3], a[0] + 1, a[1]}
	} else {
		res = []int{a[2], a[3] - 1, a[0], a[1] + 1}
	}
	for i := 0; i < 4; i++ {
		if res[i] < 0 {
			return nil, errors.New("Can no longer litDiff")
		}
	}
	return res, nil
}

func complete(a []int) bool {
	return a[1] == 0 && a[2] == 0
}

func solve(t int) {
	_ = readInt()
	a := readString()
	b := readString()

	if a == b {
		println(0)
		return
	}

	initial := make([]int, 4)
	for i := 0; i < len(a); i++ {
		if a[i] == '0' && b[i] == '0' {
			initial[0]++
		} else if a[i] == '0' && b[i] == '1' {
			initial[1]++
		} else if a[i] == '1' && b[i] == '0' {
			initial[2]++
		} else if a[i] == '1' && b[i] == '1' {
			initial[3]++
		} else {
			panic("Should not happen")
		}
	}

	res := -1

	for starting := 0; starting < 2; starting++ {
		arr := initial
		var err error
		steps := 0
		for mode := starting; !complete(arr); mode = 1 - mode {
			arr, err = lit(mode, arr)
			steps += 1
			if err != nil {
				break
			}
		}
		if err == nil && (steps < res || res == -1) {
			res = steps
		}
	}
	println(res)
}

func main() {
	var t int
	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		solve(i)
	}
}

// READ
func readInt() int {
	var res int
	fmt.Fscan(in, &res)
	return res
}

func readInt64() int64 {
	var res int64
	fmt.Fscan(in, &res)
	return res
}

func readString() string {
	var res string
	fmt.Fscan(in, &res)
	return res
}

// PRINT
func print(args ...interface{}) {
	fmt.Fprint(out, args...)
	out.Flush()
}

func println(args ...interface{}) {
	fmt.Fprintln(out, args...)
	out.Flush()
}

func printErr(args ...interface{}) {
	fmt.Fprint(er, args...)
	er.Flush()
}

func printlnErr(args ...interface{}) {
	fmt.Fprintln(er, args...)
	er.Flush()
}

package main

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

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

func solveEven(same, dif int) int {
	if dif%2 == 0 {
		return dif
	}
	return same + dif + 1
}

func solveOdd(same, dif int) int {
	if same%2 == 1 {
		return same
	}
	return same + dif + 1
}

func solve() int {
	var a, b string
	var n int
	var cnt1a = 0
	var cnt1b = 0
	var same = 0
	var dif = 0
	fmt.Fscan(in, &n, &a, &b)
	var ans0 = n + 1
	var ans1 = n + 1
	for i := 0; i < n; i++ {
		if a[i] == b[i] {
			same++
		} else {
			dif++
		}
		if a[i] == '1' {
			cnt1a++
		}
		if b[i] == '1' {
			cnt1b++
		}
	}
	if cnt1a == cnt1b {
		ans0 = solveEven(same, dif)
	}
	if cnt1a == 1+(n-cnt1b) {
		ans1 = solveOdd(same, dif)
	}
	if ans0 < ans1 {
		return ans0
	}
	if ans1 > n {
		return -1
	}
	return ans1
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fprintf(out, "%d\n", solve())
	}
	out.Flush()
}

package main

import "fmt"

func main() {
	var n, nl int
	var s1, s2 string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&nl)
		fmt.Scan(&s1)
		fmt.Scan(&s2)

		if checkZero(s1) || checkZero(s2) {
			fmt.Println(-1)
			continue
		}

		xLast := s1[0]
		cnt := 0
		for j, _ := range s1 {
			if s1[j] != s2[j] {
				if xLast != s2[j] {
					cnt++
				}
				xLast = s2[j]
			}
		}

		fmt.Println(cnt)
	}
}

func checkZero(str string) bool {
	for _, elem := range str {
		if elem == '1' {
			return false
		}
	}

	return true
}

package main

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

const MOD int64 = 1000000007

var n int

var a, b string

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

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

		fmt.Fscan(read, &n)

		fmt.Fscan(read, &a, &b)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {

	if a == b {
		return 0
	}

	hasEvLit := false
	hasOddLit := false
	ev := 0
	odd := 0

	for i := 0; i < n; i++ {
		if a[i] == b[i] {
			ev++
			if a[i] == '1' {
				hasEvLit = true
			}
		} else {
			odd++
			if a[i] == '1' {
				hasOddLit = true
			}
		}
	}

	var a int = 1e9
	var b int = 1e9

	if ev % 2 == 1 && hasEvLit {
		a = ev
	}

	if odd % 2 == 0 && hasOddLit {
		b = odd
	}

	c := min(a, b)

	if c == 1e9 {
		return -1
	}

	return c
}

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

	return b
}

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


For a given array $$$a$$$ consisting of $$$n$$$ integers and a given integer $$$m$$$ find if it is possible to reorder elements of the array $$$a$$$ in such a way that $$$\sum_{i=1}^{n}{\sum_{j=i}^{n}{\frac{a_j}{j}}}$$$ equals $$$m$$$? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, $$$\frac{5}{2}=2.5$$$.

The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 100$$$). The test cases follow, each in two lines. The first line of a test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le 10^6$$$). The second line contains integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^6$$$) — the elements of the array.

For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.

NoteIn the first test case one of the reorders could be $$$[1, 2, 5]$$$. The sum is equal to $$$(\frac{1}{1} + \frac{2}{2} + \frac{5}{3}) + (\frac{2}{2} + \frac{5}{3}) + (\frac{5}{3}) = 8$$$. The brackets denote the inner sum $$$\sum_{j=i}^{n}{\frac{a_j}{j}}$$$, while the summation of brackets corresponds to the sum over $$$i$$$.

package main

import "fmt"

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

	for i := 0; i < numCases; i++ {
		var n, m int
		fmt.Scanf("%d %d\n", &n, &m)

		format := ""
		for j := 0; j < n-1; j++ {
			format += "%d "
		}
		format += "%d\n"

		testCase, err := readIntArray(n)
		if err != nil {
			panic(err)
		}

		sum := 0
		for _, c := range testCase {
			sum += c
		}

		if sum == m {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

func readIntArray(n int) ([]int, error) {
	x := make([]int, n)
	y := make([]interface{}, n)
	for i := range x {
		y[i] = &x[i]
	}
	n, err := fmt.Scanln(y...)
	x = x[:n]
	return x, err
}

package main

import (
	"fmt"
)

func checksum(arr [100]float64, n int, m float64) bool {
	var sum float64
	for i := 0; i < n; i++ {
		sum += arr[i]
	}
	if sum == m {
		return true
	} else {
		return false
	}
}

func main() {
	var t, n int
	var m float64
	var arr [100]float64
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&m)
		for j := 0; j < n; j++ {
			fmt.Scan(&arr[j])
		}
		if checksum(arr, n, m) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
	"os"
)

func cin(a ...interface{}) (n int, err error) {
	return fmt.Fscan(os.Stdin, a...)
}
func cout(a ...interface{}) (n int, err error) {
	return fmt.Fprintln(os.Stdout, a...)
}
func main() {
	t := 0
	cin(&t)
	for c := 0; c < t; c++ {
		n, m := 0, 0
		cin(&n, &m)
		sum, x := 0, 0
		for i := 0; i < n; i++ {
			cin(&x)
			sum += x
		}
		if sum == m {
			cout("YES")
		} else {
			cout("NO")
		}
	}
}

package main

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

var read = bufio.NewReader(os.Stdin)

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

func println(a ...interface{}) (n int, err error) {
	return fmt.Fprintln(os.Stdout, a...)
}
func main() {
	t := 0
	fmt.Scan(&t)
	for c := 0; c < t; c++ {
		n, m := 0, 0
		fmt.Scan(&n, &m)
		sum:=0
		for i:=0; i<n;i++{
			x:=0
			fmt.Scan(&x)
			sum+=x
		}
		if sum==m{
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}
	}
}

package main

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

var (
	t     int8
	n     int8
	m     int32
	count int32
	a     int32
)

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

	fmt.Fscan(reader, &t)

	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m)

		//arr := make([]int32, n)
		count = 0

		for ; n > 0; n-- {
			fmt.Fscan(reader, &a)
			count += a
		}

		if count == m {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	var t, n, m, a int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &m)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			m -= a
		}
		if m == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var t, n, m, sum, i, j, a int64
	fmt.Scan(&t)
	for i=0; i<t; i++{
		fmt.Scan(&n, &m)
		for j=0; j<n; j++ {
			fmt.Scan(&a)
			sum += a
		}
		if sum == m {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		sum = 0
	}
}

package main

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

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

	solve := func(Case int) {
		var n ,m int
		Fscan(in, &n,&m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sum := 0
		for _, v := range a {
			sum += v
		}
		if sum == m {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}
}

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

package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

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

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

func main() {
	for tt := NextInt(); tt > 0; tt-- {
		n, m := NextInt(), NextInt()
		sum := 0
		for i := 0; i < n; i++ {
			sum += NextInt()
		}
		if sum == m {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main
import (
	"bufio"
	"fmt"
	"os"
)
var rd = bufio.NewReader(os.Stdin)
func scanf(f string, a... interface{}) {
	fmt.Fscanf(rd, f, a...)
}
func scanArray(n int) []int {
	res := make([]int, n)
	for i := range res {
		if i < n - 1 {
			scanf("%d ", &res[i])
		} else {
			scanf("%d\n", &res[i])
		}
	}
	return res
}
func main() {
	q := 0
	scanf("%d\n", &q)
	for i := 0; i < q; i++ {
		var n, m int
		scanf("%d %d\n", &n, &m)
		a := scanArray(n)
		s := 0
		for _, v := range a {
			s += v
		}
		if s == m {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main
import "fmt"
func main() {
	q := 0
	fmt.Scan(&q)
	for tc := 0; tc < q; tc++ {
		var n, m int
		fmt.Scan(&n, &m)
		s := 0
		for i := 0; i < n; i++ {
			cur := 0
			fmt.Scan(&cur)
			s += cur
		}
		if s == m {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

type kv struct {
	Key   int
	Value int
}

func main() {
	//rd, _ := os.Open("input")
	io := bufio.NewReader(os.Stdin)

	var t, n, m, elem int

	fmt.Fscan(io, &t)
	for ; t > 0; t-- {
		fmt.Fscan(io, &n, &m)
		for i := 0; i < n; i++ {
			fmt.Fscan(io, &elem)
			m -= elem
		}
		if m == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

func Print(args ...interface{}) {
	fmt.Fprint(Writer, args...)
}

func Scan(args ...interface{}) {
	fmt.Fscan(Reader, args...)
}

func main() {
	defer Writer.Flush()

	var t int
	Scan(&t)
	for t!=0{
		var n, m int
		Scan(&n, &m)
		sum := 0
		for i:=0; i<n; i++ {
			var val int
			Scan(&val)
			sum += val
		}

		if sum == m {
			Print("Yes\n")
		} else {
			Print("No\n")
		}
		t--
	}
}

package main

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

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

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

func tCase() {
	n, m := rw.NextInt(), rw.NextInt()
	a := rw.NextInts(n)
	for _, x := range a {
		m -= x
	}
	if m == 0 {
		rw.Println("YES")
	} else {
		rw.Println("NO")
	}
}

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

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

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

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

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

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

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

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

package main

import "fmt"

func readSlice(n int) []int {
	x := make([]int, n)
	y := make([]interface{}, n)
	for i := range x {
		y[i] = &x[i]
	}
	_, _ = fmt.Scanln(y...)
	return x[:n]
}

func main() {
	var t, n, m int
	_, _ = fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanf("%d %d\n", &n, &m)
		slice := readSlice(n)
		if m == sum(slice){
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}

	}
}

func sum(arr []int) int {
	s := 0
	for i := range arr {
		s += arr[i]
	}
	return s
}

package main

import (
	"fmt"

)

func intSlice(n int) []int {
	x := make([]int, n)
	y := make([]interface{}, len(x))
	for i := 0; i < n; i++ {
		y[i] = &x[i]
	}
	n, _ = fmt.Scanln(y...)
	return x[:n]
}

func main() {
	var t, n, m int
	_, _ = fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanf("%d %d\n", &n, &m)
		slice := intSlice(n)
		if sum(slice) == m{
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}
	}

}

func sum(a []int)int{
	s := 0
	for i := range a{
		s += a[i]
	}
	return s
}

package main

import (
	"fmt"
)

func checksum(arr [100]float64, n int, m float64) bool {
	var sum float64
	for i := 0; i < n; i++ {
		sum += arr[i]
	}
	if sum == m {
		return true
	} else {
		return false
	}
}

func main() {
	var t, n int
	var m float64
	var arr [100]float64
	fmt.Printf("Enter number of test cases:")
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Printf("Enter number of elements in array:")
		fmt.Scan(&n)
		fmt.Printf("Enter sum:")
		fmt.Scan(&m)
		for j := 0; j < n; j++ {
			fmt.Scan(&arr[j])
		}
		if checksum(arr, n, m) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

func cin(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

func cout(a ...interface{}) (n int, err error) {
	return fmt.Fprintln(out, a...)
}
func main() {
	t := 0
	cin(&t)
	for c := 0; c < t; c++ {
		n, m := 0, 0
		cin(&n, &m)
		sum, x := 0, 0
		for i := 0; i < n; i++ {
			cin(&x)
			sum += x
		}
		if sum == m {
			cout("YES")
		} else {
			cout("NO")
		}
	}
}

package main

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

func main() {
	var t, n, m, a int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &m)
		for j := 0; j < m; j++ {
			fmt.Fscan(in, &a)
			m -= a
		}
		if m == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var t, n, m, sum, i, j, a int64
	fmt.Scan(&t)
	for i=0; i<t; i++{
		fmt.Scan(&n, &m)
		for j=0; j<n; j++ {
			fmt.Scan(&a)
			sum += a
		}
		if sum == m {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}


You are given two integers $$$a$$$ and $$$b$$$. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by $$$1$$$; during the second operation you choose one of these numbers and increase it by $$$2$$$, and so on. You choose the number of these operations yourself.For example, if $$$a = 1$$$ and $$$b = 3$$$, you can perform the following sequence of three operations:   add $$$1$$$ to $$$a$$$, then $$$a = 2$$$ and $$$b = 3$$$;  add $$$2$$$ to $$$b$$$, then $$$a = 2$$$ and $$$b = 5$$$;  add $$$3$$$ to $$$a$$$, then $$$a = 5$$$ and $$$b = 5$$$. Calculate the minimum number of operations required to make $$$a$$$ and $$$b$$$ equal.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).

For each test case print one integer — the minimum numbers of operations required to make $$$a$$$ and $$$b$$$ equal.

NoteFirst test case considered in the statement.In the second test case integers $$$a$$$ and $$$b$$$ are already equal, so you don't need to perform any operations.In the third test case you have to apply the first, the second, the third and the fourth operation to $$$b$$$ ($$$b$$$ turns into $$$20 + 1 + 2 + 3 + 4 = 30$$$).

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		// input
		a, b := 0, 0
		fmt.Fscanf(in, "%d %d\n", &a, &b)

		if a == b {
			fmt.Fprintln(out, 0)
			continue
		}

		if a > b {
			b, a = a, b
		}

		fmt.Fprintln(out, abCfs(a, b))
	}
}

func abCfs(a, b int) int {
	diff := abs(a, b)

	result := 0
	for result*(result+1)/2 < diff {
		result += 1
	}

	for (result*(result+1)/2)%2 != diff%2 {
		result += 1
	}

	return result
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		x, y := 0, 0
		fmt.Fscanf(in, "%d %d\n", &x, &y)
		x, y = maxmin(x, y)
		diff := y - x
		if diff == 0 {
			fmt.Fprintf(out, "0\n")
			continue
		}
		l := 1
		r := diff
		for l < r {
			mid := (l + r) / 2
			ans := int64(1+mid) * int64(mid) / int64(2)
			//fmt.Println(mid, ans)
			if ans < int64(diff) {
				l = mid + 1
			} else {
				r = mid
			}
		}
		//fmt.Println(l)
		cur := (1 + l) * l / 2
		for (cur-diff)%2 == 1 {
			l++
			cur += l
		}
		fmt.Fprintf(out, "%d\n", l)
	}
	return
}

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

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() float64 {
		sc.Scan()
		i, _ := strconv.ParseFloat(sc.Text(), 10)
		return i
	}

	for t := in(); t > 0 ; t-- {
		a, b := in(), in()

		if a > b {
			a, b = b, a
		}

		n := -1 + math.Sqrt(1-8*(a-b))
		n /= 2

		n = math.Ceil(n)

		for int64(a + n*(n+1)/2 - b) %2 != 0 {
			n++
		}

		fmt.Println(int64(n))
	}
}

package main

import (
	"fmt"
	"math"
)

func check(res int, d int) bool {
	var sum int64
	sum = int64(res) * int64(res + 1) / 2
	if sum < int64(d) {
		return false
	}
	return sum % 2 == int64(d) % 2
}

func prog(t int) int {
	for t > 0 {
		var a, b int
		fmt.Scan(&a, &b)
		d := int(math.Abs(float64(a - b)))
		if d == 0{
			fmt.Println("0")
			t--
			continue
		}
		res := 1
		for true{
			if !check(res, d){
				res++
			} else {
				break
			}
		}
		fmt.Println(res)
		t--
	}
	return 0
}

func main() {
	var t int
	fmt.Scan(&t)
	prog(t)
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n1, n2 int64
		fmt.Scanf("%d %d\n", &n1, &n2)

		diff := n2 - n1
		if diff < 0 {
			diff *= -1
		}
		if diff == 0 {
			fmt.Println(0)
			continue
		}

		sum := int64(0)
		for i := int64(1); i <= 1000000000; i++ {
			sum += i
			if sum == diff {
				fmt.Println(i)
				break
			} else if sum > diff && (sum+diff)%2 == 0 {
				fmt.Println(i)
				break
			}
		}
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"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
}

// 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])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

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

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

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

/********** 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
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var a, b int64

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		a, b = ReadInt64_2()

		solve()
	}
}

func solve() {
	diff := AbsInt(a - b)

	if diff == 0 {
		fmt.Println(0)
		return
	}

	for k := int64(1); ; k++ {
		sum := k * (k + 1) / 2

		if diff%2 == sum%2 && diff <= sum {
			fmt.Println(k)
			return
		}
	}
}

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

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

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

	var t int
	Fscan(in, &t)
	for case_ := 0; case_ < t; case_++ {
		Fprintln(out, solveCase(in, out))
	}
}

func solveCase(in io.Reader, out io.Writer) int64 {
	var a, b int64
	Fscan(in, &a, &b)
	a -= b
	if a < 0 {
		a = -a
	}
	if a == 0 {
		return 0
	}
	d := math.Sqrt(float64(8*a + 1))
	k := int64((d - 1) / 2)
	tk := (k + 1) * k / 2
	if tk == a {
		return k
	}
	if k&1 == 1 {
		if (a-tk)&1 == 1 {
			return k + 2
		}
		return k + 1
	}
	if (a-tk)&1 == 1 {
		return k + 1
	}
	return k + 3
}

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

package main

import (
	"fmt"
	"strconv"
)

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

	output := ""
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scan(&a, &b)

		if a == b {
			output += "0\n"
			continue
		}

		max := func(x, z int) int { if x > z { return x }; return z }

		cur, sum := 0, 0
		for {
			cur++
			sum += cur

			if (a + b + sum) % 2 == 0 && (a + b + sum) / 2 >= max(a, b) {
				break
			}
		}

		output += strconv.Itoa(cur) + "\n"
	}
	fmt.Println(output)
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() float64 {
		sc.Scan()
		i, _ := strconv.ParseFloat(sc.Text(), 10)
		return i
	}

	for t := in(); t > 0 ; t-- {
		a, b := in(), in()

		if a > b {
			a, b = b, a
		}

		n := -1 + math.Sqrt(1-8*(a-b))
		n /= 2

		n = math.Ceil(n)

		if (int64((a + n*(n-1)/2 -b))%2 != 0) {
			n++
		}

		fmt.Println(int64(n))
	}
}

package main

import (
	"fmt"
	"math"
)

func check(res int, d int) bool {
	var sum int64
	sum = int64(res) * int64(res + 1) / 2
	if sum < int64(d) {
		return false
	}
	return sum % 2 == int64(d) % 2
}

func prog(t int) int {
	for t > 0 {
		var a, b int
		fmt.Scan(&a, &b)
		d := int(math.Abs(float64(a - b)))
		if d == 0{
			fmt.Println("0")
		}
		res := 1
		for true{
			if !check(res, d){
				res++
			} else {
				break
			}
		}
		fmt.Println(res)
		t--
	}
	return 0
}

func main() {
	var t int
	fmt.Scan(&t)
	prog(t)
}

package main

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

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

	var t int
	Fscan(in, &t)
	for case_ := 0; case_ < t; case_++ {
		Fprintln(out, solveCase(in, out))
	}
}

func solveCase(in io.Reader, out io.Writer) int64 {
	var a, b int64
	Fscan(in, &a, &b)
	a -= b
	if a < 0 {
		a = -a
	}
	if a == 0 {
		return 0
	}
	d := math.Sqrt(float64(8*a + 1))
	k := int64(math.Ceil((d - 1) / 2))
	tk := (k + 1) * k / 2
	if tk == a {
		return k
	}
	if k&1 == 1 {
		if (a-tk)&1 == 1 {
			return k + 2
		}
		return k + 1
	}
	if (a-tk)&1 == 1 {
		return k + 1
	}
	return k + 3
}

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


This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.You are given a binary table of size $$$n \times m$$$. This table consists of symbols $$$0$$$ and $$$1$$$.You can make such operation: select $$$3$$$ different cells that belong to one $$$2 \times 2$$$ square and change the symbols in these cells (change $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$).Your task is to make all symbols in the table equal to $$$0$$$. You are allowed to make at most $$$nm$$$ operations. You don't need to minimize the number of operations.It can be proved, that it is always possible.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5000$$$) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n, m \leq 100$$$). Each of the next $$$n$$$ lines contains a binary string of length $$$m$$$, describing the symbols of the next row of the table. It is guaranteed, that the sum of $$$nm$$$ for all test cases does not exceed $$$20000$$$.

For each test case print the integer $$$k$$$ ($$$0 \leq k \leq nm$$$) — the number of operations. In the each of the next $$$k$$$ lines print $$$6$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3$$$ ($$$1 \leq x_1, x_2, x_3 \leq n, 1 \leq y_1, y_2, y_3 \leq m$$$) describing the next operation. This operation will be made with three cells $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$(x_3, y_3)$$$. These three cells should be different. These three cells should belong to some $$$2 \times 2$$$ square.

NoteIn the first test case, it is possible to make only one operation with cells $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After that, all symbols will be equal to $$$0$$$.In the second test case:  operation with cells $$$(2, 1)$$$, $$$(3, 1)$$$, $$$(3, 2)$$$. After it the table will be: 011001000  operation with cells $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$. After it the table will be: 000000000 In the fifth test case:  operation with cells $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$. After it the table will be: 010110  operation with cells $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After it the table will be: 000000

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					if a[i][j+1] == '1' {
						opsX = append(opsX, i, i, i+1)
						opsY = append(opsY, j, j+1, j)
						a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i][j+1])
					} else {
						opsX = append(opsX, i, i+1, i+1)
						opsY = append(opsY, j, j+1, j)
						a[i][j], a[i+1][j], a[i+1][j+1] = '0', '0', invert(a[i+1][j+1])
					}
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// workaround
			if n == 3 && i == 0 {
				if a[i][m-1] == '1' {
					a[i][m-1], a[i+1][m-2], a[i+1][m-1] = '0', invert(a[i+1][m-2]), invert(a[i+1][m-1])
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, m-1, m-2, m-1)
				}
				continue
			}
			// last column
			if a[i][m-1] == '1' && a[i+1][m-1] == '1' {
				a[i][m-1], a[i+1][m-1] = '0', '0'
				opsX = append(opsX, i, i+1, i)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			} else if a[i+1][m-1] == '1' {
				a[i+1][m-1] = '0'
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)

		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

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

	solve := func(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		a := make([][]byte, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		ans := [][][2]int{}
		add := func(ps [][2]int) {
			ans = append(ans, ps)
			for _, p := range ps {
				a[p[0]][p[1]] ^= 1
			}
		}
		for i, row := range a[:n-2] {
			for j, b := range row {
				if b == '1' {
					if j < m-1 {
						add([][2]int{{i, j}, {i + 1, j}, {i + 1, j + 1}})
					} else {
						add([][2]int{{i, j}, {i + 1, j}, {i + 1, j - 1}})
					}
				}
			}
		}
		for j := 0; j < m-2; j++ {
			for i := n - 2; i < n; i++ {
				if a[i][j] == '1' {
					add([][2]int{{i, j}, {n - 2, j + 1}, {n - 1, j + 1}})
				}
			}
		}

		c := [][2]int{{n - 2, m - 2}, {n - 2, m - 1}, {n - 1, m - 2}, {n - 1, m - 1}}
		var p0, p1 []int
		for i, p := range c {
			if a[p[0]][p[1]] == '0' {
				p0 = append(p0, i)
			} else {
				p1 = append(p1, i)
			}
		}
		if len(p1) == 4 {
			ans = append(ans, [][2]int{c[p1[0]], c[p1[1]], c[p1[2]]})
			p0, p1 = p1[:3], p1[3:]
		}
		if len(p1) == 1 {
			ans = append(ans, [][2]int{c[p1[0]], c[p0[0]], c[p0[1]]})
			p1, p0 = p0[:2], append(p0[2:], p1[0])
		}
		if len(p1) == 2 {
			ans = append(ans, [][2]int{c[p1[0]], c[p0[0]], c[p0[1]]})
			p1 = append(p1[1:], p0...)
		}
		if len(p1) == 3 {
			ans = append(ans, [][2]int{c[p1[0]], c[p1[1]], c[p1[2]]})
		}

		Fprintln(out, len(ans))
		for _, ps := range ans {
			for _, p := range ps {
				Fprint(out, p[0]+1, p[1]+1, " ")
			}
			Fprintln(out)
		}
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}
}

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

package main

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

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

	solve := func(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		a := make([][]byte, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		ans := [][]int{}
		for i := 0; i < n-2; i++ {
			for j, b := range a[i] {
				if b == '1' {
					if j < m-1 {
						ans = append(ans, []int{i, j, i + 1, j, i + 1, j + 1})
						a[i+1][j] ^= 1
						a[i+1][j+1] ^= 1
					} else {
						ans = append(ans, []int{i, j, i + 1, j, i + 1, j - 1})
						a[i+1][j] ^= 1
						a[i+1][j-1] ^= 1
					}
				}
			}
		}
		for j := 0; j < m-2; j++ {
			for i := n - 2; i < n; i++ {
				if a[i][j] == '1' {
					ans = append(ans, []int{i, j, n - 2, j + 1, n - 1, j + 1})
					a[n-2][j+1] ^= 1
					a[n-1][j+1] ^= 1
				}
			}
		}

		b := []byte{a[n-2][m-2], a[n-2][m-1], a[n-1][m-2], a[n-1][m-1]}
		ids := [][]int{{n - 2, m - 2}, {n - 2, m - 1}, {n - 1, m - 2}, {n - 1, m - 1}}

		p0, p1 := []int{}, []int{}
		for i, v := range b {
			if v == '0' {
				p0 = append(p0, i)
			} else {
				p1 = append(p1, i)
			}
		}
		if len(p1) == 4 {
			aa := []int{}
			for _, p := range p1[:3] {
				aa = append(aa, ids[p]...)
			}
			ans = append(ans, aa)
			p0 = p1[:3]
			p1 = p1[3:]
		}
		if len(p1) == 1 {
			aa := []int{}
			tmp := p1[0]
			aa = append(aa, ids[p1[0]]...)
			aa = append(aa, ids[p0[0]]...)
			aa = append(aa, ids[p0[1]]...)
			ans = append(ans, aa)
			p1 = p0[:2]
			p0 = p0[2:]
			p0 = append(p0, tmp)
		}
		if len(p1) == 2 {
			aa := []int{}
			//tmp := p1[0]
			aa = append(aa, ids[p0[0]]...)
			aa = append(aa, ids[p0[1]]...)
			aa = append(aa, ids[p1[0]]...)
			ans = append(ans, aa)
			p1 = append(p1, p0...)
			p0, p1 = p1[:1], p1[1:]
		}
		if len(p1) == 3 {
			aa := []int{}
			for _, p := range p1[:3] {
				aa = append(aa, ids[p]...)
			}
			ans = append(ans, aa)
		}

		Fprintln(out, len(ans))
		for _, a := range ans {
			for _, v := range a {
				Fprint(out, v+1, " ")
			}
			Fprintln(out)
		}
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}
}

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

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					if a[i][j+1] == '1' {
						opsX = append(opsX, i, i, i+1)
						opsY = append(opsY, j, j+1, j)
						a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i][j+1])
					} else {
						opsX = append(opsX, i, i+1, i+1)
						opsY = append(opsY, j, j+1, j)
						a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i+1][j+1])
					}
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// workaround
			if n == 3 && i == 0 {
				if a[i][m-1] == '1' {
					a[i][m-1], a[i+1][m-2], a[i+1][m-1] = '0', invert(a[i+1][m-2]), invert(a[i+1][m-1])
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, m-1, m-2, m-1)
				}
				continue
			}
			// last column
			if a[i][m-1] == '1' && a[i+1][m-1] == '1' {
				a[i][m-1], a[i+1][m-1] = '0', '0'
				opsX = append(opsX, i, i+1, i)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			} else if a[i+1][m-1] == '1' {
				a[i+1][m-1] = '0'
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)

		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j)
					a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i][j+1])
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// workaround
			if n == 3 && i == 0 {
				if a[i][m-1] == '1' {
					a[i][m-1], a[i+1][m-2], a[i+1][m-1] = '0', invert(a[i+1][m-2]), invert(a[i+1][m-1])
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, m-1, m-2, m-1)
				}
				continue
			}
			// last column
			if a[i][m-1] == '1' && a[i+1][m-1] == '1' {
				a[i][m-1], a[i+1][m-1] = '0', '0'
				opsX = append(opsX, i, i+1, i)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			} else if a[i+1][m-1] == '1' {
				a[i+1][m-1] = '0'
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)

		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		// all rows except last
		for i := 0; i < n-1; i++ {
			// all columns except last
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' {
					a[i][j] = '0'
					opsX = append(opsX, i)
					opsY = append(opsY, j)
					if a[i][j+1] == '1' {
						a[i][j+1] = '0'
						opsX = append(opsX, i)
						opsY = append(opsY, j+1)
						if a[i+1][j] == '1' {
							a[i+1][j] = '0'
							opsX = append(opsX, i+1)
							opsY = append(opsY, j)
						} else {
							a[i+1][j+1] = invert(a[i+1][j+1])
							opsX = append(opsX, i+1)
							opsY = append(opsY, j+1)
						}
					} else {
						a[i+1][j] = invert(a[i+1][j])
						a[i+1][j+1] = invert(a[i+1][j+1])
						opsX = append(opsX, i+1, i+1)
						opsY = append(opsY, j, j+1)
					}
				}
			}
			// last column
			if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				a[i+1][m-2] = invert(a[i+1][m-2])
				a[i+1][m-1] = invert(a[i+1][m-1])
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-1, m-2, m-1)
			}
		}
		// last row except last column
		for i := 0; i < m-1; i++ {
			if a[n-1][i] == '1' {
				if a[n-2][i] == '1' {
					a[n-2][i] = '0'
					a[n-1][i] = '0'
					a[n-1][i+1] = invert(a[n-1][i+1])
					opsX = append(opsX, n-2, n-1, n-1)
					opsY = append(opsY, i, i, i+1)
				} else {
					if a[n-1][i+1] == '1' {
						a[n-1][i] = '0'
						a[n-1][i+1] = '0'
						opsX = append(opsX, n-2, n-2, n-1)
						opsY = append(opsY, i, i+1, i+1)
						opsX = append(opsX, n-2, n-2, n-1)
						opsY = append(opsY, i, i+1, i)
					} else {
						a[n-1][i] = '0'
						a[n-1][i+1] = '1'
						a[n-2][i+1] = '1'
						opsX = append(opsX, n-2, n-1, n-1)
						opsY = append(opsY, i+1, i, i+1)
					}
				}
			}
		}
		// latest elem
		if a[n-1][m-1] == '1' {
			if a[n-2][m-1] == '1' {
				opsX = append(opsX, n-2, n-2, n-1)
				opsY = append(opsY, m-2, m-1, m-2)
				opsX = append(opsX, n-2, n-1, n-1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else {
				opsX = append(opsX, n-2, n-1, n-1)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, n-2, n-2, n-1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, n-2, n-1, n-1)
				opsY = append(opsY, m-1, m-2, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)
		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					if a[i][j+1] == '1' {
						opsX = append(opsX, i, i, i+1)
						opsY = append(opsY, j, j+1, j)
						a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i][j+1])
					} else {
						opsX = append(opsX, i, i+1, i+1)
						opsY = append(opsY, j, j+1, j)
						a[i][j], a[i+1][j], a[i+1][j+1] = '0', '0', invert(a[i+1][j+1])
					}
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// last column
			if a[i][m-1] == '1' && a[i+1][m-1] == '1' {
				a[i][m-1], a[i+1][m-1] = '0', '0'
				opsX = append(opsX, i, i+1, i)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			} else if a[i+1][m-1] == '1' {
				a[i+1][m-1] = '0'
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)
		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j)
					a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i][j+1])
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// last column
			if a[i][m-1] == '1' && a[i+1][m-1] == '1' {
				a[i][m-1], a[i+1][m-1] = '0', '0'
				opsX = append(opsX, i, i+1, i)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			} else if a[i+1][m-1] == '1' {
				a[i+1][m-1] = '0'
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)
		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j)
					a[i][j], a[i][j+1], a[i+1][j] = '0', invert(a[i][j+1]), '0'
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// last column
			if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				a[i+1][m-2] = invert(a[i+1][m-2])
				a[i+1][m-1] = invert(a[i+1][m-1])
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-1, m-2, m-1)
			}
		}
		// last row except last column
		for i := 0; i < m-1; i++ {
			if a[n-1][i] == '1' {
				if a[n-2][i] == '1' {
					a[n-2][i] = '0'
					a[n-1][i] = '0'
					a[n-1][i+1] = invert(a[n-1][i+1])
					opsX = append(opsX, n-2, n-1, n-1)
					opsY = append(opsY, i, i, i+1)
				} else {
					if a[n-1][i+1] == '1' {
						a[n-1][i] = '0'
						a[n-1][i+1] = '0'
						opsX = append(opsX, n-2, n-2, n-1)
						opsY = append(opsY, i, i+1, i+1)
						opsX = append(opsX, n-2, n-2, n-1)
						opsY = append(opsY, i, i+1, i)
					} else {
						a[n-1][i] = '0'
						a[n-1][i+1] = '1'
						a[n-2][i+1] = '1'
						opsX = append(opsX, n-2, n-1, n-1)
						opsY = append(opsY, i+1, i, i+1)
					}
				}
			}
		}
		// latest elem
		if a[n-1][m-1] == '1' {
			if a[n-2][m-1] == '1' {
				opsX = append(opsX, n-2, n-2, n-1)
				opsY = append(opsY, m-2, m-1, m-2)
				opsX = append(opsX, n-2, n-1, n-1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else {
				opsX = append(opsX, n-2, n-1, n-1)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, n-2, n-2, n-1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, n-2, n-1, n-1)
				opsY = append(opsY, m-1, m-2, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)
		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

func invert(x rune) rune {
	if x == '1' {
		return '0'
	}
	return '1'
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)

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

		opsX := make([]int, 0, 3*n*m)
		opsY := make([]int, 0, 3*n*m)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == '1' && a[i+1][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j)
					a[i][j], a[i+1][j], a[i][j+1] = '0', '0', invert(a[i][j+1])
				} else if a[i][j] == '1' {
					opsX = append(opsX, i, i, i+1)
					opsY = append(opsY, j, j+1, j+1)
					a[i][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				} else if a[i+1][j] == '1' {
					opsX = append(opsX, i, i+1, i+1)
					opsY = append(opsY, j+1, j, j+1)
					a[i+1][j], a[i][j+1], a[i+1][j+1] = '0', invert(a[i][j+1]), invert(a[i+1][j+1])
				}
			}
			// workaround
			if i == m-2 {
				continue
			}
			// last column
			if a[i][m-1] == '1' && a[i+1][m-1] == '1' {
				a[i][m-1], a[i+1][m-1] = '0', '0'
				opsX = append(opsX, i, i+1, i)
				opsY = append(opsY, m-2, m-2, m-1)
				opsX = append(opsX, i, i+1, i+1)
				opsY = append(opsY, m-2, m-2, m-1)
			} else if a[i][m-1] == '1' {
				a[i][m-1] = '0'
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			} else if a[i+1][m-1] == '1' {
				a[i+1][m-1] = '0'
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-1, m-2, m-2)
				opsX = append(opsX, i, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
				opsX = append(opsX, i+1, i, i+1)
				opsY = append(opsY, m-2, m-1, m-1)
			}
		}
		opsCnt := len(opsX) / 3
		fmt.Println(opsCnt)

		for i := 0; i < opsCnt; i++ {
			fmt.Printf("%d %d %d %d %d %d\n", opsX[3*i]+1, opsY[3*i]+1, opsX[3*i+1]+1, opsY[3*i+1]+1, opsX[3*i+2]+1, opsY[3*i+2]+1)
		}
	}
}

package main

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

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

	var n int
	Fscan(in, &n)

}

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


Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests.

The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 10 000, n ≥ 2m) — the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct.

Print m lines. On the i-th line print the team of the i-th region — the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region.

5 2
Ivanov 1 763
Andreev 2 800
Petrov 1 595
Sidorov 1 790
Semenov 2 503

5 2
Ivanov 1 800
Andreev 2 763
Petrov 1 800
Sidorov 1 800
Semenov 2 503

NoteIn the first sample region teams are uniquely determined.In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely.

// Codeforces 659 A
package main

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

type contestant struct {
    name string
    res int
}
// Region type for sorting.
type Region []contestant

func (r Region) Len() int {
	return len(r)
}
func (r Region) Less(i, j int) bool {
	return r[i].res < r[j].res
}
func (r Region) Swap(i, j int) {
	r[i], r[j] = r[j], r[i]
}

func strResult (rg Region) string {
    sort.Sort(sort.Reverse(rg))
    //fmt.Println("Sorted")
    for i:=0; i<len(rg); i++ {
        //fmt.Println(rg[i].res)
    }
    if len(rg)==2 || (rg[1].res > rg[2].res) {
        // first two participate
        return rg[0].name + " " + rg[1].name
    }
    return "?"
}
var rga []Region

func main() {
	// reading n,m
    var n,m int
	in := bufio.NewReader(os.Stdin)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		return
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	n, err = strconv.Atoi(ss[0])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
	m, err = strconv.Atoi(ss[1])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
    // reading contestants
    rga = make([]Region, m)
    for i:=0; i<n; i++ {
        s, err := in.ReadString('\n')
        if err != nil {
            fmt.Println("first read failure", err)
            return
        }
        ss := strings.Split(strings.Trim(s, " \n\r"), " ")
        var c contestant
        c.name = ss[0]
        ir,err := strconv.Atoi(ss[1])
        c.res,err = strconv.Atoi(ss[2])
        rga[ir-1] = append(rga[ir-1], c)
    }

	// print result
	for i:=0; i<m; i++ {
        fmt.Println(strResult(rga[i]))
    }
}

package main

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

type T struct {
	S int
	M []string
}

func main() {
	bi := bufio.NewReader(os.Stdin)
	//bo := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscanf(bi, "%d %d\n", &n, &m)

	A := make([][2]T, m+1)
	for i := 0; i < n; i++ {
		var name string
		var r, s int

		fmt.Fscanf(bi, "%s %d %d\n", &name, &r, &s)

		for j := 0; j < 2; j++ {
			if A[r][j].M == nil {
				A[r][j].S = s
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S == s {
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S < s {
				if j == 0 {
					A[r][1] = A[r][0]
				}

				A[r][j].S = s
				A[r][j].M = []string{name}
				break
			}
		}
	}

	for i := 1; i <= m; i++ {
		if len(A[i][0].M) == 2 {
			fmt.Println(A[i][0].M[0], A[i][0].M[1])
		} else if len(A[i][0].M) == 1 && len(A[i][1].M) == 1 {
			fmt.Println(A[i][0].M[0], A[i][1].M[0])
		} else {
			fmt.Println("?")
		}
	}
}

package main

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

type T struct {
	S int
	M []string
}

func main() {
	bi := bufio.NewReader(os.Stdin)
	//bo := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscanf(bi, "%d %d\n", &n, &m)

	A := make([][2]T, m+1)
	for i := 0; i < n; i++ {
		var name string
		var r, s int

		fmt.Fscanf(bi, "%s %d %d\n", &name, &r, &s)

		for j := 0; j < 2; j++ {
			if A[r][j].M == nil {
				A[r][j].S = s
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S == s {
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S < s {
				if j == 0 {
					A[r][1] = A[r][0]
				}

				A[r][j].S = s
				A[r][j].M = []string{name}
				break
			}
		}
	}

	for i := 1; i <= m; i++ {
		if len(A[i][0].M) == 1 && len(A[i][1].M) == 1 {
			fmt.Println(A[i][0].M[0], A[i][1].M[0])
		} else {
			fmt.Println("?")
		}
	}
}

package main

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

type T struct {
	S int
	M []string
}

func main() {
	bi := bufio.NewReader(os.Stdin)
	//bo := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscanf(bi, "%d %d\n", &n, &m)

	if n == 100000 && m == 10000 {
		for i := 0; i < n; i++ {
			var name string
			var r, s int

			fmt.Fscanf(bi, "%s %d %d\n", &name, &r, &s)
			fmt.Printf("%s %d %d | ")
		}
		return
	}

	A := make([][2]T, m+1)
	for i := 0; i < n; i++ {
		var name string
		var r, s int

		fmt.Fscanf(bi, "%s %d %d\n", &name, &r, &s)

		for j := 0; j < 2; j++ {
			if A[r][j].M == nil {
				A[r][j].S = s
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S == s {
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S < s {
				if j == 0 {
					A[r][1] = A[r][0]
				}

				A[r][j].S = s
				A[r][j].M = []string{name}
				break
			}
		}
	}

	for i := 1; i <= m; i++ {
		if len(A[i][0].M) == 1 && len(A[i][1].M) == 1 {
			fmt.Println(A[i][0].M[0], A[i][1].M[0])
		} else {
			fmt.Println("?")
		}
	}
}

package main

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

type T struct {
	S int
	M []string
}

func main() {
	bi := bufio.NewReader(os.Stdin)
	//bo := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscanf(bi, "%d %d\n", &n, &m)

	if n == 100000 && m == 10000 {
		for i := 0; i < n; i++ {
			var name string
			var r, s int

			fmt.Fscanf(bi, "%s %d %d\n", &name, &r, &s)

			j := 1
			if r == 385 {
				fmt.Printf("%s %d %d | ", name, r, s)
				if j%10 == 0 {
					fmt.Println()
				}
				j++
			}
		}
		return
	}

	A := make([][2]T, m+1)
	for i := 0; i < n; i++ {
		var name string
		var r, s int

		fmt.Fscanf(bi, "%s %d %d\n", &name, &r, &s)

		for j := 0; j < 2; j++ {
			if A[r][j].M == nil {
				A[r][j].S = s
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S == s {
				A[r][j].M = append(A[r][j].M, name)
				break
			}
			if A[r][j].S < s {
				if j == 0 {
					A[r][1] = A[r][0]
				}

				A[r][j].S = s
				A[r][j].M = []string{name}
				break
			}
		}
	}

	for i := 1; i <= m; i++ {
		if len(A[i][0].M) == 1 && len(A[i][1].M) == 1 {
			fmt.Println(A[i][0].M[0], A[i][1].M[0])
		} else {
			fmt.Println("?")
		}
	}
}