The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively.Polycarpus has found a job, he is a system administrator. One day he came across n IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separated by dots. For example, the record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In this problem an arbitrary group of four 8-bit numbers is a correct IP address.Having worked as an administrator for some time, Polycarpus learned that if you know the IP address, you can use the subnet mask to get the address of the network that has this IP addess.The subnet mask is an IP address that has the following property: if we write this IP address as a 32 bit string, that it is representable as "11...11000..000". In other words, the subnet mask first has one or more one bits, and then one or more zero bits (overall there are 32 bits). For example, the IP address 2.0.0.0 is not a correct subnet mask as its 32-bit record looks as 00000010000000000000000000000000.To get the network address of the IP address, you need to perform the operation of the bitwise "and" of the IP address and the subnet mask. For example, if the subnet mask is 255.192.0.0, and the IP address is 192.168.1.2, then the network address equals 192.128.0.0. In the bitwise "and" the result has a bit that equals 1 if and only if both operands have corresponding bits equal to one.Now Polycarpus wants to find all networks to which his IP addresses belong. Unfortunately, Polycarpus lost subnet mask. Fortunately, Polycarpus remembers that his IP addresses belonged to exactly k distinct networks. Help Polycarpus find the subnet mask, such that his IP addresses will belong to exactly k distinct networks. If there are several such subnet masks, find the one whose bit record contains the least number of ones. If such subnet mask do not exist, say so.

The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 105) — the number of IP addresses and networks. The next n lines contain the IP addresses. It is guaranteed that all IP addresses are distinct.

In a single line print the IP address of the subnet mask in the format that is described in the statement, if the required subnet mask exists. Otherwise, print -1.

package main

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

func solve() {
	n := readInt()
	k := readInt()

	netmasks := make([]uint, 31)
	netmasks[0] = 1 << 31
	for i := 1; i < len(netmasks); i++ {
		netmasks[i] = (netmasks[i-1] >> 1) | (1 << 31)
	}

	ips := make([]uint, n)
	for i := 0; i < n; i++ {
		var a, b, c, d uint
		s := readString()
		fmt.Sscanf(s, "%d.%d.%d.%d", &a, &b, &c, &d)
		ips[i] = (a << 24) | (b << 16) | (c << 8) | (d << 0)
	}

	for _, v := range netmasks {
		if tryit(ips, v, k) {
			return
		}
	}
	fmt.Println("-1")
}

func tryit(ips []uint, mask uint, k int) bool {
	m := make(map[uint]bool)
	for _, ip := range ips {
		m[ip&mask] = true
	}
	if k == len(m) {
		fmt.Printf("%d.%d.%d.%d\n",
			(mask>>24)&0xff,
			(mask>>16)&0xff,
			(mask>>8)&0xff,
			(mask>>0)&0xff)
		return true
	}
	return false
}

var scanner *bufio.Scanner

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

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

package main

import (
    "strconv"
    "bufio"
    "os"
    "fmt"
    "strings"
)
// var file, _ = os.Open("input.txt")
// var outfile, _ = os.Create("output.txt")
// var in = bufio.NewScanner(file)
// var out = bufio.NewWriter(outfile)
var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func next() string {
    if !in.Scan() {
        panic("Scan error")
    }
    return in.Text()
}

func nextInt() int {
    ret, _ := strconv.Atoi(next())
    return ret
}

func nextFloat() float64 {
    ret, _ := strconv.ParseFloat(next(), 64)
    return ret
}

func toMask(mask uint32) string {
    var p1, p2, p3, p4 int
    p4 = int(mask & 255)
    mask = mask >> 8
    p3 = int(mask & 255)
    mask = mask >> 8
    p2 = int(mask & 255)
    mask = mask >> 8
    p1 = int(mask & 255)
    mask = mask >> 8
    return strconv.Itoa(p1) + "." + strconv.Itoa(p2) + "." + strconv.Itoa(p3) + "." + strconv.Itoa(p4)
}

func main() {
    defer out.Flush()
    in.Split(bufio.ScanWords)
    n := nextInt()
    k := nextInt()
    a := make([]uint32, n)
    for i := 0; i < n; i++ {
        s := strings.Split(next(), ".")
        for j := 0; j < 4; j++ {
            x, _ := strconv.Atoi(s[j])
            a[i] = a[i] << 8 + uint32(x)
        }
    }

    var mask uint32
    ok := false
    for i := 0; i < 31; i++ {
        mask = 0
        for j := 0; j <= i; j++ {
            mask = mask << 1 + 1
        }
        for j := i + 1; j < 32; j++ {
            mask = mask << 1
        }
        m := make(map[uint32]bool)
        for j := 0; j < n; j++ {
            m[mask & a[j]] = true
        }
        if len(m) == k {
            fmt.Fprintln(out, toMask(mask))
            ok = true
            return
        }
    }

    if !ok {
        fmt.Fprintln(out, -1)
    }
}

package main

import (
    "strconv"
    "bufio"
    "os"
    "fmt"
    "strings"
)
// var file, _ = os.Open("input.txt")
// var outfile, _ = os.Create("output.txt")
// var in = bufio.NewScanner(file)
// var out = bufio.NewWriter(outfile)
var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func next() string {
    if !in.Scan() {
        panic("Scan error")
    }
    return in.Text()
}

func nextInt() int {
    ret, _ := strconv.Atoi(next())
    return ret
}

func nextFloat() float64 {
    ret, _ := strconv.ParseFloat(next(), 64)
    return ret
}

func toMask(mask uint32) string {
    var p1, p2, p3, p4 int
    p4 = int(mask & 255)
    mask = mask >> 8
    p3 = int(mask & 255)
    mask = mask >> 8
    p2 = int(mask & 255)
    mask = mask >> 8
    p1 = int(mask & 255)
    mask = mask >> 8
    return strconv.Itoa(p1) + "." + strconv.Itoa(p2) + "." + strconv.Itoa(p3) + "." + strconv.Itoa(p4)
}

func main() {
    defer out.Flush()
    in.Split(bufio.ScanWords)
    n := nextInt()
    k := nextInt()
    a := make([]uint32, n)
    for i := 0; i < n; i++ {
        s := strings.Split(next(), ".")
        for j := 0; j < 4; j++ {
            x, _ := strconv.Atoi(s[j])
            a[i] = a[i] << 8 + uint32(x)
        }
    }

    var mask uint32
    ok := false
    for i := 0; i < 32; i++ {
        mask = 0
        for j := 0; j <= i; j++ {
            mask = mask << 1 + 1
        }
        for j := i + 1; j < 32; j++ {
            mask = mask << 1
        }
        m := make(map[uint32]bool)
        for j := 0; j < n; j++ {
            m[mask & a[j]] = true
        }
        if len(m) == k {
            fmt.Fprintln(out, toMask(mask))
            ok = true
            return
        }
    }

    if !ok {
        fmt.Fprintln(out, -1)
    }
}


Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).Your task is to count the number of segments where Horace can "see an elephant".

The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.

Print the number of segments in the bears' wall where Horace can "see an elephant".

NoteThe picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.

package main

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

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

const oo = 1 << 31 - 1

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n, m int = nextInt(), nextInt()
	if m == 1 {
		fmt.Fprintln(out, n)
		return
	}
	var a, b []int
	var prev int = nextInt()
	for i := 0; i + 1 < n; i++ {
		var x int = nextInt()
		a = append(a, x - prev)
		prev = x
	}
	prev = nextInt()
	for i := 0; i + 1 < m; i++ {
		var x int = nextInt()
		b = append(b, x - prev)
		prev = x
	}
	b = append(b, oo)
	p := make([]int, m)
	for i := 1; i < m; i++ {
		j := p[i - 1]
		for j > 0 && b[i] != b[j] {
			j = p[j - 1]
		}
		if b[i] == b[j] {
			j += 1
		}
		p[i] = j
	}
	var ret int = 0
	prev = 0
	for i := 0; i < n - 1; i++ {
		j := prev
		for j > 0 && a[i] != b[j] {
			j = p[j - 1]
		}
		if a[i] == b[j] {
			j += 1
		}
		if j == m - 1 {
			ret += 1
		}
		prev = j
	}

	fmt.Fprintln(out, ret)
}

func next() string {
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

func nextInt() int {
	ret, _ := strconv.Atoi(next())
	return ret
}

package main

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

func computePrefix(pattern []int) []int {
	t := make([]int, len(pattern))

	if len(pattern) == 0 {
		return t
	}

	t[0] = 0
	i := 1
	matched := 0

	for i < len(pattern) {
		if pattern[i] == pattern[matched] {
			matched++
			t[i] = matched
			i++
		} else if matched > 0{
			matched = t[matched-1]
		} else {
			// t[i] = 0
			i++
		}
	}

	return t
}

func readArray(reader *bufio.Reader, n int) []int {
	ret := make([]int, n-1)
	sline, _ := reader.ReadString('\n')
	sline = strings.TrimSpace(sline)
	line := strings.Split(sline, " ")

	p, _ := strconv.Atoi(line[0])
	for i := 0; i < n-1; i++ {
		q, _ := strconv.Atoi(line[i+1])
		ret[i] = q - p
		p = q
	}

	return ret
}

func solve(pattern []int, prefix []int, s []int) int {
	if len(pattern) == 0 {
		return len(s) + 1
	}

	i := 0
	j := 0
	ret := 0

	for j < len(s) {
		if pattern[i] == s[j] {
			if i < len(pattern) - 1 {
				i++
			} else {
				i = prefix[i]
				ret++
			}
			j++
		} else if i > 0 {
			i = prefix[i-1]
		} else {
			j++
		}
	}

	return ret
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	nw := strings.Split(line, " ")
	n, _ := strconv.Atoi(strings.TrimSpace(nw[0]))
	w, _ := strconv.Atoi(strings.TrimSpace(nw[1]))
	if n < w {
		fmt.Println("0")
		return
	}
	s := readArray(reader, n)
	pattern := readArray(reader, w)
	prefix := computePrefix(pattern)
	fmt.Println(solve(pattern, prefix, s))
}

package main

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

func computePrefix(pattern []int) []int {
	t := make([]int, len(pattern))

	if len(pattern) == 0 {
		return t
	}

	t[0] = 0
	i := 1
	matched := 0

	for i < len(pattern) {
		if pattern[i] == pattern[matched] {
			matched++
			t[i] = matched
			i++
		} else if matched > 0{
			matched = t[matched-1]
		} else {
			// t[i] = 0
			i++
		}
	}

	return t
}

func readArray(reader *bufio.Reader, n int) []int {
	ret := make([]int, n-1)
	sline, _ := reader.ReadString('\n')
	line := strings.Split(sline, " ")

	p, _ := strconv.Atoi(line[0])
	for i := 0; i < n-1; i++ {
		q, _ := strconv.Atoi(line[i+1])
		ret[i] = q - p
		p = q
	}

	return ret
}

func solve(pattern []int, prefix []int, s []int) int {
	if len(pattern) == 0 {
		return len(s) + 1
	}

	i := 0
	j := 0
	ret := 0

	for j < len(s) {
		if pattern[i] == s[j] {
			if i < len(pattern) - 1 {
				i++
			} else {
				i = prefix[i]
				ret++
			}
			j++
		} else if i > 0 {
			i = prefix[i-1]
		} else {
			j++
		}
	}

	return ret
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	nw := strings.Split(line, " ")
	n, _ := strconv.Atoi(strings.TrimSpace(nw[0]))
	w, _ := strconv.Atoi(strings.TrimSpace(nw[1]))
	if n < w {
		fmt.Println("0")
		return
	}
	s := readArray(reader, n)
	pattern := readArray(reader, w)
	prefix := computePrefix(pattern)
	fmt.Println(solve(pattern, prefix, s))
}


Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:You are given an array $$$a$$$ of $$$n$$$ integers. You are also given an integer $$$k$$$. Lord Omkar wants you to do $$$k$$$ operations with this array.Define one operation as the following:   Set $$$d$$$ to be the maximum value of your array.  For every $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_{i}$$$ with $$$d-a_{i}$$$. The goal is to predict the contents in the array after $$$k$$$ operations. Please help Ray determine what the final sequence will look like!

Each test contains multiple test cases. The first line contains the number of cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^{18}$$$) – the length of your array and the number of operations to perform. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(-10^9 \leq a_{i} \leq 10^9)$$$ – the initial contents of your array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each case, print the final version of array $$$a$$$ after $$$k$$$ operations described above.

NoteIn the first test case the array changes as follows:Initially, the array is $$$[-199, 192]$$$. $$$d = 192$$$.After the operation, the array becomes $$$[d-(-199), d-192] = [391, 0]$$$.

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

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

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

func Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func Abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

const (
	oo  = 100000000
	mod = 1000000007
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

var (
	adj   [][]int
	depth []int
)

func pow(a, b int64) int64 {
	if b == 0 {
		return 1
	}
	res := pow(a, b/2)
	res = mult(res, res)
	if b%2 == 1 {
		res = mult(res, a)
	}
	return res
}

func solve() {
	n, k := nextInt(), nextLong()
	arr := make([]int, n)
	for i := range arr {
		arr[i] = nextInt()
	}
	op(arr)
	k--
	if k%2 == 1 {
		op(arr)
	}
	for _, v := range arr {
		printf("%d ", v)
	}
	printf("\n")
}

func op(arr []int) {
	max := -1000000000
	for _, v := range arr {
		max = Max(max, v)
	}
	for i, v := range arr {
		arr[i] = max - v
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		file1, err := os.Open("input.txt")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create("output.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// printf("Case %d: ", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

func main() {
	var t, n int
	var k int64
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &k)
		data := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &data[j])
		}
		if k%2 == 0 {
			do(data)
			do(data)
		} else {
			do(data)
		}
		for j := 0; j < n; j++ {
			fmt.Printf("%d ", data[j])
		}
		fmt.Println()
	}
}

func do(a []int) {
	max := a[0]
	for i := 1; i < len(a); i++ {
		if max < a[i] {
			max = a[i]
		}
	}
	for i := 0; i < len(a); i++ {
		a[i] = max - a[i]
	}
}

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		var n int
		s, _ := reader.ReadBytes('\n')
		pos := readInt(s, 0, &n)
		var k uint64
		readUint64(s, pos+1, &k)
		A := readNNums(reader, n)
		solve(n, k, A)
		for i := 0; i < n; i++ {
			buf.WriteString(fmt.Sprintf("%d ", A[i]))
		}
		buf.WriteByte('\n')
	}
	fmt.Print(buf.String())
}

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 operate(n int, A []int) {
	x := A[0]
	for i := 1; i < n; i++ {
		if x < A[i] {
			x = A[i]
		}
	}
	for i := 0; i < n; i++ {
		A[i] = x - A[i]
	}
}

func solve(n int, k uint64, A []int) {
	operate(n, A)
	if k == 1 {
		return
	}

	k--

	if k&1 == 1 {
		operate(n, A)
	}
}

package main

import (
	"bufio"
	"fmt"
	"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() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	for t := in(); t > 0; t-- {
		n, k := in(), in()

		a := make([]int64, n)

		max := func() int64 {
			var m int64 = a[0]

			for i := range a {
				if a[i] > m {
					m = a[i]
				}
			}

			return m
		}

		for i := range a {
			a[i] = in()
		}

		d := max()
		k--
		for i := range a {
			a[i] = d - a[i]
		}

		r := k % 2

		if r == 1 {
			d := max()
			for i := range a {
				a[i] = d - a[i]
			}
		}

		for i := range a {
			fmt.Print(a[i], " ")
		}
		fmt.Println()
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n, k int64
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &k)
		a := make([]int64, n)
		max := int64(math.MinInt64)
		for j := int64(0); j < n; j++ {
			fmt.Fscan(in, &a[j])
			if a[j] > max {
				max = a[j]
			}
		}
		newMax := int64(math.MinInt64)
		for j := int64(0); j < n; j++ {
			a[j] = max - a[j]
			if k%2 == 1 {
				fmt.Print(a[j], " ")
			} else {
				if a[j] > newMax {
					newMax = a[j]
				}
			}
		}

		if k%2 == 0 {
			for j := int64(0); j < n; j++ {
				a[j] = newMax - a[j]
				fmt.Print(a[j], " ")
			}
			fmt.Println()
		}
	}
}

package main

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

var t, n int
var k int64

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

	fmt.Fscan(input, &t)

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

		fmt.Fscan(input, &n, &k)

		a := make([]int, n)

		mx := int(-1e9 - 5)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &a[i])
			if a[i] > mx {
				mx = a[i]
			}
		}

		mxx := int(-1e9 - 5)
		for i := 0; i < n; i++ {
			a[i] = mx - a[i]
			if a[i] > mxx {
				mxx = a[i]
			}
		}

		if k%2 == 0 {
			for i := 0; i < n; i++ {
				a[i] = mxx - a[i]
			}
		}

		for i := 0; i < n; i++ {
			fmt.Printf("%d ", a[i])
		}

		fmt.Println()
	}
}

package main

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

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

	if err := run(os.Args[1:], s); err != nil {
		fmt.Println(err)
	}
}

func run(args []string, s *bufio.Scanner) error {
	// Want to scan words instead of lines? s.Split(bufio.ScanWords)
	s.Split(bufio.ScanWords)
	s.Scan()

	testCases, err := strconv.Atoi(s.Text())
	if err != nil {
		return err
	}

	for testCases > 0 && s.Scan() {
		n, err := strconv.Atoi(s.Text())
		if err != nil {
			return err
		}
		s.Scan()
		//k, err := strconv.Atoi(s.Text())
		k, success := big.NewInt(0).SetString(s.Text(), 10)
		if !success {
			return fmt.Errorf("could not convert to big int")
		}

		var a []int

		for i := 0; i < n && s.Scan(); i++ {
			elem, err := strconv.Atoi(s.Text())
			if err != nil {
				return err
			}
			a = append(a, elem)
		}

		solve(a, k)

		testCases--
	}

	return nil
}

func solve(a []int, k *big.Int) {
	m := big.NewInt(0)
	if m.Mod(k, big.NewInt(2)).Cmp(big.NewInt(0)) == 0 {
		pprint(play(a, 2))
	} else {
		pprint(play(a, 1))
	}
}

func pprint(a []int) {
	for i, v := range a {
		fmt.Print(strconv.Itoa(v))
		if i != len(a)-1 {
			fmt.Print(" ")
		}
	}
	fmt.Println()
}

func play(a []int, k int) []int {
	if k == 0 {
		return a
	}

	max := a[0]
	var p []int
	for _, v := range a {
		if v > max {
			max = v
		}
	}

	for j := 0; j < len(a); j++ {
		p = append(p, max-a[j])
	}
	return play(p, k-1)
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF  = int64(math.MaxInt64)
	NINF = int64(math.MinInt64)
	MOD  = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

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

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

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		res := sol(n, k, a)
		for _, i := range res {
			printf("%d ", i)
		}
		printf("\n")
	}
}

func sol(n, k int64, a []int64) []int64 {
	maxi := max(a[0], a[1:]...)
	// make everything positive
	for i := range a {
		a[i] = maxi - a[i]
	}
	k--
	maxi = max(a[0], a[1:]...)
	k = k & 1
	if k == 1 {
		for i := range a {
			a[i] = maxi - a[i]
		}
		k--
	}
	if k != 0 {
		panic("k not 0")
	}
	return a
}

package main

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

func main() {
	var t, n, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &k)
		data := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &data[j])
		}
		if k%2 == 0 {
			do(data)
			do(data)
		} else {
			do(data)
		}
		for j := 0; j < n; j++ {
			fmt.Printf("%d ", data[j])
		}
		fmt.Println()
	}
}

func do(a []int) {
	max := a[0]
	for i := 1; i < len(a); i++ {
		if max < a[i] {
			max = a[i]
		}
	}
	for i := 0; i < len(a); i++ {
		a[i] = max - a[i]
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n, k int
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &k)
		a := make([]int64, n)
		max := int64(math.MinInt64)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			if a[j] > max {
				max = a[j]
			}
		}
		newMax := int64(math.MinInt64)
		for j := 0; j < n; j++ {
			a[j] = max - a[j]
			if k%2 == 1 {
				fmt.Print(a[j], " ")
			} else {
				if a[j] > newMax {
					newMax = a[j]
				}
			}
		}

		if k%2 == 0 {
			for j := 0; j < n; j++ {
				a[j] = newMax - a[j]
				fmt.Print(a[j], " ")
			}
			fmt.Println()
		}
	}
}

package main

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

var t, n, k int

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

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		fmt.Fscan(input, &n, &k)
		a := make([]int, n)

		mx := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &a[i])
			if a[i] > mx {
				mx = a[i]
			}
		}

		if k%2 == 1 {
			for i := 0; i < n; i++ {
				a[i] = mx - a[i]
			}
		} else {
			for i := 0; i < n; i++ {
				if a[i] == mx {
					a[i] = 0
				}
			}
		}

		for i := 0; i < n; i++ {
			fmt.Printf("%d ", a[i])
		}

		fmt.Println()
	}
}

package main

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

var t, n, k int

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

	fmt.Fscan(input, &t)

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

		fmt.Fscan(input, &n, &k)

		a := make([]int, n)

		mx := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &a[i])
			if a[i] > mx {
				mx = a[i]
			}
		}

		mxx := 0
		for i := 0; i < n; i++ {
			a[i] = mx - a[i]
			if a[i] > mxx {
				mxx = a[i]
			}
		}

		if k%2 == 0 {
			for i := 0; i < n; i++ {
				a[i] = mxx - a[i]
			}
		}

		for i := 0; i < n; i++ {
			fmt.Printf("%d ", a[i])
		}

		fmt.Println()
	}
}

package main

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

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

	if err := run(os.Args[1:], s); err != nil {
		fmt.Println(err)
	}
}

func run(args []string, s *bufio.Scanner) error {
	// Want to scan words instead of lines? s.Split(bufio.ScanWords)
	s.Split(bufio.ScanWords)
	s.Scan()

	testCases, err := strconv.Atoi(s.Text())
	if err != nil {
		return err
	}

	for testCases > 0 && s.Scan() {
		n, err := strconv.Atoi(s.Text())
		if err != nil {
			return err
		}
		s.Scan()
		k, err := strconv.Atoi(s.Text())
		if err != nil {
			return err
		}

		var a []int

		for i := 0; i < n && s.Scan(); i++ {
			elem, err := strconv.Atoi(s.Text())
			if err != nil {
				return err
			}
			a = append(a, elem)
		}

		solve(a, k)

		testCases--
	}

	return nil
}

// 0. Check if current permutation exists in the map
// 0a. If it does then the final array will look like loop[(k - loopStart) % loopLen]
// 1. Save current permutation in hash map
// 2. Find max value, d
// 3. for each value, value = d - value
// 4. GOTO 0.

func solve(a []int, k int) {
	m := make(map[string]int)

	for i := 0; i < k; i++ {
		arrKey := key(a)
		if loopStart, ok := m[arrKey]; ok {
			loopLen := i - loopStart
			index := (k - i) % loopLen
			pos := loopStart + index
			for ke, val := range m {
				if val == pos {
					printAns(ke)
					return
				}
			}
		} else {
			m[arrKey] = i
		}

		var max int = -1 * (2 << 30)
		for _, v := range a {
			if v > max {
				max = v
			}
		}

		for j := 0; j < len(a); j++ {
			a[j] = max - a[j]
		}
	}

	printAns(key(a))
}

func key(a []int) string {
	k := ""
	for i, v := range a {
		k += strconv.Itoa(v)
		if i != len(a) - 1 {
			k += "~"
		}
	}
	return k
}

func printAns(s string) {
	parts := strings.Split(s, "~")
	fmt.Println(strings.Join(parts, " "))
}

package main

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

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

	if err := run(os.Args[1:], s); err != nil {
		fmt.Println(err)
	}
}

func run(args []string, s *bufio.Scanner) error {
	// Want to scan words instead of lines? s.Split(bufio.ScanWords)
	s.Split(bufio.ScanWords)
	s.Scan()

	testCases, err := strconv.Atoi(s.Text())
	if err != nil {
		return err
	}

	for testCases > 0 && s.Scan() {
		n, err := strconv.Atoi(s.Text())
		if err != nil {
			return err
		}
		s.Scan()
		k, err := strconv.Atoi(s.Text())
		if err != nil {
			return err
		}

		var a []int

		for i := 0; i < n && s.Scan(); i++ {
			elem, err := strconv.Atoi(s.Text())
			if err != nil {
				return err
			}
			a = append(a, elem)
		}

		solve2(a, k)

		testCases--
	}

	return nil
}

// 0. Check if current permutation exists in the map
// 0a. If it does then the final array will look like loop[(k - loopStart) % loopLen]
// 1. Save current permutation in hash map
// 2. Find max value, d
// 3. for each value, value = d - value
// 4. GOTO 0.

func solve2(a []int, k int) {
	odd := play(a, 1)
	even := play(a, 2)

	if k % 2 == 0 {
		for i, v := range even {
			fmt.Print(strconv.Itoa(v))
			if i != len(even) - 1 {
				fmt.Print(" ")
			}
		}
		fmt.Println()
	} else {
		for i, v := range odd {
			fmt.Print(strconv.Itoa(v))
			if i != len(odd) - 1 {
				fmt.Print(" ")
			}
		}
		fmt.Println()
	}
}

func play(a []int, k int) []int {
	if k == 0 {
		return a
	}

	max := a[0]
	var p []int
	for _, v := range a {
		if v > max {
			max = v
		}
	}

	for j := 0; j < len(a); j++ {
		p = append(p, max - a[j])
	}
	return play(p, k -1)
}

func solve(a []int, k int) {
	m := make(map[string]int)

	for i := 0; i < k; i++ {
		arrKey := key(a)
		if loopStart, ok := m[arrKey]; ok {
			loopLen := i - loopStart
			index := (k - i) % loopLen
			pos := loopStart + index
			for ke, val := range m {
				if val == pos {
					printAns(ke)
					return
				}
			}
		} else {
			m[arrKey] = i
		}

		max := a[0]
		for _, v := range a {
			if v > max {
				max = v
			}
		}

		for j := 0; j < len(a); j++ {
			a[j] = max - a[j]
		}
	}

	printAns(key(a))
}

func key(a []int) string {
	k := ""
	for i, v := range a {
		k += strconv.Itoa(v)
		if i != len(a) - 1 {
			k += "~"
		}
	}
	return k
}

func printAns(s string) {
	parts := strings.Split(s, "~")
	fmt.Println(strings.Join(parts, " "))
}


In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.Lesha knows that today he can study for at most $$$a$$$ hours, and he will have $$$b$$$ hours to study tomorrow. Note that it is possible that on his planet there are more hours in a day than on Earth. Lesha knows that the quality of his knowledge will only depend on the number of lecture notes he will read. He has access to an infinite number of notes that are enumerated with positive integers, but he knows that he can read the first note in one hour, the second note in two hours and so on. In other words, Lesha can read the note with number $$$k$$$ in $$$k$$$ hours. Lesha can read the notes in arbitrary order, however, he can't start reading a note in the first day and finish its reading in the second day.Thus, the student has to fully read several lecture notes today, spending at most $$$a$$$ hours in total, and fully read several lecture notes tomorrow, spending at most $$$b$$$ hours in total. What is the maximum number of notes Lesha can read in the remaining time? Which notes should he read in the first day, and which — in the second?

The only line of input contains two integers $$$a$$$ and $$$b$$$ ($$$0 \leq a, b \leq 10^{9}$$$) — the number of hours Lesha has today and the number of hours Lesha has tomorrow.

In the first line print a single integer $$$n$$$ ($$$0 \leq n \leq a$$$) — the number of lecture notes Lesha has to read in the first day. In the second line print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq a$$$), the sum of all $$$p_i$$$ should not exceed $$$a$$$. In the third line print a single integer $$$m$$$ ($$$0 \leq m \leq b$$$) — the number of lecture notes Lesha has to read in the second day. In the fourth line print $$$m$$$ distinct integers $$$q_1, q_2, \ldots, q_m$$$ ($$$1 \leq q_i \leq b$$$), the sum of all $$$q_i$$$ should not exceed $$$b$$$. All integers $$$p_i$$$ and $$$q_i$$$ should be distinct. The sum $$$n + m$$$ should be largest possible.

NoteIn the first example Lesha can read the third note in $$$3$$$ hours in the first day, and the first and the second notes in one and two hours correspondingly in the second day, spending $$$3$$$ hours as well. Note that Lesha can make it the other way round, reading the first and the second notes in the first day and the third note in the second day.In the second example Lesha should read the third and the sixth notes in the first day, spending $$$9$$$ hours in total. In the second day Lesha should read the first, second fourth and fifth notes, spending $$$12$$$ hours in total.

//   C. Успеть все
//   https://codeforces.com/contest/1031/problem/C
//   tags: greedy
package main

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

type scanner struct {
	s *bufio.Scanner
}

func initScanner(filename string) scanner {
	//f, _ := os.Open(filename)
	f := os.Stdin
	return scanner{s: bufio.NewScanner(f)}
}

func (s scanner) ReadInt() int {
	s.s.Scan()
	val, _ := strconv.Atoi(s.s.Text())
	return val
}

func sum(x float64) float64 {
	return x * (x + 1) / 2
}

func Implode(glue string, args []int) string {
	data := make([]string, len(args))
	for i, s := range args {
		data[i] = fmt.Sprint(s)
	}
	return strings.Join(data, glue)
}

func main() {

	scanner := initScanner("input.txt")
	scanner.s.Split(bufio.ScanWords)

	a := scanner.ReadInt()
	b := scanner.ReadInt()

	x := 1
	for ; sum(float64(x)) < float64(a+b); x++ {
	}

	var outA []int
	var outB []int

	for limit := 0; x != 0 && limit < 2; x-- {
		aX := a
		aY := b
		limit++

		var aRes []int
		var bRes []int

		for tempX := x; tempX > 0; tempX--{
			if aX >= tempX {
				aX -= tempX
				aRes = append(aRes, tempX)
			} else {
				if aY >= tempX {
					aY -= tempX
					bRes = append(bRes, tempX)
				}
			}
		}

		if len(aRes)+len(bRes) > len(outA)+len(outB) {
			outA = aRes
			outB = bRes
		}
	}

	fmt.Println(len(outA))
	fmt.Println(Implode(" ", outA))
	fmt.Println(len(outB))
	fmt.Println(Implode(" ", outB))
}

package main

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

type scanner struct {
	s *bufio.Scanner
}

func initScanner(filename string) scanner {
	//f, _ := os.Open(filename)
	f := os.Stdin
	return scanner{s: bufio.NewScanner(f)}
}

func (s scanner) ReadInt() int {
	s.s.Scan()
	val, _ := strconv.Atoi(s.s.Text())
	return val
}

func sum(x float64) float64 {
	return x * (x + 1) / 2
}

func Implode(glue string, args []int) string {
	data := make([]string, len(args))
	for i, s := range args {
		data[i] = fmt.Sprint(s)
	}
	return strings.Join(data, glue)
}

func main() {

	scanner := initScanner("input.txt")
	scanner.s.Split(bufio.ScanWords)

	a := scanner.ReadInt()
	b := scanner.ReadInt()

	x := 1
	for ; sum(float64(x)) < float64(a+b); x++ {
	}

	var outA []int
	var outB []int

	for limit := 1; x != 0 && limit < 10; x-- {
		aX := a
		aY := b
		limit++

		var aRes []int
		var bRes []int

		for tempX := x; tempX > 0; tempX--{
			if aX >= tempX {
				aX -= tempX
				aRes = append(aRes, tempX)
			} else {
				if aY >= tempX {
					aY -= tempX
					bRes = append(bRes, tempX)
				}
			}
		}

		if len(aRes)+len(bRes) > len(outA)+len(outB) {
			outA = aRes
			outB = bRes
		}
	}

	fmt.Println(len(outA))
	fmt.Println(Implode(" ", outA))
	fmt.Println(len(outB))
	fmt.Println(Implode(" ", outB))
}

package main

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

type scanner struct {
	s *bufio.Scanner
}

func initScanner(filename string) scanner {
	//f, _ := os.Open(filename)
	f := os.Stdin
	return scanner{s: bufio.NewScanner(f)}
}

func (s scanner) ReadInt() int {
	s.s.Scan()
	val, _ := strconv.Atoi(s.s.Text())
	return val
}

func sum(x float64) float64 {
	return x * (x + 1) / 2
}

func Implode(glue string, args []int) string {
	data := make([]string, len(args))
	for i, s := range args {
		data[i] = fmt.Sprint(s)
	}
	return strings.Join(data, glue)
}

func main() {

	scanner := initScanner("input.txt")
	scanner.s.Split(bufio.ScanWords)

	a := scanner.ReadInt()
	b := scanner.ReadInt()

	//fmt.Println(a, b)

	x := 1
	for ; sum(float64(x)) < float64(a+b); x++ {
	}

	for ; x != 1; x-- {
		aX := a
		aY := b

		var aRes []int
		var bRes []int

		for tempX := x; tempX > 0; tempX--{
			if aX >= tempX {
				aX -= tempX
				aRes = append(aRes, tempX)
			} else {
				if aY >= tempX {
					aY -= tempX
					bRes = append(bRes, tempX)
				}
			}
		}

		if aX+aY == 0 {
			fmt.Println(len(aRes))
			fmt.Println(Implode(" ", aRes))
			fmt.Println(len(bRes))
			fmt.Println(Implode(" ", bRes))
			return
		}
	}
}

package main

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

type scanner struct {
	s *bufio.Scanner
}

func initScanner(filename string) scanner {
	//f, _ := os.Open(filename)
	f := os.Stdin
	return scanner{s: bufio.NewScanner(f)}
}

func (s scanner) ReadInt() int {
	s.s.Scan()
	val, _ := strconv.Atoi(s.s.Text())
	return val
}

func sum(x float64) float64 {
	return x * (x + 1) / 2
}

func Implode(glue string, args []int) string {
	data := make([]string, len(args))
	for i, s := range args {
		data[i] = fmt.Sprint(s)
	}
	return strings.Join(data, glue)
}

func main() {

	scanner := initScanner("input.txt")
	scanner.s.Split(bufio.ScanWords)

	a := scanner.ReadInt()
	b := scanner.ReadInt()

	x := 1
	for ; sum(float64(x)) < float64(a+b); x++ {
	}

	var outA []int
	var outB []int

	for limit := 1; x != 1 && limit < 10; x-- {
		aX := a
		aY := b
		limit++

		var aRes []int
		var bRes []int

		for tempX := x; tempX > 0; tempX--{
			if aX >= tempX {
				aX -= tempX
				aRes = append(aRes, tempX)
			} else {
				if aY >= tempX {
					aY -= tempX
					bRes = append(bRes, tempX)
				}
			}
		}

		if len(aRes)+len(bRes) > len(outA)+len(outB) {
			outA = aRes
			outB = bRes
		}
	}

	fmt.Println(len(outA))
	fmt.Println(Implode(" ", outA))
	fmt.Println(len(outB))
	fmt.Println(Implode(" ", outB))
}

//   C. Успеть все
//   https://codeforces.com/contest/1031/problem/C
//   tags: greedy
package main

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

type scanner struct {
	s *bufio.Scanner
}

func initScanner(filename string) scanner {
	//f, _ := os.Open(filename)
	f := os.Stdin
	return scanner{s: bufio.NewScanner(f)}
}

func (s scanner) ReadInt() int {
	s.s.Scan()
	val, _ := strconv.Atoi(s.s.Text())
	return val
}

func sum(x float64) float64 {
	return x * (x + 1) / 2
}

func Implode(glue string, args []int) string {
	data := make([]string, len(args))
	for i, s := range args {
		data[i] = fmt.Sprint(s)
	}
	return strings.Join(data, glue)
}

func main() {

	scanner := initScanner("input.txt")
	scanner.s.Split(bufio.ScanWords)

	a := scanner.ReadInt()
	b := scanner.ReadInt()

	x := 1
	for ; sum(float64(x)) < float64(a+b); x++ {
	}

	var outA []int
	var outB []int

	for limit := 0; x != 0 && limit < 1; x-- {
		aX := a
		aY := b
		limit++

		var aRes []int
		var bRes []int

		for tempX := x; tempX > 0; tempX--{
			if aX >= tempX {
				aX -= tempX
				aRes = append(aRes, tempX)
			} else {
				if aY >= tempX {
					aY -= tempX
					bRes = append(bRes, tempX)
				}
			}
		}

		if len(aRes)+len(bRes) > len(outA)+len(outB) {
			outA = aRes
			outB = bRes
		}
	}

	fmt.Println(len(outA))
	fmt.Println(Implode(" ", outA))
	fmt.Println(len(outB))
	fmt.Println(Implode(" ", outB))
}


Once Divan analyzed a sequence $$$a_1, a_2, \ldots, a_n$$$ consisting of $$$n$$$ non-negative integers as follows. He considered each non-empty subsequence of the sequence $$$a$$$, computed the bitwise XOR of its elements and added up all the XORs, obtaining the coziness of the sequence $$$a$$$.A sequence $$$c$$$ is a subsequence of a sequence $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements. For example, $$$[1, \, 2, \, 3, \, 4]$$$, $$$[2, \, 4]$$$, and $$$[2]$$$ are subsequences of $$$[1, \, 2, \, 3, \, 4]$$$, but $$$[4, \, 3]$$$ and $$$[0]$$$ are not.Divan was very proud of his analysis, but now he lost the sequence $$$a$$$, and also the coziness value! However, Divan remembers the value of bitwise OR on $$$m$$$ contiguous subsegments of the sequence $$$a$$$. It turns out that each element of the original sequence is contained in at least one of these $$$m$$$ segments.Divan asks you to help find the coziness of the sequence $$$a$$$ using the information he remembers. If several coziness values are possible, print any.As the result can be very large, print the value modulo $$$10^9 + 7$$$.

The first line contains one integer number $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. The first line of each test case contains two integer numbers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of the sequence and the number of contiguous segments whose bitwise OR values Divan remembers, respectively. The following $$$m$$$ lines describe the segments, one per line. Each segment is described with three integers $$$l$$$, $$$r$$$, and $$$x$$$ ($$$1 \le l \le r \le n$$$, $$$0 \le x \le 2^{30} - 1$$$) — the first and last elements of the segment and the bitwise OR of $$$a_l, a_{l + 1}, \ldots, a_r$$$, respectively. It is guaranteed that each element of the sequence is contained in at least one of the segments. It is guaranteed that there exists a sequence that satisfies all constraints. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$.

For each test case print the coziness any suitable sequence $$$a$$$ modulo $$$10^9 + 7$$$.

NoteIn first example, one of the sequences that fits the constraints is $$$[0, 2]$$$. Consider all its non-empty subsequences:  $$$[0]$$$: the bitwise XOR of this subsequence is $$$0$$$;  $$$[2]$$$: the bitwise XOR of this subsequence is $$$2$$$;  $$$[0, 2]$$$: the bitwise XOR of this subsequence is $$$2$$$. The sum of all results is $$$4$$$, so it is the answer.In second example, one of the sequences that fits the constraints is $$$[0, \, 5, \, 5]$$$.In third example, one of the sequences that fits the constraints is $$$[5, \, 6, \, 7, \, 0, \, 2]$$$.

package main

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

const MOD int64 = 1e9+7

var n, m int

var l, r, x []int

var fact []int64

var twoN []int64

var inv []int64

var w *bufio.Writer

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	var t int

	fmt.Fscan(read, &t)

	precompute()

	//st := time.Now().UnixNano()

	for i := 0; i < t; i++ {
		fmt.Fscan(read, &n, &m)
		//n = 50000
	//	m = 50000

		l = make([]int, m)
		r = make([]int, m)
		x= make([]int, m)

		for j := 0; j < m; j++ {
			fmt.Fscan(read, &l[j], &r[j], &x[j])
			//l[j] = j + 1
			//r[j] = j + 1
			//x[j] = 0
		}

		ans := solve()

		fmt.Fprintln(w, ans)
	}
	w.Flush()

	//en := time.Now().UnixNano()

	//fmt.Println((en - st)/1000000)
}

type pair struct {
	v, id int
}

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] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func precompute() {
	fact = make([]int64, 2e5 + 1)
	twoN = make([]int64, 2e5 + 1)

	inv = make([]int64, 2e5 + 1)

	var p int64 = 1

	for i := 0; i < 2e5 + 1; i++ {
		twoN[i] = p

		p = (p * 2) % MOD
	}

	fact[0] = 1

	for i := 1; i < 2e5 + 1; i++ {
		fact[i] = (fact[i - 1] * int64(i)) % MOD
	}

	for i := 0; i < 2e5 + 1; i ++ {
		inv[i] = modInverse(fact[i], MOD)
	}
}

func solve() int64 {
	//1 2 5 3 6 7

	var cnt [31]int

	var pairs []*pair

	for i := 0; i < m; i++ {
		pairs = append(pairs, &pair{l[i], i})
	}
	sort.Sort(ByV(pairs))

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

		rmz := -1

		for j := 0; j < m; j++ {
			p := pairs[j]

			left, right := p.v, r[p.id]
			left--
			right--

			xor := x[p.id]

			b := xor >> uint(i) & 0x1

			if b == 0 {
				if rmz == -1 {
					cnt[i] += left

				} else {

					if left > rmz {
						cnt[i] += left - rmz - 1
					}
				}

				if right > rmz {
					rmz = right
				}
			}
		}
		if rmz == -1 {
			cnt[i] += n

		} else if rmz < n - 1 {
			cnt[i] += (n -1 - rmz)
		}
	}

	var ans int64

	for j := 0; j < 30; j++ {
		o := cnt[j]

		if o > n {
			panic("can't be")
		}

		z := n - o

		//1 1 1 0 0 1

		var s int64

		for i := 1; i <= o; i+=2 {
			//i choose o

			nchoose := ((fact[o] * inv[i] % MOD) * inv[o - i]) % MOD

			temp := (nchoose * twoN[z]) % MOD

			s = (s + temp) % MOD
		}

		s = (s * twoN[j]) % MOD

		ans = (ans + s) % MOD
	}

	return ans
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
	g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

		// m is added to handle negative x
		res := (x % m + m) % m
		return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import (
	"bufio"
	"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)
}

const mod = 1e9 + 7

var fac, invfac []int64

func bitAt(n, bit int64) int {
	if n&(1<<bit) == 0 {
		return 0
	}
	return 1
}

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func solveForBit(n int, bit int64, segs [][3]int) int64 {
	res := make([]int, n+1)
	for _, seg := range segs {
		l, r, or := seg[0], seg[1], int64(seg[2])
		if bitAt(or, bit) == 0 {
			res[l-1]++
			res[r]--
		}
	}
	for i := 1; i < n; i++ {
		res[i] += res[i-1]
	}

	var cnt0, cnt1 int64
	for _, i := range res[:len(res)-1] {
		if i > 0 {
			cnt0++
		} else {
			cnt1++
		}
	}
	if cnt1 == 0 {
		return 0
	}
	var totalCases = (binpow(2, cnt0) * binpow(2, cnt1-1)) % mod
	ans := (totalCases * binpow(2, bit)) % mod
	return ans
}

func solveC() {
	var n, m int
	scan(&n, &m)
	segs := make([][3]int, m)
	for i := range segs {
		for j := 0; j < 3; j++ {
			scan(&segs[i][j])
		}
	}

	fac = make([]int64, n+1)
	fac[0] = 1
	fac[1] = 1
	for i := 2; i <= n; i++ {
		fac[i] = fac[i-1] * int64(i)
		fac[i] %= mod
	}
	invfac = make([]int64, n+1)
	for i := 0; i <= n; i++ {
		invfac[i] = binpow(fac[i], mod-2)
	}

	var ans int64
	for bit := int64(0); bit < 30; bit++ {
		ans += solveForBit(n, bit, segs)
		ans %= mod
	}

	printf("%v\n", ans)
}

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

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

package main

import (
	"bufio"
	"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)
}

const mod = 1e9 + 7

var fac, invfac []int64

func bitAt(n, bit int64) int {
	if n&(1<<bit) == 0 {
		return 0
	}
	return 1
}

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func solveForBit(n int, bit int64, segs [][3]int) int64 {
	res := make([]int, n+1)
	for _, seg := range segs {
		l, r, or := seg[0], seg[1], int64(seg[2])
		if bitAt(or, bit) == 0 {
			res[l-1]++
			res[r]--
		}
	}
	for i := 1; i < n; i++ {
		res[i] += res[i-1]
	}

	var cnt0, cnt1 int64
	for _, i := range res[:len(res)-1] {
		if i > 0 {
			cnt0++
		} else {
			cnt1++
		}
	}

	var zeroes, totalCases int64 = binpow(2, cnt0), 0
	for k := int64(1); k <= cnt1; k += 2 {
		ones := ((fac[cnt1] * invfac[k]) % mod * invfac[cnt1-k]) % mod
		totalCases += (ones * zeroes) % mod
		totalCases %= mod
	}
	ans := (totalCases * binpow(2, bit)) % mod
	return ans
}

/*
	1: 1 1 1 0 0 => cases: 4 ans: 4  = 1 1 1 0 0
	2: 1 1 1 0 1 => cases: 8 ans: 16 = 2 2 2 0 2
	4: 1 1 1 0 0 => cases: 4 ans: 16 = 4 4 4 0 0

	[7, 7, 7, 0, 2]
*/

func solveC() {
	var n, m int
	scan(&n, &m)
	segs := make([][3]int, m)
	for i := range segs {
		for j := 0; j < 3; j++ {
			scan(&segs[i][j])
		}
	}

	fac = make([]int64, n+1)
	fac[0] = 1
	fac[1] = 1
	for i := 2; i <= n; i++ {
		fac[i] = fac[i-1] * int64(i)
		fac[i] %= mod
	}
	invfac = make([]int64, n+1)
	for i := 0; i <= n; i++ {
		invfac[i] = binpow(fac[i], mod-2)
	}

	var ans int64
	for bit := int64(0); bit < 30; bit++ {
		ans += solveForBit(n, bit, segs)
		ans %= mod
	}

	printf("%v\n", ans)
}

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

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solveC()
	}
	// n := 5
	// ans := 0
	// for mask := 1; mask < (1 << n); mask++ {
	// 	xor := 0
	// 	for i := 0; i < n; i++ {
	// 		if bitAt(int64(mask), int64(i)) == 1 {
	// 			xor ^= i
	// 		}
	// 	}
	// 	ans += xor
	// }
	// printf("%d", ans)
}

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

		var l, r, x, res, mod, pt int64
		mod = 1_000_000_007
		pt = 1
		for i := 0; i < n-1; i++ {
			pt = pt * 2 % mod
		}
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &l, &r, &x)
			res |= x
		}
		res = (res % mod * pt) % mod

		fmt.Fprintln(out, res)
	}
}

package main

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

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

	pow := func(x int64, n int) (res int64) {
		x %= mod
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return
	}

	solve := func(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		type pair struct{ l, r, x int }
		a := make([]pair, m)
		for i := range a {
			Fscan(in, &a[i].l, &a[i].r, &a[i].x)
		}
		//b := make([]int, m*2)
		ans := int64(0)
		for i := 0; i < 30; i++ {
			d := make([]int, n+2)
			for _, p := range a {
				if p.x>>i&1 == 0 {
					d[p.l]++
					d[p.r+1]--
				}
			}
			//sort.Ints(c)
			cnt0 := 0
			s := 0
			for _, v := range d {
				s+=v
				if s > 0 {
					cnt0++
				}
			}
			//pre := 1
			//for _, v := range c {
			//	if v>>1 > pre {
			//		if s > 0 {
			//			cnt0 += v>>1 - pre
			//		}
			//		pre = v >> 1
			//	}
			//	if v&1 > 0 {
			//		s++
			//	} else {
			//		s--
			//	}
			//}
			cnt1 := n - cnt0
			if cnt1 > 0 {
				cur := pow(2, cnt1-1) * pow(2, cnt0) % mod
				ans = (ans + cur<<i) % mod
			}
		}
		Fprintln(out, ans)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}

}

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

package main

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

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

func solveCase() {
	var n, m, fr, to, sumOr int
	fmt.Fscan(in, &n, &m)
	var totalOr int64 = 0
	const mod = int64(1e9 + 7)
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &fr, &to, &sumOr)
		totalOr = totalOr | int64(sumOr)
	}
	var ans = totalOr % mod
	for i := 1; i < n; i++ {
		ans = (ans * 2) % mod
	}
	fmt.Fprintln(out, ans)
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
	out.Flush()
}

package main

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

const MOD int64 = 1e9+7

var n, m int

var l, r, x []int

var fact []int64

var twoN []int64

var inv []int64

var w *bufio.Writer

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	var t int

	fmt.Fscan(read, &t)

	precompute()

	//st := time.Now().UnixNano()

	for i := 0; i < t; i++ {
		fmt.Fscan(read, &n, &m)
		//n = 50000
		//m = 50000

		l = make([]int, m)
		r = make([]int, m)
		x= make([]int, m)

		for j := 0; j < m; j++ {
			fmt.Fscan(read, &l[j], &r[j], &x[j])
			//l[j] = j + 1
			//r[j] = j + 1
			//x[j] = 0
		}

		ans := solve()

		fmt.Fprintln(w, ans)
	}
	w.Flush()

	//en := time.Now().UnixNano()

	//fmt.Println((en - st)/1000000)
}

type pair struct {
	v, id int
}

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] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func precompute() {
	fact = make([]int64, 2e5 + 1)
	twoN = make([]int64, 2e5 + 1)

	inv = make([]int64, 2e5 + 1)

	var p int64 = 1

	for i := 0; i < 2e5 + 1; i++ {
		twoN[i] = p

		p = (p * 2) % MOD
	}

	fact[0] = 1

	for i := 1; i < 2e5 + 1; i++ {
		fact[i] = (fact[i - 1] * int64(i)) % MOD
	}

	for i := 0; i < 2e5 + 1; i ++ {
		inv[i] = modInverse(fact[i], MOD)
	}
}

func solve() int64 {
	//1 2 5 3 6 7

	var cnt [31]int

	order := make([]int, m)
	for i := 0; i < m; i++ {
		order[i] = i
	}
	sort.Slice(order, func(i, j int) bool {
		return l[i] < l[j]
	})

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

		rmz := -1

		for j := 0; j < m; j++ {
			left, right := l[order[j]], r[order[j]]
			left--
			right--

			xor := x[order[j]]

			b := xor >> uint(i) & 0x1

			if b == 0 {
				if rmz == -1 {
					cnt[i] += left

				} else {

					if left > rmz {
						cnt[i] += left - rmz - 1
					}
				}

				if right > rmz {
					rmz = right
				}
			}
		}
		if rmz == -1 {
			cnt[i] += n

		} else if rmz < n - 1 {
			cnt[i] += (n -1 - rmz)
		}
	}

	var ans int64

	for j := 0; j < 30; j++ {
		o := cnt[j]

		if o > n {
			panic("can't be")
		}

		z := n - o

		//1 1 1 0 0 1

		var s int64

		for i := 1; i <= o; i+=2 {
			//i choose o

			nchoose := ((fact[o] * inv[i] % MOD) * inv[o - i]) % MOD

			temp := (nchoose * twoN[z]) % MOD

			s = (s + temp) % MOD
		}

		s = (s * twoN[j]) % MOD

		ans = (ans + s) % MOD
	}

	return ans
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
	g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

		// m is added to handle negative x
		res := (x % m + m) % m
		return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import (
	"bufio"
	"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)
}

const mod = 1e9 + 7

var fac, invfac []int64

func bitAt(n, bit int64) int {
	if n&(1<<bit) == 0 {
		return 0
	}
	return 1
}

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func solveForBit(n int, bit int64, segs [][3]int) int64 {
	res := make([]int, n+1)
	for _, seg := range segs {
		l, r, or := seg[0], seg[1], int64(seg[2])
		if bitAt(or, bit) == 0 {
			res[l-1]++
			res[r]--
		}
	}
	for i := 1; i < n; i++ {
		res[i] += res[i-1]
	}

	var cnt0, cnt1 int64
	for _, i := range res[:len(res)-1] {
		if i > 0 {
			cnt0++
		} else {
			cnt1++
		}
	}

	var totalCases int64
	for k := int64(1); k <= cnt1; k += 2 {
		x1 := (fac[cnt1] * invfac[k]) % mod
		x2 := (x1 * invfac[cnt1-k]) % mod
		totalCases += x2
		totalCases %= mod
	}

	return (totalCases * binpow(2, bit)) % mod
}

func solveC() {
	var n, m int
	scan(&n, &m)
	segs := make([][3]int, m)
	for i := range segs {
		for j := 0; j < 3; j++ {
			scan(&segs[i][j])
		}
	}

	fac = make([]int64, n+1)
	fac[0] = 1
	fac[1] = 1
	for i := 2; i <= n; i++ {
		fac[i] = fac[i-1] * int64(i)
		fac[i] %= mod
	}
	invfac = make([]int64, n+1)
	for i := 0; i <= n; i++ {
		invfac[i] = binpow(fac[i], mod-2)
	}

	var ans int64
	for bit := int64(0); bit < 30; bit++ {
		ans += solveForBit(n, bit, segs)
		ans %= mod
	}

	printf("%v\n", ans)
}

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

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

package main

import (
	"bufio"
	"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)
}

const mod = 1e9 + 7

var fac, invfac []int64

func bitAt(n, bit int64) int {
	if n&(1<<bit) == 0 {
		return 0
	}
	return 1
}

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func solveForBit(n int, bit int64, segs [][3]int) int64 {
	res := make([]int, n+1)
	for _, seg := range segs {
		l, r, or := seg[0], seg[1], int64(seg[2])
		if bitAt(or, bit) == 0 {
			res[l-1]++
			res[r]--
		}
	}
	for i := 1; i < n; i++ {
		res[i] += res[i-1]
	}

	var cnt0, cnt1 int64
	for _, i := range res[:len(res)-1] {
		if i > 0 {
			cnt0++
		} else {
			cnt1++
		}
	}

	var ans int64
	for k := int64(1); k <= cnt1; k += 2 {
		totalCases := ((fac[cnt1] * invfac[k]) % mod * invfac[cnt1-k]) % mod
		ans += (totalCases * binpow(2, bit)) % mod
		ans %= mod
	}

	return ans
}

func solveC() {
	var n, m int
	scan(&n, &m)
	segs := make([][3]int, m)
	for i := range segs {
		for j := 0; j < 3; j++ {
			scan(&segs[i][j])
		}
	}

	fac = make([]int64, n+1)
	fac[1] = 1
	for i := 2; i <= n; i++ {
		fac[i] = fac[i-1] * int64(i)
		fac[i] %= mod
	}
	invfac = make([]int64, n+1)
	for i := 1; i <= n; i++ {
		invfac[i] = binpow(fac[i], mod-2)
	}

	var ans int64
	for bit := int64(0); bit < 30; bit++ {
		ans += solveForBit(n, bit, segs)
		ans %= mod
	}

	printf("%v\n", ans)
}

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

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

package main

import (
	"bufio"
	"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)
}

const mod = 1e9 + 7

func bitAt(n int, bit int) int {
	if n&(1<<bit) == 0 {
		return 0
	}
	return 1
}

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func solveForBit(n int, bit int, segs [][3]int) int64 {
	res := make([]int, n+1)
	for _, seg := range segs {
		l, r, or := seg[0], seg[1], seg[2]
		if bitAt(or, bit) == 0 {
			res[l-1]++
			res[r]--
		}
	}
	for i := 1; i < n; i++ {
		res[i] += res[i-1]
	}

	var cnt0, cnt1 int64
	for _, i := range res[:len(res)-1] {
		if i > 0 {
			cnt0++
		} else {
			cnt1++
		}
	}

	val := binpow(2, int64(bit))
	totalCases := (binpow(2, cnt0) * (binpow(2, cnt1) - 1)) % mod
	return (totalCases * val) % mod
}

func C() {
	var n, m int
	scan(&n, &m)
	segs := make([][3]int, m)
	for i := range segs {
		for j := 0; j < 3; j++ {
			scan(&segs[i][j])
		}
	}

	var ans int64
	for bit := 0; bit < 30; bit++ {
		ans += solveForBit(n, bit, segs)
		ans %= mod
	}

	printf("%v\n", ans)
}

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

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

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

		var l, r, x, res, mod, pt int64
		mod = 1_000_000_007
		pt = 1
		for i := 0; i < n-1; i++ {
			pt = pt * 2
		}
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &l, &r, &x)
			res |= x
		}
		res = (res % mod * pt % mod) % mod

		fmt.Fprintln(out, res)
	}
}


George is a cat, so he loves playing very much.Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from 1 to n. Then the i-th card from the left contains number pi (1 ≤ pi ≤ n). Vitaly wants the row to have exactly k cards left. He also wants the i-th card from left to have number bi written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation n - k times.In one remove operation George can choose w (1 ≤ w; w is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as x1, x2, ..., xw (from the left to the right). After that, George can remove the card xi, such that xi ≤ xj for each j (1 ≤ j ≤ w). After the described operation George gets w pieces of sausage.George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!

The first line contains integers n and k (1 ≤ k ≤ n ≤ 106) — the initial and the final number of cards. The second line contains n distinct space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the initial row of cards.  The third line contains k space-separated integers b1, b2, ..., bk — the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for n - k times.

Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"time"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, k int
	for I(&n, &k) == nil {
		rand.Seed(time.Now().Unix())
		solve(n, k)
	}
	w.Flush()
}

var tree []int

func update(i, val int) {
	for i < len(tree) {
		tree[i] += val
		i += i & -i
	}
}

func sum(i int) int {
	var s int
	for i > 0 {
		s += tree[i]
		i -= i & -i
	}
	return s
}

type Node struct {
	Left     *Node
	Value    int
	Priority int
	Right    *Node
}

func newNode(value int) *Node {
	return &Node{nil, value, rand.Intn(100), nil}
}

func rightRotate(node *Node) *Node {
	res := node.Left
	node.Left = res.Right
	res.Right = node
	return res
}
func leftRotate(node *Node) *Node {
	res := node.Right
	node.Right = res.Left
	res.Left = node
	return res
}

func insert(node *Node, value int) *Node {
	if node == nil {
		return newNode(value)
	}
	if node.Value > value {
		node.Left = insert(node.Left, value)
		if node.Left.Priority > node.Priority {
			node = rightRotate(node)
		}
		return node
	}
	node.Right = insert(node.Right, value)
	if node.Right.Priority > node.Priority {
		node = leftRotate(node)
	}
	return node
}

var hi, lo *Node

func higher(node *Node, value int) *Node {
	hi = new(Node)
	doHigher(node, value)
	return hi
}

func doHigher(node *Node, value int) {
	if node == nil {
		return
	}
	if node.Value > value {
		hi = node
		doHigher(node.Left, value)
	} else {
		lo = node
		doHigher(node.Right, value)
	}
}

func solve(n, k int) {
	MAX := n + 2
	tree = make([]int, MAX)
	node := &Node{nil, 0, 100, nil}
	insert(node, n+1)
	pos := make([]int, MAX)
	nrm := make([]bool, MAX)
	for i := 1; i <= n; i++ {
		pos[readInt()] = i
	}
	for i := 0; i < k; i++ {
		nrm[readInt()] = true
	}
	var ans int64
	for i := 1; i <= n; i++ {
		if nrm[i] {
			insert(node, pos[i])
		} else {
			r := higher(node, pos[i])
			l := lo
			rr, ll := r.Value-1, l.Value
			ans += int64(rr) - int64(ll) - int64(sum(rr)) + int64(sum(ll))
			update(pos[i], 1)
		}
	}
	O(ans, "\n")
}

func readInt() int {
	var o bool
	var i int
	b, err := r.ReadByte()
	for err == nil {
		if b != ' ' && b != '\n' && b != '\r' {
			i = i*10 + int(b) - 48
			o = true
		} else if o {
			break
		}
		b, err = r.ReadByte()
	}
	return i
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"time"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, k int
	for I(&n, &k) == nil {
		rand.Seed(time.Now().Unix())
		solve(n, k)
	}
	w.Flush()
}

var tree []int

func update(i, val int) {
	for i < len(tree) {
		tree[i] += val
		i += i & -i
	}
}

func sum(i int) int {
	var s int
	for i > 0 {
		s += tree[i]
		i -= i & -i
	}
	return s
}

type Node struct {
	Left     *Node
	Value    int
	Priority int
	Right    *Node
}

func newNode(value int) *Node {
	return &Node{nil, value, rand.Intn(100), nil}
}

func rightRotate(node *Node) *Node {
	res := node.Left
	node.Left = res.Right
	res.Right = node
	return res
}
func leftRotate(node *Node) *Node {
	res := node.Right
	node.Right = res.Left
	res.Left = node
	return res
}

func insert(node *Node, value int) *Node {
	if node == nil {
		return newNode(value)
	}
	if node.Value > value {
		node.Left = insert(node.Left, value)
		if node.Left.Priority > node.Priority {
			node = rightRotate(node)
		}
		return node
	}
	node.Right = insert(node.Right, value)
	if node.Right.Priority > node.Priority {
		node = leftRotate(node)
	}
	return node
}

var hi, lo *Node

func higher(node *Node, value int) *Node {
	hi = new(Node)
	doHigher(node, value)
	return hi
}

func doHigher(node *Node, value int) {
	if node == nil {
		return
	}
	if node.Value > value {
		hi = node
		doHigher(node.Left, value)
	} else {
		lo = node
		doHigher(node.Right, value)
	}
}

func solve(n, k int) {
	MAX := n + 2
	tree = make([]int, MAX)
	node := &Node{nil, 0, 100, nil}
	insert(node, n+1)
	pos := make([]int, MAX)
	nrm := make([]bool, MAX)
	for i := 1; i <= n; i++ {
		pos[readInt(i == n)] = i
	}
	for i := 0; i < k; i++ {
		nrm[readInt(i == k-1)] = true
	}
	var ans int64
	for i := 1; i <= n; i++ {
		if nrm[i] {
			insert(node, pos[i])
		} else {
			r := higher(node, pos[i])
			l := lo
			rr, ll := r.Value-1, l.Value
			ans += int64(rr)
			ans -= int64(ll)
			ans -= int64(sum(rr))
			ans += int64(sum(ll))
			update(pos[i], 1)
		}
	}
	O(ans, "\n")
}

func readInt(endl bool) (n int) {
	var delim byte = ' '
	if endl {
		delim = '\n'
	}
	buf, _ := r.ReadBytes(delim)
	for _, v := range buf {
		if v != ' ' && v != '\n' && v != '\r' {
			n = n*10 + int(v-'0')
		}
	}
	return
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"time"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, k int
	for I(&n, &k) == nil {
		rand.Seed(time.Now().Unix())
		solve(n, k)
	}
	w.Flush()
}

var tree []int

func update(i, val int) {
	for i < len(tree) {
		tree[i] += val
		i += i & -i
	}
}

func sum(i int) int {
	var s int
	for i > 0 {
		s += tree[i]
		i -= i & -i
	}
	return s
}

type Node struct {
	Left     *Node
	Value    int
	Priority int
	Right    *Node
}

func newNode(value int) *Node {
	return &Node{nil, value, rand.Intn(100), nil}
}

func rightRotate(node *Node) *Node {
	res := node.Left
	node.Left = res.Right
	res.Right = node
	return res
}
func leftRotate(node *Node) *Node {
	res := node.Right
	node.Right = res.Left
	res.Left = node
	return res
}

func insert(node *Node, value int) *Node {
	if node == nil {
		return newNode(value)
	}
	if node.Value > value {
		node.Left = insert(node.Left, value)
		if node.Left.Priority > node.Priority {
			node = rightRotate(node)
		}
		return node
	}
	node.Right = insert(node.Right, value)
	if node.Right.Priority > node.Priority {
		node = leftRotate(node)
	}
	return node
}

var hi, lo *Node

func higher(node *Node, value int) *Node {
	hi = new(Node)
	doHigher(node, value)
	return hi
}

func doHigher(node *Node, value int) {
	if node == nil {
		return
	}
	if node.Value > value {
		hi = node
		doHigher(node.Left, value)
	} else {
		lo = node
		doHigher(node.Right, value)
	}
}

func solve(n, k int) {
	MAX := n + 2
	tree = make([]int, MAX)
	node := &Node{nil, 0, 100, nil}
	insert(node, n+1)
	pos := make([]int, MAX)
	nrm := make([]bool, MAX)
	r.ReadLine()
	for i := 1; i <= n; i++ {
		var delim byte = ' '
		if i == n {
			delim = '\n'
		}
		ooo, _ := r.ReadBytes(delim)
		pos[toInt(ooo)] = i
	}
	for i := 0; i < k; i++ {
		var delim byte = ' '
		if i == k-1 {
			delim = '\n'
		}
		ooo, _ := r.ReadBytes(delim)
		nrm[toInt(ooo)] = true
	}
	var ans int64
	for i := 1; i <= n; i++ {
		if nrm[i] {
			insert(node, pos[i])
		} else {
			r := higher(node, pos[i])
			l := lo
			rr, ll := r.Value-1, l.Value
			ans += int64(rr)
			ans -= int64(ll)
			ans -= int64(sum(rr))
			ans += int64(sum(ll))
			update(pos[i], 1)
		}
	}
	O(ans, "\n")
}

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		if v != ' ' && v != '\n' && v != '\r' {
			n = n*10 + int(v-'0')
		}
	}
	return
}

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, k int
	for I(&n, &k) == nil {
		solve(n, k)
	}
	w.Flush()
}

var tree []int

func update(i, val int) {
	for i < len(tree) {
		tree[i] += val
		i += i & -i
	}
}

func sum(i int) int {
	var s int
	for i > 0 {
		s += tree[i]
		i -= i & -i
	}
	return s
}

type Node struct {
	Left  *Node
	Value int
	Right *Node
}

func insert(node *Node, value int) *Node {
	if node == nil {
		return &Node{nil, value, nil}
	}
	if node.Value > value {
		node.Left = insert(node.Left, value)
		return node
	}
	node.Right = insert(node.Right, value)
	return node
}

var hi *Node

func higher(node *Node, value int) *Node {
	hi = new(Node)
	doHigher(node, value)
	return hi
}

func doHigher(node *Node, value int) {
	if node == nil {
		return
	}
	if node.Value > value {
		hi = node
		doHigher(node.Left, value)
	} else {
		doHigher(node.Right, value)
	}
}

var lo *Node

func lower(node *Node, value int) *Node {
	lo = new(Node)
	doLower(node, value)
	return lo
}

func doLower(node *Node, value int) {
	if node == nil {
		return
	}
	if node.Value < value {
		lo = node
		doLower(node.Right, value)
	} else {
		doLower(node.Left, value)
	}
}

func solve(n, k int) {
	MAX := n + 10
	tree = make([]int, MAX)
	node := new(Node)
	insert(node, 0)
	insert(node, n+1)
	pos := make([]int, MAX)
	nrm := make([]bool, MAX)
	var tmp int
	for i := 1; i <= n; i++ {
		I(&tmp)
		pos[tmp] = i
	}
	for i := 0; i < k; i++ {
		I(&tmp)
		nrm[tmp] = true
	}
	var ans int
	for i := 1; i <= n; i++ {
		if nrm[i] {
			insert(node, pos[i])
		} else {
			r := higher(node, pos[i])
			l := lower(node, pos[i])
			rr, ll := r.Value-1, l.Value
			ans += rr - ll - (sum(rr) - sum(ll))
			update(pos[i], 1)
		}
	}
	O(ans, "\n")
}


Someone give a strange birthday present to Ivan. It is hedgehog — connected undirected graph in which one vertex has degree at least $$$3$$$ (we will call it center) and all other vertices has degree 1. Ivan thought that hedgehog is too boring and decided to make himself $$$k$$$-multihedgehog.Let us define $$$k$$$-multihedgehog as follows: $$$1$$$-multihedgehog is hedgehog: it has one vertex of degree at least $$$3$$$ and some vertices of degree 1. For all $$$k \ge 2$$$, $$$k$$$-multihedgehog is $$$(k-1)$$$-multihedgehog in which the following changes has been made for each vertex $$$v$$$ with degree 1: let $$$u$$$ be its only neighbor; remove vertex $$$v$$$, create a new hedgehog with center at vertex $$$w$$$ and connect vertices $$$u$$$ and $$$w$$$ with an edge. New hedgehogs can differ from each other and the initial gift. Thereby $$$k$$$-multihedgehog is a tree. Ivan made $$$k$$$-multihedgehog but he is not sure that he did not make any mistakes. That is why he asked you to check if his tree is indeed $$$k$$$-multihedgehog.

First line of input contains $$$2$$$ integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{9}$$$) — number of vertices and hedgehog parameter. Next $$$n-1$$$ lines contains two integers $$$u$$$ $$$v$$$ ($$$1 \le u, \,\, v \le n; \,\, u \ne v$$$) — indices of vertices connected by edge. It is guaranteed that given graph is a tree.

Print "Yes" (without quotes), if given graph is $$$k$$$-multihedgehog, and "No" (without quotes) otherwise.

Note2-multihedgehog from the first example looks like this:Its center is vertex $$$13$$$. Hedgehogs created on last step are: [4 (center), 1, 2, 3], [6 (center), 7, 8, 9], [5 (center), 10, 11, 12, 13].Tree from second example is not a hedgehog because degree of center should be at least $$$3$$$.

package main

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

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

	var n, k, v, w int
	Fscan(in, &n, &k)
	if n < 4 {
		Fprint(out, "No")
		return
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	vis := make([]bool, n)
	centers := map[int]int{}
	for i, gi := range g {
		if len(gi) == 1 {
			vis[i] = true
			centers[gi[0]]++
		}
	}
	for {
		k--
		newCenters := map[int]int{}
		for v, cnt1 := range centers {
			if cnt1 < 3 {
				Fprint(out, "No")
				return
			}
			ct := -1
			for _, w := range g[v] {
				if vis[w] {
					cnt1++
				} else {
					if centers[w] > 0 || ct != -1 {
						Fprint(out, "No")
						return
					}
					ct = w
				}
			}
			if ct == -1 {
				if k == 0 {
					Fprint(out, "Yes")
				} else {
					Fprint(out, "No")
				}
				return
			}
			vis[v] = true
			newCenters[ct]++
		}
		centers = newCenters
	}
}

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

package main

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

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

	var n, k, v, w, ct int
	Fscan(in, &n, &k)
	if n < 4 {
		Fprint(out, "No")
		return
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	vis := make([]bool, n)
	centers := map[int]bool{}
	for i, gi := range g {
		if len(gi) == 1 {
			vis[i] = true
			centers[gi[0]] = true
		}
	}
	for {
		k--
		newCenters := map[int]bool{}
		for v := range centers {
			cnt1, cnt2 := 0, 0
			for _, w := range g[v] {
				if vis[w] {
					cnt1++
				} else {
					if centers[w] {
						Fprint(out, "No")
						return
					}
					cnt2++
					ct = w
				}
			}
			if cnt1 < 3 || cnt2 > 1 {
				Fprint(out, "No")
				return
			}
			if cnt2 == 0 {
				if k == 0 {
					Fprint(out, "Yes")
				} else {
					Fprint(out, "No")
				}
				return
			}
			vis[v] = true
			newCenters[ct] = true
		}
		centers = newCenters
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1067B(_r io.Reader, _w io.Writer) (ans bool) {
	in := bufio.NewReader(_r)
	defer func() {
		if ans {
			Fprint(_w, "Yes")
		} else {
			Fprint(_w, "No")
		}
	}()

	var n, k, v, w int
	Fscan(in, &n, &k)
	if n < 4 {
		return false
	}
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	vis := make([]bool, n+1)
	centers := map[int]int{}
	for i, gi := range g {
		if len(gi) == 1 {
			vis[i] = true
			centers[gi[0]]++
		}
	}
	for {
		k--
		newCenters := map[int]int{}
		for v, cnt1 := range centers {
			if cnt1 < 3 {
				return false
			}
			ct := 0
			for _, w := range g[v] {
				if !vis[w] {
					if ct != 0 || centers[w] > 0 {
						return false
					}
					ct = w
				}
			}
			if ct == 0 {
				return k == 0
			}
			vis[v] = true
			newCenters[ct]++
		}
		centers = newCenters
	}
}

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

package main

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

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

	var n, k, v, w int
	Fscan(in, &n, &k)
	if n < 4 {
		Fprint(out, "No")
		return
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	vis := make([]bool, n)
	centers := map[int]int{}
	for i, gi := range g {
		if len(gi) == 1 {
			vis[i] = true
			centers[gi[0]]++
		}
	}
	for {
		k--
		newCenters := map[int]int{}
		for v, cnt1 := range centers {
			if cnt1 < 3 {
				Fprint(out, "No")
				return
			}
			ct := -1
			for _, w := range g[v] {
				if !vis[w] {
					if centers[w] > 0 || ct != -1 {
						Fprint(out, "No")
						return
					}
					ct = w
				}
			}
			if ct == -1 {
				if k == 0 {
					Fprint(out, "Yes")
				} else {
					Fprint(out, "No")
				}
				return
			}
			vis[v] = true
			newCenters[ct]++
		}
		centers = newCenters
	}
}

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

package main

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

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

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

	vis := make([]bool, n)
	centers := map[int]bool{}
	for i, gi := range g {
		if len(gi) == 1 {
			vis[i] = true
			centers[gi[0]] = true
		}
	}
	for {
		k--
		newCenters := map[int]bool{}
		for v := range centers {
			cnt1, cnt2 := 0, 0
			for _, w := range g[v] {
				if vis[w] {
					cnt1++
				} else {
					cnt2++
					ct = w
				}
			}
			if cnt1 < 3 || cnt2 > 1 {
				Fprint(out, "No")
				return
			}
			if cnt2 == 0 {
				if k == 0 {
					Fprint(out, "Yes")
				} else {
					Fprint(out, "No")
				}
				return
			}
			vis[v] = true
			newCenters[ct] = true
		}
		centers = newCenters
	}
}

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


Positive integer $$$x$$$ is called divisor of positive integer $$$y$$$, if $$$y$$$ is divisible by $$$x$$$ without remainder. For example, $$$1$$$ is a divisor of $$$7$$$ and $$$3$$$ is not divisor of $$$8$$$.We gave you an integer $$$d$$$ and asked you to find the smallest positive integer $$$a$$$, such that  $$$a$$$ has at least $$$4$$$ divisors;  difference between any two divisors of $$$a$$$ is at least $$$d$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 3000$$$) — the number of test cases. The first line of each test case contains a single integer $$$d$$$ ($$$1 \leq d \leq 10000$$$).

For each test case print one integer $$$a$$$ — the answer for this test case.

NoteIn the first test case, integer $$$6$$$ have following divisors: $$$[1, 2, 3, 6]$$$. There are $$$4$$$ of them and the difference between any two of them is at least $$$1$$$. There is no smaller integer with at least $$$4$$$ divisors.In the second test case, integer $$$15$$$ have following divisors: $$$[1, 3, 5, 15]$$$. There are $$$4$$$ of them and the difference between any two of them is at least $$$2$$$.The answer $$$12$$$ is INVALID because divisors are $$$[1, 2, 3, 4, 6, 12]$$$. And the difference between, for example, divisors $$$2$$$ and $$$3$$$ is less than $$$d=2$$$.

package main

import "fmt"
var arr [20100]bool
func erat()int{
	n:=len(arr)
	for i:=2;i<n;i++{
		arr[i] = true
	}
	p:=2
	for p*p<n{
		if arr[p] == true{
			for j:=2;j*p<n;j++{
				arr[p*j]=false
			}
		}
		for i:=p+1;;i++{
			if arr[i]==true{
				p = i
				break
			}
		}
	}
	return -1
}
func find(d int) int{
	for i:=d;;i++{
		if arr[i]==true{
			return i
		}
	}
}
func main(){
	var t,d int
	erat()
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		fmt.Scan(&d)
			erd:=find(1+d)
			fmt.Println(erd*find(erd+d))

	}

}

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	// Sieve of Eratosthenes
	notPrime := make([]bool, 1000005)
	for i := 2; i < len(notPrime); i++ {
		if !notPrime[i] {
			for j := i * 2; j < len(notPrime); j += i {
				notPrime[j] = true
			}
		}
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var d int
		Fscan(in, &d)

		first := 1 + d
		for notPrime[first] {
			first++
		}
		second := first + d
		for notPrime[second] {
			second++
		}
		Fprintln(out, first * second)
	}

}

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

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

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	// Fill the prime table
	isPrime := make([]bool, 1000005)
	for i, _ := range isPrime {
		isPrime[i] = true
	}
	for i := 2; i < len(isPrime); i++ {
		if isPrime[i] {
			for j := i * 2; j < len(isPrime); j += i {
				isPrime[j] = false
			}
		}
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var d int
		Fscan(in, &d)

		first := 1 + d
		for !isPrime[first] {
			first++
		}
		second := first + d
		for !isPrime[second] {
			second++
		}
		Fprintln(out, first * second)
	}

}

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

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

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func ScanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func ScanInt() int {
	return int(ScanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		d := ScanInt()

		var p, q int

		for p = 1 + d; ; p++ {
			isPrime := true

			for i := 2; isPrime && i*i <= p; i++ {
				if p%i == 0 {
					isPrime = false
				}
			}

			if isPrime {
				break
			}
		}

		for q = p + d; ; q++ {
			isPrime := true

			for i := 2; isPrime && i*i <= q; i++ {
				if q%i == 0 {
					isPrime = false
				}
			}

			if isPrime {
				break
			}
		}

		fmt.Println(p * q)
	}
}

package main

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

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

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &d)

		solve(d)

	}

}

func solve(d int) {

	front := d + 1

	for !PrimeOrNot(front) {
		front++
	}

	//fmt.Println(front)

	behind := front + d

	for !PrimeOrNot(behind) {
		behind++
	}

	//fmt.Println(behind)

	fmt.Fprintln(out, front*behind)

}

//判斷是否為質數
func PrimeOrNot(num int) bool {

	for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			return false
		}
	}

	return true
}

package main

import "fmt"

func main() {
	var (
		t, d int
		ans  []int
	)
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&d)
		ans = append(ans, solve(d))
	}

	for _, i := range ans {
		fmt.Println(i)
	}
}

func isPrime(x int) bool {
	for i := 2; i*i <= x; i++ {
		if x%i == 0 {
			return false
		}
	}
	return true
}

func solve(d int) int {
	ans := [3]int{1}

	for i := 1; i < 3; i++ {
		for x := ans[i-1] + d; ; x++ {
			if isPrime(x) {
				ans[i] = x
				break
			}
		}
	}

	return ans[2] * ans[1]
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1e6), 1e6)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

var sieve = make([]bool, 20015)

func init() {
	for i := 2; i < 20015; i++ {
		for j := i + i; j < len(sieve); j += i {
			sieve[j] = true
		}
	}
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		d := sint64()
		printf("%d\n", sol(d))
	}
}

func sol(d int64) int64 {
	d2 := nextPrime(1 + d)
	d3 := nextPrime(d2 + d)
	return d2 * d3
}

func nextPrime(from int64) int64 {
	for i := from; i < int64(len(sieve)); i++ {
		if !sieve[i] {
			return i
		}
	}
	panic("x")
}

package main

import "fmt"

func main(){
	var t,d int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		fmt.Scan(&d)
		fmt.Println((1+d)*(1+2*d))
	}

}

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var d int
		Fscan(in, &d)

		Fprintln(out, (1+d) * (1+2*d))
	}

}

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

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

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func ScanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func ScanInt() int {
	return int(ScanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		d := ScanInt()

		if d == 1 {
			fmt.Println(6)
		} else {
			for p := 3 + d; ; p++ {
				isPrime := true

				for i := 2; isPrime && i*i <= p; i++ {
					if p%i == 0 {
						isPrime = false
					}
				}

				if isPrime {
					fmt.Println(3 * p)
					break
				}
			}
		}
	}
}

package main

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

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

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &d)

		solve(d)

	}

}

func solve(d int) {

	fmt.Fprintln(out, (d+1)*((d+1)+d))

}


Arkady is playing Battleship. The rules of this game aren't really important.There is a field of $$$n \times n$$$ cells. There should be exactly one $$$k$$$-decker on the field, i. e. a ship that is $$$k$$$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$) — the size of the field and the size of the ship. The next $$$n$$$ lines contain the field. Each line contains $$$n$$$ characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).

Output two integers — the row and the column of a cell that belongs to the maximum possible number of different locations of the ship. If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.

NoteThe picture below shows the three possible locations of the ship that contain the cell $$$(3, 2)$$$ in the first sample.

package main

import (
	"fmt"
)

func main() {
	var n, k int
	var str [105]string
	var cnt [105][105]int

	fmt.Scan(&n, &k)
	for i := 0; i < n; i++ {
		fmt.Scan(&str[i])
	}

	res := 0
	resx, resy := 1, 1

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {

			if str[i][j] == '.' && j + k - 1 < n {
				for r := j; r < j + k; r++ {
					cnt[i][r]++
					if str[i][r] != '.' {
						for rr := r; rr >= j; rr-- {
							cnt[i][rr]--
						}
						break
					}
				}
			}

			if str[i][j] == '.' && i + k - 1 < n {
				for r := i; r < i + k; r++ {
					cnt[r][j]++
					if str[r][j] != '.' {
						for rr := r; rr >= i; rr-- {
							cnt[rr][j]--
						}
						break
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if cnt[i][j] > res {
				res = cnt[i][j]
				resx = i + 1
				resy = j + 1
			}
		}
	}
	fmt.Println(resx, resy)

}

package main

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

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var n, k, advance, back int
	var colMax, rowMax, max = 0, 0, 0
	fmt.Fscanf(reader, "%d %d\n", &n, &k)

	var buf []byte
	// instantiate field
	field := make([][]int, n)
	for i := range field {
		field[i] = make([]int, n)
	}

	for i := range field {
		buf, _ = reader.ReadBytes('\n')
		for j := range buf {
			switch buf[j] {
			case '#':
				field[i][j] = -1
			case '.':
				// upwards
				if i - (k - 1) >= 0 {
					for advance = 0; advance < k && i - advance >= 0; advance++ {
						if field[i - advance][j] != -1 {
							field[i - advance][j] += 1
							if max < field[i - advance][j] {
								max = field[i - advance][j]
								rowMax = i - advance
								colMax = j
							}
						} else {
							// backtrack
							for back = advance - 1; back >= 0; back-- {
								field[i - back][j] -= 1
								if rowMax == i - back && colMax == j {
									max -= 1
								}
							}
							break
						}
					}
				}
				// left
				if j - (k - 1) >= 0 {
					for advance = 0; advance < k && j - advance >= 0; advance++ {
						if field[i][j - advance] != -1 {
							field[i][j - advance] += 1
							if max < field[i][j - advance] {
								max = field[i][j - advance]
								rowMax = i
								colMax = j - advance
							}
						} else {
							// backtrack
							for back = advance - 1; back >= 0; back-- {
								field[i][j - back] -= 1
								if rowMax == i && colMax == j - back {
									max -= 1
								}
							}
							break
						}
					}
				}
			default:
			}
		}
	}

	fmt.Println(rowMax + 1, colMax + 1)
}

package main

import (
	"fmt"
)

func main() {
	var n, k int
	var str [105]string
	var cnt [105][105]int

	fmt.Scan(&n, &k)
	for i := 0; i < n; i++ {
		fmt.Scan(&str[i])
	}

	res := 0
	resx, resy := 0, 0

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {

			if str[i][j] == '.' && j + k - 1 < n {
				for r := j; r < j + k; r++ {
					cnt[i][r]++
					if str[i][r] != '.' {
						for rr := r; rr >= j; rr-- {
							cnt[i][rr]--
						}
						break
					}
				}
			}

			if str[i][j] == '.' && i + k - 1 < n {
				for r := i; r < i + k; r++ {
					cnt[r][j]++
					if str[r][j] != '.' {
						for rr := r; rr >= i; rr-- {
							cnt[rr][j]--
						}
						break
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if cnt[i][j] > res {
				res = cnt[i][j]
				resx = i + 1
				resy = j + 1
			}
		}
	}
	fmt.Println(resx, resy)

}