Madoka's father just reached $$$1$$$ million subscribers on Mathub! So the website decided to send him a personalized award — The Mathhub's Bit Button! The Bit Button is a rectangular table with $$$n$$$ rows and $$$m$$$ columns with $$$0$$$ or $$$1$$$ in each cell. After exploring the table Madoka found out that: A subrectangle $$$A$$$ is contained in a subrectangle $$$B$$$ if there's no cell contained in $$$A$$$ but not contained in $$$B$$$. Two subrectangles intersect if there is a cell contained in both of them. A subrectangle is called black if there's no cell with value $$$0$$$ inside it. A subrectangle is called nice if it's black and it's not contained in another black subrectangle. The table is called elegant if there are no two nice intersecting subrectangles.For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle.  Help Madoka to determine whether the table is elegant.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 200$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $$$n, m$$$ ($$$1 \le n, m \le 100$$$). The next $$$n$$$ lines contain strings of length $$$m$$$ consisting of zeros and ones — the description of the table. It is guaranteed that the sum of the values of $$$n$$$ and the sum of the values of $$$m$$$ for all test cases do not exceed $$$777$$$.

For each test case print "YES" if its table is elegant or print "NO" otherwise. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

NoteIn the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.   In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.

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

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

		var overlap bool
	Loop:
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				var c int
				for _, ch := range z[i][j : j+2] {
					if ch == '1' {
						c++
					}
				}

				for _, ch := range z[i+1][j : j+2] {
					if ch == '1' {
						c++
					}
				}

				if c == 3 {
					overlap = true
					break Loop
				}
			}
		}
		if !overlap {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

func main() {
	_r, _w := os.Stdin, os.Stdout
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var solve func()
	// Todo...
	solve = func() {
		var n, m int
		Fscan(in, &n, &m)
		rec := make([][]int, n+2)
		for i := 0; i < n+2; i++ {
			rec[i] = make([]int, m+2)
		}
		for i := 0; i < n+2; i++ {
			for j := 0; j < m+2; j++ {
				rec[i][j] = 1
			}
		}
		var s string
		for i := 1; i <= n; i++ {
			Fscan(in, &s)
			for j := 1; j <= m; j++ {
				if s[j-1] == '1' {
					rec[i][j] = 1
				} else {
					rec[i][j] = 0
				}
			}
		}
		ok := true
		for i := 1; i <= n; i++ {
			for j := 2; j <= m; j++ {
				if rec[i][j] == 0 {
					continue
				}
				if rec[i][j] == rec[i][j-1] && rec[i+1][j] != rec[i+1][j-1] {
					ok = false
				}
				if rec[i][j] == rec[i][j-1] && rec[i-1][j] != rec[i-1][j-1] {
					ok = false
				}
			}
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
	// End...
	var T int = 1
	Fscan(in, &T)
	for i := 0; i < T; i++ {
		solve()
	}
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

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

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

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

		mat := make([][]byte, n)
		for i := range mat { fmt.Fscan(r, &mat[i]) }

		res := "YES"
		for i := 1; i < n; i++ {
			for j := 1; j < m; j++ {
				if mat[i][j] == '1' && mat[i][j] == mat[i-1][j-1] && mat[i][j-1] != mat[i-1][j] { res = "NO" }
				if mat[i-1][j] == '1' && mat[i][j] != mat[i-1][j-1] && mat[i][j-1] == mat[i-1][j] { res = "NO" }
			}
		}

		fmt.Fprintln(w, res)
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n, m int

		Fscan(in, &n, &m)

		a := make([]string, n)

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

		flag := true
		for i := 0; i < n && flag; i++ {
			for j := 0; j < m && flag; j++ {
				if a[i][j] == '0' {
					if i > 0 && j > 0 && a[i-1][j-1] == '1' && a[i][j-1] == '1' && a[i-1][j] == '1' {
						Fprintln(out, "NO")
						flag = false
						break
					}

					if i > 0 && j < m-1 && a[i-1][j] == '1' && a[i-1][j+1] == '1' && a[i][j+1] == '1' {
						Fprintln(out, "NO")
						flag = false
						break
					}

					if i < n-1 && j > 0 && a[i][j-1] == '1' && a[i+1][j-1] == '1' && a[i+1][j] == '1' {
						Fprintln(out, "NO")
						flag = false
						break
					}

					if i < n-1 && j < m-1 && a[i][j+1] == '1' && a[i+1][j+1] == '1' && a[i+1][j] == '1' {
						Fprintln(out, "NO")
						flag = false
						break
					}

				}
			}
		}
		if flag {
			Fprintln(out, "YES")
		}

	}

}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for n > 0 {
		n--
		var a, b int
		fmt.Scan(&a, &b)
		x := make(map[int]string, a)
		var inp string
		for i := 0; i < a; i++ {
			fmt.Scan(&inp)
			x[i] = inp
		}
		var k int
	iForLoop:
		for i := 0; i < a-1; i++ {
			for j := 0; j < b-1; j++ {
				if (string(x[i][j]) == "1" && string(x[i][j+1]) == "1") || (string(x[i+1][j]) == "1" && string(x[i+1][j+1]) == "1") {
					k = 0
					if string(x[i][j]) == "1" {
						k++
					}
					if string(x[i][j+1]) == "1" {
						k++
					}
					if string(x[i+1][j]) == "1" {
						k++
					}
					if string(x[i+1][j+1]) == "1" {
						k++
					}
					if k == 3 {
						fmt.Println("NO")
						break iForLoop
					}
				}
			}
		}
		if k != 3 {
			fmt.Println("YES")
		}

	}
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scan(&q)
	for t := 0; t < q; t++ {
		var n, m int
		fmt.Scan(&n, &m)
		matrix := make([][]int, n)
		for i, _ := range matrix {
			matrix[i] = make([]int, m)
			var s string
			fmt.Scan(&s)
			for j, v := range s {
				matrix[i][j] = int(v - '0')
			}
		}
		var continueCondition bool = true
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if matrix[i][j]+matrix[i+1][j]+matrix[i][j+1]+matrix[i+1][j+1] == 3 {
					continueCondition = false
					break
				}
			}
			if !continueCondition {
				break
			}
		}
		if continueCondition {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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-- {
		var n, m int
		var check bool = false
		fmt.Fscan(in, &n, &m)
		board := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &board[i])
		}
		if n == 1 || m == 1 {
			fmt.Println("YES")
			continue
		}
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if board[i][j] == '1' && board[i][j+1] == '0' && board[i+1][j] == '1' && board[i+1][j+1] == '1' {
					check = true
					break
				}
				if board[i][j] == '0' && board[i][j+1] == '1' && board[i+1][j] == '1' && board[i+1][j+1] == '1' {
					check = true
					break
				}
				if board[i][j] == '1' && board[i][j+1] == '1' && board[i+1][j] == '0' && board[i+1][j+1] == '1' {
					check = true
					break
				}
				if board[i][j] == '1' && board[i][j+1] == '1' && board[i+1][j] == '1' && board[i+1][j+1] == '0' {
					check = true
					break
				}
			}
			if check {
				break
			}
		}
		if check {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}

}

package main

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

/*

 */

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	tbl := make([]string, n)

	for i := range tbl {
		scanf("%s\n", &tbl[i])
	}

	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			cnt := 0
			if tbl[i][j] == '1' {
				cnt++
			}
			if tbl[i][j+1] == '1' {
				cnt++
			}
			if tbl[i+1][j] == '1' {
				cnt++
			}
			if tbl[i+1][j+1] == '1' {
				cnt++
			}

			if cnt == 3 {
				fmt.Fprintln(writer, "NO")
				return
			}
		}
	}

	fmt.Fprintln(writer, "YES")
}

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

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

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	idx := 0
	for i, num := range nums {
		if num > nums[idx] {
			idx = i
		}
	}

	return nums[idx]
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

package main

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

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

		var f bool
		table := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &table[i])
		}

		for i := 1; i < n; i++ {
			if table[i-1] == table[i] {
				continue
			}

			for j := 0; j < m; j++ {
				if table[i-1][j] != table[i][j] {
					if j == 0 && j+1 < m {
						if table[i-1][j+1] == '1' && table[i][j+1] == '1' {
							f = true
						}
					} else if j > 0 && table[i-1][j-1] == '1' && table[i][j-1] == '1' {
						f = true
					}
				}
			}
		}

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

package main

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

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

		var s string
		fmt.Fscan(in, &s)
		z, _ := strconv.ParseInt(s, 2, 64)

		overlap := func(a, b int64) bool {
			for a > 0 && b > 0 {
				if a&b&1 > 0 {
					return true
				}
				a >>= 1
				b >>= 1
			}
			return false
		}

		var f bool
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &s)

			d, _ := strconv.ParseInt(s, 2, 64)
			if z != d && overlap(z, d) {
				f = true
			}
			z = d
		}

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

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

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

		r := false
		for _, v := range table {
			f := true
			for _, ch := range v {
				if ch == '0' {
					f = false
					break
				}
			}

			if f {
				r = true
				break
			}
		}

		c := false
		for j := 0; j < m; j++ {
			f := true
			for i := 0; i < n; i++ {
				if table[i][j] == '0' {
					f = false
					break
				}
			}

			if f {
				c = true
				break
			}
		}

		if r && c {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

func main() {
	_r, _w := os.Stdin, os.Stdout
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var solve func()
	// Todo...
	solve = func() {
		var n, m int
		Fscan(in, &n, &m)
		rec := make([][]int, n+2)
		for i := 0; i < n+2; i++ {
			rec[i] = make([]int, m+2)
		}
		for i := 0; i < n+2; i++ {
			for j := 0; j < m+2; j++ {
				rec[i][j] = 1
			}
		}
		var s string
		for i := 1; i <= n; i++ {
			Fscan(in, &s)
			for j := 1; j <= m; j++ {
				if s[j-1] == '1' {
					rec[i][j] = 1
				} else {
					rec[i][j] = 0
				}
			}
		}
		ok := true
		for i := 1; i <= n; i++ {
			for j := 2; j <= m; j++ {
				if rec[i][j] == 0 {
					continue
				}
				if rec[i][j] == rec[i][j-1] && rec[i+1][j] != rec[i+1][j-1] {
					ok = false
				}
			}
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
	// End...
	var T int = 1
	Fscan(in, &T)
	for i := 0; i < T; i++ {
		solve()
	}
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

func main() {
	_r, _w := os.Stdin, os.Stdout
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var solve func()
	// Todo...
	solve = func() {
		var n, m int
		Fscan(in, &n, &m)
		rec := make([][]int, n+2)
		for i := 0; i < n+2; i++ {
			rec[i] = make([]int, m+2)
		}
		for i := 0; i < n+2; i++ {
			for j := 0; j < m+2; j++ {
				rec[i][j] = 1
			}
		}
		var s string
		for i := 1; i <= n; i++ {
			Fscan(in, &s)
			for j := 1; j <= m; j++ {
				if s[j-1] == '1' {
					rec[i][j] = 1
				} else {
					rec[i][j] = 0
				}
			}
		}
		ok := true
		for i := 1; i <= n; i++ {
			for j := 2; j <= m; j++ {
				if rec[i][j] == rec[i][j-1] && rec[i+1][j] != rec[i+1][j-1] {
					ok = false
				}
			}
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
	// End...
	var T int = 1
	Fscan(in, &T)
	for i := 0; i < T; i++ {
		solve()
	}
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

func main() {
	_r, _w := os.Stdin, os.Stdout
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var solve func()
	// Todo...
	solve = func() {
		var n, m int
		Fscan(in, &n, &m)
		rec := make([][]int, n+2)
		for i := 0; i < n+2; i++ {
			rec[i] = make([]int, m+2)
		}
		for i := 0; i < n+2; i++ {
			for j := 0; j < m+2; j++ {
				rec[i][j] = 1
			}
		}
		var s string
		for i := 1; i <= n; i++ {
			Fscan(in, &s)
			for j := 1; j <= m; j++ {
				if s[j-1] == '1' {
					rec[i][j] = 1
				} else {
					rec[i][j] = 0
				}
			}
		}
		ok := true
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				if rec[i+1][j] == 1 && rec[i-1][j] == 1 && rec[i][j+1] == 1 && rec[i][j-1] == 1 {
					if (i == 1 && j == 1) || (i == n && j == m) || (i == n && j == 1) || (i == 1 && j == m) {
						continue
					}
					if n == 1 || m == 1 {
						continue
					}
					//Fprintln(out, i, j, "---")
					ok = false
				}
			}
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
	// End...
	var T int = 1
	Fscan(in, &T)
	for i := 0; i < T; i++ {
		solve()
	}
}
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
}


A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string "aab" is an anagram of the string "aba" and the string "aaa" is not.The string t is called a substring of the string s if it can be read starting from some position in the string s. For example, the string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".You are given a string s, consisting of lowercase Latin letters and characters "?". You are also given a string p, consisting of lowercase Latin letters only. Let's assume that a string is good if you can obtain an anagram of the string p from it, replacing the "?" characters by Latin letters. Each "?" can be replaced by exactly one character of the Latin alphabet. For example, if the string p = «aba», then the string "a??" is good, and the string «?bc» is not. Your task is to find the number of good substrings of the string s (identical substrings must be counted in the answer several times).

The first line is non-empty string s, consisting of no more than 105 lowercase Latin letters and characters "?". The second line is non-empty string p, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string p can exceed the length of the string s.

Print the single number representing the number of good substrings of string s. Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times.

NoteConsider the first sample test. Here the string s has two good substrings: "b??" (after we replace the question marks we get "baa"), "???" (after we replace the question marks we get "baa").Let's consider the second sample test. Here the string s has two good substrings: "ab?" ("?" can be replaced by "c"), "b?c" ("?" can be replaced by "a").

package main

import (
    "fmt"
)

func main() {
    var s, p []byte
    var tpl, count [26]int
    var ans int
    fmt.Scan(&s)
    fmt.Scan(&p)
    for i := 0; i < len(p); i++ {
        tpl[p[i] - 'a']++
    }

    if len(s) < len(p) {
        fmt.Println(0)
        return
    }

    for i := 0; i < len(p) - 1; i++ {
        if s[i] != '?' {
            count[s[i] - 'a']++
        }
    }

    for i := len(p) - 1; i < len(s); i++ {
        j := 0
        if s[i] != '?' {
            count[s[i] - 'a']++
        }
        for j = 0; j < 26; j++ {
            if count[j] > tpl[j] {
                break
            }
        }
        if j == 26 {
            ans++
        }

        if s[i - len(p) + 1] != '?' {
            count[s[i - len(p) + 1] - 'a']--
        }
    }

    fmt.Println(ans)
    return
}

package main

import (
    "fmt"
)

func main() {
    var s, p []byte
    var tpl, count [26]int
    var ans int
    fmt.Scan(&s)
    fmt.Scan(&p)
    for i := 0; i < len(p); i++ {
        tpl[p[i] - 'a']++
    }

    if len(s) < len(p) {
        fmt.Println(0)
        return
    }

    for i := 0; i < len(p) - 1; i++ {
        if s[i] != '?' {
            count[s[i] - 'a']++
        }
    }

    for i := len(p) - 1; i < len(p); i++ {
        j := 0
        if s[i] != '?' {
            count[s[i] - 'a']++
        }
        for j = 0; j < 26; j++ {
            if count[j] > tpl[j] {
                break
            }
        }
        if j == 26 {
            ans++
        }

        count[s[i - len(p) + 1] - 'a']--
    }

    fmt.Println(ans)
    return
}

package main

import (
    "fmt"
)

func main() {
    var s, p []byte
    var tpl, count [26]int
    var ans int
    fmt.Scan(&s)
    fmt.Scan(&p)
    for i := 0; i < len(p); i++ {
        tpl[p[i] - 'a']++
    }

    if len(s) < len(p) {
        fmt.Println(0)
        return
    }

    for i := 0; i < len(p) - 1; i++ {
        if s[i] != '?' {
            count[s[i] - 'a']++
        }
    }

    for i := len(p) - 1; i < len(p); i++ {
        j := 0
        for j = 0; j < 26; j++ {
            count[j] = 0
        }
        if s[i] != '?' {
            count[s[i] - 'a']++
        }
        for j = 0; j < 26; j++ {
            if count[j] > tpl[j] {
                break
            }
        }

        if j == 26 {
            ans++
        }
    }

    fmt.Println(ans)

    return
}


There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the appropriate coordinates of vertices.

The first line contains two integers a, b (1 ≤ a, b ≤ 1000), separated by a single space.

In the first line print either "YES" or "NO" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value.

// 407A
package main

import (
	"fmt"
)

func main() {
	var a, b, i, j int
	fmt.Scan(&a, &b)
	for i = 1; i <= 2000; i++ {
		for j = 1; j <= 2000; j++ {
			if i*i+j*j == a*a {
				if (j*b)%a == 0 && (i*b)%a == 0 {
					if i != j*b/a {
						fmt.Println("YES")
						fmt.Println("0 0")
						fmt.Println(i, j)
						fmt.Println(j*b/a, -i*b/a)
						return
					}
				}
			}
		}
	}
	fmt.Println("NO")
}


Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th vertex has $$$m_i$$$ outgoing edges that are labeled as $$$e_i[0]$$$, $$$e_i[1]$$$, $$$\ldots$$$, $$$e_i[m_i-1]$$$, each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The $$$i$$$-th vertex also has an integer $$$k_i$$$ written on itself.A travel on this graph works as follows.   Gildong chooses a vertex to start from, and an integer to start with. Set the variable $$$c$$$ to this integer.  After arriving at the vertex $$$i$$$, or when Gildong begins the travel at some vertex $$$i$$$, add $$$k_i$$$ to $$$c$$$.  The next vertex is $$$e_i[x]$$$ where $$$x$$$ is an integer $$$0 \le x \le m_i-1$$$ satisfying $$$x \equiv c \pmod {m_i}$$$. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly.For example, assume that Gildong starts at vertex $$$1$$$ with $$$c = 5$$$, and $$$m_1 = 2$$$, $$$e_1[0] = 1$$$, $$$e_1[1] = 2$$$, $$$k_1 = -3$$$. Right after he starts at vertex $$$1$$$, $$$c$$$ becomes $$$2$$$. Since the only integer $$$x$$$ ($$$0 \le x \le 1$$$) where $$$x \equiv c \pmod {m_i}$$$ is $$$0$$$, Gildong goes to vertex $$$e_1[0] = 1$$$. After arriving at vertex $$$1$$$ again, $$$c$$$ becomes $$$-1$$$. The only integer $$$x$$$ satisfying the conditions is $$$1$$$, so he goes to vertex $$$e_1[1] = 2$$$, and so on.Since Gildong is quite inquisitive, he's going to ask you $$$q$$$ queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of $$$c$$$. Note that you should not count the vertices that will be visited only finite times.

The first line of the input contains an integer $$$n$$$ ($$$1 \le n \le 1000$$$), the number of vertices in the graph. The second line contains $$$n$$$ integers. The $$$i$$$-th integer is $$$k_i$$$ ($$$-10^9 \le k_i \le 10^9$$$), the integer written on the $$$i$$$-th vertex. Next $$$2 \cdot n$$$ lines describe the edges of each vertex. The $$$(2 \cdot i + 1)$$$-st line contains an integer $$$m_i$$$ ($$$1 \le m_i \le 10$$$), the number of outgoing edges of the $$$i$$$-th vertex. The $$$(2 \cdot i + 2)$$$-nd line contains $$$m_i$$$ integers $$$e_i[0]$$$, $$$e_i[1]$$$, $$$\ldots$$$, $$$e_i[m_i-1]$$$, each having an integer value between $$$1$$$ and $$$n$$$, inclusive. Next line contains an integer $$$q$$$ ($$$1 \le q \le 10^5$$$), the number of queries Gildong wants to ask. Next $$$q$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x \le n$$$, $$$-10^9 \le y \le 10^9$$$) each, which mean that the start vertex is $$$x$$$ and the starting value of $$$c$$$ is $$$y$$$.

For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex $$$x$$$ with starting integer $$$y$$$.

NoteThe first example can be shown like the following image:  Three integers are marked on $$$i$$$-th vertex: $$$i$$$, $$$k_i$$$, and $$$m_i$$$ respectively. The outgoing edges are labeled with an integer representing the edge number of $$$i$$$-th vertex.The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex ($$$c$$$ after $$$k_i$$$ added)".  $$$1(0) \to 2(0) \to 2(0) \to \ldots$$$  $$$2(0) \to 2(0) \to \ldots$$$  $$$3(-1) \to 1(-1) \to 3(-1) \to \ldots$$$  $$$4(-2) \to 2(-2) \to 2(-2) \to \ldots$$$  $$$1(1) \to 3(1) \to 4(1) \to 1(1) \to \ldots$$$  $$$1(5) \to 3(5) \to 1(5) \to \ldots$$$ The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example.  The queries for the second example works as follows:  $$$1(4) \to 2(-1) \to 2(-6) \to \ldots$$$  $$$2(-5) \to 2(-10) \to \ldots$$$  $$$3(-4) \to 1(0) \to 2(-5) \to 2(-10) \to \ldots$$$  $$$4(-3) \to 1(1) \to 3(-2) \to 4(-3) \to \ldots$$$  $$$1(5) \to 3(2) \to 1(6) \to 2(1) \to 2(-4) \to \ldots$$$  $$$1(9) \to 3(6) \to 2(1) \to 2(-4) \to \ldots$$$

package main
import "fmt"
import "os"
import "bufio"

var input *bufio.Reader

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
}

const MOD = 2520
const N = 1005

var m, k [N]int
var e [N][12]int
var ans [N][MOD]int
var pos [N][MOD]int
var vec [N*MOD]int
var vis [N]bool

func getmod(a int) int {
    return (a%MOD+MOD)%MOD
}

func getans(u, w, index int) int {
    w = getmod(w+k[u])
    if ans[u][w] != 0 {
        return ans[u][w]
    }
    pos[u][w] = index
    vec[index] = u
    v := e[u][w%m[u]]

    if ans[v][getmod(w+k[v])] != 0 {
        ans[u][w] = ans[v][getmod(w+k[v])]
    } else if pos[v][getmod(w+k[v])]!=0 {
        l := pos[v][getmod(w+k[v])]; r := index
        tans := 0
        for i:=l; i<=r; i++ {
            if !vis[vec[i]] {
                tans++
                vis[vec[i]] = true
            }
        }
        for i:=l; i<=r; i++ {
            vis[vec[i]] = false
        }
        ans[u][w] = tans
    } else {
        ans[u][w] = getans(v, w, index+1)
    }
    return ans[u][w]
}

func main(){
    input = bufio.NewReader(os.Stdin)

    var n int
    fmt.Fscanf(input, "%d\n", &n)
    for i:=1; i<=n; i++ {
        fmt.Fscanf(input, "%d", &k[i])
    }
    fmt.Fscanf(input, "\n")
    for i:=1; i<=n; i++ {
        fmt.Fscanf(input, "%d\n", &m[i])
        for j:=0; j<m[i]; j++ {
            fmt.Fscanf(input, "%d", &e[i][j])
        }
        fmt.Fscanf(input, "\n")
    }
    var Q int
    fmt.Fscanf(input, "%d\n", &Q)
    for i:=1; i<=Q; i++ {
        var x, y int
        fmt.Fscanf(input, "%d%d\n", &x, &y)
        fmt.Printf("%d\n", getans(x, y, 1))
    }
}

package main
import "fmt"
import "os"
import "bufio"

var input *bufio.Reader

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
}

const MOD = 2520
const N = 1005

var m, k [N]int
var e [N][12]int
var ans [N][MOD]int
var pos [N][MOD]int
var vec [N*MOD]int
var vis [N]bool

func getmod(a int) int {
    return (a%MOD+MOD)%MOD
}

func getans(u, w, index int) int {
    w = getmod(w+k[u])
    if ans[u][w] != 0 {
        return ans[u][w]
    }
    pos[u][w] = index
    vec[index] = u
    v := e[u][w%m[u]]

    if ans[v][getmod(w+k[v])] != 0 {
        ans[u][w] = ans[v][getmod(w+k[v])]
    } else if pos[v][getmod(w+k[v])]!=0 {
        l := pos[v][getmod(w+k[v])]; r := index
        tans := 0
        for i:=l; i<=r; i++ {
            if !vis[vec[i]] {
                tans++
                vis[vec[i]] = true
            }
        }
        for i:=l; i<=r; i++ {
            vis[vec[i]] = false
        }
        ans[u][w] = tans
    } else {
        ans[u][w] = getans(v, w, index+1)
    }
    return ans[u][w]
}

func main(){
    input = bufio.NewReader(os.Stdin)

    var n int
    fmt.Fscanf(input, "%d\n", &n)
    for i:=1; i<=n; i++ {
        fmt.Fscanf(input, "%d", &k[i])
    }
    fmt.Fscanf(input, "\n")
    for i:=1; i<=n; i++ {
        fmt.Fscanf(input, "%d\n", &m[i])
        for j:=0; j<m[i]; j++ {
            fmt.Fscanf(input, "%d", &e[i][j])
        }
        fmt.Fscanf(input, "\n")
    }
    var Q int
    fmt.Fscanf(input, "%d\n", &Q)
    for i:=1; i<=Q; i++ {
        var x, y int
        fmt.Fscanf(input, "%d%d\n", &x, &y)
        //fmt.Printf("%d\n", getans(x, y, 1))
        fmt.Println(getans(x, y, 1))
    }
}


You are given an undirected unweighted connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Your task is to choose at most $$$\lfloor\frac{n}{2}\rfloor$$$ vertices in this graph so each unchosen vertex is adjacent (in other words, connected by an edge) to at least one of chosen vertices.It is guaranteed that the answer exists. If there are multiple answers, you can print any.You will be given multiple independent queries to answer.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^5$$$) — the number of queries. Then $$$t$$$ queries follow. The first line of each query contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$n - 1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$) — the number of vertices and the number of edges, respectively. The following $$$m$$$ lines denote edges: edge $$$i$$$ is represented by a pair of integers $$$v_i$$$, $$$u_i$$$ ($$$1 \le v_i, u_i \le n$$$, $$$u_i \ne v_i$$$), which are the indices of vertices connected by the edge. There are no self-loops or multiple edges in the given graph, i. e. for each pair ($$$v_i, u_i$$$) there are no other pairs ($$$v_i, u_i$$$) or ($$$u_i, v_i$$$) in the list of edges, and for each pair ($$$v_i, u_i$$$) the condition $$$v_i \ne u_i$$$ is satisfied. It is guaranteed that the given graph is connected. It is guaranteed that $$$\sum m \le 2 \cdot 10^5$$$ over all queries.

For each query print two lines. In the first line print $$$k$$$ ($$$1 \le \lfloor\frac{n}{2}\rfloor$$$) — the number of chosen vertices. In the second line print $$$k$$$ distinct integers $$$c_1, c_2, \dots, c_k$$$ in any order, where $$$c_i$$$ is the index of the $$$i$$$-th chosen vertex. It is guaranteed that the answer exists. If there are multiple answers, you can print any.

NoteIn the first query any vertex or any pair of vertices will suffice.  Note that you don't have to minimize the number of chosen vertices. In the second query two vertices can be enough (vertices $$$2$$$ and $$$4$$$) but three is also ok.

package main

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

func main() {

	var read = bufio.NewReader(os.Stdin)
	var write = bufio.NewWriter(os.Stdout)
	var tn int
	fmt.Fscan(read, &tn)
	for t := 0; t < tn; t++ {
		//тесты
		var n, m int
		var n1, n2 int
		fmt.Fscan(read, &n, &m)
		var a = make([]int, n)
		for i := 0; i < m; i++ {
			var v, u int
			fmt.Fscan(read, &v, &u)
			v = v - 1
			u = u - 1
			if a[v] == 0 && a[u] == 0 {
				a[v] = 1
				a[u] = 2
				n1 = n1 + 1
				n2 = n2 + 1
			} else if a[v] == 0 && a[u] == 1 {
				a[v] = 2
				n2 = n2 + 1
			} else if a[v] == 0 && a[u] == 2 {
				a[v] = 1
				n1 = n1 + 1
			} else if a[v] == 1 && a[u] == 0 {
				a[u] = 2
				n2 = n2 + 1
			} else if a[v] == 2 && a[u] == 0 {
				a[u] = 1
				n1 = n1 + 1
			}
		}
		var r = n1
		var tr = 1
		if n2 < r {
			r = n2
			tr = 2
		}
		fmt.Fprintln(write, r)
		for i := 0; i < n; i++ {
			if a[i] == tr {
				fmt.Fprint(write, (i + 1), " ")
			}
		}
		fmt.Fprintln(write, "")

	}

	write.Flush()

}

package main

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

type colorer struct {
	black []string
	white []string
}

func NewColorer() *colorer {
	return &colorer{
		black: []string{},
		white: []string{},
	}
}

func (c *colorer) Color(s string, color bool) {
	if color {
		c.black = append(c.black, s)
	} else {
		c.white = append(c.white, s)
	}
}

func (c *colorer) GetShorter() []string {
	if len(c.black) <= len(c.white) {
		return c.black
	}
	return c.white
}

func sol(al map[string]map[string]bool) []string {
	visited := map[string]bool{}
	c := NewColorer()
	color := true
	for v, _ := range al {
		if visited[v] {
			continue
		}
		toVisit := map[string]bool{v: true}
		for len(toVisit) > 0 {
			toVisitNext := map[string]bool{}
			for visiting, _ := range toVisit {
				if visited[visiting] {
					continue
				}
				visited[visiting] = true
				c.Color(visiting, color)
				for visitNext, _ := range al[visiting] {
					if !visited[visitNext] {
						toVisitNext[visitNext] = true
					}
				}
			}
			toVisit = toVisitNext
			color = !color
		}
	}
	return c.GetShorter()
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

type colorer struct {
	black []string
	white []string
}

func NewColorer() *colorer {
	return &colorer{
		black: []string{},
		white: []string{},
	}
}

func (c *colorer) Color(s string, color bool) {
	if color {
		c.black = append(c.black, s)
	} else {
		c.white = append(c.white, s)
	}
}

func (c *colorer) GetShorter() []string {
	if len(c.black) <= len(c.white) {
		return c.black
	}
	return c.white
}

func sol(al map[string]map[string]bool) []string {
	visited := map[string]bool{}
	ret := []string{}
	for v, _ := range al {
		if visited[v] {
			continue
		}
		c := NewColorer()
		color := true
		toVisit := map[string]bool{v: true}
		for len(toVisit) > 0 {
			toVisitNext := map[string]bool{}
			for visiting, _ := range toVisit {
				if visited[visiting] {
					continue
				}
				visited[visiting] = true
				c.Color(visiting, color)
				for visitNext, _ := range al[visiting] {
					if !visited[visitNext] {
						toVisitNext[visitNext] = true
					}
				}
			}
			toVisit = toVisitNext
			color = !color
		}
		ret = append(ret, c.GetShorter()...)
	}
	return ret
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

type colorer struct {
	isBlack bool
	black   []string
	white   []string
}

func NewColorer() *colorer {
	return &colorer{
		isBlack: true,
		black:   []string{},
		white:   []string{},
	}
}

func (c *colorer) Color(s string) {
	if c.isBlack {
		c.black = append(c.black, s)
	} else {
		c.white = append(c.white, s)
	}
	c.isBlack = !c.isBlack
}

func (c *colorer) GetShorter() []string {
	if len(c.black) <= len(c.white) {
		return c.black
	}
	return c.white
}

func sol(al map[string]map[string]bool) []string {
	visited := map[string]bool{}
	ret := []string{}
	for k, _ := range al {
		if visited[k] {
			continue
		}
		c := NewColorer()
		toVisit := []string{k}
		for len(toVisit) > 0 {
			toVisitNext := []string{}
			for _, visiting := range toVisit {
				if visited[visiting] {
					continue
				}
				visited[visiting] = true
				c.Color(visiting)
				for visitNext, _ := range al[visiting] {
					if !visited[visitNext] {
						toVisitNext = append(toVisitNext, visitNext)
					}
				}
			}
			toVisit = toVisitNext
		}
		ret = append(ret, c.GetShorter()...)
	}
	return ret
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

type colorer struct {
	isBlack bool
	black   []string
	white   []string
}

func NewColorer() *colorer {
	return &colorer{
		isBlack: true,
		black:   []string{},
		white:   []string{},
	}
}

func (c *colorer) Color(s string) {
	if c.isBlack {
		c.black = append(c.black, s)
	} else {
		c.white = append(c.white, s)
	}
	c.isBlack = !c.isBlack
}

func (c *colorer) GetShorter() []string {
	if len(c.black) <= len(c.white) {
		return c.black
	}
	return c.white
}

func sol(al map[string]map[string]bool) []string {
	visited := map[string]bool{}
	ret := []string{}
	for v, _ := range al {
		if visited[v] {
			continue
		}
		c := NewColorer()
		toVisit := map[string]bool{v: true}
		for len(toVisit) > 0 {
			toVisitNext := map[string]bool{}
			for visiting, _ := range toVisit {
				if visited[visiting] {
					continue
				}
				visited[visiting] = true
				c.Color(visiting)
				for visitNext, _ := range al[visiting] {
					if !visited[visitNext] {
						toVisitNext[visitNext] = true
					}
				}
			}
			toVisit = toVisitNext
		}
		ret = append(ret, c.GetShorter()...)
	}
	return ret
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

func sol(al map[string]map[string]bool) []string {
	l := []string{}
	r := []string{}
	b := true
	visited := map[string]bool{}
	toVisit := []string{}
	for k, _ := range al {
		toVisit = append(toVisit, k)
	}
	for len(toVisit) > 0 {
		toVisitNext := []string{}
		for _, visiting := range toVisit {
			if visited[visiting] {
				continue
			}
			visited[visiting] = true
			if b {
				l = append(l, visiting)
			} else {
				r = append(r, visiting)
			}
			b = !b
			for visitNext, _ := range al[visiting] {
				if !visited[visitNext] {
					toVisitNext = append(toVisitNext, visitNext)
				}
			}
		}
		toVisit = toVisitNext
	}

	if len(l) <= len(r) {
		return l
	}
	return r
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

type twoSlice struct {
	switcher bool
	slice1   []string
	slice2   []string
}

func NewTwoSlice() *twoSlice {
	return &twoSlice{
		switcher: true,
		slice1:   []string{},
		slice2:   []string{},
	}
}

func (ts *twoSlice) Add(s string) {
	if ts.switcher {
		ts.slice1 = append(ts.slice1, s)
	} else {
		ts.slice2 = append(ts.slice2, s)
	}
	ts.switcher = !ts.switcher
}

func (ts *twoSlice) GetRes() []string {
	if len(ts.slice1) <= len(ts.slice2) {
		return ts.slice1
	}
	return ts.slice2
}

func sol(al map[string]map[string]bool) []string {
	ts := NewTwoSlice()
	visited := map[string]bool{}
	toVisit := []string{}
	for k, _ := range al {
		toVisit = append(toVisit, k)
	}
	for len(toVisit) > 0 {
		toVisitNext := []string{}
		for _, visiting := range toVisit {
			if visited[visiting] {
				continue
			}
			visited[visiting] = true
			ts.Add(visiting)
			for visitNext, _ := range al[visiting] {
				if !visited[visitNext] {
					toVisitNext = append(toVisitNext, visitNext)
				}
			}
		}
		toVisit = toVisitNext
	}
	return ts.GetRes()
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

func sol(al map[string]map[string]bool) []string {
	b := true
	l := []string{}
	r := []string{}
	visited := map[string]bool{}
	for k, _ := range al {
		if visited[k] {
			continue
		}
		toVisit := []string{k}
		for len(toVisit) > 0 {
			tmp := []string{}
			for _, visiting := range toVisit {
				if visited[visiting] {
					continue
				}
				visited[visiting] = true
				if b {
					l = append(l, visiting)
				} else {
					r = append(r, visiting)
				}
				b = !b
				for v2, _ := range al[visiting] {
					if !visited[v2] {
						tmp = append(tmp, v2)
					}
				}
			}
			toVisit = tmp
		}
	}

	if len(l) <= len(r) {
		return l
	}
	return r
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}

package main

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

func sol(al map[string]map[string]bool) []string {
	l := []string{}
	r := []string{}
	visited := map[string]bool{}
	for k, _ := range al {
		if visited[k] {
			continue
		}
		b := true
		toVisit := []string{k}
		for len(toVisit) > 0 {
			tmp := []string{}
			for _, visiting := range toVisit {
				if visited[visiting] {
					continue
				}
				visited[visiting] = true
				if b {
					l = append(l, visiting)
				} else {
					r = append(r, visiting)
				}
				b = !b
				for v2, _ := range al[visiting] {
					if !visited[v2] {
						tmp = append(tmp, v2)
					}
				}
			}
			toVisit = tmp
		}
	}

	if len(l) <= len(r) {
		return l
	}
	return r
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		al := map[string]map[string]bool{}
		scanner.Scan()
		nm := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(nm[1])
		for j := 0; j < m; j++ {
			scanner.Scan()
			e := strings.Split(scanner.Text(), " ")
			l, r := e[0], e[1]
			if _, ok := al[l]; !ok {
				al[l] = map[string]bool{}
			}
			if _, ok := al[r]; !ok {
				al[r] = map[string]bool{}
			}
			al[l][r] = true
			al[r][l] = true
		}
		res := sol(al)
		fmt.Println(len(res))
		fmt.Println(strings.Join(res, " "))
	}
}


Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and m edges. One of the vertices contains a chip. Initially the chip is located at vertex s. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't move the chip loses. If the game lasts for 106 turns the draw is announced.Vasya was performing big laboratory work in "Spelling and parts of speech" at night before the game, so he fell asleep at the very beginning of the game. Petya decided to take the advantage of this situation and make both Petya's and Vasya's moves.Your task is to help Petya find out if he can win the game or at least draw a tie.

The first line of input contain two integers n and m — the number of vertices and the number of edges in the graph (2 ≤ n ≤ 105, 0 ≤ m ≤ 2·105). The next n lines contain the information about edges of the graph. i-th line (1 ≤ i ≤ n) contains nonnegative integer ci — number of vertices such that there is an edge from i to these vertices and ci distinct integers ai, j — indices of these vertices (1 ≤ ai, j ≤ n, ai, j ≠ i). It is guaranteed that the total sum of ci equals to m. The next line contains index of vertex s — the initial position of the chip (1 ≤ s ≤ n).

If Petya can win print «Win» in the first line. In the next line print numbers v1, v2, ..., vk (1 ≤ k ≤ 106) — the sequence of vertices Petya should visit for the winning. Vertex v1 should coincide with s. For i = 1... k - 1 there should be an edge from vi to vi + 1 in the graph. There must be no possible move from vertex vk. The sequence should be such that Petya wins the game. If Petya can't win but can draw a tie, print «Draw» in the only line. Otherwise print «Lose».

NoteIn the first example the graph is the following:  Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins.In the second example the graph is the following:  Initially the chip is located at vertex 2. The only possible Petya's move is to go to vertex 1. After that he has to go to 3 for Vasya. Now it's Petya's turn but he has no possible move, so Petya loses.In the third example the graph is the following:  Petya can't win, but he can move along the cycle, so the players will draw a tie.

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func readInt() int {
	var n int
	fmt.Fscan(in, &n)
	return n
}

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

	n := readInt()
	_ = readInt()
	edges := make([][]int, n)
	for i := range edges {
		for c := readInt(); c > 0; c-- {
			edges[i] = append(edges[i], readInt()-1)
		}
	}
	s := readInt() - 1
	type state struct {
		petya bool
		v     int
	}
	q := []state{{true, s}}
	parent := map[state]int{q[0]: 0}
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		if len(edges[v.v]) == 0 && !v.petya {
			var ans []int
			for v != (state{true, s}) {
				ans = append(ans, v.v)
				v.v = parent[v]
				v.petya = !v.petya
			}
			ans = append(ans, s)
			fmt.Fprintln(out, "Win")
			for i := len(ans) - 1; i >= 0; i-- {
				fmt.Fprint(out, ans[i]+1)
				if i == 0 {
					fmt.Fprintln(out)
				} else {
					fmt.Fprint(out, " ")
				}
			}
			return
		}
		for _, u := range edges[v.v] {
			uu := state{!v.petya, u}
			if _, has := parent[uu]; has {
				continue
			}
			parent[uu] = v.v
			q = append(q, uu)
		}
	}

	inStack := make([]bool, n)
	visited := make([]bool, n)
	var dfs func(int)
	dfs = func(v int) {
		visited[v] = true
		inStack[v] = true
		for _, u := range edges[v] {
			if !visited[u] {
				dfs(u)
			} else if inStack[u] {
				fmt.Println("Draw")
				os.Exit(0)
			}
		}
		inStack[v] = false
	}
	dfs(s)

	fmt.Println("Lose")
}

package main

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

var n, m int
var start int
var graph [][]int
var used [][2]bool
var nodes map[int]bool
var path []int

func dfs(v, t int) {
	path = append(path, v)
	if len(graph[v]) == 0 && t == 1 {
		fmt.Println("Win")
		for _, v := range path {
			fmt.Printf("%d ", v+1)
		}
		os.Exit(0)
	}
	used[v][t] = true
	for _, vnext := range graph[v] {
		if !used[vnext][1 - t] {
			dfs(vnext, 1 - t)
		}
	}
	path = path[:len(path) - 1]
}

func cycle(v int) {
	nodes[v] = true
	for _, vnext := range graph[v] {
		if nodes[vnext] {
			fmt.Println("Draw")
			os.Exit(0)
		}
		cycle(vnext)
	}
	delete(nodes, v)
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &m)
	graph = make([][]int, n)
	for i := 0; i < n; i++ {
		var c, tmp int
		fmt.Fscanf(inBuf, "%d", &c)
		for j := 0; j < c; j++ {
			fmt.Fscanf(inBuf, "%d", &tmp)
			graph[i] = append(graph[i], tmp - 1)
		}
		fmt.Fscanf(inBuf, "\n")
	}
	fmt.Fscanf(inBuf, "%d", &start)
	start--
	used = make([][2]bool, n)
	path = make([]int, 0)
	dfs(start, 0)
	nodes = make(map[int]bool)
	cycle(start)
	fmt.Println("Lose")
}

package main

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

var scanner *bufio.Scanner
var t [1e6 + 1]bool

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

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

func readInt() int64 {
	scanner.Scan()
	val, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return val
}

func dfs(x int64, b int64, g [][]int64, marque [][]int64) bool {
	// fmt.Println(x)
	if marque[x][b] == -1 {
		return false
	} else if marque[x][b] > 0 {
		return true
	}

	if len(g[x]) == 0 && b == 1 {
		marque[x][b] = x
		return true
	}

	marque[x][b] = -1
	for _, y := range g[x] {
		if dfs(y, 1-b, g, marque) {
			marque[x][b] = y
			return true
		}
	}

	return false
}

func check_cycle(x int64, g [][]int64, marque_cycle []int64) bool {
	if marque_cycle[x] == 1 {
		return false
	} else if marque_cycle[x] == 2 {
		return true
	}

	marque_cycle[x] = 2
	for _, y := range g[x] {
		if check_cycle(y, g, marque_cycle) {
			return true
		}
	}
	marque_cycle[x] = 1
	return false
}

func main() {
	init_scanner()
	n, _ := readInt(), readInt()
	g, marque := make([][]int64, n+1), make([][]int64, n+1)
	for i := 1; int64(i) <= n; i++ {
		l := readInt()
		g[i] = make([]int64, l)
		marque[i] = make([]int64, 2)
		for j := 0; int64(j) < l; j++ {
			g[i][j] = readInt()
		}
	}
	s := readInt()
	if dfs(s, 0, g, marque) {
		fmt.Println("Win")
		b := 0
		for len(g[s]) > 0 {
			fmt.Printf("%d ", s)
			s = marque[s][b]
			b = 1 - b
		}
		fmt.Println(s)
	} else if check_cycle(s, g, make([]int64, n+1)) {
		fmt.Println("Draw")
	} else {
		fmt.Println("Lose")
	}
}

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

type Node struct {
	Adj []int
	// The id of the previous node if this node is reachable by odd number of edges.
	// 0 means unknown.
	OddPrev  int
	EvenPrev int
}

const Unknown = 0

func main() {
	r := RapidReader()
	var n, m int
	for {
		if _, err := fmt.Fscan(r, &n, &m); err != nil {
			break
		}
		g := make([]Node, n+1)
		for x := 1; x <= n; x++ {
			var cnt int
			fmt.Fscan(r, &cnt)
			for j := 0; j < cnt; j++ {
				var y int
				fmt.Fscan(r, &y)
				g[x].Adj = append(g[x].Adj, y)
			}
		}
		var s int
		fmt.Fscan(r, &s)

		traversal(g, n, s)

		zeroOut := 0
		winEnd := Unknown
		for x := 1; x <= n; x++ {
			if len(g[x].Adj) == 0 {
				zeroOut++
				if g[x].OddPrev != Unknown {
					winEnd = x
					break
				}
			}
		}

		if winEnd != Unknown {
			fmt.Println("Win")
			var ans []int
			var walk func(int, bool)
			walk = func(x int, odd bool) {
				ans = append(ans, x)
				if odd && x != g[x].OddPrev {
					walk(g[x].OddPrev, false)
				} else if !odd && x != g[x].EvenPrev {
					walk(g[x].EvenPrev, true)
				}
			}
			walk(winEnd, true)
			for i := len(ans) - 1; i >= 0; i-- {
				fmt.Printf("%d ", ans[i])
			}
			fmt.Println()
			continue
		}

		if hasCircle(g, n, s) {
			fmt.Println("Draw")
		} else {
			fmt.Println("Lose")
		}
	}
}

func hasCircle(g []Node, n, s int) bool {
	rank := make([]int, n+1, n+1)
	yes := false
	var dfs func(int, int)
	dfs = func(x, r int) {
		rank[x] = r
		for _, y := range g[x].Adj {
			if rank[y] != 0 && rank[y] < r {
				yes = true
			}
			if rank[y] == 0 {
				dfs(y, r+1)
			}
		}
		rank[x] = 1000 * 1000
	}
	dfs(s, 1)
	return yes
}

func traversal(g []Node, n, s int) {
	g[s].EvenPrev = s
	stack := []int{s}

	for len(stack) > 0 {
		x := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		for _, y := range g[x].Adj {
			if g[x].OddPrev != Unknown && g[y].EvenPrev == Unknown {
				g[y].EvenPrev = x
				stack = append(stack, y)
			}
			if g[x].EvenPrev != Unknown && g[y].OddPrev == Unknown {
				g[y].OddPrev = x
				stack = append(stack, y)
			}
		}
	}
}

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func readInt() int {
	var n int
	fmt.Fscan(in, &n)
	return n
}

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

	n := readInt()
	_ = readInt()
	edges := make([][]int, n)
	for i := range edges {
		for c := readInt(); c > 0; c-- {
			edges[i] = append(edges[i], readInt()-1)
		}
	}
	s := readInt() - 1
	type state struct {
		petya bool
		v     int
	}
	q := []state{{true, s}}
	parent := map[state]int{q[0]: 0}
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		if len(edges[v.v]) == 0 && !v.petya {
			var ans []int
			for v != (state{true, s}) {
				ans = append(ans, v.v)
				v.v = parent[v]
				v.petya = !v.petya
			}
			ans = append(ans, s)
			fmt.Fprintln(out, "Win")
			for i := len(ans) - 1; i >= 0; i-- {
				fmt.Fprint(out, ans[i]+1)
				if i == 0 {
					fmt.Fprintln(out)
				} else {
					fmt.Fprint(out, " ")
				}
			}
			return
		}
		for _, u := range edges[v.v] {
			uu := state{!v.petya, u}
			if _, has := parent[uu]; has {
				continue
			}
			parent[uu] = v.v
			q = append(q, uu)
		}
	}

	inStack := make([]bool, n)
	visited := make([]bool, n)
	var dfs func(int)
	dfs = func(v int) {
		visited[v] = true
		inStack[v] = true
		for _, u := range edges[v] {
			if !visited[u] {
				dfs(v)
			} else if inStack[u] {
				fmt.Println("Draw")
				os.Exit(0)
			}
		}
		inStack[v] = false
	}

	fmt.Println("Lose")
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

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

	n := readInt()
	_ = readInt()
	edges := make([][]int, n)
	for i := range edges {
		for c := readInt(); c > 0; c-- {
			edges[i] = append(edges[i], readInt()-1)
		}
	}
	s := readInt() - 1
	type state struct {
		petya bool
		v     int
	}
	q := []state{{true, s}}
	parent := map[state]int{q[0]: 0}
	draw := false
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		if len(edges[v.v]) == 0 && !v.petya {
			var ans []int
			for v != (state{true, s}) {
				ans = append(ans, v.v)
				v.v = parent[v]
				v.petya = !v.petya
			}
			ans = append(ans, s)
			fmt.Fprintln(out, "Win")
			for i := len(ans) - 1; i >= 0; i-- {
				fmt.Fprint(out, ans[i]+1)
				if i == 0 {
					fmt.Fprintln(out)
				} else {
					fmt.Fprint(out, " ")
				}
			}
			return
		}
		for _, u := range edges[v.v] {
			uu := state{!v.petya, u}
			if _, has := parent[uu]; has {
				draw = true
				continue
			}
			parent[uu] = v.v
			q = append(q, uu)
		}
	}
	if draw {
		fmt.Println("Draw")
	} else {
		fmt.Println("Lose")
	}
}

package main

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

var n, m int
var start int
var graph [][]int
var used [][2]bool
var marked []bool
var path []int

func dfs(v, t int) {
	path = append(path, v)
	if len(graph[v]) == 0 && t == 1 {
		fmt.Println("Win")
		for _, v := range path {
			fmt.Printf("%d ", v+1)
		}
		os.Exit(0)
	}
	used[v][t] = true
	for _, vnext := range graph[v] {
		if !used[vnext][1 - t] {
			dfs(vnext, 1 - t)
		}
	}
	path = path[:len(path) - 1]
}

func cycle(v int) {
	marked[v] = true
	for _, vnext := range graph[v] {
		if marked[vnext] {
			fmt.Println("Draw")
			os.Exit(0)
		}
		cycle(vnext)
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &m)
	graph = make([][]int, n)
	for i := 0; i < n; i++ {
		var c, tmp int
		fmt.Fscanf(inBuf, "%d", &c)
		for j := 0; j < c; j++ {
			fmt.Fscanf(inBuf, "%d", &tmp)
			graph[i] = append(graph[i], tmp - 1)
		}
		fmt.Fscanf(inBuf, "\n")
	}
	fmt.Fscanf(inBuf, "%d", &start)
	start--
	used = make([][2]bool, n)
	path = make([]int, 0)
	dfs(start, 0)
	marked = make([]bool, n)
	cycle(start)
	fmt.Println("Lose")
}

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

type Node struct {
	Adj []int
	// The id of the previous node if this node is reachable by odd number of edges.
	// 0 means unknown.
	OddPrev  int
	EvenPrev int
}

const Unknown = 0

func main() {
	r := RapidReader()
	var n, m int
	for {
		if _, err := fmt.Fscan(r, &n, &m); err != nil {
			break
		}
		g := make([]Node, n+1)
		for x := 1; x <= n; x++ {
			var cnt int
			fmt.Fscan(r, &cnt)
			for j := 0; j < cnt; j++ {
				var y int
				fmt.Fscan(r, &y)
				g[x].Adj = append(g[x].Adj, y)
			}
		}
		var s int
		fmt.Fscan(r, &s)

		traversal(g, n, s)

		zeroOut := 0
		winEnd := Unknown
		for x := 1; x <= n; x++ {
			if len(g[x].Adj) == 0 {
				zeroOut++
				if g[x].OddPrev != Unknown {
					winEnd = x
					break
				}
			}
		}

		if winEnd != Unknown {
			fmt.Println("Win")
			var ans []int
			var walk func(int, bool)
			walk = func(x int, odd bool) {
				ans = append(ans, x)
				if odd && x != g[x].OddPrev {
					walk(g[x].OddPrev, false)
				} else if !odd && x != g[x].EvenPrev {
					walk(g[x].EvenPrev, true)
				}
			}
			walk(winEnd, true)
			for i := len(ans) - 1; i >= 0; i-- {
				fmt.Printf("%d ", ans[i])
			}
			fmt.Println()
			continue
		}

		if hasCircle(g, n, s) {
			fmt.Println("Draw")
		} else {
			fmt.Println("Lose")
		}
	}
}

func hasCircle(g []Node, n, s int) bool {
	rank := make([]int, n+1, n+1)
	yes := false
	var dfs func(int, int)
	dfs = func(x, r int) {
		rank[x] = r
		for _, y := range g[x].Adj {
			if rank[y] != 0 && rank[y] < r {
				yes = true
			}
			if rank[y] == 0 {
				dfs(y, r+1)
			}
		}
	}
	dfs(s, 1)
	return yes
}

func traversal(g []Node, n, s int) {
	g[s].EvenPrev = s
	stack := []int{s}

	for len(stack) > 0 {
		x := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		for _, y := range g[x].Adj {
			if g[x].OddPrev != Unknown && g[y].EvenPrev == Unknown {
				g[y].EvenPrev = x
				stack = append(stack, y)
			}
			if g[x].EvenPrev != Unknown && g[y].OddPrev == Unknown {
				g[y].OddPrev = x
				stack = append(stack, y)
			}
		}
	}
}

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

type Node struct {
	Adj []int
	// The id of the previous node if this node is reachable by odd number of edges.
	// 0 means unknown.
	OddPrev  int
	EvenPrev int
}

const Unknown = 0

func main() {
	r := RapidReader()
	var n, m int
	for {
		if _, err := fmt.Fscan(r, &n, &m); err != nil {
			break
		}
		g := make([]Node, n+1)
		for x := 1; x <= n; x++ {
			var cnt int
			fmt.Fscan(r, &cnt)
			for j := 0; j < cnt; j++ {
				var y int
				fmt.Fscan(r, &y)
				g[x].Adj = append(g[x].Adj, y)
			}
		}
		var s int
		fmt.Fscan(r, &s)

		traversal(g, n, s)

		zeroOut := 0
		winEnd := Unknown
		for x := 1; x <= n; x++ {
			if len(g[x].Adj) == 0 {
				zeroOut++
				if g[x].OddPrev != Unknown {
					winEnd = x
					break
				}
			}
		}

		if winEnd != Unknown {
			fmt.Println("Win")
			var ans []int
			var walk func(x int, odd bool)
			walk = func(x int, odd bool) {
				ans = append(ans, x)
				if odd && x != g[x].OddPrev {
					walk(g[x].OddPrev, false)
				} else if !odd && x != g[x].EvenPrev {
					walk(g[x].EvenPrev, true)
				}
			}
			walk(winEnd, true)
			for i := len(ans) - 1; i >= 0; i-- {
				fmt.Printf("%d ", ans[i])
			}
			fmt.Println()
			continue
		}
		if zeroOut > 0 {
			fmt.Println("Lose")
			continue
		}
		fmt.Println("Draw")
	}
}

func traversal(g []Node, n int, s int) {
	g[s].EvenPrev = s
	var stack []int
	stack = append(stack, s)

	for len(stack) > 0 {
		x := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		for _, y := range g[x].Adj {
			if g[x].OddPrev != Unknown && g[y].EvenPrev == Unknown {
				g[y].EvenPrev = x
				stack = append(stack, y)
			}
			if g[x].EvenPrev != Unknown && g[y].OddPrev == Unknown {
				g[y].OddPrev = x
				stack = append(stack, y)
			}
		}
	}
}

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}


You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. It is guaranteed that $$$a$$$ is sorted from small to large.For each operation, we generate a new array $$$b_i=a_{i+1}-a_{i}$$$ for $$$1 \le i < n$$$. Then we sort $$$b$$$ from small to large, replace $$$a$$$ with $$$b$$$, and decrease $$$n$$$ by $$$1$$$.After performing $$$n-1$$$ operations, $$$n$$$ becomes $$$1$$$. You need to output the only integer in array $$$a$$$ (that is to say, you need to output $$$a_1$$$).

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2\le n\le 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2.5\cdot 10^5$$$, and the sum of $$$a_n$$$ over all test cases does not exceed $$$5\cdot 10^5$$$.

For each test case, output the answer on a new line.

NoteTo simplify the notes, let $$$\operatorname{sort}(a)$$$ denote the array you get by sorting $$$a$$$ from small to large.In the first test case, $$$a=[1,10,100]$$$ at first. After the first operation, $$$a=\operatorname{sort}([10-1,100-10])=[9,90]$$$. After the second operation, $$$a=\operatorname{sort}([90-9])=[81]$$$.In the second test case, $$$a=[4,8,9,13]$$$ at first. After the first operation, $$$a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$$$. After the second operation, $$$a=\operatorname{sort}([4-1,4-4])=[0,3]$$$. After the last operation, $$$a=\operatorname{sort}([3-0])=[3]$$$.

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(A []int) int {
	// a0, a1, a2
	// a1 - a0, a2 - a1  => sum() = an - a0
	n := len(A)

	for i := n - 1; i >= 1; i-- {
		pre := A[i]
		ok := false
		for j := i - 1; j >= 0; j-- {
			ok = A[j] == 0
			A[j] = pre - A[j]
			pre -= A[j]
			if ok {
				sort.Ints(A[j : i+1])
				break
			}
		}
		if !ok {
			sort.Ints(A[:i])
		}
	}

	return A[0]
}

package main

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

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

func CF1708D(in io.Reader, out io.Writer) {
	_r := bufio.NewReader(in)
	_w := bufio.NewWriter(out)
	defer _w.Flush()
	var T int64
	fmt.Fscan(_r, &T)
	for ; T > 0; T-- {
		var n, cnt int64
		fmt.Fscan(_r, &n)
		arr := make([]int, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(_r, &arr[i])
		}
		for i := int64(1); i < n; i++ {
			if len(arr) == 0 {
				break
			}
			sort.Ints(arr)
			b := make([]int, 0)
			if cnt > 0 {
				cnt--
				b = append(b, arr[0])
			}
			for j := 1; j < len(arr); j++ {
				if arr[j] == arr[j-1] {
					cnt++
				} else {
					b = append(b, arr[j]-arr[j-1])
				}
			}
			arr = b
		}
		if cnt > 0 {
			fmt.Fprintln(_w, 0)
		} else {
			fmt.Fprintln(_w, arr[0])
		}
	}

}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	notZero := false
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		if arr[i] != 0 {
			notZero = true
		}
	}
	if !notZero {
		fmt.Fprintln(out, 0)
		return
	}
	curZero := 0
	for n > 1 {
		for i := curZero; i < n-1; i++ {
			arr[i] = arr[i+1] - arr[i]
		}
		sliced := arr[curZero : n-1]
		sort.Slice(sliced, func(i, j int) bool {
			return sliced[i] < sliced[j]
		})
		n--
		if curZero > 0 && arr[curZero] != 0 {
			curZero--
		}
		for curZero < n-1 && arr[curZero+1] == 0 {
			curZero++
		}
		if curZero == n-1 && arr[curZero] == 0 {
			fmt.Fprintln(out, 0)
			return
		}
	}
	fmt.Fprintln(out, arr[0])
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

func Println(a ...interface{}) { _, _ = fmt.Fprintln(writer, a...) }

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

	T := ReadInt()
	for t := 0; t < T; t++ {
		Solve()
	}
}

func Solve() {
	n := ReadInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ReadInt()
	}
	for len(a) > 1 {
		l := 0
		for i := len(a) - 1; i > 0; i-- {
			if a[i] == 0 {
				l = i
				break
			}
			a[i] = a[i] - a[i-1]
		}
		a = a[1:]
		sort.Slice(a[l:], func(i, j int) bool {
			return a[l+i] < a[l+j]
		})
	}
	Println(a[0])
}

func ReadInt() (value int) {
	_, _ = fmt.Fscan(reader, &value)
	return
}

package main

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

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

func CF1708D(in io.Reader, out io.Writer) {
	_r := bufio.NewReader(in)
	_w := bufio.NewWriter(out)
	defer _w.Flush()
	var T int64
	fmt.Fscan(_r, &T)
	for ; T > 0; T-- {
		var n, cnt int64
		fmt.Fscan(_r, &n)
		arr := make([]int, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(_r, &arr[i])
			if arr[i] == 0 {
				cnt++
			}
		}
		for len(arr) > 1 {
			b := make([]int, 0)
			if cnt > 0 {
				b = append(b, arr[0])
				cnt--
			}
			for i := 1; i < len(arr); i++ {
				x := arr[i] - arr[i-1]
				if x > 0 {
					b = append(b, x)
				} else {
					cnt++
				}
			}
			sort.Ints(b)
			arr = b
		}
		if len(arr) > 0 {
			fmt.Fprintln(_w, arr[0])
		} else {
			fmt.Fprintln(_w, 0)
		}
	}

}

package main

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

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

func CF1708D(in io.Reader, out io.Writer) {
	_r := bufio.NewReader(in)
	_w := bufio.NewWriter(out)
	defer _w.Flush()
	var T int64
	fmt.Fscan(_r, &T)
	for ; T > 0; T-- {
		var n, cnt int64
		fmt.Fscan(_r, &n)
		arr := make([]int, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(_r, &arr[i])
			if arr[i] == 0 {
				cnt++
			}
		}
		for len(arr) > 1 {
			b := make([]int, 0)
			if cnt > 0 {
				b = append(b, 0)
				cnt--
			}
			for i := 1; i < len(arr); i++ {
				x := arr[i] - arr[i-1]
				if x > 0 {
					b = append(b, x)
				} else {
					cnt++
				}
			}
			sort.Ints(b)
			arr = b
		}
		if len(arr) > 0 {
			fmt.Fprintln(_w, arr[0])
		} else {
			fmt.Fprintln(_w, 0)
		}
	}

}

package main

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

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

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

	var T int64
	fmt.Fscan(_r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(_r, &n)
		arr := make([]int, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(_r, &arr[i])
		}
		b := arr
		if b[0] == 0 && b[len(b)-1] == 0 {
			break
		}
		for len(b) > 1 {
			newB := make([]int, len(b)-1)
			for i := 1; i < len(b); i++ {
				newB[i-1] = b[i] - b[i-1]
			}
			b = newB
			sort.Ints(b)
			if b[0] == 0 && b[len(b)-1] == 0 {
				break
			}
		}
		fmt.Fprintln(_w, b[0])
	}

}

package main

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

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

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

	var T int64
	fmt.Fscan(_r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(_r, &n)
		arr := make([]int, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(_r, &arr[i])
		}
		b := arr
		for len(b) > 1 {
			newB := make([]int, len(b)-1)
			for i := 1; i < len(b); i++ {
				newB[i-1] = b[i] - b[i-1]
			}
			b = newB
			sort.Ints(b)
			i := 1
			for i < len(b) && b[i] == 0 {
				i++
			}
			b = append([]int{b[0]}, b[i:]...)
		}
		fmt.Fprintln(_w, b[0])
	}

}

package main

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

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

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

	var T int64
	fmt.Fscan(_r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(_r, &n)
		arr := make([]int, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(_r, &arr[i])
		}
		b := arr
		for len(b) > 1 {
			newB := make([]int, len(b)-1)
			for i := 1; i < len(b); i++ {
				newB[i-1] = b[i] - b[i-1]
			}
			b = newB
			sort.Ints(b)
			i := 1
			for i+1 < len(b) && b[i+1] == 0 {
				i++
			}
			b = append([]int{b[0]}, b[i:]...)
		}
		fmt.Fprintln(_w, b[0])
	}

}


Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into some other ones.This morning he has n cards with integers lined up in front of him. Each integer is not less than 1, but not greater than l. When Vasya waves his magic wand, two rightmost cards vanish from the line and a new card magically appears in their place. It contains the difference between the left and the right numbers on the two vanished cards. Vasya was very interested to know what would happen next, and so he waved with his magic wand on and on, until the table had a single card left.Suppose that Vasya originally had the following cards: 4, 1, 1, 3 (listed from left to right). Then after the first wave the line would be: 4, 1, -2, and after the second one: 4, 3, and after the third one the table would have a single card with number 1.Please note that in spite of the fact that initially all the numbers on the cards were not less than 1 and not greater than l, the numbers on the appearing cards can be anything, no restrictions are imposed on them.It is now evening. Vasya is very tired and wants to return everything back, but does not remember which cards he had in the morning. He only remembers that there were n cards, they contained integers from 1 to l, and after all magical actions he was left with a single card containing number d.Help Vasya to recover the initial set of cards with numbers.

The single line contains three space-separated integers: n (2 ≤ n ≤ 100) — the initial number of cards on the table, d (|d| ≤ 104) — the number on the card that was left on the table after all the magical actions, and l (1 ≤ l ≤ 100) — the limits for the initial integers.

If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing n integers from 1 to l. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards from left to right. If there are several suitable sets of numbers, you can print any of them.

//  URL: http://codeforces.com/contest/231/problem/B
//  PROBLEM RESTATEMENT
//      Given an array a of size N, 1 <= a[i] <= an integer l.
//      You can select last two elements repeatedly, replace with difference.
//      You do this until you only have one element left.
//      Given the final element d, recompute a possible array if possible.
//      2 <= n <= 100, d <= 10^4, 1 <= l <= 100.
//  APPROACH
//      1. If we simplified equations for n = 2, n = 3, n = 4, we get:
//         f(n)                     = a - b
//         f(n) = a - (b  - c)      = a - b + c
//         f(n) = a - (b - (c - d)) = a - b + c - d
//      2. Our approach is as follows: consider the maximum and minimum f(n)
//         that can be created for array of size n. Calculate the difference
//         `delta` between the maximum f(n) and d. Modify the elements of the
//         array by exactly delta.
//  SOLUTION
//      1. The range is:
//         max f(n) = l * ceil(n/2) - floor(n/2)
//         min f(n) = -l * floor(n/2) + ceil(n/2)
//         (To find these functions, count number of elements you can set to
//         l, and elements you can set to 1.)
//      2. If the d is outside of this range, return -1.
//      3. If d is in this range, create an array of size n with maximum
//         value (setting even-indexed elements to l, and odd-indexed elements
//         to 1). Compute the difference between this sum and d to determine
//         the delta.
//      4. Given the delta, and this maximum array, subtract from
//         even-indexed elements, and add to odd-indexed elements, until the
//         delta is 0. This can be done in O(n) time.

package main

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

func createMaxArray(n int, l int) []int {
	maxArray := make([]int, n)
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			maxArray[i] = l
		} else {
			maxArray[i] = 1
		}
	}
	return maxArray
}

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func solve(n int, delta int, l int) []int {
	maxArray := createMaxArray(n, l)
	for i := 0; i < n; i++ {
		elementDelta := min(delta, l-1)
		delta -= elementDelta
		if i%2 == 0 {
			maxArray[i] -= elementDelta
		} else {
			maxArray[i] += elementDelta
		}
	}
	return maxArray
}

func main() {
	reader, writer := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, d, l int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &d, &l)
	maxFn := l*int(math.Ceil(float64(n)/2)) - int(math.Floor(float64(n)/2))
	minFn := -l*int(math.Floor(float64(n)/2)) + int(math.Ceil(float64(n)/2))
	if d < minFn || d > maxFn {
		fmt.Fprintln(writer, -1)
		return
	}
	arr := solve(n, maxFn-d, l)
	for i := 0; i < n; i++ {
		fmt.Fprintf(writer, "%d ", arr[i])
	}
	fmt.Fprintln(writer)
}