You are given $$$n$$$ words of equal length $$$m$$$, consisting of lowercase Latin alphabet letters. The $$$i$$$-th word is denoted $$$s_i$$$.In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example:  you can change 'e' to 'd' or to 'f';  'a' can only be changed to 'b';  'z' can only be changed to 'y'. The difference between two words is the minimum number of moves required to make them equal. For example, the difference between "best" and "cost" is $$$1 + 10 + 0 + 0 = 11$$$.Find the minimum difference of $$$s_i$$$ and $$$s_j$$$ such that $$$(i < j)$$$. In other words, find the minimum difference over all possible pairs of the $$$n$$$ words.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 50$$$, $$$1 \leq m \leq 8$$$) — the number of strings and their length respectively. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single string $$$s_i$$$ of length $$$m$$$, consisting of lowercase Latin letters.

For each test case, print a single integer — the minimum difference over all possible pairs of the given strings.

6

2 4

best

cost

6 3

abb

zba

bef

cdu

ooo

zzz

2 7

aaabbbc

bbaezfe

3 2

ab

ab

ab

2 8

aaaaaaaa

zzzzzzzz

3 1

a

u

y

NoteFor the second test case, one can show that the best pair is ("abb","bef"), which has difference equal to $$$8$$$, which can be obtained in the following way: change the first character of the first string to 'b' in one move, change the second character of the second string to 'b' in $$$3$$$ moves and change the third character of the second string to 'b' in $$$4$$$ moves, thus making in total $$$1 + 3 + 4 = 8$$$ moves.For the third test case, there is only one possible pair and it can be shown that the minimum amount of moves necessary to make the strings equal is $$$35$$$.For the fourth test case, there is a pair of strings which is already equal, so the answer is $$$0$$$.

package main

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

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

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

	t := 0
	fmt.Fscan(in, &t)
	for k := 0; k < t; k++ {
		n, m := 0, 0
		fmt.Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := int(1e9)
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				str1 := a[i]
				str2 := a[j]
				tmp := 0
				for x := 0; x < m; x++ {
					tmp += abs(int(str1[x]) - int(str2[x]))
				}
				ans = min(ans, tmp)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

package main

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

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

	n := readInt(in)
	for i := 0; i < n; i++ {
		solution(in, out)
	}
}

func solution(in *bufio.Reader, out *bufio.Writer) {
	arr := readArrInt(in)
	n := arr[0]
	words := make([]string, n)
	for i := 0; i < n; i++ {
		words[i] = readLine(in)
	}

	min := math.MaxInt
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			if i == j {
				continue
			}

			sum := 0
			for k := 0; k < len(words[i]); k++ {
				sum += Abs(int(words[i][k]) - int(words[j][k]))
			}

			if sum < min {
				min = sum
			}
		}
	}

	out.WriteString(strconv.Itoa(min) + "\n")

}

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main
import (
    "fmt"
    "os"
    "bufio"
)
const INF = int(1e9)
func abs(x int) int {
    if x<0 {
        x = -x
    }
    return x
}
func main() {
    r := bufio.NewReader(os.Stdin)
    w := bufio.NewWriter(os.Stdout)
    defer w.Flush()

    var t int
    fmt.Fscan(r, &t)
    for _i:=0;_i<t;_i++ {
        var n,m int
        fmt.Fscan(r, &n, &m)
        s := make([]string, n)
        for i:=0;i<n;i++ {
            fmt.Fscan(r, &s[i])
        }
        ans := INF
        for i:=0;i<n;i++ {
            for j:=0;j<i;j++ {
                res := 0
                for k:=0;k<m;k++ {
                    res += abs(int(s[i][k])-int(s[j][k]))
                }
                if ans>res {
                    ans=res
                }
            }
        }
        fmt.Fprintln(w, ans)
    }
}

package main

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

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

func solve() string {
	var n, m int
	fmt.Fscan(in, &n, &m)
	var words = make([]string, n)
	for i := range words {
		fmt.Fscan(in, &words[i])
	}
	var res = math.MaxInt
	var temp int
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			temp = 0
			for k := 0; k < m; k++ {
				if words[i][k] > words[j][k] {
					temp += int(words[i][k] - words[j][k])
				} else {
					temp += int(words[j][k] - words[i][k])
				}
			}
			if temp < res {
				res = temp
			}
		}
	}

	return strconv.FormatInt(int64(res), 10)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

package main

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

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

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

	var T int
	var n int
	var sn int
	var x string

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &sn)

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

		ans := 1<<30
		for i, v := range row {
			for j := i + 1; j < n; j++ {
				t := 0
				vv := row[j]
				for k := 0; k < sn; k++ {
					if vv[k] >v[k] {
						t += int(vv[k]-v[k])
					} else {
						t += int(v[k]-vv[k])
					}
				}
				if t < ans {
					ans = t
				}
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var N,T,Q,sum int
	var S string

	//fmt.Fscan(in, &N)

	//num := make(map[int]int)
	//全体の個数
	fmt.Fscan(in, &N)

	for i := 0;i < N;i++{
		min := 100000000
		fmt.Fscan(in, &T,&Q)
		list := make([][]int,T)
		//文字の個数
		for j := 0;j < T;j++{
			fmt.Fscan(in, &S)
			val := make([]int,Q)
			for number,k := range S{
				val[number] = int(k)
			}
			list[j] = val
		}

		//文字列の長さ
		for k := 0;k < T-1;k++{
			for l := k+1;l < T;l++{
				for m := 0;m < Q;m++{
					a := list[k][m]-list[l][m]
					sum += int(math.Abs(float64(a)))
				}
				if min > sum {
					min = sum
				}
				sum = 0
			}
		}
		fmt.Println(min)
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
var letters = map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 6,
	"g": 7,
	"h": 8,
	"i": 9,
	"j": 10,
	"k": 11,
	"l": 12,
	"m": 13,
	"n": 14,
	"o": 15,
	"p": 16,
	"q": 17,
	"r": 18,
	"s": 19,
	"t": 20,
	"u": 21,
	"v": 22,
	"w": 23,
	"x": 24,
	"y": 25,
	"z": 26,
}
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,m int
		fmt.Fscan(in,&n,&m)
		list:= make([]string,n)
		for j:=0;j<n;j++{
			fmt.Fscan(in,&list[j])
		}
		smallest:=999
		for j:=0;j<n-1;j++{
			for l:=j+1;l<n;l++{
				tally:=0
				for k:=0;k<m;k++{
					tally+=abs(letters[string(list[j][k])]-letters[string(list[l][k])])
				}
				if tally <= smallest {
					smallest= tally
				}
			}
		}
		fmt.Fprintln(out,smallest)
	}
}
func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}

package main

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

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

func handle(a string, b string) int {
	cnt := 0
	for i := 0; i < len(a); i++ {
		cnt += int(math.Abs(float64(a[i]) - float64(b[i])))
	}
	return cnt
}

func main() {
	defer out.Flush()
	var t, n, m int
	for fmt.Fscan(in, &t); t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		s := make([]string, n)
		for i := range s {
			fmt.Fscan(in, &s[i])
		}
		res := 100000
		for i := 0; i < n; i++ {
			for j := 0; j < i; j++ {
				tmp := handle(s[i], s[j])
				if tmp < res {
					res = tmp
				}
			}
		}
		fmt.Fprintln(out, res)
	}
}

package main

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

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

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

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n, m int
		fmt.Fscan(in, &n, &m)
		a := make([]string, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
		}
		ans := math.MaxInt32
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				w1 := a[i]
				w2 := a[j]
				now := 0
				for k := 0; k < m; k++ {
					now += int(math.Abs(float64(int(w1[k]) - int(w2[k]))))
				}
				ans = min(ans, now)
			}
		}
		fmt.Println(ans)
	} //fim do loop de test cases
} // fim main

package main

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

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

func (in *CF1676C) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1676C) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1676C
Date: 13/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1676/C
Problem: CF1676C
**/
func (in *CF1676C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		arr := make([]string, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextString()
		}
		min := -1
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if i != j {
					diff := 0
					for p := 0; p < m; p++ {
						d := int(math.Abs(float64(arr[i][p]) - float64(arr[j][p])))
						diff += d
					}
					if min == -1 {
						min = diff
					} else if min > diff {
						min = diff
					}

				}
			}
		}
		fmt.Println(min)
	}
}

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

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

package main

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

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

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

func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, m int
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := math.MaxInt
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := 0
				for k := 0; k < m; k++ {
					diff += Abs(int(a[i][k]) - int(a[j][k]))
				}
				if diff < ans {
					ans = diff
				}
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, m int
		fmt.Fscan(in, &n, &m)
		s := make([]string, n)
		for i := 0;i < n; i++ {
			fmt.Fscan(in, &s[i])
		}
		min := func(a, b int) int {
			if a < b {
				return a
			}
			return b
		}
		abs := func(a, b int) int {
			if a > b {
				return a - b
			}
			return b - a
		}
		var ans int = 1e9

		for i := 0;i < n; i++ {
			for j := i + 1;j < n; j++ {
				temp := 0
				for k := 0;k < m; k++ {
					temp += abs(int(s[i][k]), int(s[j][k]))
				}
				ans = min(ans, temp)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

func toInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	testCases, _ := strconv.Atoi(scanner.Text())

	for idx := 0; idx < testCases; idx++ {
		scanner.Scan()
		splitted := strings.Split(scanner.Text(), " ")
		words, length := toInt(splitted[0]), toInt(splitted[1])

		wds := make([]string, words)
		for idx := 0; idx < words; idx++ {
			scanner.Scan()
			wds[idx] = scanner.Text()
		}

		minimum := math.MaxInt
		for i := 0; i < words; i++ {
			for j := i + 1; j < words; j++ {
				diff := 0
				for l := 0; l < length; l++ {
					diff += abs(int(wds[i][l]) - int(wds[j][l]))
				}
				if diff < minimum {
					minimum = diff
				}
			}
		}
		fmt.Println(minimum)
	}
}

package main

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

func getValue(s string, t string, m int) int {
	cnt := 0
	for i := 0; i < m; i++ {
		cnt += int( math.Abs( float64( int(rune(s[i])-96) - int(rune(t[i])-96) ) ) )
	}
	return cnt
}

func main() {
	var (
		t int
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Scanf("%d\n", &t)

	for t > 0 {
		t--

		text, _ := reader.ReadString('\n')
		ninps := strings.Fields(text)
		n, _ := strconv.Atoi(ninps[0])
		m, _ := strconv.Atoi(ninps[1])

		x := make([]string, n)

		for i := 0; i < n; i++ {
			s, _ := reader.ReadString('\n')
			if s[len(s)-1] == 10 {
				s = s[:len(s)-1]
			}
			if s[len(s)-1] == 13 {
				s = s[:len(s)-1]
			}
			x[i] = s
		}

		min := math.MaxInt32
		for i := 0; i < n-1; i++ {
			for j := i+1; j < n; j++ {
				a := getValue(x[i], x[j], m)
				if a < min {
					min = a
					//fmt.Println("M: ", (i+1), (j+1))
				}
			}
		}
		fmt.Println(min)
	}
}

package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func maxFloat(a float64, nums ...float64) float64 {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

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

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

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

		minDist := math.MaxInt

		for i := 0; i < n; i++ {
			s1 := arr[i]
			for j := i + 1; j < n; j++ {
				s2 := arr[j]
				curDist := 0
				for z := 0; z < m; z++ {
					curDist += int(math.Abs(float64(s1[z]) - float64(s2[z])))
				}
				minDist = min(minDist, curDist)
			}
		}

		fmt.Fprintln(out, minDist)
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)
func slove() {
	var n, m int
	fmt.Fscan(r, &n, &m)
	ss := make([]string, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &ss[i])
	}
	ans := 26*8
	for i := range ss {
		for j:=i+1;j<n;j++ {
			ans = min(ans, diff(ss[i], ss[j]))
		}
	}
	fmt.Fprintln(w, ans)
}

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

func diff(a, b string) (ans int) {
	for i := range a {
		ans += int(abs(int(a[i])-int(b[i])))
	}
	return
}

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

package main

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

func abs(n int) int {
	if n < 0 {
		return n * -1
	}
	return n
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

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

		fn := func(s1, s2 string) int {
			var d int
			for i := range s1 {
				a := int(s1[i] - 'a')
				b := int(s2[i] - 'a')
				d += abs(a - b)
			}
			return d
		}
		min := math.MaxInt32
		for i := 0; i < n-1; i++ {
			for j := i + 1; j < n; j++ {
				if fn(words[i], words[j]) < min {
					min = fn(words[i], words[j])
				}
			}
		}
		fmt.Fprintln(out, min)
	}
}

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()

	solve := func(curCase int) {
		var n ,m int
		Fscan(in, &n,&m)
		a := make([]string, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		mi := int(1e9)
		for i, v := range a {
			for _, w := range a[:i] {
				s := 0
				for k := range v {
					s+=abs(int(v[k]-'a')-int(w[k]-'a'))
				}
				mi = min(mi, s)
			}
		}
		Fprintln(out, mi)
	}

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

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

}

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

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

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

package main

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

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

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

	t := 0
	fmt.Fscan(in, &t)
	for k := 0; k < t; k++ {
		n, m := 0, 0
		fmt.Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := int(1e9)
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				str1 := a[i]
				str2 := a[j]
				tmp := 0
				for x := 0; x < m; x++ {
					tmp += abs(int(str1[x]) - int(str2[x]))
				}
				ans = min(ans, tmp)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

func abs(x int) int {
	fmt.Println("xxx", x)
	if x < 0 {
		return -x
	}
	return x
}

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

package main

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

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

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

	t := 0
	fmt.Fscan(in, &t)
	for k := 0; k < t; k++ {
		n, m := 0, 0
		fmt.Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := int(1e8)
		sort.Slice(a, func(i, j int) bool {
			return a[i] < a[j]
		})
		for i := 1; i < n; i++ {
			str1 := a[i-1]
			str2 := a[i]
			tmp := 0
			for j := 0; j < m; j++ {
				tmp += abs(int(str2[j] - str1[j]))
			}
			ans = min(ans, tmp)
		}
		fmt.Fprintln(out, ans)
	}
}

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

package main

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

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

	n := readInt(in)
	for i := 0; i < n; i++ {
		solution(in, out)
	}
}

func solution(in *bufio.Reader, out *bufio.Writer) {
	n := readInt(in)
	readInt(in)

	words := make([]string, n)
	for i := 0; i < n; i++ {
		words[i] = readLine(in)
	}

	min := math.MaxInt
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			if i == j {
				continue
			}

			sum := 0
			for k := 0; k < len(words[i]); k++ {
				sum += Abs(int(words[i][k] - words[j][k]))
			}

			if sum < min {
				min = sum
			}
		}
	}

	out.WriteString(strconv.Itoa(min) + "\n")

}

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main
import (
    "fmt"
    "os"
    "bufio"
)
const INF = int(1e9)
func abs(x int) int {
    if x<0 {
        x = -x
    }
    return x
}
func main() {
    r := bufio.NewReader(os.Stdin)
    w := bufio.NewWriter(os.Stdout)
    defer w.Flush()

    var t int
    fmt.Fscan(r, &t)
    for _i:=0;_i<t;_i++ {
        var n,m int
        fmt.Fscan(r, &n, &m)
        s := make([]string, n)
        for i:=0;i<n;i++ {
            fmt.Fscan(r, &s[i])
        }
        ans := INF
        for i:=0;i<n;i++ {
            for j:=0;j<i;j++ {
                res := 0
                for k:=0;k<m;k++ {
                    res += abs(int(s[i][k]-s[j][k]))
                }
                if ans>res {
                    ans=res
                }
            }
        }
        fmt.Fprintln(w, ans)
    }
}

package main

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

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

func solve() string {
	var n, m int
	fmt.Fscan(in, &n, &m)
	var words = make([]string, n)
	for i := range words {
		fmt.Fscan(in, &words[i])
	}
	var res = math.MaxInt
	var temp int
	sort.Strings(words)
	for i := 0; i < len(words)-1; i++ {
		temp = 0
		for j := 0; j < m; j++ {
			if words[i][j] > words[i+1][j] {
				temp += int(words[i][j] - words[i+1][j])
			} else {
				temp += int(words[i+1][j] - words[i][j])
			}
		}
		if temp < res {
			res = temp
		}
	}

	return strconv.FormatInt(int64(res), 10)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

package main

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

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

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

	var T int
	var n int
	var sn int
	var x string

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &sn)

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

		ans := 1<<30
		for i, v := range row {
			for j := i + 1; j < n; j++ {
				t := 0
				vv := row[j]
				for k := 0; k < sn; k++ {
					tt := int(vv[k]-v[k])
					if tt < 0 {
						tt = -tt
					}
					t += tt
				}
				if t < ans {
					ans = t
				}
			}
		}

		Fprintln(out, ans)
	}
}


Arthur owns a ski resort on a mountain. There are $$$n$$$ landing spots on the mountain numbered from $$$1$$$ to $$$n$$$ from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot.A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks.Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks.Arthur doesn't want to close too many spots. He will be happy to find any way to close at most $$$\frac{4}{7}n$$$ spots so that the remaining part is safe. Help him find any suitable way to do so.

The first line contains a single positive integer $$$T$$$ — the number of test cases. $$$T$$$ test case description follows. The first line of each description contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of landing spots and tracks respectively. The following $$$m$$$ lines describe the tracks. Each of these lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 \leq x < y \leq n$$$) — indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer $$$k$$$ ($$$0 \leq k \leq \frac{4}{7}n$$$) — the number of spots to be closed. In the next line, print $$$k$$$ distinct integers — indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize $$$k$$$. It can be shown that a suitable answer always exists.

NoteIn the first sample case, closing any two spots is suitable.In the second sample case, closing only the spot $$$1$$$ is also suitable.

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		E := make([][]int, m)
		for i := 0; i < m; i++ {
			E[i] = readNNums(reader, 2)
		}
		res := solve(n, E)
		var buf bytes.Buffer
		buf.WriteString(fmt.Sprintf("%d\n", len(res)))
		for i := 0; i < len(res); i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		fmt.Println(buf.String())
	}
}

func solve(n int, E [][]int) []int {
	out := make([][]int, n)
	for i := 0; i < n; i++ {
		out[i] = make([]int, 0, 2)
	}
	for _, e := range E {
		u, v := e[0], e[1]
		u--
		v--
		out[u] = append(out[u], v)
	}
	cnt := make([]int, n)
	res := make([]int, 0, 4*n/7)
	for i := 0; i < n; i++ {
		if cnt[i] > 1 {
			// need to remove it
			res = append(res, i+1)
		} else {
			// keep it
			for _, j := range out[i] {
				cnt[j] = max(cnt[j], cnt[i]+1)
			}
		}
	}

	return res
}

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

package main

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

const (
	mod = int64(998244353)
)

var cur []int
var inDegree []int
var cannotComeBlocker []int
var n, m int
var found bool
var edges [][]int
var ans []int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	tt := 1
	fmt.Fscanf(in, "%d\n", &tt)
	for t := 0; t < tt; t++ {
		n, m = 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &m)
		edges = make([][]int, n)
		o, p := 0, 0
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%d %d\n", &o, &p)
			o--
			p--
			edges[o] = append(edges[o], p)
		}
		cur = []int{}
		found = false
		inDegree = make([]int, n)
		cannotComeBlocker = make([]int, n)
		dfs(0)
		curMap := make(map[int]bool)
		for _, tmp := range ans {
			curMap[tmp] = true
		}
		fmt.Fprintf(out, "%d\n", n-len(ans))
		isFirst := true
		for i := 0; i < n; i++ {
			if !curMap[i] {
				if !isFirst {
					fmt.Fprintf(out, " ")
				} else {
					isFirst = false
				}
				fmt.Fprintf(out, "%d", i+1)
			}
		}
		fmt.Fprintf(out, "\n")
	}
	return
}

func dfs(i int) {
	//fmt.Println(i, cur, inDegree, cannotComeBlocker, n*4/7)
	if found {
		return
	}
	if (n - len(cur)) <= n*4/7 {
		//fmt.Println("wtf?")
		ans = make([]int, 0)
		for i := 0; i < len(cur); i++ {
			ans = append(ans, cur[i])
		}
		found = true
		return
	}
	if i == n {
		return
	}
	if cannotComeBlocker[i] > 0 {
		dfs(i + 1)
		return
	}
	if inDegree[i] == 0 {
		for _, next := range edges[i] {
			inDegree[next]++
		}
		cur = append(cur, i)
		dfs(i + 1)
		cur = cur[:len(cur)-1]
		for _, next := range edges[i] {
			inDegree[next]--
		}
	} else {
		//fmt.Println("debug", edges[i])
		for _, next := range edges[i] {
			cannotComeBlocker[next]++
		}
		cur = append(cur, i)
		dfs(i + 1)
		cur = cur[:len(cur)-1]
		for _, next := range edges[i] {
			cannotComeBlocker[next]--
		}
	}
	dfs(i + 1)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	I := bufio.NewReader(os.Stdin)
	O := bufio.NewWriter(os.Stdout)
	defer O.Flush()
	var t, n, m, v, w int
	for Fscan(I, &t); t > 0; t-- {
		Fscan(I, &n, &m)
		g := make([][]int, n+1)
		for ; m > 0; m-- {
			Fscan(I, &v, &w)
			g[w] = append(g[w], v)
		}
		a := []interface{}{}
		l := make([]int, n+1)
		for w, u := range g {
			for _, v := range u {
				if l[v]+1 > l[w] {
					l[w] = l[v] + 1
				}
			}
			if l[w] > 1 {
				l[w] = -1
				a = append(a, w)
			}
		}
		Fprintln(O, len(a))
		Fprintln(O, a...)
	}
}

package main

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

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

	var t, n, m, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		g := make([][]int, n+1)
		for ; m > 0; m-- {
			Fscan(in, &v, &w)
			g[w] = append(g[w], v)
		}
		ans := []interface{}{}
		dp := make([]int, n+1)
		for w, vs := range g {
			for _, v := range vs {
				if dp[v]+1 > dp[w] {
					dp[w] = dp[v] + 1
				}
			}
			if dp[w] > 1 {
				dp[w] = -1
				ans = append(ans, w)
			}
		}
		Fprintln(out, len(ans))
		Fprintln(out, ans...)
	}
}

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

package main

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

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

	for t := r(); t > 0; t-- {
		n := r()
		g := make([][]int, n+1)
		for m := r(); m > 0; m-- {
			v, w := r(), r()
			g[w] = append(g[w], v)
		}
		ans := []interface{}{}
		dp := make([]int, n+1)
		for w, vs := range g {
			for _, v := range vs {
				if dp[v]+1 > dp[w] {
					dp[w] = dp[v] + 1
				}
			}
			if dp[w] > 1 {
				dp[w] = -1
				ans = append(ans, w)
			}
		}
		Fprintln(out, len(ans))
		Fprintln(out, ans...)
	}
}

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

package main

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

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

	for t := r(); t > 0; t-- {
		n := r()
		g := make([][]int, n+1)
		for m := r(); m > 0; m-- {
			v, w := r(), r()
			g[w] = append(g[w], v)
		}
		c := 0
		s := []byte{}
		dp := make([]int, n+1)
		for w, vs := range g {
			for _, v := range vs {
				if dp[v]+1 > dp[w] {
					dp[w] = dp[v] + 1
				}
			}
			if dp[w] > 1 {
				dp[w] = -1
				c++
				s = append(s, strconv.Itoa(w)...)
				s = append(s, ' ')
			}
		}
		Fprintf(out, "%d\n%s\n", c, s)
	}
}

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

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		E := make([][]int, m)
		for i := 0; i < m; i++ {
			E[i] = readNNums(reader, 2)
		}
		res := solve(n, E)
		var buf bytes.Buffer
		buf.WriteString(fmt.Sprintf("%d\n", len(res)))
		for i := 0; i < len(res); i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		fmt.Println(buf.String())
	}
}

func solve(n int, E [][]int) []int {
	degree := make([]int, n)
	out := make([][]int, n)
	for i := 0; i < n; i++ {
		out[i] = make([]int, 0, 2)
	}
	for _, e := range E {
		u, v := e[0], e[1]
		u--
		v--
		out[u] = append(out[u], v)
		degree[v]++
	}
	res := make([]int, 0, 4*n/7)
	for i := 0; i < n; i++ {
		if degree[i] > 1 {
			res = append(res, i+1)
			for _, j := range out[i] {
				degree[j]--
			}
		}
	}

	return res
}

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		E := make([][]int, m)
		for i := 0; i < m; i++ {
			E[i] = readNNums(reader, 2)
		}
		res := solve(n, E)
		var buf bytes.Buffer

		for i := 0; i < len(res); i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		fmt.Println(buf.String())
	}
}

func solve(n int, E [][]int) []int {
	degree := make([]int, n)
	out := make([][]int, n)
	for i := 0; i < n; i++ {
		out[i] = make([]int, 0, 2)
	}
	for _, e := range E {
		u, v := e[0], e[1]
		u--
		v--
		out[u] = append(out[u], v)
		degree[v]++
	}
	res := make([]int, 0, 4*n/7)
	for i := 0; i < n; i++ {
		if degree[i] > 1 {
			res = append(res, i+1)
			for _, j := range out[i] {
				degree[j]--
			}
		}
	}

	return res
}

package main

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

const (
	mod = int64(998244353)
)

var cur []int
var inDegree []int
var cannotComeBlocker []int
var n, m int
var found bool
var edges [][]int
var ans []int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	tt := 1
	fmt.Fscanf(in, "%d\n", &tt)
	for t := 0; t < tt; t++ {
		n, m = 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &m)
		edges = make([][]int, n)
		o, p := 0, 0
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%d %d\n", &o, &p)
			edges[o] = append(edges[o], p)
		}
		cur = []int{}
		found = false
		inDegree = make([]int, n)
		cannotComeBlocker = make([]int, n)
		dfs(0)
		curMap := make(map[int]bool)
		for _, tmp := range ans {
			curMap[tmp] = true
		}
		fmt.Fprintf(out, "%d\n", n-len(ans))
		isFirst := true
		for i := 0; i < n; i++ {
			if !curMap[i] {
				if !isFirst {
					fmt.Fprintf(out, " ")
				} else {
					isFirst = false
				}
				fmt.Fprintf(out, "%d", i+1)
			}
		}
		fmt.Fprintf(out, "\n")
	}
	return
}

func dfs(i int) {
	//fmt.Println(cur)
	if i == n {
		return
	}
	if found {
		return
	}
	if (n - len(cur)) <= n*4/7 {
		ans = cur
		found = true
		return
	}
	if cannotComeBlocker[i] > 0 {
		dfs(i + 1)
		return
	}
	if inDegree[i] == 0 {
		for _, next := range edges[i] {
			inDegree[next]++
		}
		cur = append(cur, i)
		dfs(i + 1)
		cur = cur[:len(cur)-1]
		for _, next := range edges[i] {
			inDegree[next]--
		}
	} else {
		for _, next := range edges[i] {
			cannotComeBlocker[next]++
		}
		cur = append(cur, i)
		dfs(i + 1)
		cur = cur[:len(cur)-1]
		for _, next := range edges[i] {
			cannotComeBlocker[next]--
		}
	}
	dfs(i + 1)
}

package main

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

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

	var t, n, m, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		g := make([][]int, n+1)
		has := make([]bool, n+1)
		for ; m > 0; m-- {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			has[w] = true
		}

		ans := []interface{}{}
		ans2 := []interface{}{}
		q := []int{}
		for i := 1; i <= n; i++ {
			if !has[i] {
				ans = append(ans, i)
				q = append(q, i)
			}
		}
		dep := make([]int, n+1)
		for len(q) > 0 {
			v, q = q[0], q[1:]
			for _, w := range g[v] {
				if dep[w] == 0 {
					dep[w] = dep[v] + 1
					if dep[w]&1 == 0 {
						ans = append(ans, w)
					} else {
						ans2 = append(ans2, w)
					}
					q = append(q, w)
				}
			}
		}
		if len(ans) > len(ans2) {
			ans, ans2 = ans2, ans
		}
		Fprintln(out, len(ans))
		Fprintln(out, ans...)
	}
}

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

package main

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

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

	var t, n, m, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		g := make([][]int, n+1)
		has := make([]bool, n+1)
		for ; m > 0; m-- {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			has[w] = true
		}

		st := 1
		for ; st <= n && has[st]; st++ {
		}
		dep := make([]int, n+1)
		vs := []interface{}{st}
		vs2 := []interface{}{}
		q := []int{st}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			for _, w := range g[v] {
				if dep[w] == 0 {
					dep[w] = dep[v] + 1
					if dep[w]&1 == 0 {
						vs = append(vs, w)
					} else {
						vs2 = append(vs2, w)
					}
					q = append(q, w)
				}
			}
		}
		if len(vs) > len(vs2) {
			vs, vs2 = vs2, vs
		}
		Fprintln(out, len(vs))
		Fprintln(out, vs...)
	}
}

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


You are given a weighted tree consisting of $$$n$$$ vertices. Recall that a tree is a connected graph without cycles. Vertices $$$u_i$$$ and $$$v_i$$$ are connected by an edge with weight $$$w_i$$$.Let's define the $$$k$$$-coloring of the tree as an assignment of exactly $$$k$$$ colors to each vertex, so that each color is used no more than two times. You can assume that you have infinitely many colors available. We say that an edge is saturated in the given $$$k$$$-coloring if its endpoints share at least one color (i.e. there exists a color that is assigned to both endpoints).Let's also define the value of a $$$k$$$-coloring as the sum of weights of saturated edges.Please calculate the maximum possible value of a $$$k$$$-coloring of the given tree.You have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 5 \cdot 10^5$$$) – the number of queries. The first line of each query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 5 \cdot 10^5$$$) — the number of vertices in the tree and the number of colors to assign to each vertex, respectively. Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$, $$$1 \le w_i \le 10^5$$$) — the labels of vertices it connects and the weight of the edge. It is guaranteed that the given edges form a tree. It is guaranteed that sum of all $$$n$$$ over all queries does not exceed $$$5 \cdot 10^5$$$.

For each query print one integer — the maximum value of a $$$k$$$-coloring of the given tree.

NoteThe tree corresponding to the first query in the example:One of the possible $$$k$$$-colorings in the first example: $$$(1), (1), (2), (2)$$$, then the $$$1$$$-st and the $$$3$$$-rd edges are saturated and the sum of their weights is $$$8$$$.The tree corresponding to the second query in the example:One of the possible $$$k$$$-colorings in the second example: $$$(1, 2), (1, 3), (2, 4), (5, 6), (7, 8), (3, 4), (5, 6)$$$, then the $$$1$$$-st, $$$2$$$-nd, $$$5$$$-th and $$$6$$$-th edges are saturated and the sum of their weights is $$$14$$$.

package main

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

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

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 1000000)
	inn.Split(bufio.ScanWords)

	for tt := rd(); tt > 0; tt-- {
		n, k := rd(), rd()
		g := make([][][2]int, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w := rd(), rd(), rd()
			g[u] = append(g[u], [2]int{w, v})
			g[v] = append(g[v], [2]int{w, u})
		}
		var dfs func(u, fa int) (int64, int64)
		dfs = func(u, fa int) (int64, int64) {
			extra := []int64{}
			var cur int64
			for _, it := range g[u] {
				w, to := it[0], it[1]
				if to != fa {
					yes, no := dfs(to, u)
					cur += no
					extra = append(extra, yes-no+int64(w))
				}
			}
			sort.Slice(extra, func(i, j int) bool {
				return extra[i] > extra[j]
			})
			for i := 0; i < min(len(extra), k); i++ {
				if extra[i] > 0 {
					cur += extra[i]
				}
			}
			yes, no := cur, cur
			if k <= len(extra) && extra[k-1] > 0 {
				yes -= extra[k-1]
			}
			return yes, no
		}
		_, ans := dfs(1, 0)
		Fprintln(out, ans)
	}

}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.Atoi(string(data))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.ParseInt(string(data), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 10000000)
	inn.Split(bufio.ScanWords)

	for tt := rd(); tt > 0; tt-- {
		n, k := rd(), rd()
		g := make([][][2]int, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w := rd(), rd(), rd()
			g[u] = append(g[u], [2]int{w, v})
			g[v] = append(g[v], [2]int{w, u})
		}
		var dfs func(u, fa int) (int64, int64)
		dfs = func(u, fa int) (int64, int64) {
			extra := []int64{}
			var cur int64
			for _, it := range g[u] {
				w, to := it[0], it[1]
				if to != fa {
					yes, no := dfs(to, u)
					cur += no
					extra = append(extra, yes-no+int64(w))
				}
			}
			sort.Slice(extra, func(i, j int) bool {
				return extra[i] > extra[j]
			})
			for i := 0; i < min(len(extra), k); i++ {
				if extra[i] > 0 {
					cur += extra[i]
				}
			}
			yes, no := cur, cur
			if k <= len(extra) && extra[k-1] > 0 {
				yes -= extra[k-1]
			}
			return yes, no
		}
		_, ans := dfs(1, 0)
		Fprintln(out, ans)
	}

}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.Atoi(string(data))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.ParseInt(string(data), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 10000000)
	inn.Split(bufio.ScanWords)

	for tt := rd(); tt > 0; tt-- {
		n, k := rd(), rd()
		g := make([][][2]int, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w := rd(), rd(), rd()
			g[u] = append(g[u], [2]int{w, v})
			g[v] = append(g[v], [2]int{w, u})
		}
		var dfs func(u, fa int) (int, int)
		dfs = func(u, fa int) (int, int) {
			extra := []int{}
			cur := 0
			for _, it := range g[u] {
				w, to := it[0], it[1]
				if to != fa {
					yes, no := dfs(to, u)
					cur += no
					extra = append(extra, yes-no+w)
				}
			}
			sort.Slice(extra, func(i, j int) bool {
				return extra[i] > extra[j]
			})
			for i := 0; i < min(len(extra), k); i++ {
				if extra[i] > 0 {
					cur += extra[i]
				}
			}
			yes, no := cur, cur
			if k <= len(extra) && extra[k-1] > 0 {
				yes -= extra[k-1]
			}
			return yes, no
		}
		_, ans := dfs(1, 0)
		Fprintln(out, ans)
	}

}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.Atoi(string(data))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.ParseInt(string(data), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}


Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.You've got string s = s1s2... sn (n is the length of the string), consisting only of characters "." and "#" and m queries. Each query is described by a pair of integers li, ri (1 ≤ li < ri ≤ n). The answer to the query li, ri is the number of such integers i (li ≤ i < ri), that si = si + 1.Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.

The first line contains string s of length n (2 ≤ n ≤ 105). It is guaranteed that the given string only consists of characters "." and "#". The next line contains integer m (1 ≤ m ≤ 105) — the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li, ri (1 ≤ li < ri ≤ n).

Print m integers — the answers to the queries in the order in which they are given in the input.

package main

import (
	"fmt"
	//"sort"
	"unicode/utf8"
	"bufio"
	"os"
	"strconv"
)

func main() {
	var s string
	var m,l,r int
	fmt.Scan(&s,&m)
	input := bufio.NewScanner(os.Stdin)
    	input.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
    	len:= utf8.RuneCountInString(s)
	a:=make([]int,100001)
	a[1]=0
	for i:=1;i<len;i++ {
		if s[i]==s[i-1] {
			a[i+1]=a[i]+1
		} else{
			a[i+1]=a[i]
		}
	}
	var semen int
	for i:=m;i>0;i--{
		input.Scan()
       		aa, _ := strconv.Atoi(input.Text())
		l=aa
       		input.Scan()
       		bb, _ := strconv.Atoi(input.Text())
		r=bb
		semen=a[r]-a[l]
		aab:= strconv.Itoa(semen);
		out.WriteString(aab)
		out.Flush()
		fmt.Println()
		//fmt.Println(a[r]-a[l])
	}
	out.Flush()
}

package main
import "fmt"

func main() {
    var input string
    var numQueries, l, r int
    fmt.Scan(&input, &numQueries)
    prefix := make([]int, len(input))
    if input[0] == input[1] {
        prefix[0] = 1
    } else {
        prefix[0] = 0
    }
    for i:=1; i<len(input)-1; i++ {
        if input[i] == input[i+1] {
            prefix[i] = prefix[i-1] + 1
        } else {
            prefix[i] = prefix[i-1]
        }
    }
    for i:=0; i<numQueries; i++ {
        fmt.Scan(&l, &r)
        ans := prefix[r-2]
        if l-1 > 0 {
            ans -= prefix[l-2]
        }
        fmt.Println(ans)
    }
}

// http://codeforces.com/contest/313/problem/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		str string
		l, r, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReaderSize(os.Stdin, 1<<20)
	output := bufio.NewWriterSize(os.Stdout, 1<<20)

	fmt.Fscanln(input, &str)
	arr := make([]int, len(str) + 1)

	for i := 1; i < len(str); i++ {
		if str[i] == str[i - 1] {
			arr[i] += 1
		}

		arr[i] += arr[i - 1]
	}

	fmt.Fscanln(input, &m)
	//fmt.Fprintln(output, arr)

	for i := 0; i < m; i++ {
		fmt.Fscanln(input, &l, &r)
		fmt.Fprintln(output, arr[r - 1] - arr[l - 1])
	}

	output.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)
    var s string
    var m int
    fmt.Scan(&s)
    dp := make([]int,len(s))
    for i := 0; i < len(s); i++ {
        if i != (len(s)-1) {
            if s[i] == s[i+1] { dp[i]++ }
        }
        if i != 0 { dp[i] += dp[i-1] }
    }
    fmt.Scan(&m)
    for i := 0; i < m; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        scanner.Scan()
        b, _ := strconv.Atoi(scanner.Text())
        a--; b--;
        if a == 0 {
            writer.WriteString(strconv.Itoa(dp[b-1]))
        } else {
            writer.WriteString(strconv.Itoa(dp[b-1]-dp[a-1]))
        }
        writer.WriteRune('\n')
    }
    writer.Flush()
}

package main

import "bufio"
import "os"
import "fmt"

func Read(s *bufio.Reader) string {
    line, _ := s.ReadString('\n')
    return line
}

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

    var s string = Read(reader)

    var n int
    fmt.Sscanln(Read(reader), &n)

    if n > 0 {
        var length int = len(s)

        dp := make([]int, length)
        dp[0] = 0

        for i := 1; i < length; i++ {
            var r int = 0
            if s[i] == s[i - 1] {
                r = 1
            }
            dp[i] = dp[i - 1] + r
        }

        writer := bufio.NewWriter(os.Stdout)

        for i := 0; i < n; i++ {
            var l, r  int
            fmt.Sscanln(Read(reader), &l, &r)
            fmt.Fprintln(writer, dp[r - 1] - dp[l - 1])
        }

        writer.Flush()
    }
}

package main

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

/**
......
4
3 4
2 3
1 6
2 6
*/

func main() {
	reader := bufio.NewReader(os.Stdin)
	var s string
	fmt.Fscanln(reader, &s)
	a := make([]int, len(s))
	for i := 1; i < len(s); i++ {
		if s[i] == s[i-1] {
			a[i] = a[i-1] + 1
		} else {
			a[i] = a[i-1]
		}
	}
	//fmt.Println(a)
	var n int
	fmt.Fscanln(reader, &n)
	for i := 0; i < n; i++ {
		var start, end int
		fmt.Fscanln(reader, &start, &end)
		fmt.Println(a[end-1] - a[start-1])
	}
}

package main

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

func main() {
	var n, m int
	var str string
	fmt.Scan(&str)
	fmt.Scan(&m)

	n = len(str)
	an := make([]int, n, n)

	an[0] = 0
	k := 0
	for i := 1; i < n; i++ {
		if str[i] == str[i-1] {
			k++
		}
		an[i] = k
	}

	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		bs.Scan()
		l, _ := strconv.Atoi(bs.Text())
		bs.Scan()
		r, _ := strconv.Atoi(bs.Text())
		fmt.Println(an[r-1] - an[l-1])
	}
}

package main

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

func main() {
	var s string
	var count, low, high int

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &s)
	slice := []rune(s)
	length := len(slice)
	answer := make([]int, length)
	for i := 1; i < length; i++ {
		answer[i] += answer[i-1]
		if slice[i] == slice[i-1] {
			answer[i]++
		}
	}
	fmt.Fscanln(reader, &count)
	for i := 0; i < count; i++ {
		fmt.Fscanln(reader, &low, &high)
		fmt.Println(answer[high-1] - answer[low-1])
	}
}

package main

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

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

// func readWord(scanner *bufio.Scanner) string {
// 	scanner.Scan()
// 	return scanner.Text()
// }

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	// examStr := readWord(scanner)
	var examStr string
	fmt.Scanln(&examStr)
	// fmt.Scanf("%s", examStr)

	rsum := make([]int, len(examStr)+1)

	// pre-calculation
	if examStr[0] == examStr[1] {
		rsum[1] = 1
	}
	for i := 1; i < len(examStr)-1; i++ {
		rsum[i+1] = rsum[i]
		if examStr[i] == examStr[i+1] {
			rsum[i+1] += 1
		}
	}
	rsum[len(examStr)] = rsum[len(examStr)-1]

	// response to request
	// n := readInt(scanner)
	var n int
	fmt.Scanln(&n)
	for i := 0; i < n; i++ {
		l := readInt(scanner)
		r := readInt(scanner)

		// fmt.Scanln(&l, &r)

		fmt.Println(rsum[r-1] - rsum[l-1])
	}
}

package main

import (
	"fmt"
	//"sort"
	"unicode/utf8"
	"bufio"
	"os"
	"strconv"
)

func main() {
	var s string
	var m,l,r int
	fmt.Scan(&s,&m)
	input := bufio.NewScanner(os.Stdin)
    	input.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
    	len:= utf8.RuneCountInString(s)
	a:=make([]int,100001)
	a[1]=0
	for i:=1;i<len;i++ {
		if s[i]==s[i-1] {
			a[i+1]=a[i]+1
		} else{
			a[i+1]=a[i]
		}
	}
	var semen int
	for i:=m;i>0;i--{
		input.Scan()
       		aa, _ := strconv.Atoi(input.Text())
		l=aa
       		input.Scan()
       		bb, _ := strconv.Atoi(input.Text())
		r=bb
		semen=a[r]-a[l]
		aab:= strconv.Itoa(semen);
		out.WriteString(aab)
		fmt.Println()
		//fmt.Println(a[r]-a[l])
	}
	out.Flush()
}

// http://codeforces.com/contest/313/problem/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		str string
		l, r, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReaderSize(os.Stdin, 1<<20)
	output := bufio.NewWriterSize(os.Stdout, 1<<20)

	fmt.Fscanln(input, &str)
	arr := make([]int, len(str) + 1)

	for i := 1; i < len(str); i++ {
		if str[i] == str[i - 1] {
			arr[i] += 1
		}

		arr[i] += arr[i - 1]
	}

	fmt.Fscanln(input, &m)
	fmt.Fprintln(output, arr)

	for i := 0; i < m; i++ {
		fmt.Fscanln(input, &l, &r)
		fmt.Fprintln(output, arr[r - 1] - arr[l - 1])
	}

	output.Flush()
}

package main

import "bufio"
import "os"
import "fmt"

func Read(s *bufio.Reader) string {
    line, _, _ := s.ReadLine()
    return string(line)
}

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

    var s string = Read(reader)

    var n int
    fmt.Sscanln(Read(reader), &n)

    var length int = len(s)

    dp := make([]int, length)
    dp[0] = 0

    for i := 1; i < length; i++ {
        var r int = 0
        if s[i] == s[i - 1] {
            r = 1
        }
        dp[i] = dp[i - 1] + r
    }

    writer := bufio.NewWriter(os.Stdout)

    for i := 0; i < n; i++ {
        var l, r  int
        fmt.Sscanln(Read(reader), &l, &r)
        fmt.Fprintln(writer, dp[r - 1] - dp[l - 1])
    }

    writer.Flush()
}

package main

import "bufio"
import "os"
import "fmt"

func Read(s *bufio.Reader) string {
    line, _, _ := s.ReadLine()
    return string(line)
}

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

    var s string = Read(reader)

    var n int
    fmt.Sscanln(Read(reader), &n)

    if n > 0 {
        var length int = len(s)

        dp := make([]int, length)
        dp[0] = 0

        for i := 1; i < length; i++ {
            var r int = 0
            if s[i] == s[i - 1] {
                r = 1
            }
            dp[i] = dp[i - 1] + r
        }

        writer := bufio.NewWriter(os.Stdout)

        for i := 0; i < n; i++ {
            var l, r  int
            fmt.Sscanln(Read(reader), &l, &r)
            fmt.Fprintln(writer, dp[r - 1] - dp[l - 1])
            writer.Flush()
        }
    }
}

package main

import "bufio"
import "os"
import "fmt"

func Read(s *bufio.Reader) string {
    line, _, _ := s.ReadLine()
    return string(line)
}

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

    var s string = Read(reader)

    var n int
    fmt.Sscanln(Read(reader), &n)

    if n > 0 {
        var length int = len(s)

        dp := make([]int, length)
        dp[0] = 0

        for i := 1; i < length; i++ {
            var r int = 0
            if s[i] == s[i - 1] {
                r = 1
            }
            dp[i] = dp[i - 1] + r
        }

        writer := bufio.NewWriter(os.Stdout)

        for i := 0; i < n; i++ {
            var l, r  int
            fmt.Sscanln(Read(reader), &l, &r)
            fmt.Fprintln(writer, dp[r - 1] - dp[l - 1])
        }

        writer.Flush()
    }
}

package main

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

/**
......
4
3 4
2 3
1 6
2 6
*/

func main() {
	reader := bufio.NewReader(os.Stdin)
	var s string
	fmt.Fscanln(reader, &s)
	a := make([]int, len(s))
	for i := 1; i < len(s); i++ {
		if s[i] == s[i-1] {
			a[i] = a[i-1] + 1
		} else {
			a[i] = a[i-1]
		}
	}
	fmt.Println(a)
	var n int
	fmt.Fscanln(reader, &n)
	for i := 0; i < n; i++ {
		var start, end int
		fmt.Fscanln(reader, &start, &end)
		fmt.Println(a[end-1] - a[start-1])
	}
}

package main

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

func main() {
	var s string
	var count, low, high, a int
	ret := make([]int, 0)
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &s)
	slice := []rune(s)
	fmt.Fscanln(reader, &count)
	for i := 0; i < count; i++ {
		a = 0
		fmt.Fscanln(reader, &low, &high)
		temp := slice
		temp = temp[low-1 : high]
		for j := 0; j < len(temp)-1; j++ {
			if temp[j] == temp[j+1] {
				a++
			}
		}
		ret = append(ret, a)
	}
	fmt.Println(ret)
}

package main

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

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

// func readWord(scanner *bufio.Scanner) string {
// 	scanner.Scan()
// 	return scanner.Text()
// }

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	// examStr := readWord(scanner)
	reader := bufio.NewReader(os.Stdin)
	var examStr string
	fmt.Fscanln(reader, &examStr)
	// fmt.Scanf("%s", examStr)

	rsum := make([]int, len(examStr)+1)

	// pre-calculation
	if examStr[0] == examStr[1] {
		rsum[1] = 1
	}
	for i := 1; i < len(examStr)-1; i++ {
		rsum[i+1] = rsum[i]
		if examStr[i] == examStr[i+1] {
			rsum[i+1] += 1
		}
	}
	rsum[len(examStr)] = rsum[len(examStr)-1]

	// response to request
	n := readInt(scanner)
	for i := 0; i < n; i++ {
		l := readInt(scanner)
		r := readInt(scanner)

		fmt.Println(rsum[r-1] - rsum[l-1])
	}
}


This problem has unusual memory constraint.At evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared n papers with the income of some company for some time periods. Note that the income can be positive, zero or negative.Igor and Zhenya placed the papers in a row and decided to take turns making moves. Igor will take the papers from the left side, Zhenya will take the papers from the right side. Igor goes first and takes 1 or 2 (on his choice) papers from the left. Then, on each turn a player can take k or k + 1 papers from his side if the opponent took exactly k papers in the previous turn. Players can't skip moves. The game ends when there are no papers left, or when some of the players can't make a move.Your task is to determine the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.

The first line contains single positive integer n (1 ≤ n ≤ 4000) — the number of papers. The second line contains n integers a1, a2, ..., an ( - 105 ≤ ai ≤ 105), where ai is the income on the i-th paper from the left.

Print the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.

NoteIn the first example it's profitable for Igor to take two papers from the left to have the sum of the incomes equal to 4. Then Zhenya wouldn't be able to make a move since there would be only one paper, and he would be able to take only 2 or 3..

package main

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

type Solver func(l, r, k int) int64

const maxN = 4001
const maxK = 90
const empty = 1<<50
const sz = maxN*maxK*maxK

func solve() {
    //generate(); return
    n := readInt()
    a := readInt64Array(n)
    sl, sr := make([]int64, n+1), make([]int64, n+1)
    for i := 1; i <= n; i++ {
        sl[i] = sl[i-1] + a[i-1]
        sr[n-i] = sr[n-i+1] + a[n-i]
    }

    mem := [sz]int64{}
    for i := range mem {
        mem[i] = empty
    }
    var dp, f2 Solver
    dp = func(l, r, k int) int64 {
        dr := n-1-r-l
        code := k + maxK*dr + maxK*maxK*l
        if mem[code] == empty {
            ans, any := int64(0), false
            for _, curK := range []int{k, k+1} {
                if last := l+curK-1; last <= r {
                    cur := sl[last+1] - sl[l] + f2(last+1, r, curK)
                    if !any || cur > ans {
                        ans, any = cur, true
                    }
                }
            }

            mem[code] = ans
        }
        return mem[code]
    }
    f2 = func(l, r, k int) int64 {
        ans, any := int64(0), false
        for _, curK := range []int{k, k+1} {
            if last := r-curK+1; last >= l {
                cur := sr[r+1] - sr[last] + dp(l, last-1, curK)
                if !any || cur < ans {
                    ans, any = cur, true
                }
            }
        }
        return ans
    }
    fmt.Println(dp(0, n-1, 1))
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

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

// Math

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

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

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

// IO

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
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/


You are given an integer $$$n$$$. You have to construct a permutation of size $$$n$$$.A permutation is an array where each integer from $$$1$$$ to $$$s$$$ (where $$$s$$$ is the size of permutation) occurs exactly once. For example, $$$[2, 1, 4, 3]$$$ is a permutation of size $$$4$$$; $$$[1, 2, 4, 5, 3]$$$ is a permutation of size $$$5$$$; $$$[1, 4, 3]$$$ is not a permutation (the integer $$$2$$$ is absent), $$$[2, 1, 3, 1]$$$ is not a permutation (the integer $$$1$$$ appears twice).A subsegment of a permutation is a contiguous subsequence of that permutation. For example, the permutation $$$[2, 1, 4, 3]$$$ has $$$10$$$ subsegments: $$$[2]$$$, $$$[2, 1]$$$, $$$[2, 1, 4]$$$, $$$[2, 1, 4, 3]$$$, $$$[1]$$$, $$$[1, 4]$$$, $$$[1, 4, 3]$$$, $$$[4]$$$, $$$[4, 3]$$$ and $$$[3]$$$.The value of the permutation is the number of its subsegments which are also permutations. For example, the value of $$$[2, 1, 4, 3]$$$ is $$$3$$$ since the subsegments $$$[2, 1]$$$, $$$[1]$$$ and $$$[2, 1, 4, 3]$$$ are permutations.You have to construct a permutation of size $$$n$$$ with minimum possible value among all permutations of size $$$n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th of them contains one integer $$$n$$$ ($$$3 \le n \le 50$$$) representing the $$$i$$$-th test case.

For each test case, print $$$n$$$ integers — the permutation of size $$$n$$$ with minimum possible value. If there are multiple such permutations, print any of them.

NoteIn the first example, the permutation $$$[1, 4, 3, 5, 2]$$$ is one of the possible answers; its value is $$$2$$$.In the second example, the permutation $$$[4, 1, 6, 2, 5, 3]$$$ is one of the possible answers; its value is $$$2$$$.

package main

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

var reader = bufio.NewReader(os.Stdin)

const N = 52

var n, m int
var (
	a, g, q [N]int
)

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

	var find bool
	var seen []bool
	var dfs func(u, v, maxi int)
	path := make([]int, 0)
	var ans []int
	dfs = func(u, v, maxi int) {
		if v > 2 {
			return
		}
		if u == n {
			if v == 2 {
				find = true
				for i := 0; i < len(path); i++ {
					ans[i] = path[i]
				}
			}
			return
		}
		for i := 1; i <= n; i++ {
			if !seen[i] {
				seen[i] = true
				path = append(path, i)
				// optimize
				if maxi == i || len(path) == i {
					dfs(u+1, v+1, maxi+1)
				} else {
					dfs(u+1, v, maxi)
				}

				seen[i] = false
				path = path[:len(path)-1]
				if find == true {
					return
				}
			}
		}
		return
	}
	for t > 0 {
		t--
		fmt.Fscan(reader, &n)
		fmt.Print("1 ")

		for i := n; i > 1; i-- {
			fmt.Printf("%d ", i)
		}
		fmt.Println()
	}
}

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

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		for j:=1;j<=n/2;j++{
			fmt.Fprintf(out,"%v %v ",j,n+1-j)
		}
		if n&1==1{fmt.Fprintf(out,"%v",n/2+1)}
		fmt.Fprintln(out)
	}
}

package main

import (
	"fmt"
)

func main() {
	// var num int
	// fmt.Scan(&num)
	// for t := 0; t < num; t++ {
	// 	var n int
	// 	fmt.Scan(&n)
	// 	var numArr []int
	// 	for i := 0; i < n; i++ {
	// 		var num3 int
	// 		fmt.Scan(&num3)
	// 		numArr = append(numArr, num3)
	// 	}
	// 	var res int
	// 	res = A(numArr)
	// fmt.Println(res)
	// }

	var num int
	fmt.Scan(&num)
	for t := 0; t < num; t++ {
		var n int
		fmt.Scan(&n)

		B(n)
	}
}
func A(arr []int) int {
	var num int
	count := 1
	num = 10 - len(arr)
	count = num * (num - 1)
	count = (count / 2) * 6

	return count

}

func B(num int) {
	cnt := 1
	minn := 1
	maxx := num
	for cnt <= num {
		if cnt%2 == 1 {
			fmt.Printf("%d ", minn)
			minn++
		} else {

			fmt.Printf("%d ", maxx)
			maxx--
		}
		cnt++
	}
	fmt.Println()
}

package main

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

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

func (in *CF1743B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1743B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1743B
Date: 23/10/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1743/B
Problem: CF1743B
**/
func (in *CF1743B) Run() {
	var ans strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		for i, j := 1, n; i <= j; i++ {
			val := 0
			if i == j {
				val = i
			} else {
				val = j
				ans.WriteString(fmt.Sprintf("%v ", i))
			}
			ans.WriteString(fmt.Sprintf("%v ", val))
			j--
		}
		ans.WriteString("\n")
	}
	fmt.Print(ans.String())
}

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

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

package main

import "fmt"
func solve(){
	var n int;
	fmt.Scan(&n);
	for i:=1;i<=n;i++{
		if i!=2{
			fmt.Printf("%d ",i);
		}
	}
	if n>=2{
		fmt.Printf("2");
	}
	fmt.Println("");

}
func main(){
	var t int;
	fmt.Scan(&t);
	for t>0{
		t--;
		solve();
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int, n)
		d := 1
		for i := 0; i < n; i += 2 {
			aa[i] = d
			d++
		}

		d = n
		for i := 1; i < n; i += 2 {
			aa[i] = d
			d--
		}

		for _, v := range aa {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

func solve() {
    var n int
    Fscan(in, &n)

    Fprint(out, 1, " ")
    for i := n; i > 1; i-- {
        Fprint(out, i, " ")
    }
    Fprint(out, "\n")
}

func main() {
    var T int
    Fscan(in, &T)
    for ; T > 0; T-- {
        solve()
    }
    out.Flush()
}

package main

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

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

func debug(a ...interface{}) {
	for _, e := range a {
		printf("%v ", e)
	}
	printf("\n")
	writer.Flush()
}

func solve() {
	var n int
	scanf("%d\n", &n)
	printf("%d ", 1)
	for i:=n; i>=2; i-- {
		printf("%d ", i)
	}
	printf("\n")
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

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

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

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

	var T int
	var n int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		a := []int{}
		for i := 1; i <= n; i += 2 {
			a = append(a, i)
		}
		for i := n / 2 * 2; i >= 2; i -= 2 {
			a = append(a, i)
		}
		for _, v := range a {
			Fprintf(out, "%d ", v)
		}
		Fprintln(out, "")
	}
}

package main

import "fmt"

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

package main

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

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

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

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

		for i := 0; i < n; i++ {
			j := i/2 + 1
			if i % 2 == 1 {
				j = n - i/2
			}
			fmt.Fprintf(w, "%d ", j)
		}
		fmt.Fprintln(w, "")
	}

}

package main

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

var reader = bufio.NewReader(os.Stdin)

var n, m int

func main() {
	var t int
	fmt.Fscan(reader, &t)
	minimumValue := 0
	isValid := func(a []int) bool {
		n := len(a)
		for i := 0; i < n; i++ {
			if a[i] > n {
				return false
			}
		}
		return true
	}
	var find bool
	var seen []bool
	var dfs func(u int)
	path := make([]int, 0)
	var ans []int
	dfs = func(u int) {
		if u == n {
			value := 0
			for i := 0; i <= len(path); i++ {
				for j := i + 1; j <= len(path); j++ {
					if isValid(path[i:j]) {
						value++
					}
				}
			}
			if value == 2 {
				find = true
				ans = make([]int, len(path))
				for i := 0; i < len(path); i++ {
					ans[i] = path[i]
				}
			}
			return
		}
		for i := 1; i <= n; i++ {
			if !seen[i] {
				seen[i] = true
				path = append(path, i)
				dfs(u + 1)
				if minimumValue == 2 {
					return
				}
				seen[i] = false
				path = path[:len(path)-1]
				if find == true {
					return
				}
			}
		}
		return
	}
	for t > 0 {
		t--
		fmt.Fscan(reader, &n)
		seen = make([]bool, n+1)
		find = false
		//dfs(0)
		for i := n; i >= 1; i-- {
			fmt.Printf("%d ", i)
		}
		fmt.Println()
	}
}

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

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		if n==1{fmt.Fprintln(out,1);continue}
		fmt.Fprintln(out,2)
	}
}

package main

import (
	"fmt"
)

func main() {
	// var num int
	// fmt.Scan(&num)
	// for t := 0; t < num; t++ {
	// 	var n int
	// 	fmt.Scan(&n)
	// 	var numArr []int
	// 	for i := 0; i < n; i++ {
	// 		var num3 int
	// 		fmt.Scan(&num3)
	// 		numArr = append(numArr, num3)
	// 	}
	// 	var res int
	// 	res = A(numArr)
	// fmt.Println(res)
	// }

	var num int
	fmt.Scan(&num)
	for t := 0; t < num; t++ {
		var n int
		fmt.Scan(&n)

		B(n)
	}
}
func A(arr []int) int {
	var num int
	count := 1
	num = 10 - len(arr)
	count = num * (num - 1)
	count = (count / 2) * 6

	return count

}

func B(num int) {
	cnt := 1
	minn := 1
	maxx := num
	for cnt <= num {
		if cnt%2 == 1 {
			fmt.Printf("%d ", minn)
			minn++
		} else {

			fmt.Printf("%d ", maxx)
			maxx++
		}
		cnt++
	}
	fmt.Println()
}


This is an interactive problem!Nastia has a hidden permutation $$$p$$$ of length $$$n$$$ consisting of integers from $$$1$$$ to $$$n$$$. You, for some reason, want to figure out the permutation. To do that, you can give her an integer $$$t$$$ ($$$1 \le t \le 2$$$), two different indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$, $$$i \neq j$$$), and an integer $$$x$$$ ($$$1 \le x \le n - 1$$$). Depending on $$$t$$$, she will answer:   $$$t = 1$$$: $$$\max{(\min{(x, p_i)}, \min{(x + 1, p_j)})}$$$;  $$$t = 2$$$: $$$\min{(\max{(x, p_i)}, \max{(x + 1, p_j)})}$$$. You can ask Nastia at most $$$\lfloor \frac {3 \cdot n} { 2} \rfloor + 30$$$ times. It is guaranteed that she will not change her permutation depending on your queries. Can you guess the permutation?

The input consists of several test cases. In the beginning, you receive the integer $$$T$$$ ($$$1 \le T \le 10\,000$$$) — the number of test cases. At the beginning of each test case, you receive an integer $$$n$$$ ($$$3 \le n \le 10^4$$$) — the length of the permutation $$$p$$$. It's guaranteed that the permutation is fixed beforehand and that the sum of $$$n$$$ in one test doesn't exceed $$$2 \cdot 10^4$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
type (
	initData struct{ n int }
	request  struct{ t, i, j, x int }
	response struct{ v int }
	answer   struct{ ans []int }
)

type interaction interface {
	readInitData() initData
	query(request) response
	printAnswer(answer)
}

type io struct {
	in  *bufio.Reader
	out *bufio.Writer
}

func (io io) readInitData() (d initData) {
	Fscan(io.in, &d.n)
	return
}

func (io io) query(req request) (resp response) {
	Fprintln(io.out, "?", req.t, req.i, req.j, req.x)
	io.out.Flush()
	Fscan(io.in, &resp.v)
	if resp.v < 0 {
		panic(-1)
	}
	return
}

func (io io) printAnswer(a answer) {
	Fprint(io.out, "! ")
	for _, v := range a.ans {
		Fprint(io.out, v, " ")
	}
	Fprintln(io.out)
	io.out.Flush()
}

func doInteraction(it interaction) {
	q := func(t, i, j, x int) int { return it.query(request{t, i + 1, j + 1, x}).v }
	dt := it.readInitData()
	n := dt.n
	a := make([]int, n)
	defer func() { it.printAnswer(answer{a}) }()

	for i := 1; i < n; {
		mi := q(2, i-1, i, 1)
		mx := q(1, i-1, i, n-1)
		v := q(1, i-1, i, mi)
		if v == mi {
			a[i-1], a[i] = mx, mi
		} else {
			a[i-1], a[i] = mi, mx
		}
		i += 2
		if n&1 > 0 && i == n {
			i = n - 1
		}
	}

	pos := map[int][]int{}
	for i, v := range a {
		pos[v] = append(pos[v], i)
	}
	for _, ps := range pos {
		if len(ps) == 1 {
			continue
		}
		if len(ps) == 3 {
			if n > 3 {
				panic(-1)
			}
			a = []int{3,2,1}
		}
		i, j := ps[0], ps[1]
		mi := min(q(2, i, j, 1), q(2, j, i, 1))
		mx := max(q(1, i, j, n-1), q(1, j, i, n-1))
		v := q(1, i, j, mi)
		w := q(2, i, j, mi)
		if v > w {
			a[i], a[j] = mi, mx
		} else {
			a[i], a[j] = mx, mi
		}
	}
}

// TODO: check output format before submit
func run() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	T := 1
	Fscan(in, &T)
	for ; T > 0; T-- {
		doInteraction(io{in, out})
	}
}

func main() { run() }

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
}


Kawashiro Nitori is a girl who loves competitive programming. One day she found a rooted tree consisting of $$$n$$$ vertices. The root is vertex $$$1$$$. As an advanced problem setter, she quickly thought of a problem.Kawashiro Nitori has a vertex set $$$U=\{1,2,\ldots,n\}$$$. She's going to play a game with the tree and the set. In each operation, she will choose a vertex set $$$T$$$, where $$$T$$$ is a partial virtual tree of $$$U$$$, and change $$$U$$$ into $$$T$$$.A vertex set $$$S_1$$$ is a partial virtual tree of a vertex set $$$S_2$$$, if $$$S_1$$$ is a subset of $$$S_2$$$, $$$S_1 \neq S_2$$$, and for all pairs of vertices $$$i$$$ and $$$j$$$ in $$$S_1$$$, $$$\operatorname{LCA}(i,j)$$$ is in $$$S_1$$$, where $$$\operatorname{LCA}(x,y)$$$ denotes the lowest common ancestor of vertices $$$x$$$ and $$$y$$$ on the tree. Note that a vertex set can have many different partial virtual trees.Kawashiro Nitori wants to know for each possible $$$k$$$, if she performs the operation exactly $$$k$$$ times, in how many ways she can make $$$U=\{1\}$$$ in the end? Two ways are considered different if there exists an integer $$$z$$$ ($$$1 \le z \le k$$$) such that after $$$z$$$ operations the sets $$$U$$$ are different.Since the answer could be very large, you need to find it modulo $$$p$$$. It's guaranteed that $$$p$$$ is a prime number.

The first line contains two integers $$$n$$$ and $$$p$$$ ($$$2 \le n \le 2000$$$, $$$10^8 + 7 \le p \le 10^9+9$$$). It's guaranteed that $$$p$$$ is a prime number. Each of the next $$$n-1$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$), representing an edge between $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree.

The only line contains $$$n-1$$$ integers — the answer modulo $$$p$$$ for $$$k=1,2,\ldots,n-1$$$.

package main

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

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

	n, mod := readTwoNums(reader)

	edges := make([][]int, n-1)

	for i := 0; i < n-1; i++ {
		edges[i] = readNNums(reader, 2)
	}

	res := solve(n, mod, edges)

	var buf bytes.Buffer

	for i := 0; i < n-1; i++ {
		buf.WriteString(fmt.Sprintf("%d ", res[i]))
	}

	buf.WriteByte('\n')
	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func modAdd(a, b int, p int) int {
	a += b
	if a >= p {
		a -= p
	}
	return a
}

func modMul(a, b int, p int) int {
	r := int64(a) * int64(b)
	r %= int64(p)
	return int(r)
}

func pow(a, b int, p int) int {
	r := 1
	for b > 0 {
		if b&1 == 1 {
			r = modMul(r, a, p)
		}
		a = modMul(a, a, p)
		b >>= 1
	}
	return r
}

func solve(n int, mod int, E [][]int) []int {
	F := make([]int, n+1)
	F[0] = 1
	for i := 1; i <= n; i++ {
		F[i] = modMul(i, F[i-1], mod)
	}

	I := make([]int, n+1)
	I[n] = pow(F[n], mod-2, mod)
	for i := n - 1; i >= 0; i-- {
		I[i] = modMul(i+1, I[i+1], mod)
	}

	nCr := func(n int, r int) int {
		if n < r {
			return 0
		}
		res := F[n]
		res = modMul(res, I[r], mod)
		res = modMul(res, I[n-r], mod)
		return res
	}

	g := NewGraph(n, 2*n)

	for _, e := range E {
		u, v := e[0], e[1]
		u--
		v--
		g.AddEdge(u, v)
		g.AddEdge(v, u)
	}

	dp := make([][]int, n+1)
	S := make([][]int, n+1)
	pre := make([][]int, n+1)
	suf := make([][]int, n+1)

	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
		S[i] = make([]int, n+1)
		pre[i] = make([]int, n+2)
		suf[i] = make([]int, n+2)
		for j := 0; j <= n+1; j++ {
			pre[i][j] = 1
			suf[i][j] = 1
		}
	}

	var dfs func(p int, u int)

	dfs = func(p int, u int) {
		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if p != v {
				dfs(u, v)
			}
		}

		var cnt int

		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if p != v {
				cnt++
				for j := 0; j <= n; j++ {
					suf[cnt][j] = S[v][j]
					pre[cnt][j] = S[v][j]
				}
			}
		}

		if cnt == 0 {
			for j := 1; j <= n; j++ {
				dp[u][j] = 1
				S[u][j] = modAdd(S[u][j-1], dp[u][j], mod)
			}
			return
		}

		for j := 0; j <= n; j++ {
			for i := 1; i <= cnt; i++ {
				pre[i][j] = modMul(pre[i-1][j], pre[i][j], mod)
			}
			for i := cnt; i >= 1; i-- {
				suf[i][j] = modMul(suf[i][j], suf[i+1][j], mod)
			}
		}

		for i := 1; i <= n; i++ {
			dp[u][i] = pre[cnt][i]
		}

		if u != 0 {
			var c int
			for i := g.node[u]; i > 0; i = g.next[i] {
				v := g.to[i]
				if p == v {
					continue
				}
				c++
				var sum int
				for mx := 1; mx <= n; mx++ {
					dp[u][mx] = modAdd(dp[u][mx], modMul(sum, dp[v][mx], mod), mod)
					sum = modAdd(sum, modMul(pre[c-1][mx], suf[c+1][mx], mod), mod)
				}
			}
		}
		for i := 1; i <= cnt; i++ {
			for j := 0; j <= n; j++ {
				suf[i][j] = 1
				pre[i][j] = 1
			}
		}
		for i := 1; i <= n; i++ {
			S[u][i] = modAdd(dp[u][i], S[u][i-1], mod)
		}
	}

	dfs(0, 0)

	res := make([]int, n-1)

	for i := 1; i < n; i++ {
		var ans int
		for j := 1; j <= i; j++ {
			tmp := modMul(nCr(i, j), dp[0][j], mod)
			if (i-j)&1 == 1 {
				ans = modAdd(ans, mod-tmp, mod)
			} else {
				ans = modAdd(ans, tmp, mod)
			}
		}
		res[i-1] = ans
	}

	return res
}

type Graph struct {
	node []int
	next []int
	to   []int
	cur  int
}

func NewGraph(n int, e int) *Graph {
	g := new(Graph)
	g.node = make([]int, n)
	g.next = make([]int, e+1)
	g.to = make([]int, e+1)
	g.cur = 0
	return g
}

func (g *Graph) AddEdge(u, v int) {
	g.cur++
	g.next[g.cur] = g.node[u]
	g.node[u] = g.cur
	g.to[g.cur] = v
}

func (g *Graph) Reset() {
	g.cur = 0
}