You are given two integers $$$a$$$ and $$$b$$$. You may perform any number of operations on them (possibly zero).During each operation you should choose any positive integer $$$x$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$ or $$$a := a - 2x$$$, $$$b := b - x$$$. Note that you may choose different values of $$$x$$$ in different operations.Is it possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously?Your program should answer $$$t$$$ independent test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers $$$a$$$ and $$$b$$$ for this test case ($$$0 \le a, b \le 10^9$$$).

For each test case print the answer to it — YES if it is possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

NoteIn the first test case of the example two operations can be used to make both $$$a$$$ and $$$b$$$ equal to zero:  choose $$$x = 4$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$. Then $$$a = 6 - 4 = 2$$$, $$$b = 9 - 8 = 1$$$;  choose $$$x = 1$$$ and set $$$a := a - 2x$$$, $$$b := b - x$$$. Then $$$a = 2 - 2 = 0$$$, $$$b = 1 - 1 = 0$$$.

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for ; n > 0; n-- {
		var a, b int
		fmt.Scan(&a, &b)
		if a > b {
			a, b = b, a
		}
		if (a+b)%3 == 0 && a*2 >= b {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

	defer out.Flush()

	var t,a,b int

	fmt.Fscanf(in , "%d\n", &t)

	for T := 0 ; T < t ; T ++ {
		fmt.Fscanf(in , "%d %d\n" , &a , &b)

		if a > b {
			a, b = b, a
		}

		if ( a + b )% 3 == 0 && 2*a >= b {
			fmt.Fprintf(out, "YES\n")
		}else{
			fmt.Fprintf(out, "NO\n")
		}
	}

}

package main

import "fmt"

func check(a int, b int) bool {
	/*
		2x + y = a
		and
		2y + x = b

		x = (a - y) / 2
		x = b - 2y
		a - y = 2b - 4y
		2b - a = 3y
	*/
	return (a*2-b)%3 == 0 && (a*2-b)/3 >= 0 && (b*2-a)%3 == 0 && (b*2-a)/3 >= 0
}

func main() {
	var t int
	var a, b int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		fmt.Scanln(&a, &b)
		can := check(a, b)
		if can {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var a, b int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		a, b = ReadInt2()

		solve()
	}
}

func solve() {
	if (a+b)%3 != 0 {
		fmt.Println("NO")
		return
	}

	m := (a + b) / 3
	diff := AbsInt(a - b)

	if diff > m {
		fmt.Println("NO")
		return
	}

	// if (m-diff)%2 == 0 {
	// 	fmt.Println("YES")
	// } else {
	// 	fmt.Println("NO")
	// }
	fmt.Println("YES")
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

/*******************************************************************/

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var a, b int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		a, b = ReadInt2()

		solve()
	}
}

func solve() {
	if (a+b)%3 != 0 {
		fmt.Println("NO")
		return
	}

	m := (a + b) / 3
	diff := AbsInt(a - b)

	if diff > m {
		fmt.Println("NO")
		return
	}

	if (m-diff)%2 == 0 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

/*******************************************************************/

package main

import (
	"fmt"
	"os"
)

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

func main() {
	var n int
	fmt.Fscanln(os.Stdin, &n)
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Fscanln(os.Stdin, &a, &b)
		if (a+b)%3 == 0 && min(a, b) >= (a+b)/3 {
			fmt.Println("YES")
		} else {
			fmt.Println(("NO"))
		}
	}
}

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var t int
	Fscan(in, &t)
	for case_ := 0; case_ < t; case_++ {
		if solveCase(in, out) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func solveCase(in io.Reader, out io.Writer) bool {
	var a, b int
	Fscan(in, &a, &b)
	if a < b {
		a, b = b, a
	}
	if a > 2*b {
		return false
	}
	return (2*b-a)%3 == 0
}

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

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 ifElseI(cond bool, a, b int) int {
	if cond {
		return a
	}
	return b
}
func ifElseS(cond bool, a, b string) string {
	if cond {
		return a
	}
	return b
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		x, y := 0, 0
		fmt.Fscanf(in, "%d %d\n", &x, &y)
		x, y = maxmin(x, y)
		if y > 2*x {
			fmt.Fprintf(out, "NO\n")
		} else {
			if (2*x-y)%3 == 0 {
				fmt.Fprintf(out, "YES\n")
			} else {
				fmt.Fprintf(out, "NO\n")
			}
		}
	}
	return
}

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

package main

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

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

func main(){
	defer writer.Flush()
	var T int
	scanf("%d\n",&T)
	for cas:=1;cas<=T;cas++{
		var a,b int
		scanf("%d %d\n",&a,&b)
		if a>b{
			t:=a
			a=b
			b=t
		}
		tmp:=2*a-b;
		if tmp<0||tmp%3!=0{
			printf("NO\n")
		}else {
			printf("YES\n")
		}
	}
}

package main

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

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

	var n, a, b int
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &b)
		if a > b {
			a, b = b, a
		}
		if ((a+b)%3 == 0) && ((2 * a) >= b) {
			fmt.Fprintf(writer, "YES\n")
		} else {
			fmt.Fprintf(writer, "NO\n")
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for ; n > 0; n-- {
		var a, b int
		fmt.Scan(&a, &b)
		if (a+b)%3 == 0 && a*2 >= b {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func check(a int, b int) bool {
	return (a*2-b)%3 == 0 && (a*2-b)/3 >= 0
}

func main() {
	var t int
	var a, b int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		fmt.Scanln(&a, &b)
		can := check(a, b)
		if can {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var t int
	Fscan(in, &t)
	for case_ := 0; case_ < t; case_++ {
		if solveCase(in, out) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func solveCase(in io.Reader, out io.Writer) bool {
	var a, b int
	Fscan(in, &a, &b)
	if a > b {
		a, b = b, a
	}
	if a > 2*b {
		return false
	}
	return (2*b-a)%3 == 0
}

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

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 ifElseI(cond bool, a, b int) int {
	if cond {
		return a
	}
	return b
}
func ifElseS(cond bool, a, b string) string {
	if cond {
		return a
	}
	return b
}


Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.

The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106).  The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.

Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).

NoteThe optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.In the second sample it is impossible to send jury member from city 2 back home from Metropolis.

package main

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

const INF = int64(1 << 50)

var sc *bufio.Scanner

func I() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

type Flight struct {
	day, city int
	cost      int64
}
type FlightArr []Flight

func (a FlightArr) Len() int           { return len(a) }
func (a FlightArr) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a FlightArr) Less(i, j int) bool { return a[i].day > a[j].day }

func main() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Buffer(make([]byte, 0, 1<<20), 1<<20)
	sc.Split(bufio.ScanWords)
	n, m, k := I(), I(), I()
	flights := make(FlightArr, m)
	var ft [2]FlightArr
	for i := 0; i < m; {
		d, f, t, c := I(), I(), I(), int64(I())
		if t == 0 {
			flights[i] = Flight{-d, f - 1, c}
			i++
		} else {
			m--
			flights[m] = Flight{d, t - 1, c}
		}
	}
	ft[0], ft[1] = flights[:m], flights[m:]
	costs := make([]int64, n)
	for dir, src := range ft {
		sort.Sort(src)
		for i := range costs {
			costs[i] = INF
		}
		cnt := n
		var rest FlightArr
		for i, f := range src {
			if costs[f.city] > f.cost {
				if costs[f.city] == INF {
					costs[f.city] = f.cost
					cnt--
					if cnt == 0 {
						var total int64
						for _, c := range costs {
							total += c
						}
						src[0] = Flight{f.day, 0, total}
						rest = src[i+1:]
						break
					}
				} else {
					costs[f.city] = f.cost
				}
			}
		}
		if cnt != 0 {
			fmt.Println(-1)
			return
		}
		i := 0
		for _, f := range rest {
			if costs[f.city] > f.cost {
				if src[i].day != f.day {
					i++
					src[i].day = f.day
					src[i].cost = src[i-1].cost - costs[f.city] + f.cost
				} else {
					src[i].cost -= costs[f.city] - f.cost
				}
				costs[f.city] = f.cost
			}
		}
		ft[dir] = src[:i+1]
	}
	flights = ft[1]
	i, res := len(flights)-1, INF
fin:
	for _, f := range ft[0] {
		for k >= f.day+flights[i].day {
			if i == 0 {
				break fin
			}
			i--
		}
		if k < f.day+flights[i].day && res > f.cost+flights[i].cost {
			res = f.cost + flights[i].cost
		}
	}
	if res == INF {
		res = -1
	}
	fmt.Println(res)
}


You are given a tree, consisting of $$$n$$$ vertices. Each edge has an integer value written on it.Let $$$f(v, u)$$$ be the number of values that appear exactly once on the edges of a simple path between vertices $$$v$$$ and $$$u$$$.Calculate the sum of $$$f(v, u)$$$ over all pairs of vertices $$$v$$$ and $$$u$$$ such that $$$1 \le v < u \le n$$$.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) — the number of vertices in the tree. Each of the next $$$n-1$$$ lines contains three integers $$$v, u$$$ and $$$x$$$ ($$$1 \le v, u, x \le n$$$) — the description of an edge: the vertices it connects and the value written on it. The given edges form a tree.

Print a single integer — the sum of $$$f(v, u)$$$ over all pairs of vertices $$$v$$$ and $$$u$$$ such that $$$v < u$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

/* End of MyIO */

type Edge struct {
	x, w int
}

func unique(intSlice []int) []int {
	list := []int{}
	for i, entry := range intSlice {
		if i == 0 || intSlice[i] != intSlice[i-1] {
			list = append(list, entry)
		}
	}
	return list
}

func sol() {
	n := ri()
	G := make([][]Edge, n+1)
	ordi := make([]int, n+1)
	ordo := make([]int, n+1)
	tsiz := make([]int, n+1)
	idx := make([]int, n+1)
	cup := make([]int, n+1)
	ord := 0

	sv := make([][]int, n+1)

	var ng = make([][]int, n+1)
	var dp = make([][2]int, n+1)

	for i := 1; i < n; i++ {
		u, v, w := ri(), ri(), ri()
		G[u] = append(G[u], Edge{v, w})
		G[v] = append(G[v], Edge{u, w})
	}
	var dfs1 func(u int, p int)

	dfs1 = func(u, p int) {
		ordi[u] = ord
		idx[ord] = u
		ord += 1
		tsiz[u] = 1
		for _, e := range G[u] {
			if e.x == p {
				continue
			}
			dfs1(e.x, u)
			cup[e.x] = e.w
			tsiz[u] += tsiz[e.x]
		}
		ordo[u] = ord
	}
	dfs1(1, 0)
	for i := 1; i <= n; i++ {
		sv[i] = append(sv[i], 1)
	}
	for i := 0; i < n; i++ {
		u := idx[i]
		for _, e := range G[u] {
			sv[e.w] = append(sv[e.w], u)
		}
	}

	isp := func(i, j int) bool {
		return ordi[i] <= ordi[j] && ordo[i] >= ordo[j]
	}

	var ans int64 = 0

	var dfs2 func(u, c int) int64

	dfs2 = func(u, c int) (ans int64) {

		siz := tsiz[u]

		for _, v := range ng[u] {
			ans += dfs2(v, c)
			siz -= tsiz[v]
		}

		dp[u] = [2]int{siz, 0}

		for _, v := range ng[u] {
			ans += int64(dp[v][0]) * int64(dp[u][1])
			ans += int64(dp[v][1]) * int64(dp[u][0])
			dp[u][0] += dp[v][0]
			dp[u][1] += dp[v][1]
		}
		if u != 1 {
			if cup[u] == c {
				dp[u][1] = dp[u][0]
				dp[u][0] = 0
			}
		}
		return ans
	}

	for i := 1; i <= n; i++ {
		sv[i] = unique(sv[i])
		st := make([]int, 0)
		top := 0
		for _, v := range sv[i] {
			for top > 0 && !isp(st[top-1], v) {
				st = st[:top-1]
				top -= 1
			}
			if top > 0 {
				ng[st[top-1]] = append(ng[st[top-1]], v)
			}
			st = append(st, v)
			ng[v] = ng[v][:0]
			top += 1
		}
		ans += dfs2(1, i)
	}
	myio.Println(ans)

}

func main() {
	debug.SetGCPercent(-1)
	/*
		f, err := os.Create("./cpuprofile")
		if err != nil {
			fmt.Println(err)
			return
		}
		defer f.Close()
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	*/
	in, out := os.Stdin, os.Stdout
	// in, _ = os.Open("f6.in")
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	for ; t > 0; t-- {
		sol()
	}
}

func init() {
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

/* End of MyIO */

type Edge struct {
	x, w int
}

func unique(intSlice []int) []int {
	list := []int{}
	for i, entry := range intSlice {
		if i == 0 || intSlice[i] != intSlice[i-1] {
			list = append(list, entry)
		}
	}
	return list
}

func sol() {
	n := ri()
	G := make([][]Edge, n+1)
	ordi := make([]int, n+1)
	ordo := make([]int, n+1)
	tsiz := make([]int, n+1)
	idx := make([]int, n+1)
	cup := make([]int, n+1)
	ord := 0

	sv := make([][]int, n+1)

	var ng = make([][]int, n+1)
	var dp = make([][2]int, n+1)

	for i := 1; i < n; i++ {
		u, v, w := ri(), ri(), ri()
		G[u] = append(G[u], Edge{v, w})
		G[v] = append(G[v], Edge{u, w})
	}
	var dfs1 func(u int, p int)

	dfs1 = func(u, p int) {
		ordi[u] = ord
		idx[ord] = u
		ord += 1
		tsiz[u] = 1
		for _, e := range G[u] {
			if e.x == p {
				continue
			}
			dfs1(e.x, u)
			cup[e.x] = e.w
			tsiz[u] += tsiz[e.x]
		}
		ordo[u] = ord
	}
	dfs1(1, 0)
	for i := 1; i <= n; i++ {
		sv[i] = append(sv[i], 1)
	}
	for i := 0; i < n; i++ {
		u := idx[i]
		for _, e := range G[u] {
			sv[e.w] = append(sv[e.w], u)
		}
	}

	isp := func(i, j int) bool {
		return ordi[i] <= ordi[j] && ordo[i] >= ordo[j]
	}

	var ans int64 = 0

	var dfs2 func(u, c int) int64

	dfs2 = func(u, c int) (ans int64) {

		siz := tsiz[u]

		for _, v := range ng[u] {
			ans += dfs2(v, c)
			siz -= tsiz[v]
		}

		dp[u] = [2]int{siz, 0}

		for _, v := range ng[u] {
			ans += int64(dp[v][0]) * int64(dp[u][1])
			ans += int64(dp[v][1]) * int64(dp[u][0])
			dp[u][0] += dp[v][0]
			dp[u][1] += dp[v][1]
		}
		if u != 1 {
			if cup[u] == c {
				dp[u][1] = dp[u][0]
				dp[u][0] = 0
			}
		}
		return ans
	}

	for i := 1; i <= n; i++ {
		sv[i] = unique(sv[i])
		st := make([]int, 0)
		top := 0
		for _, v := range sv[i] {
			for top > 0 && !isp(st[top-1], v) {
				st = st[:top-1]
				top -= 1
			}
			if top > 0 {
				ng[st[top-1]] = append(ng[st[top-1]], v)
			}
			st = append(st, v)
			ng[v] = ng[v][:0]
			top += 1
		}
		ans += dfs2(1, i)
	}
	myio.Println(ans)

}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

/* End of MyIO */

type Edge struct {
	x, w int
}

func unique(intSlice []int) []int {
	list := []int{}
	for i, entry := range intSlice {
		if i == 0 || intSlice[i] != intSlice[i-1] {
			list = append(list, entry)
		}
	}
	return list
}

func sol() {
	n := ri()
	G := make([][]Edge, n+1)
	ordi := make([]int, n+1)
	ordo := make([]int, n+1)
	tsiz := make([]int, n+1)
	idx := make([]int, n+1)
	cup := make([]int, n+1)
	ord := 0

	sv := make([][]int, n+1)

	// ng := make(map[int]([]int), 0)
	var ng = make([][]int, n+1)
	var dp = make([][2]int, n+1)

	// for i := 1; i <= n; i++ {
	// 	G[i] = make([]Edge, 0)
	// }
	for i := 1; i < n; i++ {
		u, v, w := ri(), ri(), ri()
		G[u] = append(G[u], Edge{v, w})
		G[v] = append(G[v], Edge{u, w})
	}
	var dfs1 func(u int, p int)

	dfs1 = func(u, p int) {
		ordi[u] = ord
		idx[ord] = u
		ord += 1
		tsiz[u] = 1
		for _, e := range G[u] {
			if e.x == p {
				continue
			}
			dfs1(e.x, u)
			cup[e.x] = e.w
			tsiz[u] += tsiz[e.x]
		}
		ordo[u] = ord
	}
	dfs1(1, 0)
	for i := 1; i <= n; i++ {
		sv[i] = append(sv[i], 1)
	}
	for i := 0; i < n; i++ {
		u := idx[i]
		for _, e := range G[u] {
			sv[e.w] = append(sv[e.w], u)
		}
	}

	isp := func(i, j int) bool {
		return ordi[i] <= ordi[j] && ordo[i] >= ordo[j]
	}

	var ans int64 = 0

	var dfs2 func(u, c int) int64

	dfs2 = func(u, c int) (ans int64) {

		siz := tsiz[u]

		for _, v := range ng[u] {
			ans += dfs2(v, c)
			siz -= tsiz[v]
		}

		dp[u] = [2]int{siz, 0}

		for _, v := range ng[u] {
			ans += int64(dp[v][0]) * int64(dp[u][1])
			ans += int64(dp[v][1]) * int64(dp[u][0])
			dp[u][0] += dp[v][0]
			dp[u][1] += dp[v][1]
		}
		if u != 1 {
			if cup[u] == c {
				dp[u][1] = dp[u][0]
				dp[u][0] = 0
			}
		} else {
			// fmt.Println("u=", u, " c= ", c, " ans= ", ans)
		}
		return ans
	}

	for i := 1; i <= n; i++ {
		// fmt.Printf("%d > ", i)
		sv[i] = unique(sv[i])
		st := make([]int, 0)
		top := 0
		// ng = make(map[int]([]int))
		// dp = make(map[int]([]int))
		for _, v := range sv[i] {
			// pop all none ancestors
			for top > 0 && !isp(st[top-1], v) {
				st = st[:top-1]
				top -= 1
			}
			if top > 0 {
				// fmt.Println("build edge: ", st[top-1], v)
				ng[st[top-1]] = append(ng[st[top-1]], v)
			}
			st = append(st, v)
			ng[v] = ng[v][:0]
			top += 1
			// fmt.Printf("%d ", v)
		}
		ans += dfs2(1, i)
		// fmt.Println()
	}
	myio.Println(ans)

}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	// t = myio.ReadInt()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

/* End of MyIO */

type Edge struct {
	x, w int
}

func unique(intSlice []int) []int {
	list := []int{}
	for i, entry := range intSlice {
		if i == 0 || intSlice[i] != intSlice[i-1] {
			list = append(list, entry)
		}
	}
	return list
}

func sol() {
	n := ri()
	G := make([][]Edge, n+1)
	ordi := make([]int, n+1)
	ordo := make([]int, n+1)
	tsiz := make([]int, n+1)
	idx := make([]int, n+1)
	cup := make([]int, n+1)
	ord := 0

	sv := make([][]int, n+1)

	// ng := make(map[int]([]int), 0)
	var ng = make([][]int, n+1)
	var dp = make([][]int, n+1)

	// for i := 1; i <= n; i++ {
	// 	G[i] = make([]Edge, 0)
	// }
	for i := 1; i < n; i++ {
		u, v, w := ri(), ri(), ri()
		G[u] = append(G[u], Edge{v, w})
		G[v] = append(G[v], Edge{u, w})
	}
	var dfs1 func(u int, p int)

	dfs1 = func(u, p int) {
		ordi[u] = ord
		idx[ord] = u
		ord += 1
		tsiz[u] = 1
		for _, e := range G[u] {
			if e.x == p {
				continue
			}
			dfs1(e.x, u)
			cup[e.x] = e.w
			tsiz[u] += tsiz[e.x]
		}
		ordo[u] = ord
	}
	dfs1(1, 0)
	for i := 1; i <= n; i++ {
		sv[i] = append(sv[i], 1)
	}
	for i := 0; i < n; i++ {
		u := idx[i]
		for _, e := range G[u] {
			sv[e.w] = append(sv[e.w], u)
		}
	}

	isp := func(i, j int) bool {
		return ordi[i] <= ordi[j] && ordo[i] >= ordo[j]
	}

	var ans int64 = 0

	var dfs2 func(u, c int) int64

	dfs2 = func(u, c int) (ans int64) {
		dp[u] = make([]int, 2)

		siz := tsiz[u]

		for _, v := range ng[u] {
			ans += dfs2(v, c)
			siz -= tsiz[v]
		}

		dp[u][0] = siz
		dp[u][1] = 0

		for _, v := range ng[u] {
			ans += int64(dp[v][0]) * int64(dp[u][1])
			ans += int64(dp[v][1]) * int64(dp[u][0])
			dp[u][0] += dp[v][0]
			dp[u][1] += dp[v][1]
		}
		if u != 1 {
			if cup[u] == c {
				dp[u][1] = dp[u][0]
				dp[u][0] = 0
			}
		} else {
			// fmt.Println("u=", u, " c= ", c, " ans= ", ans)
		}
		return ans
	}

	for i := 1; i <= n; i++ {
		// fmt.Printf("%d > ", i)
		sv[i] = unique(sv[i])
		st := make([]int, 0)
		top := 0
		// ng = make(map[int]([]int))
		// dp = make(map[int]([]int))
		for _, v := range sv[i] {
			// pop all none ancestors
			for top > 0 && !isp(st[top-1], v) {
				st = st[:top-1]
				top -= 1
			}
			if top > 0 {
				// fmt.Println("build edge: ", st[top-1], v)
				ng[st[top-1]] = append(ng[st[top-1]], v)
			}
			st = append(st, v)
			ng[v] = ng[v][:0]
			top += 1
			// fmt.Printf("%d ", v)
		}
		ans += dfs2(1, i)
		// fmt.Println()
	}
	myio.Println(ans)

}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	// t = myio.ReadInt()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

/* End of MyIO */

type Edge struct {
	x, w int
}

func unique(intSlice []int) []int {
	keys := make(map[int]bool)
	list := []int{}
	for _, entry := range intSlice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}

func sol() {
	n := ri()
	G := make([][]Edge, n+1)
	ordi := make([]int, n+1)
	ordo := make([]int, n+1)
	tsiz := make([]int, n+1)
	idx := make([]int, n+1)
	cup := make([]int, n+1)
	ord := 0

	sv := make([][]int, n+1)

	// ng := make(map[int]([]int), 0)
	var ng = make([][]int, n+1)
	var dp = make([][]int, n+1)

	// for i := 1; i <= n; i++ {
	// 	G[i] = make([]Edge, 0)
	// }
	for i := 1; i < n; i++ {
		u, v, w := ri(), ri(), ri()
		G[u] = append(G[u], Edge{v, w})
		G[v] = append(G[v], Edge{u, w})
	}
	var dfs1 func(u int, p int)

	dfs1 = func(u, p int) {
		ordi[u] = ord
		idx[ord] = u
		ord += 1
		tsiz[u] = 1
		for _, e := range G[u] {
			if e.x == p {
				continue
			}
			dfs1(e.x, u)
			cup[e.x] = e.w
			tsiz[u] += tsiz[e.x]
		}
		ordo[u] = ord
	}
	dfs1(1, 0)
	for i := 1; i <= n; i++ {
		sv[i] = append(sv[i], 1)
	}
	for i := 0; i < n; i++ {
		u := idx[i]
		for _, e := range G[u] {
			sv[e.w] = append(sv[e.w], u)
		}
	}

	isp := func(i, j int) bool {
		return ordi[i] <= ordi[j] && ordo[i] >= ordo[j]
	}

	var ans int64 = 0

	var dfs2 func(u, c int) int64

	dfs2 = func(u, c int) (ans int64) {
		dp[u] = make([]int, 2)

		siz := tsiz[u]

		for _, v := range ng[u] {
			ans += dfs2(v, c)
			siz -= tsiz[v]
		}

		dp[u][0] = siz
		dp[u][1] = 0

		for _, v := range ng[u] {
			ans += int64(dp[v][0]) * int64(dp[u][1])
			ans += int64(dp[v][1]) * int64(dp[u][0])
			dp[u][0] += dp[v][0]
			dp[u][1] += dp[v][1]
		}
		if u != 1 {
			if cup[u] == c {
				dp[u][1] = dp[u][0]
				dp[u][0] = 0
			}
		} else {
			// fmt.Println("u=", u, " c= ", c, " ans= ", ans)
		}
		return ans
	}

	for i := 1; i <= n; i++ {
		// fmt.Printf("%d > ", i)
		sv[i] = unique(sv[i])
		st := make([]int, 0)
		top := 0
		// ng = make(map[int]([]int))
		// dp = make(map[int]([]int))
		for _, v := range sv[i] {
			// pop all none ancestors
			for top > 0 && !isp(st[top-1], v) {
				st = st[:top-1]
				top -= 1
			}
			if top > 0 {
				// fmt.Println("build edge: ", st[top-1], v)
				ng[st[top-1]] = append(ng[st[top-1]], v)
			}
			st = append(st, v)
			ng[v] = ng[v][:0]
			top += 1
			// fmt.Printf("%d ", v)
		}
		ans += dfs2(1, i)
		// fmt.Println()
	}
	myio.Println(ans)

}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	// t = myio.ReadInt()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

/* End of MyIO */

type Edge struct {
	x, w int
}

func unique(intSlice []int) []int {
	keys := make(map[int]bool)
	list := []int{}
	for _, entry := range intSlice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}

func sol() {
	n := ri()
	G := make([][]Edge, n+1)
	ordi := make([]int, n+1)
	ordo := make([]int, n+1)
	tsiz := make([]int, n+1)
	idx := make([]int, n+1)
	cup := make([]int, n+1)
	ord := 0

	sv := make([][]int, n+1)

	// ng := make(map[int]([]int), 0)
	var ng map[int]([]int)
	var dp map[int]([]int)

	// for i := 1; i <= n; i++ {
	// 	G[i] = make([]Edge, 0)
	// }
	for i := 1; i < n; i++ {
		u, v, w := ri(), ri(), ri()
		G[u] = append(G[u], Edge{v, w})
		G[v] = append(G[v], Edge{u, w})
	}
	var dfs1 func(u int, p int)

	dfs1 = func(u, p int) {
		ordi[u] = ord
		idx[ord] = u
		ord += 1
		tsiz[u] = 1
		for _, e := range G[u] {
			if e.x == p {
				continue
			}
			dfs1(e.x, u)
			cup[e.x] = e.w
			tsiz[u] += tsiz[e.x]
		}
		ordo[u] = ord
	}
	dfs1(1, 0)
	for i := 1; i <= n; i++ {
		sv[i] = append(sv[i], 1)
	}
	for i := 0; i < n; i++ {
		u := idx[i]
		for _, e := range G[u] {
			sv[e.w] = append(sv[e.w], u)
		}
	}

	isp := func(i, j int) bool {
		return ordi[i] <= ordi[j] && ordo[i] >= ordo[j]
	}

	var ans int64 = 0

	var dfs2 func(u, c int) int64

	dfs2 = func(u, c int) (ans int64) {
		dp[u] = make([]int, 2)

		siz := tsiz[u]

		for _, v := range ng[u] {
			ans += dfs2(v, c)
			siz -= tsiz[v]
		}

		dp[u][0] = siz
		dp[u][1] = 0

		for _, v := range ng[u] {
			ans += int64(dp[v][0]) * int64(dp[u][1])
			ans += int64(dp[v][1]) * int64(dp[u][0])
			dp[u][0] += dp[v][0]
			dp[u][1] += dp[v][1]
		}
		if u != 1 {
			if cup[u] == c {
				dp[u][1] = dp[u][0]
				dp[u][0] = 0
			}
		} else {
			// fmt.Println("u=", u, " c= ", c, " ans= ", ans)
		}
		return ans
	}

	for i := 1; i <= n; i++ {
		// fmt.Printf("%d > ", i)
		sv[i] = unique(sv[i])
		st := make([]int, 0)
		top := 0
		ng = make(map[int]([]int))
		dp = make(map[int]([]int))
		for _, v := range sv[i] {
			// pop all none ancestors
			for top > 0 && !isp(st[top-1], v) {
				st = st[:top-1]
				top -= 1
			}
			if top > 0 {
				// fmt.Println("build edge: ", st[top-1], v)
				ng[st[top-1]] = append(ng[st[top-1]], v)
			}
			st = append(st, v)
			top += 1
			// fmt.Printf("%d ", v)
		}
		ans += dfs2(1, i)
		// fmt.Println()
	}
	myio.Println(ans)

}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	// t = myio.ReadInt()
	for ; t > 0; t-- {
		sol()
	}
}


You have a sequence of $$$n$$$ colored blocks. The color of the $$$i$$$-th block is $$$c_i$$$, an integer between $$$1$$$ and $$$n$$$.You will place the blocks down in sequence on an infinite coordinate grid in the following way.   Initially, you place block $$$1$$$ at $$$(0, 0)$$$.  For $$$2 \le i \le n$$$, if the $$$(i - 1)$$$-th block is placed at position $$$(x, y)$$$, then the $$$i$$$-th block can be placed at one of positions $$$(x + 1, y)$$$, $$$(x - 1, y)$$$, $$$(x, y + 1)$$$ (but not at position $$$(x, y - 1)$$$), as long no previous block was placed at that position. A tower is formed by $$$s$$$ blocks such that they are placed at positions $$$(x, y), (x, y + 1), \ldots, (x, y + s - 1)$$$ for some position $$$(x, y)$$$ and integer $$$s$$$. The size of the tower is $$$s$$$, the number of blocks in it. A tower of color $$$r$$$ is a tower such that all blocks in it have the color $$$r$$$.For each color $$$r$$$ from $$$1$$$ to $$$n$$$, solve the following problem independently:   Find the maximum size of a tower of color $$$r$$$ that you can form by placing down the blocks according to the rules.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.  The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output $$$n$$$ integers. The $$$r$$$-th of them should be the maximum size of an tower of color $$$r$$$ you can form by following the given rules. If you cannot form any tower of color $$$r$$$, the $$$r$$$-th integer should be $$$0$$$.

NoteIn the first test case, one of the possible ways to form a tower of color $$$1$$$ and size $$$3$$$ is:   place block $$$1$$$ at position $$$(0, 0)$$$;  place block $$$2$$$ to the right of block $$$1$$$, at position $$$(1, 0)$$$;  place block $$$3$$$ above block $$$2$$$, at position $$$(1, 1)$$$;  place block $$$4$$$ to the left of block $$$3$$$, at position $$$(0, 1)$$$;  place block $$$5$$$ to the left of block $$$4$$$, at position $$$(-1, 1)$$$;  place block $$$6$$$ above block $$$5$$$, at position $$$(-1, 2)$$$;  place block $$$7$$$ to the right of block $$$6$$$, at position $$$(0, 2)$$$.   The blocks at positions $$$(0, 0)$$$, $$$(0, 1)$$$, and $$$(0, 2)$$$ all have color $$$1$$$, forming an tower of size $$$3$$$.In the second test case, note that the following placement is not valid, since you are not allowed to place block $$$6$$$ under block $$$5$$$:  It can be shown that it is impossible to form a tower of color $$$4$$$ and size $$$3$$$.

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for itests := 0; itests < ntests; itests++ {
		var n int
		fmt.Fscan(scanner, &n)
		tower := make([]int, n)
		ltowers := make([]int, n)
		ptowers := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &tower[i])
			tower[i]--
		}
		for i := 0; i < n; i++ {
			color := tower[i]
			if (i-ptowers[color])%2 == 1 {
				ltowers[color]++
				ptowers[color] = i
			} else {
				if ltowers[color] <= 1 {
					ltowers[color] = 1
					ptowers[color] = i
				}
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fprint(printer, ltowers[i])
			fmt.Fprint(printer, " ")
		}
		fmt.Fprintln(printer)
	}
	printer.Flush()
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		c := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &c[i])
		}
		even := make(map[int]int)
		odd := make(map[int]int)
		for i := 0; i < n; i++ {
			if i%2 == 0 {
				even[c[i]] = odd[c[i]] + 1
			} else {
				odd[c[i]] = even[c[i]] + 1
			}
		}

		for i := 1; i <= n; i++ {
			fmt.Print(max(even[i], odd[i]), " ")
		}
		fmt.Println()
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		blocks := readNNums(reader, n)
		res := solve(blocks)

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

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(blocks []int) []int {
	n := len(blocks)
	// 对于有同一color的两个block, 能够叠在同一个x的必要条件是
	// (i - j) % 2 = 1
	// 这个是不是充分条件呢？
	// yes, 如果它们距离 % 2 = 1, 他们就可以叠在一起
	dp := make([]int, n)
	pos := make([][]int, n)
	for i := 0; i < n; i++ {
		pos[i] = make([]int, 2)
		for j := 0; j < 2; j++ {
			pos[i][j] = -1
		}
	}

	for i := 0; i < n; i++ {
		dp[i] = 1
		color := blocks[i] - 1
		if pos[color][1-(i%2)] >= 0 {
			dp[i] = dp[pos[color][1-(i%2)]] + 1
		}
		pos[color][i%2] = i
	}

	res := make([]int, n)

	for i := 0; i < n; i++ {
		color := blocks[i] - 1
		res[color] = max(res[color], dp[i])
	}

	return res
}

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

package main

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

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

func main() {
	defer out.Flush()

	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}
}

func solve() {
	var n, x int
	fmt.Fscan(in, &n)
	last := make([]int, n)
	res := make([]int, n)
	for i := 0; i < n; i++ {
		last[i] = -1
	}

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		x--
		// fmt.Println(x, last[x], mark[x], res[x])
		if last[x] == -1 || (i-last[x])%2 != 0 {
			res[x]++
			last[x] = i
		}
		// fmt.Println(mark[x], res[x])
	}

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

package main

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

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

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

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}
		h := make(map[int]int)
		p := make(map[int]int)
		for i, v := range arr {
			pv, ok := p[v]
			if ok {
				if (pv%2 == 1 && i%2 == 0) || (pv%2 == 0 && i%2 == 1) {
					h[v]++
				}
				p[v] = i
				continue
			}
			p[v] = i
			h[v]++
		}
		for i := 1; i <= n; i++ {
			fmt.Fprint(w, h[i], " ")
		}
		fmt.Fprintln(w)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	c      []int
)

func solve(n int, c []int) string {
	height := make([]int, n)
	last := make([]int, n)
	for i, x := range c {
		if height[x-1] == 0 {
			height[x-1]++
			last[x-1] = i
		} else {
			if (i-last[x-1])%2 == 1 {
				height[x-1]++
				last[x-1] = i
			}
		}
	}
	return strings.Trim(fmt.Sprint(height), "[]")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		c = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &c[i])
		}
		fmt.Println(solve(n, c))
	}
}

package main

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

func Min[T int | int64 | float64](x, y T) T {
	if x < y {
		return x

	}
	return y

}

func Max[T int | int64 | float64](x, y T) T {
	if x > y {
		return x

	}
	return y

}

func Less[T int | int64 | float64](x, y T) bool {
	return x < y
}

func main() {
	var t int
	read := bufio.NewReader(os.Stdin)
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(read, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &a[i])
		}

		b := make([]struct {
			an, ai int
			bn, bi int
		}, n)

		for index, val := range a {
			i := val - 1
			if b[i].an == 0 || (index-b[i].ai-1)%2 == 0 || index-b[i].ai == 1 {
				b[i].an++
				b[i].ai = index
			} else if b[i].bn == 0 || (index-b[i].bi-1)%2 == 0 || index-b[i].bi == 1 {
				b[i].bi = index
				b[i].bn++
			}
			if Less(b[i].an, b[i].bn) {
				b[i].an, b[i].ai, b[i].bn, b[i].bi = b[i].bn, b[i].bi, b[i].an, b[i].ai
			}
		}

		s := make([]string, n)
		for index, val := range b {
			s[index] = strconv.Itoa(Max(val.bn, val.an))
		}
		fmt.Println(strings.Join(s, " "))
	}
}

package main

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

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

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

func rw(in io.Reader, out io.Writer) {
	var t int
	fmt.Fscan(in, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(in, &n)
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(in, &b[i])
		}
		w1 := do(n, b)
		for _, v := range w1 {
			fmt.Fprint(out, v)
			fmt.Fprint(out, " ")
		}
		fmt.Fprintln(out)
	}
}

func do(n int, b []int) []int {
	a := make([]int, n)
	c := make([]int, n)
	for i, v := range b {
		v--
		if c[v] == 0 {
			a[v] = i
			c[v]++
		} else {
			if (i-a[v])%2 == 1 {
				a[v] = i
				c[v]++
			}
		}
	}
	return c
}

package main

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

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

func ans() {
	var n int
	Fscan(in, &n)
	var c []int = make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &c[i])
	}
	var pos map[int][]int = map[int][]int{}
	for i := 0; i < n; i++ {
		pos[c[i]] = append(pos[c[i]], i)
	}
	var results []int = make([]int, n + 1)
	for k, v := range pos {
		var cnt int = 1
		for cur := 0; cur < len(v) - 1; {
			var next int
			for next = cur + 1; next < len(v); next++ {
				if (v[next] - v[cur]) % 2 == 1 {
					cnt++
					break
				}
			}
			cur = next
		}
		results[k] = cnt
	}
	for i := 1; i <= n; i++ {
		Fprint(out, results[i], " ")
	}
	Fprintln(out)
}

func main() {
	defer out.Flush()
	var t int
	Fscan(in, &t)
	for i := 0; i < t; i++ {
		ans()
	}
}

package main

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

const MOD int64 = 1000000007

var n int

var c[]int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

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

		fmt.Fscan(read, &n)

		c = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &c[j])
		}

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

func solve() {

	pos := make(map[int][]int)

	for i := 0; i < n; i++ {
		pos[c[i]] = append(pos[c[i]], i)
	}

	ans := make(map[int]int)

	for k, v := range pos {
		prev := v[0]
		var cnt int = 1

		for i := 1; i < len(v); i++ {
			cur := v[i]

			if prev % 2 == 0 {
				if cur % 2 == 1 {
					cnt++
					prev = cur
				}

			} else {
				if cur % 2 == 0 {
					cnt++
					prev = cur
				}

			}
		}
		ans[k] = cnt
	}

	for i := 1; i <= n; i++ {
		fmt.Print(ans[i], " ")
	}

	fmt.Println()
}

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

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

package main

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

var cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	evenLast := make([]int, n+1)
	oddLast := make([]int, n+1)
	oddLast[arr[0]] = 1
	for i := 1; i < n; i++ {
		arr1 := &oddLast
		arr2 := &evenLast
		if i%2 == 1 {
			arr1, arr2 = arr2, arr1
		}

		(*arr1)[arr[i]] = max((*arr2)[arr[i]]+1, (*arr1)[arr[i]])
	}
	for i := 1; i <= n; i++ {
		fmt.Fprintf(out, "%d ", max(evenLast[i], oddLast[i]))
	}
	fmt.Fprint(out, "\n")
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

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

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

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

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

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

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

package main

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

func solve() {
	n := get()
	a := make([]struct{ l, r int }, n)
	for i := 0; i < n; i += 2 {
		j := get() - 1
		if a[j].l+1 > a[j].r {
			a[j].r = a[j].l + 1
		}
		if i+1 == n {
			break
		}
		j = get() - 1
		if a[j].r+1 > a[j].l {
			a[j].l = a[j].r + 1
		}
	}
	for _, v := range a {
		if v.l > v.r {
			put(v.l)
		} else {
			put(v.r)
		}
		space()
	}
	line()
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

func line() {
	out.WriteRune('\n')
}

func space() {
	out.WriteRune(' ')
}

package main

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

func solve(n int, a []int) string {
	lastOddIndex := make([]int, n)
	lastEvenIndex := make([]int, n)
	for i := 0; i < n; i++ {
		lastOddIndex[i] = -1
		lastEvenIndex[i] = -1
	}

	currentOddLength := make([]int, n)
	currentEvenLength := make([]int, n)
	for i := 0; i < n; i++ {
		currentOddLength[i] = -1
		currentEvenLength[i] = -1
	}
	maxLength := make([]int, n)

	for i := 0; i < n; i++ {
		u := a[i]
		if i%2 == 0 {
			if lastEvenIndex[u] == -1 {
				currentEvenLength[u] = 1
				lastEvenIndex[u] = i
			}
			if currentOddLength[u]+1 > currentEvenLength[u] {
				currentEvenLength[u] = currentOddLength[u] + 1
				lastEvenIndex[u] = i
			}
		} else {
			if lastOddIndex[u] == -1 {
				currentOddLength[u] = 1
				lastOddIndex[u] = i
			}
			if currentEvenLength[u]+1 > currentOddLength[u] {
				currentOddLength[u] = currentEvenLength[u] + 1
				lastOddIndex[u] = i
			}
		}
	}

	for i := 0; i < n; i++ {
		if currentOddLength[i] > maxLength[i] {
			maxLength[i] = currentOddLength[i]
		}
		if currentEvenLength[i] > maxLength[i] {
			maxLength[i] = currentEvenLength[i]
		}
	}

	res := make([]string, n)
	for i := 0; i < n; i++ {
		res[i] = fmt.Sprint(maxLength[i])
	}

	return strings.Join(res, " ")
}

func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	var test int
	fmt.Fscanln(inp, &test)

	for test > 0 {
		test--

		var n int
		fmt.Fscanln(inp, &n)

		a := make([]int, n)
		for i := 0; i < n; i++ {
			if i == n-1 {
				fmt.Fscanln(inp, &a[i])
			} else {
				fmt.Fscan(inp, &a[i])
			}
			a[i] -= 1
		}

		fmt.Fprintln(out, solve(n, a))
	}

	out.Flush()
}

package main

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

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

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

		aa := make([]int, n+1) // count
		ll := make([]int, n+1)
		for i := range ll {
			ll[i] = -1
		}
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if ll[v] >= 0 && (i-ll[v]-1)%2 == 0 {
				aa[v]++
			}
			ll[v] = i
		}

		for i := 1; i <= n; i++ {
			if ll[i] >= 0 {
				fmt.Fprintf(out, "%d ", aa[i]+1)
			} else {
				fmt.Fprintf(out, "%d ", 0)
			}
		}
		fmt.Fprintln(out)
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

type entry struct {
	last int
	cnt int
}

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	nums := make([]int, n)
	es := make([]entry, n)
	for i := range es {
		es[i].last = -1
	}
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
		nums[i]--
	}
	for i:=n-1;i>=0;i-- {
		if es[nums[i]].last == -1 {
			es[nums[i]].cnt++
			es[nums[i]].last = i
		} else if (es[nums[i]].last - i) % 2 == 1 {
			es[nums[i]].cnt++
			es[nums[i]].last = i
		}
	}
	for i:=0;i<len(es);i++ {
		fmt.Fprintf(ww, "%d ", es[i].cnt)
	}
	fmt.Fprintln(ww)
}

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

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for itests := 0; itests < ntests; itests++ {
		var n int
		fmt.Fscan(scanner, &n)
		tower := make([]int, n)
		ltowers := make([]int, n)
		ptowers := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &tower[i])
			tower[i]--
		}
		for i := 0; i < n; i++ {
			color := tower[i]
			if (i-ptowers[color])%2 == 1 {
				ltowers[color]++
				ptowers[color] = i
			} else {
				if ltowers[color] <= 1 {
					ltowers[color] = 1
					ptowers[color] = i
				}
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fprint(printer, ltowers[i])
			fmt.Fprint(printer, " ")
		}
		fmt.Fprintln(printer)
	}
}