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


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


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

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

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


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


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

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


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

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

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

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


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


package main

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

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

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

	var n, m int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if i == 0 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := d[1] - d[0] - abs(h[1] - h[0])
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}
		tmp /= 2

		tmp += max(h[0],h[1])
		maxH = max(maxH, tmp)
	}

	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

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

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

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

	var n, m, maxH int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if i == 0 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := d[1] - d[0] - abs(h[1] - h[0])
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		maxH = max(maxH, tmp/2 + max(h[0], h[1]))
	}
	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

import "fmt"

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}
		fmt.Println(d[1], h[1])
		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

func main() {
	var n, m int

	fmt.Scanf("%d %d\n", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d\n", &d[1], &h[1])

		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

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

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

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

	var n, m, maxH int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	d[1] = 1
	for ; m > 0; m-- {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if d[0] == 1 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := d[1] - d[0] - abs(h[1] - h[0])
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		maxH = max(maxH, tmp/2 + max(h[0], h[1]))
	}
	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

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

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

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

	var n, m int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if i == 0 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		maxH = max(maxH, tmp)
	}

	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

import "fmt"

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}
		fmt.Println(tmp)
		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		fmt.Println(n, m)
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

func max(n, m int) int {
	if n > m {
		return n
	}
	return m
}

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		fmt.Println(d[0], h[0], d[1], h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}


package main

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

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

func readSlice(n int) []int64 {
	s := make([]int64, n)
	for i := range s {
		fmt.Fscan(in, &s[i])
	}
	return s
}

func main() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	h := readSlice(n)
	p := readSlice(m)

	fmt.Println(search(2e10, func(t int64) bool {
		var hi, pi int
		for hi < n && pi < m {
			if p[pi] < h[hi] && h[hi]-p[pi] > t {
				return false
			}
			left := p[pi]
			if h[hi] < left {
				left = h[hi]
			}
			for pi < m && p[pi] <= h[hi] {
				pi++
			}
			for pi < m && (t >= (h[hi]-left)*2+p[pi]-h[hi] || t >= (p[pi]-h[hi])*2+h[hi]-left) {
				pi++
			}
			hi++
		}
		return pi == m
	}))
}

// Tiktų sort.Search, jei CF int būtų 64 bitų...
func search(n int64, f func(i int64) bool) int64 {
	i, j := int64(0), n
	for i < j {
		h := (i + j) >> 1
		if !f(h) {
			i = h + 1
		} else {
			j = h
		}
	}
	return i
}

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

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

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

func goleft(vi int, p, t int64) int {
	vj := vi
	for ; vj < m && abs(vis[vi]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func goright(vi int, p, t int64) int {
	if p <= vis[vi] {
		return 0
	}
	vj := vi
	for ; vj < m && abs(vis[vj]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n && vi < m; i++ {
		vi = max(goleft(vi, pos[i], time), goright(vi, pos[i], time))
	}
	return vi == m
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t >>= 1 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}

package main

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

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

func readSlice(n int) []int64 {
	s := make([]int64, n)
	for i := range s {
		fmt.Fscan(in, &s[i])
	}
	return s
}

func main() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	h := readSlice(n)
	p := readSlice(m)

	fmt.Println(search(2e10, func(t int64) bool {
		var hi, pi int
		for hi < n && pi < m {
			if abs(h[hi]-p[pi]) > t {
				return false
			}
			left := p[pi]
			if h[hi] < left {
				left = h[hi]
			}
			for pi < m && p[pi] <= h[hi] {
				pi++
			}
			for pi < m && (t >= (h[hi]-left)*2+p[pi]-h[hi] || t >= (p[pi]-h[hi])*2+h[hi]-left) {
				pi++
			}
			hi++
		}
		return pi == m
	}))
}

// Tiktų sort.Search, jei CF int būtų 64 bitų...
func search(n int64, f func(i int64) bool) int64 {
	i, j := int64(0), n
	for i < j {
		h := (i + j) >> 1
		if !f(h) {
			i = h + 1
		} else {
			j = h
		}
	}
	return i
}

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

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

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

func can(time int64) bool {
	vi := 0
	for i := 0; i < n; i++ {
		t, p := time, pos[i]
		for {
			target := vis[vi]
			delta := abs(p - target)
			if delta > t {
				break
			}
			t -= delta
			p = target
			vi++
			if vi == m {
				return true
			}
		}
	}
	return false
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t = t / 2 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

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

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

func goleft(vi int, p, t int64) int {
	vj := vi
	for ; vj < m && abs(p - vis[vj]) <= t; vj++ {
		p, t = vis[vj], t - abs(p - vis[vj])
	}
	return vj
}

func goright(vi int, p, t int64) int {
	if p <= vis[vi] || vi == m-1 {
		return 0
	}
	vj := vi+1
	for ; vj < m && vis[vj] < p; vj++ {}
	for ; vj < m && (vis[vj]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n && vi < m; i++ {
		vi = max(goleft(vi, pos[i], time), goright(vi, pos[i], time))
	}
	return vi == m
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t = t / 2 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

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

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

func goleft(vi int, p, t int64) int {
	vj := vi
	for ; vj < m && abs(p - vis[vj]) <= t; vj++ {
		p, t = vis[vj], t - abs(p - vis[vj])
	}
	return vj
}

func goright(vi int, p, t int64) int {
	if p <= vis[vi] || vi == m-1 {
		return 0
	}
	vj := vi
	for ; vj < m && vis[vj] < p; vj++ {}
	for ; vj < m && (vis[vj]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n && vi < m; i++ {
		vi = max(goleft(vi, pos[i], time), goright(vi, pos[i], time))
	}
	return vi == m
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t = t / 2 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}