Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar "Jupiter". According to the sweepstake rules, each wrapping has an integer written on it — the number of points that the participant adds to his score as he buys the bar. After a participant earns a certain number of points, he can come to the prize distribution center and exchange the points for prizes. When somebody takes a prize, the prize's cost is simply subtracted from the number of his points.Vasya didn't only bought the bars, he also kept a record of how many points each wrapping cost. Also, he remembers that he always stucks to the greedy strategy — as soon as he could take at least one prize, he went to the prize distribution centre and exchanged the points for prizes. Moreover, if he could choose between multiple prizes, he chose the most expensive one. If after an exchange Vasya had enough points left to get at least one more prize, then he continued to exchange points.The sweepstake has the following prizes (the prizes are sorted by increasing of their cost):   a mug (costs a points),  a towel (costs b points),  a bag (costs c points),  a bicycle (costs d points),  a car (costs e points). Now Vasya wants to recollect what prizes he has received. You know sequence p1, p2, ..., pn, where pi is the number of points Vasya got for the i-th bar. The sequence of points is given in the chronological order. You also know numbers a, b, c, d, e. Your task is to find, how many prizes Vasya received, what prizes they are and how many points he's got left after all operations are completed.

The first line contains a single integer n (1 ≤ n ≤ 50) — the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ 109). The third line contains 5 integers a, b, c, d, e (1 ≤ a < b < c < d < e ≤ 109) — the prizes' costs.

Print on the first line 5 integers, separated by a space — the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer — the number of points Vasya will have left after all operations of exchange are completed. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteIn the first sample Vasya gets 3 points after eating the first chocolate bar. Then he exchanges 2 points and gets a mug. Vasya wins a bag after eating the second chocolate bar. Then he wins a towel after eating the third chocolate bar. After all chocolate bars 3 - 2 + 10 - 10 + 4 - 4 = 1 points remains.

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

)

func main() {
	var n,i,f,j,e int64
	e=0
	a:=make([]int64,55)
	x:=make([]int64,10)
	y:=make([]int64,10)
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	fmt.Scan(&n)
	for i=1;i<=n;i++ {
		input.Scan()
		aa,_ := strconv.ParseInt(input.Text(),0,64)
		a[i]=aa
	}
	for i=1;i<=5;i++ {
		input.Scan()
		bb,_ := strconv.ParseInt(input.Text(),0,64)
		x[i]=bb
	}
	for i=1;i<=n;i++ {
		e+=a[i]
		for j=5;j>=1;j-- {
			y[j]+=e/x[j]
			e%=x[j]
		}
	}
	for f=1; f<=5;f++ {
		fmt.Print(y[f]," ")
	}
	fmt.Println()
	fmt.Println(e)
}

package main

import (
	"fmt"
)

func can_buy(cur uint64, prizes []int) int {
	for i:=4; i>=0; i-- {
		if cur >= uint64(prizes[i]) {
			return i
		}
	}
	return -1
}

func main() {
	var n int
	var cur uint64 = 0
	fmt.Scan(&n)
	points := make([]int, n)
	prizes := make([]int, 5)
	claimed_prizes := make([]uint64, 5)
	for i:=0; i<n; i++ {
		fmt.Scan(&points[i])
	}
	for i:=0; i<5; i++ {
		fmt.Scan(&prizes[i])
		claimed_prizes[i] = 0
	}
	for i:=0; i<n; i++ {
		cur += uint64(points[i])
		for idx:=can_buy(cur, prizes); idx!=-1; idx=can_buy(cur, prizes) {
			claimed_prizes[idx] += cur / uint64(prizes[idx])
			cur -= (cur / uint64(prizes[idx])) * uint64(prizes[idx])
		}
	}
	for i:=0; i<5; i++ {
		fmt.Printf("%d ", claimed_prizes[i])
	}
	fmt.Printf("\n")
	fmt.Println(cur)
}

package main

import "fmt"

func main() {
    var n int
    var x,cnt [5]int64
    fmt.Scan(&n)
    p := make([]int64,n)
    for i := 0; i < n; i++ { fmt.Scan(&p[i]) }
    for i := 0; i < 5; i++ { fmt.Scan(&x[i]) }
    pt := int64(0)
    for i := 0; i < n; i++ {
        pt += p[i]
        for j := 4; j >= 0; j-- {
            if pt >= x[j] {
                cnt[j] += pt/x[j]
                pt %= x[j]
            }
        }
    }
    fmt.Println(cnt[0],cnt[1],cnt[2],cnt[3],cnt[4])
    fmt.Println(pt)
}

package main

import (
	"fmt"
)

func can_buy(cur uint64, prizes []int) int {
	for i:=4; i>=0; i-- {
		if cur >= uint64(prizes[i]) {
			return i
		}
	}
	return -1
}

func main() {
	var n int
	var cur uint64 = 0
	fmt.Scan(&n)
	points := make([]int, n)
	prizes := make([]int, 5)
	claimed_prizes := make([]int, 5)
	for i:=0; i<n; i++ {
		fmt.Scan(&points[i])
	}
	for i:=0; i<5; i++ {
		fmt.Scan(&prizes[i])
		claimed_prizes[i] = 0
	}
	for i:=0; i<n; i++ {
		fmt.Println(i)
		cur += uint64(points[i])
		for idx:=can_buy(cur, prizes); idx!=-1; idx=can_buy(cur,prizes) {
			cur -= uint64(prizes[idx])
			claimed_prizes[idx]++
		}
	}
	for i:=0; i<5; i++ {
		fmt.Printf("%d ", claimed_prizes[i])
	}
	fmt.Printf("\n")
	fmt.Println(cur)
}


NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles:  It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself.He managed to calculate the number of outer circles $$$n$$$ and the radius of the inner circle $$$r$$$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $$$R$$$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture.

The first and the only line of the input file contains two numbers $$$n$$$ and $$$r$$$ ($$$3 \leq n \leq 100$$$, $$$1 \leq r \leq 100$$$) — the number of the outer circles and the radius of the inner circle respectively.

Output a single number $$$R$$$ — the radius of the outer circle required for building the required picture.  Your answer will be accepted if its relative or absolute error does not exceed $$$10^{-6}$$$. Formally, if your answer is $$$a$$$ and the jury's answer is $$$b$$$. Your answer is accepted if and only when $$$\frac{|a-b|}{max(1, |b|)} \le 10^{-6}$$$.

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, r float64
	fmt.Scan(&n, &r)
	a := math.Pi / n
	fmt.Printf("%.15f\n", r*math.Sin(a)/(1-math.Sin(a)))
}

package main

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

type caseInput struct {
	// FIXME test case input structure
	nums []int16
}

//	FIXME read the input
func readCaseInput(scanner *bufio.Scanner) caseInput {
	size := readInt(scanner)
	nums := make([]int16, size)
	for i := range nums {
		nums[i] = int16(readInt(scanner))
	}
	in := caseInput{nums}
	return in
}

type caseOutput struct {
	// FIXME test case output structure
	equat int
}

func writeCaseOutput(writer *bufio.Writer, out caseOutput) {
	//	FIXME write the out
	writef(writer, "%d\n", out.equat)
}

func solveCase(in caseInput) caseOutput {
	// FIXME actual solution
	sum := 0
	for _, v := range in.nums {
		sum += int(v)
	}
	sum2 := int(0)
	for i, v := range in.nums {
		sum2 += int(v)
		if sum2*2 >= sum {
			return caseOutput{i + 1}
		}
	}
	return caseOutput{len(in.nums)}
}

func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := readFloat64(scanner)
	r := readFloat64(scanner)

	angle := 2 * math.Pi / n

	z := math.Sqrt(math.Pow(1 - math.Cos(angle), 2) + math.Pow(math.Sin(angle), 2))

	x := (z * r) / (2 - z)

	writef(writer, "%.9f\n", x)
}

func main() {
	var scanner *bufio.Scanner
	if len(os.Args) > 1 {
		reader, err := os.Open(os.Args[1])
		if err != nil {
			panic(err)
		}
		defer reader.Close()
		scanner = bufio.NewScanner(reader)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024)

	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	solveSequential(scanner, writer)
}

//	GoLang shorthand methods for I/O go below
//	TODO wipe unused methods before submitting

func readString(sc *bufio.Scanner) string {
	if !sc.Scan() {
		panic("failed to scan next token")
	}

	return sc.Text()
}

func readInt64(sc *bufio.Scanner) int64 {
	if !sc.Scan() {
		panic("failed to scan next token")
	}

	res, err := strconv.ParseInt(sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func readInt(sc *bufio.Scanner) int {
	return int(readInt64(sc))
}

func readFloat64(sc *bufio.Scanner) float64 {
	sc.Scan()
	res, err := strconv.ParseFloat(sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func writef(writer *bufio.Writer, formatStr string, values ...interface{}) {
	out := fmt.Sprintf(formatStr, values...)
	_, err := writer.WriteString(out)
	if err != nil {
		panic(err)
	}
}

//	GoLang shorthand methods for math go below
//	TODO wipe unused methods before submitting

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
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n ,r float64
	fmt.Fscanf(buf, "%f %f\n", &n,&r)
	k := math.Cos((float64(n-2)*math.Pi/n)/2)
	fmt.Printf("%.10f\n",r*k/(1-k))

}

package main

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

const eps = 0.00000001

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

func calc(r float64) bool {
	return ((2.0*k-2)*(R+r)*(R+r) + 4*r*r) > 0
}

func main() {
	var n int
	fmt.Fscan(in, &n, &R)
	pi := math.Acos(-1)
	var l, r float64 = 0, 1e16
	angle := (2.0 * pi) / float64(n)
	k = math.Cos(angle)
	for r-l > eps {
		mid := (l + r) / 2.0
		if calc(mid) {
			r = mid
		} else {
			l = mid
		}
	}
	fmt.Printf("%.15f\n", l)
}

// Codeforces 1101 B Accordion train done 3 attempts
package main

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

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

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

var n,r int
var ro float64

func readData() {
	in = bufio.NewReader(os.Stdin)
	n,r,_,_,_ = readFive()
}

func solve() {
	angl := math.Pi / float64(n)
	si := math.Sin(angl)
	ro = (si * float64(r)) / (1.0 - si)
}

func printRes() {
	fmt.Println(ro)
}

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

package main

import (
	"fmt"
	"math"
	//"sort"
)

func main() {
	var n, k float64

	fmt.Scan(&n, &k)

	result := k / ((1 - math.Sin(math.Pi / n)) / (math.Sin(math.Pi/n)))
	fmt.Println(result)
}

package main

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

const eps = 0.00000001

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

func calc(r float64) bool {
	return ((2.0*k-2)*(R+r)*(R+r) + 4*r*r) > 0
}

func main() {
	var n int
	fmt.Fscan(in, &n, &R)
	pi := math.Acos(-1)
	var l, r float64 = 0, 1e16
	angle := (2.0 * pi) / float64(n)
	k = math.Cos(angle)
	fmt.Printf("%f %f %f\n", pi, angle, k)
	for r-l > eps {
		mid := (l + r) / 2.0
		if calc(mid) {
			r = mid
		} else {
			l = mid
		}
	}
	fmt.Printf("%.15f\n", l)
}


You are given a non-decreasing array of non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Also you are given a positive integer $$$k$$$.You want to find $$$m$$$ non-decreasing arrays of non-negative integers $$$b_1, b_2, \ldots, b_m$$$, such that:  The size of $$$b_i$$$ is equal to $$$n$$$ for all $$$1 \leq i \leq m$$$.  For all $$$1 \leq j \leq n$$$, $$$a_j = b_{1, j} + b_{2, j} + \ldots + b_{m, j}$$$. In the other word, array $$$a$$$ is the sum of arrays $$$b_i$$$.  The number of different elements in the array $$$b_i$$$ is at most $$$k$$$ for all $$$1 \leq i \leq m$$$. Find the minimum possible value of $$$m$$$, or report that there is no possible $$$m$$$.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \leq n \leq 100$$$, $$$1 \leq k \leq n$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_1 \leq a_2 \leq \ldots \leq a_n \leq 100$$$, $$$a_n > 0$$$).

For each test case print a single integer: the minimum possible value of $$$m$$$. If there is no such $$$m$$$, print $$$-1$$$.

NoteIn the first test case, there is no possible $$$m$$$, because all elements of all arrays should be equal to $$$0$$$. But in this case, it is impossible to get $$$a_4 = 1$$$ as the sum of zeros.In the second test case, we can take $$$b_1 = [3, 3, 3]$$$. $$$1$$$ is the smallest possible value of $$$m$$$.In the third test case, we can take $$$b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]$$$ and $$$b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]$$$. It's easy to see, that $$$a_i = b_{1, i} + b_{2, i}$$$ for all $$$i$$$ and the number of different elements in $$$b_1$$$ and in $$$b_2$$$ is equal to $$$3$$$ (so it is at most $$$3$$$). It can be proven that $$$2$$$ is the smallest possible value of $$$m$$$.

package main

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

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		n := readInt()
		k := readInt()
		a := readIntArray(n)
		freq := make(map[int]int, 0)
		for j := 0; j < n; j++ {
			freq[a[j]]++
		}
		if len(freq) <= k {
			fmt.Println(1)
		} else {
			if k == 1 {
				if len(freq) == 1 {
					fmt.Println(1)
				} else {
					fmt.Println(-1)
				}
			} else {
				res := solveSingle(a, k)
				fmt.Println(res)
			}
		}
	}
}

func solveSingle(a []int, k int) int {
	freq := make(map[int]int, 0)
	pos := 0
	max := 0
	for i := 0; i < len(a); i++ {
		freq[a[i]]++
		max = a[i]
		a[i] = 0
		pos = i
		if len(freq) == k {
			break
		}
	}
	for i := pos + 1; i < len(a); i++ {
		a[i] -= max
	}
	ok := true
	for i := 0; i < len(a); i++ {
		if a[i] > 0 {
			ok = false
			break
		}
	}
	if ok {
		return 1
	} else {
		return 1 + solveSingle(a, k)
	}
}

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

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

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

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

package main

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

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

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var nt int
	fmt.Fscanf(reader, "%d\n", &nt)
	var n, k int
	for kk := 1; kk <= nt; kk++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &k)
		var a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &a[i])
		}
		fmt.Fscanf(reader, "\n")

		var cnt = 1
		for i := 0; i < n - 1; i++ {
			if a[i] != a[i + 1] {
				cnt ++
			}
		}

		if k == 1 {
			if cnt == 1 {
				fmt.Fprintf(writer, "%d\n", 1)
			} else {
				fmt.Fprintf(writer, "-1\n")
			}
		} else {
			var ret = 1
			cnt -= min(cnt, k)
			ret += cnt / (k - 1)
			if cnt % (k - 1) != 0 {
				ret ++
			}
			fmt.Fprintf(writer, "%d\n", ret)
		}
	}
}

package main

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

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

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

func solve() {
	n, k := nextInt(), nextInt()
	has := map[int]bool{}

	for i := 0; i < n; i++ {
		x := nextInt()
		has[x] = true
	}

	if len(has) > 1 && k == 1 {
		printf("-1\n")
	} else if k == 1 {
		printf("1\n")
	} else {
		printf("%d\n", Max(1, int64((len(has)+k-3)/(k-1))))
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// 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:\n", i)
		solve()
	}

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

package main

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

func ReadInt(scanner *bufio.Scanner) int {
	if !scanner.Scan() {
		panic("cant scan")
	}
	str := scanner.Text()
	n, _ := strconv.Atoi(str)
	return n
}

func ReadIntArray(scanner *bufio.Scanner) []int {
	if !scanner.Scan() {
		panic("cant scan")
	}
	str := scanner.Text()
	tokens := strings.Split(str, " ")
	result := make([]int, len(tokens))
	for index, token := range tokens {
		n, _ := strconv.Atoi(token)
		result[index] = n
	}
	return result
}

func PrintIntArray(arr []int, writer *bufio.Writer) {
	strArray := fmt.Sprint(arr)
	_, _ = writer.WriteString(strArray[1:len(strArray)-1])
	_, _ =writer.Write([]byte{'\n'})
}

func PrintInt(number int, writer *bufio.Writer) {
	_, _ = writer.WriteString(strconv.Itoa(number))
	_, _ = writer.Write([]byte{'\n'})
}

func Solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	tmp := ReadIntArray(scanner)
	k := tmp[1]

	a := ReadIntArray(scanner)
	unique := 0
	prev := -1
	for _, elem := range a {
		if elem != prev {
			unique++
		}
		prev = elem
	}

	if k == 1 {
		if unique == 1 {
			PrintInt(1, writer)
		} else {
			PrintInt(-1, writer)
		}
	} else {
		unique -= k
		if unique <= 0 {
			PrintInt(1, writer)
		} else {
			PrintInt((unique + k - 2) / (k - 1) + 1, writer)
		}
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	t := ReadInt(scanner)
	for i := 0; i < t; i++ {
		Solve(scanner, writer)
	}
	_ = writer.Flush()
}

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		n, k := ReadInt(), ReadInt()
		a := ReadInts(n)
		fmt.Println(solve(n, k, a))
	}
}

func solve(n, k int, a []int) int {
	m := make(map[int]bool)
	for _, ai := range a {
		m[ai] = true
	}
	x := len(m)
	if k == 1 && x >= 2 {
		return -1
	}
	if x <= k {
		return 1
	}
	return (x - 1 + k - 1 - 1) / (k - 1)
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		n := readInt()
		k := readInt()
		a := readIntArray(n)
		freq := make(map[int]int, 0)
		for j := 0; j < n; j++ {
			freq[a[j]]++
		}
		if len(freq) <= k {
			fmt.Println(1)
		} else {
			if k == 1 {
				if len(freq) == 1 {
					fmt.Println(1)
				} else {
					fmt.Println(-1)
				}
			} else {
				fmt.Println(2)
			}
		}
	}
}

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

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

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

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

package main

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

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

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

func solve() {
	n, k := nextInt(), nextInt()
	has := map[int]bool{}

	for i := 0; i < n; i++ {
		x := nextInt()
		has[x] = true
	}

	if len(has) > 1 && k == 1 {
		printf("-1\n")
	} else if k == 1 {
		printf("1\n")
	} else {
		printf("%d\n", (len(has)+k-3)/(k-1))
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// 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:\n", i)
		solve()
	}

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

package main

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

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

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

func solve() {
	n, k := nextInt(), nextInt()
	has := map[int]bool{}

	for i := 0; i < n; i++ {
		x := nextInt()
		has[x] = true
	}

	if len(has) > 1 && k == 1 {
		printf("-1\n")
	} else if k == 1 {
		printf("1\n")
	} else {
		printf("%d\n", (len(has)+k-2)/(k-1))
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// 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:\n", i)
		solve()
	}

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

package main

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

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

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

func solve() {
	n, k := nextInt(), nextInt()
	has := map[int]bool{}

	for i := 0; i < n; i++ {
		x := nextInt()
		has[x] = true
	}

	if len(has) > 1 && k == 1 {
		printf("-1\n")
	} else {
		printf("%d\n", (len(has)+k-1)/k)
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// 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:\n", i)
		solve()
	}

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

package main

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

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

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

func solve() {
	n, k := nextInt(), nextInt()
	has := map[int]bool{}

	for i := 0; i < n; i++ {
		x := nextInt()
		has[x] = true
	}

	if len(has) > 1 && k == 1 {
		printf("-1\n")
	} else {
		pow := k
		num := 1
		for pow < len(has) {
			num++
			pow *= k
		}
		printf("%d\n", num)
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// 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:\n", i)
		solve()
	}

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

package main

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

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

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

func solve() {
	n, k := nextInt(), nextInt()
	has := map[int]bool{}

	for i := 0; i < n; i++ {
		x := nextInt()
		has[x] = true
	}

	if len(has) > 1 && k == 1 {
		printf("-1\n")
	} else if k == 1 {
		printf("1\n")
	} else {
		printf("%d\n", Max(0, int64((len(has)+k-3)/(k-1))))
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// 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:\n", i)
		solve()
	}

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

package main

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

func ReadInt(scanner *bufio.Scanner) int {
	if !scanner.Scan() {
		panic("cant scan")
	}
	str := scanner.Text()
	n, _ := strconv.Atoi(str)
	return n
}

func ReadIntArray(scanner *bufio.Scanner) []int {
	if !scanner.Scan() {
		panic("cant scan")
	}
	str := scanner.Text()
	tokens := strings.Split(str, " ")
	result := make([]int, len(tokens))
	for index, token := range tokens {
		n, _ := strconv.Atoi(token)
		result[index] = n
	}
	return result
}

func PrintIntArray(arr []int, writer *bufio.Writer) {
	strArray := fmt.Sprint(arr)
	_, _ = writer.WriteString(strArray[1:len(strArray)-1])
	_, _ =writer.Write([]byte{'\n'})
}

func PrintInt(number int, writer *bufio.Writer) {
	_, _ = writer.WriteString(strconv.Itoa(number))
	_, _ = writer.Write([]byte{'\n'})
}

func Solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	tmp := ReadIntArray(scanner)
	k := tmp[1]

	a := ReadIntArray(scanner)
	unique := 0
	prev := -1
	for _, elem := range a {
		if elem != prev {
			unique++
		}
		prev = elem
	}

	if k == 1 {
		if unique == 1 {
			PrintInt(1, writer)
		} else {
			PrintInt(-1, writer)
		}
	} else {
		PrintInt((unique + k - 1) / k, writer)
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	t := ReadInt(scanner)
	for i := 0; i < t; i++ {
		Solve(scanner, writer)
	}
	_ = writer.Flush()
}

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		n, k := ReadInt(), ReadInt()
		a := ReadInts(n)
		fmt.Println(solve(n, k, a))
	}
}

func solve(n, k int, a []int) int {
	m := make(map[int]bool)
	for _, ai := range a {
		m[ai] = true
	}
	x := len(m)
	if k == 1 && x >= 2 {
		return -1
	}
	if x <= k {
		return 1
	}
	return (x-k+1)/k + 1
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}


There are $$$n$$$ cities in Berland. The city numbered $$$1$$$ is the capital. Some pairs of cities are connected by a one-way road of length 1.Before the trip, Polycarp for each city found out the value of $$$d_i$$$ — the shortest distance from the capital (the $$$1$$$-st city) to the $$$i$$$-th city.Polycarp begins his journey in the city with number $$$s$$$ and, being in the $$$i$$$-th city, chooses one of the following actions:   Travel from the $$$i$$$-th city to the $$$j$$$-th city if there is a road from the $$$i$$$-th city to the $$$j$$$-th and $$$d_i < d_j$$$;  Travel from the $$$i$$$-th city to the $$$j$$$-th city if there is a road from the $$$i$$$-th city to the $$$j$$$-th and $$$d_i \geq d_j$$$;  Stop traveling. Since the government of Berland does not want all people to come to the capital, so Polycarp no more than once can take the second action from the list. in other words, he can perform the second action $$$0$$$ or $$$1$$$ time during his journey. Polycarp, on the other hand, wants to be as close to the capital as possible.  For example, if $$$n = 6$$$ and the cities are connected, as in the picture above, then Polycarp could have made the following travels (not all possible options):   $$$2 \rightarrow 5 \rightarrow 1 \rightarrow 2 \rightarrow 5$$$;  $$$3 \rightarrow 6 \rightarrow 2$$$;  $$$1 \rightarrow 3 \rightarrow 6 \rightarrow 2 \rightarrow 5$$$. Polycarp wants for each starting city $$$i$$$ to find out how close he can get to the capital. More formally: he wants to find the minimal value of $$$d_j$$$ that Polycarp can get from the city $$$i$$$ to the city $$$j$$$ according to the rules described above.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is preceded by an empty line. The first line of each test case contains two integers $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) and $$$m$$$ ($$$1 \leq m \leq 2 \cdot 10^5$$$) — number of cities and roads, respectively. This is followed by $$$m$$$ lines describing the roads. Each road is characterized by two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n, u \ne v$$$) — the numbers of cities connected by a one-way road. It is guaranteed that the sums of $$$n$$$ and $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$. It is guaranteed that for each pair of different cities $$$(u, v)$$$ there is at most one road from $$$u$$$ to $$$v$$$ (but a pair of roads from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$ — is valid). It is guaranteed that there is a path from the capital to all cities.

For each test case, on a separate line output $$$n$$$ numbers, the $$$i$$$-th of which is equal to the minimum possible distance from the capital to the city where Polycarp ended his journey.

package main

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

type node struct {
	nei  []int
	dist int
	vis  bool
	dp   [2]int
}

const MAXN = 2e5 + 5

var graph [MAXN]node

func popDist() {
	q := []int{1}
	graph[1].dist = 0
	graph[1].vis = true
	for len(q) > 0 {
		var q1 []int
		for _, v := range q {
			for _, u := range graph[v].nei {
				if !graph[u].vis {
					graph[u].dist = graph[v].dist + 1
					q1 = append(q1, u)
					graph[u].vis = true
				}
			}
		}
		q = q1
	}
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func recur(v, taken int) int {
	if graph[v].dp[taken] != -1 {
		return graph[v].dp[taken]
	}
	mn := graph[v].dist
	for _, u := range graph[v].nei {
		cost := 0
		if graph[v].dist >= graph[u].dist {
			cost = 1
		}
		if taken+cost < 2 {
			mn = min(mn, min(recur(u, taken+cost), graph[u].dist))
		}
	}
	graph[v].dp[taken] = mn
	return mn
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, m int
		fmt.Fscan(in, &n, &m)
		for i := 0; i < m; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			graph[u].nei = append(graph[u].nei, v)
		}
		popDist()
		for i := 1; i <= n; i++ {
			for j := 0; j < 2; j++ {
				graph[i].dp[j] = -1
			}
		}
		for i := 1; i <= n; i++ {
			fmt.Fprint(out, recur(i, 0), " ")
		}
		fmt.Fprintln(out, "")
		for i := 1; i <= n; i++ {
			graph[i].nei = nil
			graph[i].vis = false
			graph[i].dist = 0
		}
		t--
	}
	out.Flush()

}

package main

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

type node struct {
	nei  []int
	dist int
	vis  bool
	dp   [2]int
}

const MAXN int = 2e5 + 5

var graph [MAXN]node

func popDist() {
	q := []int{1}
	graph[1].dist = 0
	graph[1].vis = true
	for len(q) > 0 {
		var q1 []int
		for _, v := range q {
			for _, u := range graph[v].nei {
				if !graph[u].vis {
					graph[u].dist = graph[v].dist + 1
					q1 = append(q1, u)
					graph[u].vis = true
				}
			}
		}
		q = q1
	}
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func recur(v int, taken int) int {
	if graph[v].dp[taken] != -1 {
		return graph[v].dp[taken]
	}
	mn := graph[v].dist
	for _, u := range graph[v].nei {
		cost := 0
		if graph[v].dist >= graph[u].dist {
			cost = 1
		}
		if taken+cost < 2 {
			mn = min(mn, min(recur(u, taken+cost), graph[u].dist))
		}
	}
	graph[v].dp[taken] = mn
	return mn
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, m int
		fmt.Fscan(in, &n, &m)
		for i := 0; i < m; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			graph[u].nei = append(graph[u].nei, v)
		}
		popDist()
		for i := 1; i <= n; i++ {
			for j := 0; j < 2; j++ {
				graph[i].dp[j] = -1
			}
		}
		for i := 1; i <= n; i++ {
			fmt.Fprint(out, recur(i, 0), " ")
		}
		fmt.Fprintln(out, "")
		for i := 1; i <= n; i++ {
			graph[i].nei = nil
			graph[i].vis = false
			graph[i].dist = 0
		}
		t--
	}
	out.Flush()

}

package main

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

type node struct {
	nei  []int
	dist int
	vis  bool
	dp   [2]int
}

const MAXN int = 2e5 + 5

var graph [MAXN]node

func popDist() {
	q := []int{1}
	graph[1].dist = 0
	graph[1].vis = true
	for len(q) > 0 {
		var q1 []int
		for _, v := range q {
			for _, u := range graph[v].nei {
				if !graph[u].vis {
					graph[u].dist = graph[v].dist + 1
					q1 = append(q1, u)
					graph[u].vis = true
				}
			}
		}
		q = q1
	}
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func recur(v int, taken int) int {
	if graph[v].dp[taken] != -1 {
		return graph[v].dp[taken]
	}
	mn := graph[v].dist
	for _, u := range graph[v].nei {
		cost := 0
		if graph[v].dist >= graph[u].dist {
			cost = 1
		}
		if taken+cost < 2 {
			mn = min(mn, min(recur(u, taken+cost), graph[u].dist))
		}
	}
	graph[v].dp[taken] = mn
	return mn
}
func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, m int
		fmt.Fscan(in, &n, &m)
		for i := 0; i < m; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			graph[u].nei = append(graph[u].nei, v)
		}
		popDist()
		for i := 1; i <= n; i++ {
			for j := 0; j < 2; j++ {
				graph[i].dp[j] = -1
			}
		}
		for i := 1; i <= n; i++ {
			fmt.Print(recur(i, 0), " ")
		}
		fmt.Println("")
		for i := 1; i <= n; i++ {
			graph[i].nei = nil
			graph[i].vis = false
			graph[i].dist = 0
		}
		t--
	}

}

package main

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

type node struct {
	nei  []int
	dist int
	vis bool
	dp [2]int
}

const MAXN int = 2e5 + 5

var graph [MAXN] node

func popDist() {
	q := []int{1}
	graph[1].dist = 0
	graph[1].vis = true
	for len(q) > 0 {
		var q1 []int
		for _, v := range q {
			for _, u := range graph[v].nei {
				if !graph[u].vis {
					graph[u].dist = graph[v].dist + 1
					q1 = append(q1, u)
					graph[u].vis = true
				}
			}
		}
		q = q1
	}
}
func recur(v int, taken int) int {
	if graph[v].dp[taken] != -1 {
		return graph[v].dp[taken]
	}
	mn := graph[v].dist
	for _, u := range graph[v].nei {
		cost := 0
		if graph[v].dist >= graph[u].dist {
			cost = 1
		}
		if taken+cost < 2 {
			mn = int(math.Min(float64(mn), math.Min(float64(recur(u, taken+cost)), float64(graph[u].dist))))
		}
	}
	graph[v].dp[taken] = mn
	return mn
}
func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, m int
		fmt.Fscan(in, &n, &m)
		for i := 0; i < m; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			graph[u].nei = append(graph[u].nei, v)
		}
		popDist()
		for i := 1; i <= n; i++ {
			for j := 0; j < 2; j++ {
				graph[i].dp[j] = -1
			}
		}
		for i := 1; i <= n; i++ {
			fmt.Print(recur(i, 0), " ")
		}
		fmt.Println("")
		for i := 1; i <= n; i++ {
			graph[i].nei = nil
			graph[i].vis = false
			graph[i].dist = 0
		}
		t--
	}

}

package main

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

const MAXN int = 2e5 + 5

var graph [MAXN][]int
var dist [MAXN]int
var vis [MAXN]bool
var dp [MAXN][2]int

func popDist() {
	q := []int{1}
	vis[1] = true
	dist[1] = 0
	for len(q) > 0 {
		var q1 []int
		for _, v := range q {
			for _, u := range graph[v] {
				if !vis[u] {
					dist[u] = dist[v] + 1
					q1 = append(q1, u)
					vis[u] = true
				}
			}
		}
		q = q1
	}
}
func recur(v int, taken int) int {
	if dp[v][taken] != -1 {
		return dp[v][taken]
	}
	mn := dist[v]
	for _, u := range graph[v] {
		cost := 0
		if dist[v] >= dist[u] {
			cost = 1
		}
		if taken+cost < 2 {
			mn = int(math.Min(float64(mn), math.Min(float64(recur(u, taken+cost)), float64(dist[u]))))
		}
	}
	dp[v][taken] = mn
	return mn
}
func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, m int
		fmt.Fscan(in, &n, &m)
		for i := 0; i < m; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			graph[u] = append(graph[u], v)
		}
		popDist()
		for i := 1; i <= n; i++ {
			for j := 0; j < 2; j++ {
				dp[i][j] = -1
			}
		}
		for i := 1; i <= n; i++ {
			fmt.Print(recur(i, 0), " ")
		}
		fmt.Println("")
		for i := 1; i <= n; i++ {
			graph[i] = nil
			dist[i] = 0
			vis[i] = false
		}
		t--
	}

}

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(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		g := make([][]int, n+1)
		for i := 0; i < m; i++ {
			v, w := 0, 0
			Fscan(in, &v, &w)
			//v--
			//w--
			g[v] = append(g[v], w)
		}

		vis := make([]bool, n+1)
		vis[1] = true
		type pair struct{ v, d int }
		q := []pair{{1, 0}}
		dis := make([]int, n+1)
		for len(q) > 0 {
			p := q[0]
			q = q[1:]
			v, d := p.v, p.d
			dis[v] = d
			for _, w := range g[v] {
				if !vis[w] {
					vis[w] = true
					q = append(q, pair{w, d + 1})
				}
			}
		}
		const inf int = 1e9 // 1e18
		dp := make([][2]int, n+1)
		for _i := range dp {
			dp[_i] = [2]int{inf, inf}
		}

		var f func(int, int) int
		f = func(v, useBack int) int {
			if dp[v][useBack] < inf {
				return dp[v][useBack]
			}
			dp[v][useBack] = dis[v]
			for _, w := range g[v] {
				if dis[w] > dis[v] {
					dp[v][useBack] = min(dp[v][useBack], f(w, useBack)) // fixme
				} else if useBack == 0 {
					dp[v][useBack] = min(dp[v][useBack], f(w, 1))
				}
			}
			return dp[v][useBack]
		}
		for i := 1; i <= n; i++ {
			f(i, 0)
		}
		for _, v := range dp[1:] {
			Fprint(out, min(v[0], v[1]), " ")
		}
		Fprintln(out)
	}

	var t int
	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) }

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

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

type Solver struct {
	n     int64
	m     int64
	nodes map[int64]*Node
}

type Node struct {
	Val int64
	Out map[int64]bool // 1-dir
	C0  int64
	C1  int64
	D   int64
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		nodes := map[int64]*Node{}
		for i := n; i > 0; i-- {
			nodes[i] = &Node{
				Val: i,
				Out: map[int64]bool{},
				C0:  -1,
				C1:  -1,
				D:   -1,
			}
		}
		for i := m; i > 0; i-- {
			l := sint64()
			r := sint64()
			nodes[l].Out[r] = true
		}
		(&Solver{
			n:     n,
			m:     m,
			nodes: nodes,
		}).Solve()
	}
}

func (s *Solver) Solve() {
	s.computeDist()
	for i := s.n; i >= 1; i-- {
		s.computeC0(i)
	}
	for i := s.n; i >= 1; i-- {
		s.computeC1(i)
	}
	s.output()
}

func (s *Solver) output() {
	for i := int64(1); i <= s.n; i++ {
		printf("%d ", s.nodes[i].C1)
	}
	printf("\n")
}

func (s *Solver) computeDist() {
	explored := map[int64]bool{}
	toExplore := []int64{1}
	d := int64(0)
	for len(toExplore) > 0 {
		nxt := []int64{}
		for _, i := range toExplore {
			if explored[i] {
				continue
			}
			explored[i] = true
			s.nodes[i].D = d
			for out := range s.nodes[i].Out {
				if !explored[out] {
					nxt = append(nxt, out)
				}
			}
		}
		toExplore = nxt
		d++
	}
}

func (s *Solver) computeC0(n int64) int64 {
	node := s.nodes[n]
	if node.C0 != -1 {
		return node.C0
	}
	currMin := node.D
	for out := range node.Out {
		if node.D >= s.nodes[out].D {
			continue
		}
		nxt := s.computeC0(out)
		if nxt < currMin {
			currMin = nxt
		}
	}
	node.C0 = currMin
	return currMin
}

func (s *Solver) computeC1(n int64) int64 {
	node := s.nodes[n]
	if node.C1 != -1 {
		return node.C1
	}
	currMin := node.C0
	for out := range node.Out {
		if node.D >= s.nodes[out].D {
			nxt := s.computeC0(out)
			if nxt < currMin {
				currMin = nxt
			}
		} else {
			nxt := s.computeC1(out)
			if nxt < currMin {
				currMin = nxt
			}
		}
	}
	node.C1 = currMin
	return currMin
}

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(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		g := make([][]int, n+1)
		for i := 0; i < m; i++ {
			v, w := 0, 0
			Fscan(in, &v, &w)
			//v--
			//w--
			g[v] = append(g[v], w)
		}

		vis := make([]bool, n+1)
		vis[1] = true
		type pair struct{ v, d int }
		q := []pair{{1, 0}}
		dis := make([]int, n+1)
		for len(q) > 0 {
			p := q[0]
			q = q[1:]
			v, d := p.v, p.d
			dis[v] = d
			for _, w := range g[v] {
				if !vis[w] {
					vis[w] = true
					q = append(q, pair{w, d + 1})
				}
			}
		}
		const inf int = 1e9 // 1e18
		dp := make([][2]int, n+1)
		for _i := range dp {
			dp[_i] = [2]int{inf,inf}
		}

		var f func(int, int) int
		f = func(v, useBack int) int {
			if dp[v][useBack] <inf {
				return dp[v][useBack]
			}
			dp[v][useBack] = dis[v]
			for _, w := range g[v] {
				if dis[w] > dis[v] {
					dp[v][useBack] = min(dp[v][useBack], f(w, 0))
				} else if useBack == 0 {
					dp[v][useBack] = min(dp[v][useBack], f(w, 1))
				}
			}
			return dp[v][useBack]
		}
		for i := 1; i <= n; i++ {
			f(i, 0)
		}
		for _, v := range dp[1:] {
			Fprint(out, min(v[0], v[1]), " ")
		}
		Fprintln(out)
	}

	var t int
	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) }
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


You are given a rooted tree. It contains $$$n$$$ vertices, which are numbered from $$$1$$$ to $$$n$$$. The root is the vertex $$$1$$$.Each edge has two positive integer values. Thus, two positive integers $$$a_j$$$ and $$$b_j$$$ are given for each edge.Output $$$n-1$$$ numbers $$$r_2, r_3, \dots, r_n$$$, where $$$r_i$$$ is defined as follows.Consider the path from the root (vertex $$$1$$$) to $$$i$$$ ($$$2 \le i \le n$$$). Let the sum of the costs of $$$a_j$$$ along this path be $$$A_i$$$. Then $$$r_i$$$ is equal to the length of the maximum prefix of this path such that the sum of $$$b_j$$$ along this prefix does not exceed $$$A_i$$$.    Example for $$$n=9$$$. The blue color shows the costs of $$$a_j$$$, and the red color shows the costs of $$$b_j$$$. Consider an example. In this case:  $$$r_2=0$$$, since the path to $$$2$$$ has an amount of $$$a_j$$$ equal to $$$5$$$, only the prefix of this path of length $$$0$$$ has a smaller or equal amount of $$$b_j$$$; $$$r_3=3$$$, since the path to $$$3$$$ has an amount of $$$a_j$$$ equal to $$$5+9+5=19$$$, the prefix of length $$$3$$$ of this path has a sum of $$$b_j$$$ equal to $$$6+10+1=17$$$ ( the number is $$$17 \le 19$$$); $$$r_4=1$$$, since the path to $$$4$$$ has an amount of $$$a_j$$$ equal to $$$5+9=14$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$6$$$ (this is the longest suitable prefix, since the prefix of length $$$2$$$ already has an amount of $$$b_j$$$ equal to $$$6+10=16$$$, which is more than $$$14$$$); $$$r_5=2$$$, since the path to $$$5$$$ has an amount of $$$a_j$$$ equal to $$$5+9+2=16$$$, the prefix of length $$$2$$$ of this path has a sum of $$$b_j$$$ equal to $$$6+10=16$$$ (this is the longest suitable prefix, since the prefix of length $$$3$$$ already has an amount of $$$b_j$$$ equal to $$$6+10+1=17$$$, what is more than $$$16$$$); $$$r_6=1$$$, since the path up to $$$6$$$ has an amount of $$$a_j$$$ equal to $$$2$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$1$$$; $$$r_7=1$$$, since the path to $$$7$$$ has an amount of $$$a_j$$$ equal to $$$5+3=8$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$6$$$ (this is the longest suitable prefix, since the prefix of length $$$2$$$ already has an amount of $$$b_j$$$ equal to $$$6+3=9$$$, which is more than $$$8$$$); $$$r_8=2$$$, since the path up to $$$8$$$ has an amount of $$$a_j$$$ equal to $$$2+4=6$$$, the prefix of length $$$2$$$ of this path has an amount of $$$b_j$$$ equal to $$$1+3=4$$$; $$$r_9=3$$$, since the path to $$$9$$$ has an amount of $$$a_j$$$ equal to $$$2+4+1=7$$$, the prefix of length $$$3$$$ of this path has a sum of $$$b_j$$$ equal to $$$1+3+3=7$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of test cases follow. Each description begins with a line that contains an integer $$$n$$$ ($$$2 \le n \le 2\cdot10^5$$$) — the number of vertices in the tree. This is followed by $$$n-1$$$ string, each of which contains three numbers $$$p_j, a_j, b_j$$$ ($$$1 \le p_j \le n$$$; $$$1 \le a_j,b_j \le 10^9$$$) — the ancestor of the vertex $$$j$$$, the first and second values an edge that leads from $$$p_j$$$ to $$$j$$$. The value of $$$j$$$ runs through all values from $$$2$$$ to $$$n$$$ inclusive. It is guaranteed that each set of input data has a correct hanged tree with a root at the vertex $$$1$$$. It is guaranteed that the sum of $$$n$$$ over all input test cases does not exceed $$$2\cdot10^5$$$.

For each test case, output $$$n-1$$$ integer in one line: $$$r_2, r_3, \dots, r_n$$$.

NoteThe first example is clarified in the statement.In the second example:  $$$r_2=0$$$, since the path to $$$2$$$ has an amount of $$$a_j$$$ equal to $$$1$$$, only the prefix of this path of length $$$0$$$ has a smaller or equal amount of $$$b_j$$$; $$$r_3=0$$$, since the path to $$$3$$$ has an amount of $$$a_j$$$ equal to $$$1+1=2$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$100$$$ ($$$100 > 2$$$); $$$r_4=3$$$, since the path to $$$4$$$ has an amount of $$$a_j$$$ equal to $$$1+1+101=103$$$, the prefix of length $$$3$$$ of this path has an amount of $$$b_j$$$ equal to $$$102$$$, .

package main

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

const (
	N = 2e5 + 100
)

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

type int int64
type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = int(idx)
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int
var f = false

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if l < len(q) && q[l] <= sum {
		res[u] = int(l + 1)

	}
	if l < len(q) {
		if f {
			fmt.Fprintln(out, u, fa, sum, l, q[l], q)
		}
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := int(1); i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	f = false
	for i := 2; int(i) <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if !f && seq == 2 && qq == 10000 && a > 100000 {
			// fmt.Fprintln(out, fa, a, b)
			// f = true
		}
		add(int(i), fa, a, b)
		add(fa, int(i), a, b)
	}

	dfs(1, 0, 0)
	for i := 2; int(i) <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = int(q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([][3]int64, n-1)
		for i := range a {
			fmt.Fscan(r, &a[i][0], &a[i][1], &a[i][2])
		}
		w1 := do(n, a)
		for _, v := range w1 {
			fmt.Fprint(w, v, " ")
		}
		fmt.Fprintln(w)
	}
}

func do(n int, a [][3]int64) []int {
	e := make([][][3]int64, n+1)
	for i, v := range a {
		e[v[0]] = append(e[v[0]], [3]int64{int64(i + 2), v[1], v[2]})
	}
	r := make([]int, n+1)
	dfs(e, r, 1, 0, []int64{0})
	return r[2:]
}

func dfs(e [][][3]int64, r []int, x, sa int64, sb []int64) {
	for _, v := range e[x] {
		y, a, b := v[0], v[1], v[2]
		sa += a
		sb = append(sb, b+sb[len(sb)-1])
		r[y] = sort.Search(len(sb), func(h int) bool {
			return sb[h] > sa
		}) - 1
		dfs(e, r, y, sa, sb)
		sa -= a
		sb = sb[:len(sb)-1]
	}
}

package main

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

func readLine() string {
	s, err := f.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

var f = bufio.NewReader(os.Stdin)

func main() {
	t := readInts(1)[0]

	for i := 0; i < t; i++ {
		n := readInts(1)[0]
		g := make([][]int, n)
		as := make([]uint64, n)
		bs := make([]uint64, n)
		for j := 1; j < n; j++ {
			l := readInts(3)
			p, a, b := l[0], l[1], l[2]
			g[p-1] = append(g[p-1], j)
			as[j] = uint64(a)
			bs[j] = uint64(b)
		}
		res := make([]int, n)
		dfs(0, g, as, bs, make([]uint64, 1, n), 0, res)
		for _, j := range res[1:] {
			fmt.Print(j, " ")
		}
		fmt.Println()
	}
}

func dfs(c int, g [][]int, as, bs, tmp []uint64, s uint64, res []int) {
	if c != 0 {
		l, r := 0, len(tmp)-1
		for l < r {
			m := (l + r + 1) / 2
			if tmp[m] <= s {
				l = m
			} else {
				r = m - 1
			}
		}
		res[c] = l
	}
	for _, t := range g[c] {
		next := bs[t] + tmp[len(tmp)-1]
		dfs(t, g, as, bs, append(tmp, next), s+as[t], res)
	}
}

package main

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

type e struct {
	id   int
	a, b int64
}

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)

		adj := make([][]e, n+1)
		aa := make([]int, n+1)
		for i := 0; i < n-1; i++ {
			var p int
			var a, b int64
			fmt.Fscan(in, &p, &a, &b)

			adj[p] = append(adj[p], e{i + 2, a, b})
		}

		var walk func(id int, sum int64, s []int64)
		walk = func(id int, sum int64, s []int64) {
			g := sort.Search(len(s), func(i int) bool { return s[i] > sum })
			aa[id] = g
			for _, e2 := range adj[id] {
				b := e2.b
				if len(s) > 0 {
					b += s[len(s)-1]
				}
				walk(e2.id, sum+e2.a, append(s, b))
			}
		}
		walk(1, 0, []int64{})

		for i := 2; i <= n; i++ {
			fmt.Fprintf(out, "%d ", aa[i])
		}
		fmt.Fprintln(out)
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int
var f = false

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1

	}
	if l < len(q) {
		if f {
			fmt.Fprintln(out, u, fa, sum, l, q[l], q)
		}
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	f = false
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if !f && seq == 2 && qq == 10000 && a > 100000 {
			// fmt.Fprintln(out, fa, a, b)
			f = true
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = q
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int
var f = false

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1

	}
	if l < len(q) {
		if f {
			fmt.Fprintln(out, u, fa, sum, l, q[l])
		}
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	f = false
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if !f && seq == 2 && qq == 10000 && a > 100000 {
			// fmt.Fprintln(out, fa, a, b)
			f = true
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = q
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int
var f = false

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
		if f {
			fmt.Fprintln(out, u, fa, sum, l, q[l])
		}
	}
	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	f = false
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if !f && seq == 2 && qq == 10000 && a > 100000 {
			// fmt.Fprintln(out, fa, a, b)
			f = true
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = q
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if l < len(q) && q[l] <= sum {
		res[u] = l + 1
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)

		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprintf(out, "%d ", res[i])
	}
	fmt.Fprintln(out, "")
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var res [N]int

func qwq() {

	var dfs func(int, int, int)

	var idx = 0

	add := func(u, v, w1, w2 int) {
		e[idx] = edge{
			v, w1, w2, h[u],
		}
		h[u] = idx
		idx++
	}

	var q []int

	dfs = func(u, fa, sum int) {

		l, r := 0, len(q)-1
		for l < r {
			mid := (l + r + 1) / 2
			if q[mid] > sum {
				r = mid - 1
			} else {
				l = mid
			}
		}

		if l < len(q) && q[l] <= sum {
			res[u] = l + 1
		}

		for i := h[u]; i != -1; i = e[i].to {
			if e[i].v != fa {
				x := e[i].w2
				if len(q) > 0 {
					x += q[len(q)-1]
				}
				q = append(q, x)
				dfs(e[i].v, u, sum+e[i].w1)
				q = q[:len(q)-1]
			}
		}

	}

	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
		res[i] = 0
	}
	q = make([]int, 0, n+n)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var res [N]int

func qwq() {

	var dfs func(int, int, int)

	var idx = 0

	add := func(u, v, w1, w2 int) {
		e[idx] = edge{
			v, w1, w2, h[u],
		}
		h[u] = idx
		idx++
	}

	var q []int

	dfs = func(u, fa, sum int) {

		l, r := 0, len(q)-1
		for l < r {
			mid := (l + r + 1) / 2
			if q[mid] > sum {
				r = mid - 1
			} else {
				l = mid
			}
		}

		if u > 1 && q[l] <= sum {
			res[u] = l + 1
		}

		for i := h[u]; i != -1; i = e[i].to {
			if e[i].v != fa {
				x := e[i].w2
				if len(q) > 0 {
					x += q[len(q)-1]
				}
				q = append(q, x)
				dfs(e[i].v, u, sum+e[i].w1)
				q = q[:len(q)-1]
			}
		}

	}

	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
		res[i] = 0
	}
	q = make([]int, 0, n+n)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int
var res []int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}

	if u > 1 && q[l] <= sum {
		res[u] = l + 1
		// fmt.Fprintln(out, u, len(q), l, q[l], sum)
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = make([]int, 0)
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)

		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if seq == 2 && qq == 10000 && a > 100000 {
			fmt.Fprintln(out, fa, a, b)
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = q
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if seq == 2 && qq == 10000 && n == 10 {
			fmt.Fprintln(out, fa, a, b)
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = q
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if seq == 2 && qq == 10000 {
			fmt.Fprintln(out, fa, a, b)
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	qq = q
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int
var qq int
var seq int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	seq++
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		if seq == 2 && qq == 10000 {
			fmt.Fprintln(out, fa, a, b)
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	qq = q
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
	} else {
		res[u] = 0
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum int) {

	l, r := 0, len(q)-1
	for l < r {
		mid := (l + r + 1) / 2
		if q[mid] > sum {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if u > 1 && q[l] <= sum {
		res[u] = l + 1
	}

	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			x := e[i].w2
			if len(q) > 0 {
				x += q[len(q)-1]
			}
			q = append(q, x)
			dfs(e[i].v, u, sum+e[i].w1)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum, cur, cnt, idx int) {
	for idx < len(q) && cur+q[idx] <= sum {
		cur += q[idx]
		cnt++
		idx++
	}
	res[u] = cnt
	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			q = append(q, e[i].w2)
			dfs(e[i].v, u, sum+e[i].w1, cur, cnt, idx)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	ht := map[int]int{}
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		ht[a]++
		ht[b]++
		if a != 1 || b != 1 {
			if n == 200000 {
				// fmt.Fprintln(out, i, fa, a, b)
			}
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	if n == 200000 {
		fmt.Fprintln(out, ht)
	} else {
		dfs(1, 0, 0, 0, 0, 0)
		for i := 2; i <= n; i++ {
			fmt.Fprint(out, res[i], " ")
		}
		fmt.Fprintln(out)

	}

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}

}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum, cur, cnt, idx int) {
	for idx < len(q) && cur+q[idx] <= sum {
		cur += q[idx]
		cnt++
		idx++
	}
	res[u] = cnt
	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			q = append(q, e[i].w2)
			dfs(e[i].v, u, sum+e[i].w1, cur, cnt, idx)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	ht := map[int]int{}
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		ht[a]++
		ht[b]++
		if a != 1 || b != 1 {
			if n == 200000 {
				fmt.Fprintln(out, i, fa, a, b)
			}
		}
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	if n == 200000 {
		fmt.Fprintln(out, ht)
	} else {
		dfs(1, 0, 0, 0, 0, 0)
		for i := 2; i <= n; i++ {
			fmt.Fprint(out, res[i], " ")
		}
		fmt.Fprintln(out)

	}

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}

}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum, cur, cnt, idx int) {
	for idx < len(q) && cur+q[idx] <= sum {
		cur += q[idx]
		cnt++
		idx++
	}
	res[u] = cnt
	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			q = append(q, e[i].w2)
			dfs(e[i].v, u, sum+e[i].w1, cur, cnt, idx)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	ht := map[int]int{}
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		ht[a]++
		ht[b]++

		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	if n == 200000 {
		fmt.Fprintln(out, ht[1])
	} else {
		dfs(1, 0, 0, 0, 0, 0)
		for i := 2; i <= n; i++ {
			fmt.Fprint(out, res[i], " ")
		}
		fmt.Fprintln(out)

	}

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}

}

package main

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

const (
	N = 2e5 + 100
)

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

type edge struct {
	v, w1, w2, to int
}

var e [N * 10]edge
var h [N]int
var idx = 0

func add(u, v, w1, w2 int) {
	e[idx] = edge{
		v, w1, w2, h[u],
	}
	h[u] = idx
	idx++
}

var q []int = make([]int, 0, N)
var res []int

func dfs(u, fa, sum, cur, cnt, idx int) {
	for idx < len(q) && cur+q[idx] <= sum {
		cur += q[idx]
		cnt++
		idx++
	}
	res[u] = cnt
	for i := h[u]; i != -1; i = e[i].to {
		if e[i].v != fa {
			q = append(q, e[u].w2)
			dfs(e[i].v, u, sum+e[i].w1, cur, cnt, idx)
			q = q[:len(q)-1]
		}
	}

}

func qwq() {
	var n int
	fmt.Fscanln(in, &n)
	for i := 1; i <= n; i++ {
		h[i] = -1
	}
	q = q[:0]
	res = make([]int, n+1)
	idx = 0
	for i := 2; i <= n; i++ {
		var fa, a, b int
		fmt.Fscanln(in, &fa, &a, &b)
		add(i, fa, a, b)
		add(fa, i, a, b)
	}

	dfs(1, 0, 0, 0, 0, 0)
	for i := 2; i <= n; i++ {
		fmt.Fprint(out, res[i], " ")
	}
	fmt.Fprintln(out)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([][3]int, n-1)
		for i := range a {
			fmt.Fscan(r, &a[i][0], &a[i][1], &a[i][2])
		}
		w1 := do(n, a)
		for _, v := range w1 {
			fmt.Fprint(w, v, " ")
		}
		fmt.Fprintln(w)
	}
}

func do(n int, a [][3]int) []int {
	e := make([][][3]int, n+1)
	for i, v := range a {
		e[v[0]] = append(e[v[0]], [3]int{i + 2, v[1], v[2]})
	}
	r := make([]int, n+1)
	dfs(e, r, 1, 0, []int{0})
	return r[2:]
}

func dfs(e [][][3]int, r []int, x, sa int, sb []int) {
	for _, v := range e[x] {
		y, a, b := v[0], v[1], v[2]
		sa += a
		if len(sb) > 0 {
			b += sb[len(sb)-1]
		}
		sb = append(sb, b)
		r[y] = sort.Search(len(sb), func(h int) bool {
			return sb[h] > sa
		}) - 1
		dfs(e, r, y, sa, sb)
		sa -= a
		sb = sb[:len(sb)-1]
	}
}

package main

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

func readLine() string {
	s, err := f.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

var f = bufio.NewReader(os.Stdin)

func main() {
	t := readInts(1)[0]

	for i := 0; i < t; i++ {
		n := readInts(1)[0]
		g := make([][]int, n)
		as := make([]int, n)
		bs := make([]int, n)
		for j := 1; j < n; j++ {
			l := readInts(3)
			p, a, b := l[0], l[1], l[2]
			g[p-1] = append(g[p-1], j)
			as[j] = a
			bs[j] = b
		}
		res := make([]int, n)
		dfs(0, g, as, bs, make([]int, 1, n), 0, res)
		for _, j := range res[1:] {
			fmt.Print(j, " ")
		}
		fmt.Println()
	}
}

func dfs(c int, g [][]int, as, bs, tmp []int, s int, res []int) {
	if c != 0 {
		l, r := 0, len(tmp)-1
		for l < r {
			m := (l + r + 1) / 2
			if tmp[m] <= s {
				l = m
			} else {
				r = m - 1
			}
		}
		res[c] = l
	}
	for _, t := range g[c] {
		next := bs[t] + tmp[len(tmp)-1]
		dfs(t, g, as, bs, append(tmp, next), s+as[t], res)
	}
}


As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!The sequence of round brackets is called valid if and only if:   the total number of opening brackets is equal to the total number of closing brackets;  for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets. Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.

First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively. The second line contains string s of length m consisting of characters '(' and ')' only.

Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.

NoteIn the first sample there are four different valid pairs:   p = "(", q = "))"  p = "()", q = ")"  p = "", q = "())"  p = "", q = ")()" In the second sample the only way to obtain a desired string is choose empty p and q.In the third sample there is no way to get a valid sequence of brackets.

package main

import "fmt"

const mod = 1e9 + 7

var combs [2002][2002]int64

func init() {
	combs[0][0] = 1
	for l := 1; l < len(combs); l++ {
		for open := 0; open <= l; open++ {
			if open > 0 {
				combs[l][open] = combs[l-1][open-1]
			}
			if open+1 <= l-1 {
				combs[l][open] = (combs[l][open] + combs[l-1][open+1]) % mod
			}
		}
	}
}

func main() {
	var n, m int
	var s string
	fmt.Scan(&n, &m, &s)
	openBefore := 0
	closeAfter := 0
	for i := range s {
		if s[i] == '(' {
			closeAfter++
		} else {
			closeAfter--
		}
		if openBefore < -closeAfter {
			openBefore = -closeAfter
		}
	}
	var ans int64
	for pLen := 0; pLen <= n-m; pLen++ {
		for open := openBefore; open <= pLen; open++ {
			clos := open + closeAfter
			if clos >= 0 && clos <= n-m-pLen {
				ans = (ans + combs[pLen][open]*combs[n-m-pLen][clos]) % mod
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"

var n, m int
var s string
const mod = 1e9 + 7
var f [2010][2010]int

func gao() {
	maxOwn, curOpen := 0, 0
	for _, c := range s {
		if c == '(' {
			curOpen++
		} else {
			curOpen--
		}
		if curOpen < 0 {
			if -curOpen > maxOwn { maxOwn = -curOpen }
		}
	}
	l := n - m
	f[0][0] = 1

	for i := 0; i < l; i++ {
		for j := 0; j <= i; j++ {
			if f[i][j] == 0 { continue }

			f[i+1][j+1] = (f[i+1][j+1] + f[i][j]) % mod
			if j > 0 {
				f[i+1][j-1] = (f[i+1][j-1] + f[i][j]) % mod
			}
		}
	}

	var ans int64
	for i := maxOwn; i <= l; i++ {
		for j := maxOwn; j <= i; j++ {
			if f[i][j] == 0 { continue }

			k := j + curOpen
			if k > l - i { continue }
			rv := f[l - i][k]

			ans = (ans + int64(f[i][j]) * int64(rv)) % mod
		}
	}
	fmt.Println(ans)
}

func main() {
	fmt.Scanf("%d%d\n", &n, &m)
	fmt.Scanf("%s\n", &s)

	gao()
}

// Codeforces 629 C XXX TODO
package main

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

// Mod module for answer
var Mod = 1000000007
// Mdim max length of start and end pieces.
var Mdim = 2004

var pa []int

func mki(n int, k int) int {
    if n<0 || n>=Mdim || k<0 || k>n {
        fmt.Println("bad indexes ", n, " ", k)
        panic(0)
    }
    return (n*(n+1))/2 + k
}

func main() {
    // reading n

    var n,m int
    in    := bufio.NewReader(os.Stdin)
    s,err := in.ReadString('\n')
    if err != nil {
        fmt.Println("first read failure", err)
        return
    }
    ss := strings.Split(strings.Trim(s," \n\r")," ")
    n,err = strconv.Atoi(ss[0])
    m,err1 := strconv.Atoi(ss[1])
    if err != nil || err1 != nil {
        fmt.Println("second read failure", err)
        return
    }
    skb,err2 := in.ReadString('\n')
    if err2 != nil {
        fmt.Println("failure reading skobki string", err2)
        return
    }
    skb = strings.Trim(skb," \n\r")
    if len(skb)!=m {
        fmt.Printf("bad skb string |%s|\n", skb)
        return
    }

    // prepare tables
    nm := n-m
    pa = make([]int, Mdim*Mdim)
    pa[mki(0,0)] = 1
    pa[mki(1,0)] = 0
    pa[mki(1,1)] = 1
    for ln:=2; ln<=nm; ln++ {
        for ex:=0; ex<=ln; ex++ {
            plx := 0
            if ex>0 {
                plx += pa[mki(ln-1,ex-1)]
            }
            if ln >= ex+2 {
                plx += pa[mki(ln-1,ex+1)]
            }
            pa[mki(ln,ex)] = plx % Mod
        }
    }
    // for ln:=0; ln<=nm; ln++ {
    //     fmt.Printf("ln=%2d: ", ln)
    //     for ex:=0; ex<=ln; ex++ {
    //         fmt.Print(pa[mki(ln,ex)], " ")
    //     }
    //     fmt.Println()
    // }
    // count skobki in skb
    minl := 0
    lev := 0
    nop := 0   // number of '('
    for i:=0; i<len(skb); i++ {
        if skb[i]=='(' {
            lev--
            nop++
        } else {
            lev++
        }
        if lev > minl {
            minl = lev
        }
    }
    minr := 0
    lev = 0
    for i:=len(skb)-1; i>=0; i-- {
        if skb[i]==')' {
            lev--
        } else {
            lev++
        }
        if lev > minr {
            minr = lev
        }
    }
    //fmt.Printf("minl,minr,nop,nm = %d %d %d %d\n",minl,minr,nop, nm)

    // get count
    var cnt,cntl,cntr int64
    cnt = 0
    nle := m - nop
    //fmt.Printf("nop,nle = %d %d\n", nop,nle)
    for lnl:=0; lnl<=nm; lnl++ {
        lnr := nm - lnl
        if lnl<minl || lnr<minr || lnr<0 {
            continue
        }
        //fmt.Printf("lnl,lnr = %d %d\n", lnl,lnr)
        for nl:=minl; nl<=lnl; nl++ {
            nr := nl + (nop-nle)
            if nr<0 || lnr<nr {
                continue
            }
            cntl = int64(pa[mki(lnl,nl)])
            cntr = int64(pa[mki(lnr,nr)])
            cnt += cntr*cntl
            cnt = cnt % int64(Mod)
            //fmt.Printf("lnl,lnr,nl = %d %d %d", lnl, lnr, nl)
            //fmt.Printf("  cntl,cntr,cnt = %d %d %d\n", cntl,cntr,cnt)
        }
    }

	// print result
    fmt.Printf("%d\n", cnt)
}

package main

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

const (
	Mod = 1000000007
)

var n, m int
var inp string
var minOpenings int
var cache map[[2]int]int32

func ways(brackets, diff int) int64 {
	if brackets%2 != diff%2 {
		return 0
	}
	cacheKey := [2]int{brackets, diff}
	if val, ok := cache[cacheKey]; ok {
		return int64(val)
	}
	if brackets == 0 {
		if diff == 0 {
			cache[cacheKey] = 1
		} else {
			cache[cacheKey] = 0
		}
		return int64(cache[cacheKey])
	}

	ans := ways(brackets-1, diff+1)
	if diff > 0 {
		ans = (ans + ways(brackets-1, diff-1)) % Mod
	}
	cache[cacheKey] = int32(ans)
	return ans
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d\n%s", &n, &m, &inp)
	cache = make(map[[2]int]int32)

	openings := 0
	for i := 0; i < m; i++ {
		if inp[i] == ')' {
			openings--
		} else {
			openings++
		}
		if minOpenings > openings {
			minOpenings = openings
		}
	}

	minOpenings = -minOpenings
	maxSizePrefix := n - m

	ans := int64(0)
	for i := 0; i <= maxSizePrefix; i++ {
		for j := minOpenings; j <= i; j++ {
			if maxSizePrefix-i < j+openings {
				break
			}
			ans = (ans + (ways(i, j)*ways(maxSizePrefix-i, j+openings))%Mod) % Mod
		}
	}

	fmt.Println(ans)
}

package main

import "fmt"

const mod = 1e9 + 7

var combs [2002][2002]int64

func init() {
	combs[0][0] = 1
	for l := 1; l < len(combs); l++ {
		for open := 0; open <= l; open++ {
			if open > 0 {
				combs[l][open] = combs[l-1][open-1]
			}
			if open+1 <= l-1 {
				combs[l][open] = (combs[l][open] + combs[l-1][open+1]) % mod
			}
		}
	}
}

func main() {
	var n, m int
	var s string
	fmt.Scan(&n, &m, &s)
	openBefore := 0
	closeAfter := 0
	for i := range s {
		if s[i] == '(' {
			closeAfter++
		} else {
			closeAfter--
		}
		if openBefore < -closeAfter {
			openBefore = -closeAfter
		}
	}
	var ans int64
	for pLen := 0; pLen <= n-m; pLen++ {
		for open := openBefore; open <= pLen; open++ {
			clos := open - openBefore + closeAfter
			if clos >= 0 && clos <= n-m-pLen {
				ans = (ans + combs[pLen][open]*combs[n-m-pLen][clos]) % mod
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"

var n, m int
var s string
const mod = 1e9 + 7
var f [2010][2010]int

func gao() {
	maxOwn, curOpen := 0, 0
	for _, c := range s {
		if c == '(' {
			curOpen++
		} else {
			curOpen--
		}
		if curOpen < 0 {
			if -curOpen > maxOwn { maxOwn = -curOpen }
		}
	}
	l := n - m
	f[0][0] = 1

	for i := 0; i < l; i++ {
		for j := 0; j <= i; j++ {
			if f[i][j] == 0 { continue }

			f[i+1][j+1] = (f[i+1][j+1] + f[i][j]) % mod
			if j > 0 {
				f[i+1][j-1] = (f[i+1][j-1] + f[i][j]) % mod
			}
		}
	}

	var ans int64
	for i := maxOwn; i <= l; i++ {
		for j := maxOwn; j <= i; j++ {
			if f[i][j] == 0 { continue }

			k := j + curOpen
			if k > l - i { continue }
			rv := f[l - i][k]

			ans += int64(f[i][j]) * int64(rv)
		}
	}
	fmt.Println(ans)
}

func main() {
	fmt.Scanf("%d%d\n", &n, &m)
	fmt.Scanf("%s\n", &s)

	gao()
}

// Codeforces 629 C XXX TODO
package main

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

// Mod module for answer
var Mod = 1000000007
// Mdim max length of start and end pieces.
var Mdim = 2004

var pa []int

func mki(n int, k int) int {
    if n<0 || n>=Mdim || k<0 || k>n {
        fmt.Println("bad indexes ", n, " ", k)
        panic(0)
    }
    return (n*(n+1))/2 + k
}

func main() {
    // reading n

    var n,m int
    in    := bufio.NewReader(os.Stdin)
    s,err := in.ReadString('\n')
    if err != nil {
        fmt.Println("first read failure", err)
        return
    }
    ss := strings.Split(strings.Trim(s," \n\r")," ")
    n,err = strconv.Atoi(ss[0])
    m,err1 := strconv.Atoi(ss[1])
    if err != nil || err1 != nil {
        fmt.Println("second read failure", err)
        return
    }
    skb,err2 := in.ReadString('\n')
    if err2 != nil {
        fmt.Println("failure reading skobki string", err2)
        return
    }
    skb = strings.Trim(skb," \n\r")
    if len(skb)!=m {
        fmt.Printf("bad skb string |%s|\n", skb)
        return
    }

    // prepare tables
    nm := n-m
    pa = make([]int, Mdim*Mdim)
    pa[mki(0,0)] = 1
    pa[mki(1,0)] = 0
    pa[mki(1,1)] = 1
    for ln:=2; ln<=nm; ln++ {
        for ex:=0; ex<=ln; ex++ {
            plx := 0
            if ex>0 {
                plx += pa[mki(ln-1,ex-1)]
            }
            if ln >= ex+2 {
                plx += pa[mki(ln-1,ex+1)]
            }
            pa[mki(ln,ex)] = plx % Mod
        }
    }
    // for ln:=0; ln<=nm; ln++ {
    //     fmt.Printf("ln=%2d: ", ln)
    //     for ex:=0; ex<=ln; ex++ {
    //         fmt.Print(pa[mki(ln,ex)], " ")
    //     }
    //     fmt.Println()
    // }
    // count skobki in skb
    minl := 0
    lev := 0
    nop := 0   // number of '('
    for i:=0; i<len(skb); i++ {
        if skb[i]=='(' {
            lev--
            nop++
        } else {
            lev++
        }
        if lev > minl {
            minl = lev
        }
    }
    minr := 0
    lev = 0
    for i:=len(skb)-1; i>=0; i-- {
        if skb[i]==')' {
            lev--
        } else {
            lev++
        }
        if lev > minr {
            minr = lev
        }
    }
    //fmt.Printf("minl,minr,nop,nm = %d %d %d %d\n",minl,minr,nop, nm)

    // get count
    cnt := 0
    nle := m - nop
    //fmt.Printf("nop,nle = %d %d\n", nop,nle)
    for lnl:=0; lnl<=nm; lnl++ {
        lnr := nm - lnl
        if lnl<minl || lnr<minr || lnr<0 {
            continue
        }
        //fmt.Printf("lnl,lnr = %d %d\n", lnl,lnr)
        for nl:=minl; nl<=lnl; nl++ {
            nr := nl + (nop-nle)
            if nr<0 || lnr<nr {
                continue
            }
            cntl := pa[mki(lnl,nl)]
            cntr := pa[mki(lnr,nr)]
            cnt += cntr*cntl
            cnt = cnt % Mod
            //fmt.Printf("lnl,lnr,nl = %d %d %d", lnl, lnr, nl)
            //fmt.Printf("  cntl,cntr,cnt = %d %d %d\n", cntl,cntr,cnt)
        }
    }

	// print result
    fmt.Printf("%d\n", cnt)
}


Let $$$n$$$ be an integer. Consider all permutations on integers $$$1$$$ to $$$n$$$ in lexicographic order, and concatenate them into one big sequence $$$p$$$. For example, if $$$n = 3$$$, then $$$p = [1, 2, 3, 1, 3, 2, 2, 1, 3, 2, 3, 1, 3, 1, 2, 3, 2, 1]$$$. The length of this sequence will be $$$n \cdot n!$$$.Let $$$1 \leq i \leq j \leq n \cdot n!$$$ be a pair of indices. We call the sequence $$$(p_i, p_{i+1}, \dots, p_{j-1}, p_j)$$$ a subarray of $$$p$$$. Its length is defined as the number of its elements, i.e., $$$j - i + 1$$$. Its sum is the sum of all its elements, i.e., $$$\sum_{k=i}^j p_k$$$. You are given $$$n$$$. Find the number of subarrays of $$$p$$$ of length $$$n$$$ having sum $$$\frac{n(n+1)}{2}$$$. Since this number may be large, output it modulo $$$998244353$$$ (a prime number).

The only line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$), as described in the problem statement.

Output a single integer — the number of subarrays of length $$$n$$$ having sum $$$\frac{n(n+1)}{2}$$$, modulo $$$998244353$$$.

package main

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

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

	var n int64
	Fscan(in, &n)
	ans := n
	for i := int64(2); i <= n; i++ {
		ans = (ans - 1) * i % 998244353
	}
	Fprint(out, ans)
}

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

package main

import (
	"fmt"
)

func main() {
	var n, ans, i, swp, mod int64
	swp = 1
	ans = 1
	mod = 998244353
	fmt.Scanf ( "%d", &n )
	for i = 2 ; i <= n ; i++ {
		swp *= i
		swp %= mod
		ans = ( i * ans ) % mod + swp - i
		ans %= mod
	}
	fmt.Println ( ans )
}

package main

import (
	"fmt"
)

func main() {
	var n int
	swp := 1
	ans := 1
	mod := 998244353
	fmt.Scanf ( "%d", &n )
	for i := 2 ; i <= n ; i++ {
		swp *= i
		swp %= mod
		ans = ( i * ans ) % mod + swp - i
		ans %= mod
	}
	fmt.Println ( ans )
}