Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer:   If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$;  If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.

The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.

For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, 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).

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		ans := solve()
		fmt.Println(ans)
	}
}

func solve() string {
	var x, y uint64
	fmt.Scanf("%d %d\n", &x, &y)
	if x == y {
		return "YES"
	}
	if x == 1 && y != 0 {
		return "NO"
	}
	if x == 2 && y > 3 {
		return "NO"
	}
	if x == 3 && y > 3 {
		return "NO"
	}
	return "YES"
}

package main

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

/* solution */

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		print(solveOne(readInt(), readInt()))
	}
}

func solveOne(x, y int) string {
	if x == 1 {
		if y == 1 {
			return "YES"
		} else {
			return "NO"
		}
	} else if x < 4 {
		if y >= 1 && y <= 3 {
			return "YES"
		} else {
			return "NO"
		}
	} else {
		return "YES"
	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	text, _ := reader.ReadString('\n')
	t, _ := strconv.ParseInt(strings.TrimSpace(text), 10, 64)
	for i := int64(0); i < t; i++ {
		text, _ = reader.ReadString('\n')
		parts := strings.Split(text, " ")
		x, _ := strconv.ParseInt(strings.TrimSpace(parts[0]), 10, 64)
		y, _ := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64)
		solve(x, y)
	}
}

func solve(x, y int64) {
	if y <= x ||
		(x == 1 && y == 1) ||
		(x == 2 && y == 3) {
		fmt.Println("YES")
	} else if x <= 3 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

package main

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

func main() {
	t := ReadInt()

	for t > 0 {
		a, b := ReadInt2()
		if a == 1 && b != 1 {
			fmt.Println("NO")
		} else if a == 2 && b > 3 {
			fmt.Println("NO")
		} else if a == 3 && b > 3 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
		t--
	}
}

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

	return x
}

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

	return b
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func readInt() int {
	var ch byte
	var err error
	for {
		ch, err = reader.ReadByte()
		if err != nil {
			return 0
		}
		if unicode.IsDigit(rune(ch)) {
			break
		}
	}
	result := int(ch & 0xf)
	for {
		ch, err = reader.ReadByte()
		if err != nil || !unicode.IsDigit(rune(ch)) {
			return result
		}
		result = result*10 + int(ch&0xf)
	}
}

func main() {
	t := readInt()

	for t > 0 {
		a, b := readInt(), readInt()
		if a == 1 && b != 1 {
			fmt.Println("NO")
		} else if a == 2 && b > 3 {
			fmt.Println("NO")
		} else if a == 3 && b > 3 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
		t--
	}
}

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

	return x
}

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

	return b
}

package main

import "fmt"

// Educational Codeforces Round 76
// B. Magic Stick
// https://codeforces.com/contest/1257/problem/B

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		if x == y || x >= 4 {
			fmt.Println("YES")
		} else if x == 2 && y <= 3 {
			fmt.Println("YES")
		} else if x == 3 && y <= 2 {
			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])
		// 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 x, y int64

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		x, y = ReadInt64_2()
		solve()
	}
}

func solve() {
	if x >= y {
		fmt.Println("YES")
		return
	}

	if x == 1 {
		if x >= y {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		return
	}

	if x == 2 || x == 3 {
		if y <= 3 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		return
	}

	fmt.Println("YES")

	// if x%2 == 1 {
	// 	// xが奇数
	// 	xx := (x - 1) / 2 * 3
	// 	if xx > x {
	// 		fmt.Println("YES")
	// 	} else {
	// 		fmt.Println("NO")
	// 	}
	// } else {
	// 	// xが偶数
	// 	xx := x / 2 * 3
	// 	if xx > x {
	// 		fmt.Println("YES")
	// 	} else {
	// 		fmt.Println("NO")
	// 	}
	// }
}

// func solve() {
// 	if x >= y {
// 		fmt.Println("YES")
// 		return
// 	}

// 	p := int64(1)
// 	c := int64(0)
// 	for p <= x {
// 		if p*2 > x {
// 			break
// 		} else {
// 			p *= 2
// 			c++
// 		}
// 	}
// 	// fmt.Printf("p: %d, c: %d\n", p, c)

// 	tmp := x / p
// 	xx := p * tmp
// 	// fmt.Printf("xx: %d\n", xx)
// 	for i := int64(0); i < c; i++ {
// 		xx /= 2
// 		xx *= 3
// 		if xx >= y {
// 			fmt.Println("YES")
// 			return
// 		}
// 	}

// 	if xx >= y {
// 		fmt.Println("YES")
// 	} else {
// 		fmt.Println("NO")
// 	}
// }

// func solve() {
// 	if x >= y {
// 		fmt.Println("YES")
// 		return
// 	}

// 	twoCount := 0
// 	for x%2 == 0 {
// 		twoCount++
// 		x /= 2
// 	}

// 	for i := 0; i < twoCount; i++ {
// 		x *= 3
// 		if x >= y {
// 			fmt.Println("YES")
// 			return
// 		}
// 	}

// 	if x >= y {
// 		fmt.Println("YES")
// 	} else {
// 		fmt.Println("NO")
// 	}
// }

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

var in = bufio.NewReader(os.Stdin)

func solve() {
	var x, y int
	_, _ = fmt.Fscanf(in, "%d %d\n", &x, &y)
	if x == 1 && y != 1 {
		_, _ = fmt.Println("NO")
		return
	}
	if x == 2 && y > 3 {
		_, _ = fmt.Println("NO")
		return
	}
	if x == 3 && y > 3 {
		_, _ = fmt.Println("NO")
		return
	}
	_, _ = fmt.Println("YES")
}

func main() {
	var t int
	_, _ = fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import "fmt"

func main() {
	var caseNum int
	fmt.Scanf("%d\n", &caseNum)
	for i := 0; i < caseNum; i++ {
		var x, y int
		fmt.Scanf("%d %d\n", &x, &y)
		if x == 1 {
			if y == 1 {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		} else if x <= 3 {
			if y <= 3 {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		} else {
			fmt.Println("YES")
		}
	}
}

package main

import "fmt"

func min(a, b int) int {
    if a < b {
        return a
    } else {
        return b
    }
}
func main() {
    var T int
    fmt.Scanf("%d\n", &T)
    for T > 0 {
        T--
        var x, y int
        fmt.Scanf("%d %d\n", &x, &y)
        if x == 1 && y == 1 {
            fmt.Print("YES\n")
        } else if x == 2 && y <= 3 {
            fmt.Print("YES\n")
        } else if x == 3 && y <= 3 {
            fmt.Print("YES\n")
        } else if x >= 4 {
            fmt.Print("YES\n")
        } else {
            fmt.Print("NO\n")
        }
    }
}

package main

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

func main() {
	var res string
	reader := bufio.NewReader(os.Stdin)
	cases := readOneInt64(reader)
	for i := int64(0); i < cases; i++ {
		tmp := readInt64Slice(reader, 2)
		x, y := tmp[0], tmp[1]
		switch {
		case x == y, x > 3, x < 4 && y < 4 && x != 1:
			res = "YES"
		default:
			res = "NO"
		}
		fmt.Println(res)
	}
}

func readOneInt64(r *bufio.Reader) (n int64) {
	bytes, _ := r.ReadBytes('\n')
	n, _ = strconv.ParseInt(string(bytes[:len(bytes)-2]), 10, 64)
	return
}

func readInt64Slice(r *bufio.Reader, n int) (slice []int64) {
	var bytes []byte
	slice = make([]int64, n)
	n--
	for i := 0; i < n; i++ {
		bytes, _ = r.ReadBytes(' ')
		slice[i], _ = strconv.ParseInt(string(bytes[:len(bytes)-1]), 10, 64)
	}
	slice[n] = readOneInt64(r)
	return
}

//func readOneString(r *bufio.Reader) []byte {
//	bytes, _ := r.ReadBytes('\n')
//	return bytes[:len(bytes)-2]
//}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		ans := solve()
		fmt.Println(ans)
	}
}

func solve() string {
	var x, y uint64
	fmt.Scanf("%d %d", &x, &y)
	if x == y {
		return "YES"
	}
	if x == 1 && y != 0 {
		return "NO"
	}
	if x == 2 && y != 3 {
		return "NO"
	}
	if x == 3 && y != 2 {
		return "NO"
	}
	return "YES"
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		ans := solve()
		fmt.Println(ans)
	}
}

func solve() string {
	var x, y uint64
	fmt.Scanf("%d %d\n", &x, &y)
	if x == y {
		return "YES"
	}
	if x == 1 && y != 0 {
		return "NO"
	}
	if x == 2 && y != 3 {
		return "NO"
	}
	if x == 3 && y != 2 {
		return "NO"
	}
	return "YES"
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		ans := solve()
		fmt.Println(ans)
	}
}

func solve() string {
	var x, y uint64
	fmt.Scanf("%d %d", &x, &y)
	for i := 0; i < 200; i++ {
		if x == y {
			return "YES"
		}
		switch x % 2 {
		case 0:
			x = x / 2 * 3
		case 1:
			x = x - 1
		}
	}
	return "NO"
}

package main

import "fmt"

func pow(b, n int) (r int) {
	r = 1
	for i := 0; i < n; i++ {
		r *= b
	}
	return r
}

func main() {
	var n int
	fmt.Scan(&n)
	ans := make([]string, 0)
	for i := 0; i < n; i++ {
		var x, y int
		var t string
		fmt.Scan(&x, &y)
		if x == y {
			t = "YES"
		} else if x > 1 {
			if y < x {
				t = "YES" // yから1ずつ減らせば良い
			} else { // y > x
				// xを増やしてyにしないといけない
				// 偶数なら1.5倍できる
				// 奇数でも1減らすと偶数にできるので1.5倍できる
				// ただし再現なく1.5倍できるわけではなく、1回1.5倍するごとに2を使ってしまう。
				// xを超えない最大の2の冪乗を求める
				p, q := 1, 0
				for {
					if pow(2, p) > x {
						break
					}
					p++
					q++
				}
				// fmt.Println("i", i, "q", q)
				ok := false
				tx := x
				for j := 0; j < q; j++ {
					x = x * 3 / 2
					if x >= y {
						ok = true
					}
				}
				if ok {
					t = "YES"
				} else {
					t = "NO"
					for {
						if tx%2 == 0 {
							tx = tx * 3 / 2
						} else {
							break
						}
					}
					if tx >= y {
						t = "YES"
					}
				}
			}
		} else { // x == 1 && y != 1
			t = "NO"
		}
		// fmt.Println("x", x, "y", y, "t", t)
		ans = append(ans, t)
	}
	for i := range ans {
		fmt.Println(ans[i])
	}
}

package main

import "fmt"

func pow(b, n int) (r int) {
	r = 1
	for i := 0; i < n; i++ {
		r *= b
	}
	return r
}

func main() {
	var n int
	fmt.Scan(&n)
	ans := make([]string, 0)
	for i := 0; i < n; i++ {
		var x, y int
		var t string
		fmt.Scan(&x, &y)
		if x == y {
			t = "YES"
		} else if x > 1 {
			if y < x {
				t = "YES"
			} else { // y > x
				p, q := 1, 0
				for {
					if pow(2, p) > x {
						break
					}
					p++
					q++
				}
				ok := false
				for j := 0; j < q; j++ {
					x = x * 3 / 2
					if x >= y {
						ok = true
					}
				}
				if ok {
					t = "YES"
				} else {
					t = "NO"
				}
			}
		} else { // x == 1 && y != 1
			t = "NO"
		}
		ans = append(ans, t)
	}
	for i := range ans {
		fmt.Println(ans[i])
	}
}

package main

import "fmt"

func pow(b, n int) (r int) {
	r = 1
	for i := 0; i < n; i++ {
		r *= b
	}
	return r
}

func main() {
	var n int
	fmt.Scan(&n)
	ans := make([]string, 0)
	for i := 0; i < n; i++ {
		var x, y int
		var t string
		fmt.Scan(&x, &y)
		if x == y {
			t = "YES"
		} else if x > 1 {
			if y < x {
				t = "YES" // yから1ずつ減らせば良い
			} else { // y > x
				// xを増やしてyにしないといけない
				// 偶数なら1.5倍できる
				// 奇数でも1減らすと偶数にできるので1.5倍できる
				// ただし再現なく1.5倍できるわけではなく、1回1.5倍するごとに2を使ってしまう。
				// xを超えない最大の2の冪乗を求める
				p, q := 1, 0
				for {
					if pow(2, p) > x {
						break
					}
					p++
					q++
				}
				// fmt.Println("i", i, "q", q, "x", x, "y", y)
				ok := false
				tx := x
				for j := 0; j < q; j++ {
					x = (x / 2) * 3
					if x >= y {
						ok = true
					}
				}
				// fmt.Println("i", i, "q", q, "x", x, "y", y, "ok", ok)
				if ok {
					t = "YES"
				} else {
					t = "NO"
					for {
						if tx%2 == 0 {
							tx = (tx / 2) * 3
						} else {
							break
						}
					}
					if tx >= y {
						t = "YES"
					}
				}
			}
		} else { // x == 1 && y != 1
			t = "NO"
		}
		// fmt.Println("x", x, "y", y, "t", t)
		ans = append(ans, t)
	}
	for i := range ans {
		fmt.Println(ans[i])
	}
}

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])
		// 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 x, y int64

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		x, y = ReadInt64_2()
		solve()
	}
}

func solve() {
	if x >= y {
		fmt.Println("YES")
		return
	}

	if x == 1 {
		if x >= y {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		return
	}

	if x%2 == 1 {
		// xが奇数
		xx := (x - 1) / 2 * 3
		if xx > x {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	} else {
		// xが偶数
		xx := x / 2 * 3
		if xx > x {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

// func solve() {
// 	if x >= y {
// 		fmt.Println("YES")
// 		return
// 	}

// 	p := int64(1)
// 	c := int64(0)
// 	for p <= x {
// 		if p*2 > x {
// 			break
// 		} else {
// 			p *= 2
// 			c++
// 		}
// 	}
// 	// fmt.Printf("p: %d, c: %d\n", p, c)

// 	tmp := x / p
// 	xx := p * tmp
// 	// fmt.Printf("xx: %d\n", xx)
// 	for i := int64(0); i < c; i++ {
// 		xx /= 2
// 		xx *= 3
// 		if xx >= y {
// 			fmt.Println("YES")
// 			return
// 		}
// 	}

// 	if xx >= y {
// 		fmt.Println("YES")
// 	} else {
// 		fmt.Println("NO")
// 	}
// }

// func solve() {
// 	if x >= y {
// 		fmt.Println("YES")
// 		return
// 	}

// 	twoCount := 0
// 	for x%2 == 0 {
// 		twoCount++
// 		x /= 2
// 	}

// 	for i := 0; i < twoCount; i++ {
// 		x *= 3
// 		if x >= y {
// 			fmt.Println("YES")
// 			return
// 		}
// 	}

// 	if x >= y {
// 		fmt.Println("YES")
// 	} else {
// 		fmt.Println("NO")
// 	}
// }

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

var in = bufio.NewReader(os.Stdin)

func solve() {
	var x, y int
	_, _ = fmt.Fscanf(in, "%d%d", &x, &y)
	if x == 1 && y != 1 {
		_, _ = fmt.Println("NO")
		return
	}
	if x == 2 && y > 3 {
		_, _ = fmt.Println("NO")
		return
	}
	_, _ = fmt.Println("YES")
}

func main() {
	var t int
	_, _ = fmt.Fscanf(in, "%d", &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

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

func solve() {
	var x, y int
	_, _ = fmt.Fscanf(in, "%d%d", &x, &y)
	if x == 1 && y != 1 {
		_, _ = fmt.Fprintln(out, "NO")
		return
	}
	if x == 2 && y > 3 {
		_, _ = fmt.Fprintln(out, "NO")
		return
	}
	_, _ = fmt.Fprintln(out, "YES")
}

func main() {
	var t int
	_, _ = fmt.Fscanf(in, "%d", &t)
	for i := 0; i < t; i++ {
		solve()
	}
}


You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri and integer ki. It means that you should cyclically shift the substring s[li... ri] ki times. The queries should be processed one after another in the order they are given.One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.

The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters. Second line contains a single integer m (1 ≤ m ≤ 300) — the number of queries. The i-th of the next m lines contains three integers li, ri and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.

Print the resulting string s after processing all m queries.

NoteThe sample is described in problem statement.

package main

import "fmt"

func main() {
    var s string
    fmt.Scanln(&s)

    var m int
    fmt.Scanln(&m)

    for i := 0; i < m; i++ {
        var l, r, k int
        fmt.Scanln(&l, &r, &k)
        //fmt.Println(l, r, k)
        shift(&s, l, r, k)
    }

    fmt.Println(s)

    /*var t = "abacaba"
    shift(&t, 3, 6, 1)
    fmt.Println(t)*/
}

func shift(s *string, l, r, k int) {
    i := k % (r - l + 1)
    b := []byte(*s)
    *s = string(b[:l-1]) + string(b[r-i:r]) + string(b[l-1:r-i]) + string(b[r:])
}

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var s string
	fmt.Scanln(&s)
	buf := bytes.NewBufferString(s)
	var n int
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		var l, r, k int
		fmt.Scanf("%d %d %d\n", &l, &r, &k)
		k = k % (r - l + 1)
		k = (r - l + 1) - k
		rotate(buf.Bytes(), l-1, r, k)
	}
	fmt.Println(buf.String())
}

func rotate(s []byte, l, r, k int) {
	if k == 0 || r-l == 0 {
		return
	}
	rev(s[l : l+k])
	rev(s[l+k : r])
	rev(s[l:r])
}

func rev(in []byte) {
	l := len(in)
	for i := 0; i < l/2; i++ {
		in[i], in[l-1-i] = in[l-1-i], in[i]
	}
	return
}

package main

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

func rotateStr(str string, k int) string {
	totalRotationsReq := k
	if k >= len(str) {
		totalRotationsReq = k % len(str)
	}

	resp := make([]byte, len(str))
	for idx := range str {
		respIdx := idx + totalRotationsReq
		if respIdx >= len(str) {
			respIdx -= len(str)
		}
		resp[respIdx] = str[idx]
	}
	return string(resp)
}

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	//for scanner.Scan() {
	//	line := scanner.Text()
	//	fmt.Printf("::%v::\n", line)
	//}
	//if err := scanner.Err(); err != nil {
	//	panic(err)
	//}
	//
	//os.Exit(0)

	var s string
	if !scanner.Scan() || scanner.Err() != nil {
		panic(scanner.Err())
	}
	s = scanner.Text()

	var mStr string
	if !scanner.Scan() || scanner.Err() != nil {
		panic(scanner.Err())
	}
	mStr = scanner.Text()
	m, err := strconv.Atoi(mStr)
	if err != nil {
		panic(err)
	}

	resp := s

	for i := 0; i < m; i++ {
		var lrkStr string
		if !scanner.Scan() || scanner.Err() != nil {
			panic(scanner.Err())
		}
		lrkStr = scanner.Text()
		lrkArr := strings.Split(lrkStr, " ")
		l, err := strconv.Atoi(lrkArr[0])
		if err != nil {
			panic(err)
		}
		r, err := strconv.Atoi(lrkArr[1])
		if err != nil {
			panic(err)
		}
		k, err := strconv.Atoi(lrkArr[2])
		if err != nil {
			panic(err)
		}
		//fmt.Printf("l: %v, r: %v, k: %v, resp: %v\n",
		//	l, r, k, resp)
		rotated := rotateStr(resp[l-1:r], k)
		//fmt.Printf("l: %v, r: %v, k: %v, resp: %v, resp[0:l-1]: %v, rotated: %v, resp[r:]: %v\n",
		//	l, r, k, resp, resp[0:l-1], rotated, resp[r:])
		resp = resp[0:l-1] + rotated + resp[r:]
	}

	fmt.Println(resp)

}

package main

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

func readOneLineInts(inputreader *bufio.Reader, n int) (res chan int64) {
	res = make(chan int64)
	go func() {
		input, _ :=	inputreader.ReadString('\n')
		ints := strings.Split(strings.TrimSpace(input), " ")
		for i := range ints{
			x, _ := strconv.ParseInt(ints[i],10,64)
			res <- x
		}
		close(res)
	}()

	return
}

func gcd(a, b int64) int64 {
	if b==0 {
		return a;
	} else {
		return gcd(b, a%b);
	}
}

func rotate(nums []byte, k int64) {
    if k < 0 || len(nums) == 0 {
        return
	}

	ln := int64(len(nums))

	k = k%ln
	k = ln - k

	var d,i,j int64;
	var temp byte
	d = -1
	for i = int64(0); i<gcd(ln,k); i++ {
		j=i;
		temp = nums[i];
		for {
			d=(j+k)%ln;
			if(d==i) {
				break;
			}
			nums[j] = nums[d];
			j=d;
		}
		nums[j]=temp;
	}
}
func main(){
	rs := bufio.NewReader(os.Stdin)
	ws := bufio.NewWriter(os.Stdout)

	input, _ := rs.ReadString('\n')
	mission := []byte(strings.TrimSpace(input))

	input, _ = rs.ReadString('\n')
	m, _ := strconv.ParseInt(strings.TrimSpace(input), 10, 64)

	for i := int64(0); i < m; i++ {
		ch := readOneLineInts(rs, 3);
		l := <-ch; r := <-ch; k := <-ch;
		l--; r--;
		rotate(mission[l:r+1], k)
	}

	ws.WriteString(string(mission))

	ws.Flush()
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func init() {
	scanner.Split(bufio.ScanWords)
}

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

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

func scanInt64() int64 {
	scanner.Scan()
	v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return v
}

func scanInt() int {
	scanner.Scan()
	v, _ := strconv.Atoi(scanner.Text())
	return v
}

func scanInts(n int) []int {
	l := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		l[i], _ = strconv.Atoi(scanner.Text())
	}

	return l
}

func scanInts64(n int64) []int64 {
	l := make([]int64, n)
	for i := int64(0); i < n; i++ {
		scanner.Scan()
		l[i], _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}

	return l
}

func scanBool() bool {
	scanner.Scan()
	v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return v == 1
}

func scanBools(n int64) []bool {
	l := make([]bool, n)
	for i := int64(0); i < n; i++ {
		scanner.Scan()
		v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		l[i] = v == 1
	}

	return l
}

func main() {
	s, m := scanString(), scanInt()

	for i := 0; i < m; i++ {
		l, r, k := scanInt(), scanInt(), scanInt()

		sz := r - l + 1
		k %= sz

		// fmt.Println("sz=", sz, ", k=", k)

		s = s[:l-1] + s[r-k:r] + s[l-1:r-k] + s[r:]
	}

	fmt.Println(s)
}

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}

	return -a
}

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

	return b
}

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var s string
	fmt.Scanln(&s)
	buf := bytes.NewBufferString(s)
	var n int
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		var l, r, k int
		fmt.Scanf("%d %d %d\n", &l, &r, &k)
		k = k % (r - l + 1)
		k = (r - l + 1) - k
		rotate(buf.Bytes(), l-1, r, k)
	}
	fmt.Println(s)

}

func rotate(s []byte, l, r, k int) {
	if k == 0 || r-l == 0 {
		return
	}
	rev(s[l : l+k])
	rev(s[l+k : r])
	rev(s[l:r])
}

func rev(in []byte) {
	l := len(in)
	for i := 0; i < l/2; i++ {
		in[i], in[l-1-i] = in[l-1-i], in[i]
	}
	return
}

package main

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

var ps = fmt.Printf
var ss = fmt.Scanf

func gcd(a, b int64) int64 {
	if b==0 {
		return a;
	} else {
		return gcd(b, a%b);
	}
}

func rotate(nums []byte, k int64) {
    if k < 0 || len(nums) == 0 {
        return
	}

	ln := int64(len(nums))

	k = k%ln
	k = ln - k

	var d,i,j int64;
	var temp byte
	d = -1
	for i = int64(0); i<gcd(ln,k); i++ {
		j=i;
		temp = nums[i];
		for {
			d=(j+k)%ln;
			if(d==i) {
				break;
			}
			nums[j] = nums[d];
			j=d;
		}
		nums[j]=temp;
	}
}
func main(){
	// ws := bufio.NewWriter(os.Stdout)

	var str string
	ss("%s", &str)
	mission := []byte(str)

	var m int64
	ss("%l", &m)

	for i := int64(0); i < m; i++ {
		var l,r,k int64
		ss("%l %l %l", &l, &r, &k)
		l--; r--;
		rotate(mission[l:r+1], k)
	}

	ps("%s", string(mission))
	// ws.WriteString(string(mission))
	// ws.Flush()
}

package main

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

var ps = fmt.Printf
var ss = fmt.Scanf

func gcd(a, b int64) int64 {
	if b==0 {
		return a;
	} else {
		return gcd(b, a%b);
	}
}

func rotate(nums []byte, k int64) {
    if k < 0 || len(nums) == 0 {
        return
	}

	ln := int64(len(nums))

	k = k%ln
	k = ln - k

	var d,i,j int64;
	var temp byte
	d = -1
	for i = int64(0); i<gcd(ln,k); i++ {
		j=i;
		temp = nums[i];
		for {
			d=(j+k)%ln;
			if(d==i) {
				break;
			}
			nums[j] = nums[d];
			j=d;
		}
		nums[j]=temp;
	}
}
func main(){
	// ws := bufio.NewWriter(os.Stdout)

	var str string
	ss("%s", &str)
	mission := []byte(str)

	var m int64
	ss("%d", &m)

	for i := int64(0); i < m; i++ {
		var l,r,k int64
		ss("%d %d %d", &l, &r, &k)
		l--; r--;
		rotate(mission[l:r+1], k)
	}

	ps("%s", string(mission))
	// ws.WriteString(string(mission))
	// ws.Flush()
}


A multi-subject competition is coming! The competition has $$$m$$$ different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students. He has $$$n$$$ candidates. For the $$$i$$$-th person he knows subject $$$s_i$$$ the candidate specializes in and $$$r_i$$$ — a skill level in his specialization (this level can be negative!). The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.(Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition).

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le m \le 10^5$$$) — the number of candidates and the number of subjects. The next $$$n$$$ lines contains two integers per line: $$$s_i$$$ and $$$r_i$$$ ($$$1 \le s_i \le m$$$, $$$-10^4 \le r_i \le 10^4$$$) — the subject of specialization and the skill level of the $$$i$$$-th candidate.

Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or $$$0$$$ if every valid non-empty delegation has negative sum.

NoteIn the first example it's optimal to choose candidates $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, so two of them specialize in the $$$2$$$-nd subject and other two in the $$$3$$$-rd. The total sum is $$$6 + 6 + 5 + 5 = 22$$$.In the second example it's optimal to choose candidates $$$1$$$, $$$2$$$ and $$$5$$$. One person in each subject and the total sum is $$$6 + 6 + 11 = 23$$$.In the third example it's impossible to obtain a non-negative sum.

package main

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

func main(){
	buf := bufio.NewReader(os.Stdin)
	var n,m int
	fmt.Fscanf(buf, "%d %d\n", &n,&m)
	mat := make(map[int][]int)
	for i:=1;i <= n;i++{
		var s,r int
		fmt.Fscanf(buf, "%d %d\n", &s,&r)
		v,ok := mat[s]
		if !ok{
			v = make([]int,0)
		}
		v = append(v,r)
		mat[s] = v
	}
//	fmt.Println(mat)
	var ans int64 = 0
	dp := make([]int64,n+1)
	for i:=1;i <= m;i++{
		v,ok := mat[i]
		if ok{
			sort.Ints(v)
		//	fmt.Println("i=",i,"v=",v)
			var len int  = len(v)
			var sum int64 = 0
			for j:=len-1;j >= 0;j--{
				sum += int64(v[j])
				if sum <= 0{
					break
				}
				dp[len-j] += sum
				if ans < dp[len-j]{
					ans = dp[len-j]
				}
			}
		}
	}
//	fmt.Println(dp)
	fmt.Printf("%d\n",ans)

}

package main

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

type intSlice []int

func (d intSlice) Less(i, j int) bool { return d[i] > d[j] }
func (d intSlice) Len() int           { return len(d) }
func (d intSlice) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

type dataSlice []intSlice

func (d dataSlice) Less(i, j int) bool { return len(d[i]) > len(d[j]) }
func (d dataSlice) Len() int           { return len(d) }
func (d dataSlice) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

type Cin struct {
	scanner *bufio.Scanner
}

func newCin() *Cin {
	return &Cin{
		scanner: bufio.NewScanner(os.Stdin),
	}
}

func (c *Cin) NextInt() int {
	c.scanner.Scan()
	line := c.scanner.Text()
	return c.convertInt(line)
}

func (c *Cin) NextIntSlice() []int {
	c.scanner.Scan()
	line := strings.Split(c.scanner.Text(), " ")
	var ret []int
	for _, s := range line {
		ret = append(ret, c.convertInt(s))
	}
	return ret
}

func (c *Cin) NextTwoInt() (int, int) {
	line := c.NextIntSlice()
	return line[0], line[1]
}

func (c *Cin) convertInt(data string) int {
	i, _ := strconv.Atoi(data)
	return i
}

func main() {
	//t0 := time.Now()
	cin := newCin()
	n, m := cin.NextTwoInt()
	var (
		data dataSlice
	)
	for i := 0; i < m; i++ {
		data = append(data, intSlice{})
	}
	for i := 0; i < n; i++ {
		s, r := cin.NextTwoInt()
		data[s-1] = append(data[s-1], r)
	}
	//fmt.Printf("read:%v\n", time.Since(t0))
	//t0 = time.Now()

	sort.Sort(data)
	for i := range data {
		sort.Sort(data[i])
	}
	//fmt.Printf("%+v\n", data)
	//fmt.Printf("sort:%v\n", time.Since(t0))
	//t0 = time.Now()

	var sum dataSlice
	for i := 0; i < m; i++ {
		sum = append(sum, intSlice{})
		for j := 0; j < len(data[i]); j++ {
			if j == 0 {
				sum[i] = append(sum[i], data[i][j])
			} else {
				sum[i] = append(sum[i], sum[i][j-1]+data[i][j])
			}
		}
	}

	ans := 0
	for level := 1; level <= len(data[0]); level++ {
		currentSum := 0
		for i := 0; i < m && len(data[i]) >= level; i++ {
			if sum[i][level-1] > 0 {
				currentSum += sum[i][level-1]
			}
		}
		if currentSum > ans {
			ans = currentSum
		}
	}
	fmt.Printf("%v\n", ans)
	//fmt.Printf("scan:%v\n", time.Since(t0))
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner.Split(bufio.ScanWords)
	n, m := scanNum(), scanNum()
	M := make([][]int, m+1)

	for i := 0; i < n; i++ {
		key, score := scanNum(), scanNum()
		if M[key] == nil {
			M[key] = []int{}
		}
		M[key] = append(M[key], score)
	}

	maxLen := 0
	for i := 0; i <= m; i++ {
		if len(M[i]) > maxLen {
			maxLen = len(M[i])
		}
	}
	bestScore := make([]int, maxLen+1)
	for i := 0; i <= m; i++ {
		sort.Ints(M[i])
		for j := len(M[i]) - 2; j >= 0; j-- {
			M[i][j] = M[i][j] + M[i][j+1]
		}
		for j := len(M[i]) - 1; j >= 0; j-- {
			if M[i][j] > 0 {
				bestScore[len(M[i])-j] += M[i][j]
			}
		}
	}

	max := 0
	for i := 1; i <= maxLen; i++ {
		if bestScore[i] > max {
			max = bestScore[i]
		}
	}

	fmt.Println(max)
}

package main

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

func main(){
	buf := bufio.NewReader(os.Stdin)
	var n,m int
	fmt.Fscanf(buf, "%d %d\n", &n,&m)
	mat := make(map[int][]int)
	for i:=1;i <= n;i++{
		var s,r int
		fmt.Fscanf(buf, "%d %d\n", &s,&r)
		if r <= 0{
			continue
		}
		v,ok := mat[s]
		if !ok{
			v = make([]int,0)
		}
		v = append(v,r)
		mat[s] = v
	}
//	fmt.Println(mat)
	var ans int64 = 0
	dp := make([]int64,n+1)
	for i:=1;i <= m;i++{
		v,ok := mat[i]
		if ok{
			sort.Ints(v)
//			fmt.Println("i=",i,"v=",v)
			var len int  = len(v)
			var sum int64 = 0
			for j:=len-1;j >= 0;j--{
				sum += int64(v[j])
				dp[len-j] += sum
				if ans < dp[len-j]{
					ans = dp[len-j]
				}
			}
		}
	}
	fmt.Printf("%d\n",ans)

}

package main

import (
	"fmt"
	"sort"
)

type intSlice []int

func (d intSlice) Less(i, j int) bool { return d[i] > d[j] }
func (d intSlice) Len() int           { return len(d) }
func (d intSlice) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

type dataSlice []intSlice

func (d dataSlice) Less(i, j int) bool { return len(d[i]) > len(d[j]) }
func (d dataSlice) Len() int           { return len(d) }
func (d dataSlice) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

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

func main() {
	var n, m int
	_, _ = fmt.Scanln(&n, &m)
	var (
		data dataSlice
		s, r int
	)
	for i := 0; i < m; i++ {
		data = append(data, intSlice{})
	}
	for i := 0; i < n; i++ {
		_, _ = fmt.Scanln(&s, &r)
		data[s-1] = append(data[s-1], r)
	}

	sort.Sort(data)
	for i := range data {
		sort.Sort(data[i])
	}
	//fmt.Printf("%+v\n", data)

	var sum dataSlice
	for i := 0; i < m; i++ {
		sum = append(sum, intSlice{})
		for j := 0; j < len(data[i]); j++ {
			val := data[i][j]
			if j > 0 {
				val += sum[i][j-1]
			}
			if i > 0 && j > 0 {
				val -= sum[i-1][j-1]
			}
			if i > 0 {
				val += sum[i-1][j]
			}
			sum[i] = append(sum[i], val)
		}
	}
	ans := 0
	for i := m - 1; i >= 0; i-- {
		ans = maxInt(ans, sum[i][len(sum[i])-1])
	}
	//fmt.Printf("%+v\n", sum)
	fmt.Printf("%v\n", ans)
}


You like the card board game "Set". Each card contains $$$k$$$ features, each of which is equal to a value from the set $$$\{0, 1, 2\}$$$. The deck contains all possible variants of cards, that is, there are $$$3^k$$$ different cards in total.A feature for three cards is called good if it is the same for these cards or pairwise distinct. Three cards are called a set if all $$$k$$$ features are good for them.For example, the cards $$$(0, 0, 0)$$$, $$$(0, 2, 1)$$$, and $$$(0, 1, 2)$$$ form a set, but the cards $$$(0, 2, 2)$$$, $$$(2, 1, 2)$$$, and $$$(1, 2, 0)$$$ do not, as, for example, the last feature is not good.A group of five cards is called a meta-set, if there is strictly more than one set among them. How many meta-sets there are among given $$$n$$$ distinct cards?

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^3$$$, $$$1 \le k \le 20$$$) — the number of cards on a table and the number of card features. The description of the cards follows in the next $$$n$$$ lines. Each line describing a card contains $$$k$$$ integers $$$c_{i, 1}, c_{i, 2}, \ldots, c_{i, k}$$$ ($$$0 \le c_{i, j} \le 2$$$) — card features. It is guaranteed that all cards are distinct.

Output one integer — the number of meta-sets.

NoteLet's draw the cards indicating the first four features. The first feature will indicate the number of objects on a card: $$$1$$$, $$$2$$$, $$$3$$$. The second one is the color: red, green, purple. The third is the shape: oval, diamond, squiggle. The fourth is filling: open, striped, solid.You can see the first three tests below. For the first two tests, the meta-sets are highlighted.In the first test, the only meta-set is the five cards $$$(0000,\ 0001,\ 0002,\ 0010,\ 0020)$$$. The sets in it are the triples $$$(0000,\ 0001,\ 0002)$$$ and $$$(0000,\ 0010,\ 0020)$$$. Also, a set is the triple $$$(0100,\ 1000,\ 2200)$$$ which does not belong to any meta-set.   In the second test, the following groups of five cards are meta-sets: $$$(0000,\ 0001,\ 0002,\ 0010,\ 0020)$$$, $$$(0000,\ 0001,\ 0002,\ 0100,\ 0200)$$$, $$$(0000,\ 0010,\ 0020,\ 0100,\ 0200)$$$.   In there third test, there are $$$54$$$ meta-sets.

package main

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

const MOD = 1e9 + 7

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

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

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	n := _int()
	k := _int()
	c := map[string]int64{}
	for i := 0; i < n; i++ {
		tmp := make([]rune, k)
		for j := range tmp {
			tmp[j] = rune(_int())
		}
		c[string(tmp)] = 0
	}
	printf("%d\n", sol(n, k, c))
}

func sol(n, k int, c map[string]int64) int64 {
	for s1 := range c {
		for s2 := range c {
			if s1 == s2 {
				continue
			}
			comp := complement(s1, s2)
			if _, ok := c[comp]; ok {
				// debug(s1, s2)
				c[s1]++
				c[s2]++
				c[comp]++
			}
		}
	}
	ret := int64(0)
	for _, i := range c {
		if i%6 != 0 {
			panic("invalid set count")
		}
		i /= 6
		if i < 2 {
			continue
		}
		ret += choose(i, 2)
	}
	return ret
}

func choose(n, k int64) int64 {
	if n < 0 || k < 0 || n < k {
		panic("invalid input")
	}
	numerator := fact(n)
	denominator := (fact(k) * fact(n-k)) % MOD
	return (numerator * mmi(denominator)) % MOD
}

var fv = []int64{1}

func fact(n int64) int64 {
	for i := int64(len(fv)); i <= n; i++ {
		fv = append(fv, (fv[i-1]*i)%MOD)
	}
	return fv[n]
}

func mmi(n int64) int64 {
	return pow(n, MOD-2)
}

func pow(b, p int64) int64 {
	if p == 0 {
		return 1
	}
	if p == 1 {
		return b
	}
	h := pow(b, p/2)
	if p&1 == 0 {
		return (h * h) % MOD
	}
	return (((h * h) % MOD) * b) % MOD
}

func complement(s1, s2 string) string {
	ret := []byte{}
	for i := range s1 {
		if s1[i] == s2[i] {
			ret = append(ret, s1[i])
			continue
		}
		if s1[i] != s2[i] {
			e := [3]bool{}
			e[s1[i]] = true
			e[s2[i]] = true
			for i, b := range e {
				if !b {
					ret = append(ret, byte(i))
				}
			}
		}
	}
	return string(ret)
}

func debug(s1, s2 string) {
	f := func(b []byte) []byte {
		for i := range b {
			b[i] += '0'
		}
		return b
	}
	b1 := f([]byte(s1))
	b2 := f([]byte(s2))
	b3 := f([]byte(complement(s1, s2)))
	printf("%s %s %s\n", b1, b2, b3)
}

package main

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

func get_central_card(card1, card2 []int, card_length int) []int {
	central_card := make([]int, card_length)
	for i := 0; i < card_length; i++ {
		central_card[i] = (6-card1[i]-card2[i])%3
	}
	return central_card
}

func get_key(slc []int) int {
	k := 0
	for _, v := range slc {
		k = 3*k + v
	}
	return k
}

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

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

	v2d := make([][]int, n)
	for i := 0; i < n; i++ {
		v2d[i] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Fscan(r, &v2d[i][j])
		}
	}

	m := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			v := get_central_card(v2d[i], v2d[j], k)
			m[get_key(v)]++
		}
	}

	cnt := 0
	for _, v := range v2d {
		k := get_key(v)
		cnt += (m[k]*(m[k]-1))/2
	}

	fmt.Fprintln(w, cnt)

}

package main

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

func get_central_card(card1, card2 []int, card_length int) string {
	central_card := make([]int, card_length)
	for i := 0; i < card_length; i++ {
		central_card[i] = (6-card1[i]-card2[i])%3
	}
	return fmt.Sprintf("%v", central_card)
}

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

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

	v2d := make([][]int, n)
	for i := 0; i < n; i++ {
		v2d[i] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Fscan(r, &v2d[i][j])
		}
	}

	m := make(map[string]int)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			v := get_central_card(v2d[i], v2d[j], k)
			m[v]++
		}
	}

	cnt := 0
	for _, v := range v2d {
		k := fmt.Sprintf("%v", v)
		cnt += (m[k]*(m[k]-1))/2
	}

	fmt.Fprintln(w, cnt)

}

package main

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

func check(x, y string) string {
	n := len(x)
	v := []byte{}
	tmp := []byte{'0', '1', '2'}
	for i := 0; i < n; i++ {
		if x[i] == y[i] {
			v = append(v, x[i])
		} else {
			for j := 0; j < 3; j++ {
				if tmp[j] != x[i] && tmp[j] != y[i] {
					v = append(v, tmp[j])
					break
				}
			}
		}
	}
	return string(v)
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, k int
	fmt.Fscan(in, &n, &k)
	a := make([]string, n+5)
	for i := 1; i <= n; i++ {
		v := []byte{}
		for j := 1; j <= k; j++ {
			var x int
			fmt.Fscan(in, &x)
			v = append(v, byte(x+'0'))
		}
		a[i] = string(v)
	}
	mp := map[string]int{}
	cont := map[string]bool{}
	vis := map[string]bool{}
	for i := 1; i <= n; i++ {
		cont[a[i]] = true
	}
	ans := 0
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			s := check(a[i], a[j])
			f := []string{a[i], a[j], s}
			sort.Strings(f)
			ok := f[0] + f[1] + f[2]
			if cont[s] && !vis[ok] {
				ans += mp[a[i]]
				ans += mp[a[j]]
				ans += mp[s]
				mp[a[i]]++
				mp[a[j]]++
				mp[s]++
				//fmt.Println(a[i], a[j], s, mp[a[i]], mp[a[j]], mp[s])
				vis[ok] = true
			}
		}
	}
	fmt.Fprintln(out, ans)
}

package main

import (
	"fmt"
)

var n, k int
var ar [][]int
const L int = 22
var pow [L]int64

func main() {
	fmt.Scan(&n, &k)
	ar = make([][]int, n)
	for i:=0; i<n; i++ {
		ar[i] = make([]int, k)
		for j:=0; j<k; j++ {
			fmt.Scan(&ar[i][j])
			// fmt.Printf("%d %d %d\n", i, j, ar[i][j])
		}
	}
	// fmt.Printf("%d %d %v", n, k, ar)

	pow[0] = 1
	for i:=1; i<L; i++ {
		pow[i] = pow[i-1] * 3;
	}

	getHash := func(a []int) int64 {
		var res int64 = 0
		for i:=0; i<k; i++ {
			res += int64(a[i]) * pow[i]
		}
		return res
	}

	getRequiresHash := func(i, j int) int64 {

		getOther := func(a, b int) int {
			for val:=0; val<3; val++ {
				if val != a && val != b {
					return val
				}
			}
			return -1
		}

		requires := make([]int, k)
		for x:=0; x<k; x++ {
			if ar[i][x] == ar[j][x] {
				requires[x] = ar[i][x]
				continue
			}
			requires[x] = getOther(ar[i][x], ar[j][x])
		}

		return getHash(requires)
	}

	mp := make(map[int64]int)
	for i:=0; i<n; i++ {
		for j:=i+1; j<n; j++ {
			h := getRequiresHash(i, j)
			if val, ok := mp[h]; ok {
				mp[h] = val + 1
				continue
			}
			mp[h] = 1
		}
	}

	// fmt.Printf("%v\n", mp)

	var res int64 = 0
	for i:=0; i<n; i++ {
		h := getHash(ar[i])
		val, ok := mp[h]
		if !ok {
			continue
		}
		res += int64(val) * int64(val-1) / 2
	}

	fmt.Println(res)
}

package main

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

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

func main() {
	defer out.Flush()

	var n, k int
	fmt.Fscan(in, &n, &k)
	c := make([][20]int8, n)
	exists := make(map[uint32]bool, n)
	for i := 0; i < n; i++ {
		var h uint32
		for j := 0; j < k; j++ {
			fmt.Fscan(in, &c[i][j])
			h = h*3 + uint32(c[i][j])
		}
		exists[h] = true
	}

	var ans int64
	for i := 0; i < n; i++ {
		var sets int64
		for j := 0; j < n; j++ {
			if i == j {
				continue
			}
			var h uint32
			for a := 0; a < k; a++ {
				h *= 3
				if c[i][a] != c[j][a] {
					h += 3 - uint32(c[i][a]+c[j][a])
				} else {
					h += uint32(c[i][a])
				}
			}
			if exists[h] {
				sets++
			}
		}
		sets /= 2
		ans += sets * (sets - 1) / 2
	}

	fmt.Fprintln(out, ans)
}


In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1 to 109. A toy from the new collection of the i-th type costs i bourles.Tania has managed to collect n different types of toys a1, a2, ..., an from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has.Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this.

The first line contains two integers n (1 ≤ n ≤ 100 000) and m (1 ≤ m ≤ 109) — the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys. The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the types of toys that Tanya already has.

In the first line print a single integer k — the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m. In the second line print k distinct space-separated integers t1, t2, ..., tk (1 ≤ ti ≤ 109) — the types of toys that Tanya should choose. If there are multiple answers, you may print any of them. Values of ti can be printed in any order.

NoteIn the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys.

package main

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

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	n := readInt(scanner)
	m := readInt(scanner)
	numbers := getMap(scanner, n)
	result := []int{}
	spentMoney := 0
	for i:=1;i<=300000;i++ {
		if m - spentMoney < i {
			break
		}
		if !numbers[i] {
			spentMoney += i
			result = append(result, i)
		}
	}
	fmt.Println(len(result))
	for i:=0;i<len(result);i++ {
		fmt.Print(result[i], " ")
	}
}
func getMap(scanner *bufio.Scanner, n int) map[int]bool {
	numbers := make(map[int]bool, n)
	for i:=0;i<n;i++ {
		value := readInt(scanner)
		numbers[value] = true
	}
	return numbers
}
func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	value, _ := strconv.Atoi(scanner.Text())
	return value
}

package main

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

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

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

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

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

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

func solve(n, m int) {
	in := make(map[int]bool)
	for i := 0; i < n; i++ {
		var t int
		I(&t)
		in[t] = true
	}
	var ans []int
	for i := 1; i <= 1e7; i++ {
		if !in[i] && m-i >= 0 {
			ans = append(ans, i)
			m -= i
		} else if m-i < 0 {
			break
		}
	}
	O(len(ans), "\n")
	for i := range ans {
		sep := " "
		if i == len(ans)-1 {
			sep = "\n"
		}
		O(ans[i], sep)
	}
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	k := 0
	var buy []int

	j := 0
	ok := true
	start := 1
	end := array[j]
	for m > 0 {
		for i := start; i < end; i++ {
			if i <= m {
				m -= i
				k++
				buy = append(buy, i)
			} else {
				ok = false
				break
			}
		}
		if !ok {
			break
		}
		j++
		start = end + 1
		if j < n {
			end = array[j]
		} else {
			end = 1000000000
		}
	}
	fmt.Println(k)
	fmt.Println(strings.Trim(fmt.Sprint(buy), "[]"))
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	k := 0
	var buy []int

	j := 0
	ok := true
	start := 1
	end := array[j]
	for m > 0 {
		for i := start; i < end; i++ {
			if i <= m {
				m -= i
				k++
				buy = append(buy, i)
			} else {
				ok = false
				break
			}
		}
		if !ok {
			break
		}
		j++
		start = end + 1
		if j < n {
			end = array[j]
		} else {
			end = 1000000001
		}
	}
	fmt.Println(k)
	fmt.Println(strings.Trim(fmt.Sprint(buy), "[]"))
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

// Codeforces 659 A
package main

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

var n,m int
var a sort.IntSlice

func main() {
	// reading n,m
	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])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
	m, err = strconv.Atoi(ss[1])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
    // read a
	s, err = in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		return
	}
	ss = strings.Split(strings.Trim(s, " \n\r"), " ")
    a = sort.IntSlice(make([]int, n))
    for i:=0; i<n; i++ {
        a[i],err = strconv.Atoi(ss[i])
        if err != nil {
		    fmt.Println("n atoi failure", err)
		    return
	    }
    }
    sort.Sort(a)

    // calc result
    nr:=0
    b := make([]int, 0, 100)
    ia := 0
    for i:=1; m>0; i++ {
        if ia<n && i==a[ia] {
            ia++
        } else if i<=m {
            b = append(b,i)
            nr++
            m -= i
        } else {
            break
        }
    }

	// print result
	fmt.Println(nr)
    for i:=0; i<nr; i++ {
        fmt.Printf("%d ", b[i])
    }
    fmt.Println()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	used := make(map[int]bool)
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(reader, &x)

		used[x] = true
	}

	var count, def int
	for i := 1; ; i++ {
		if count+i > m {
			break
		}
		if !used[i] && count+i <= m {
			count += i
			def++
			defer fmt.Print(i, " ")
		}

	}

	fmt.Println(def)

}

package main

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

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

	var n, m int
	fmt.Fscanf(bi, "%d %d\n", &n, &m)

	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(bi, "%d", &A[i])
	}

	sort.Ints(A)

	i, s, t := 0, int64(0), int64(0)
	for ; i < n; i++ {
		s = int64(A[i]-1)*int64(A[i])/2 - t
		if s >= int64(m) {
			break
		}
		t += int64(A[i])
	}

	k := 0
	if s != int64(m) {
		var a, b, c float64 = 1, 1, float64(-2 * (int64(m) + t))
		k = int((math.Sqrt(b*b-4*a*c)-b)/2/a) - i
	} else {
		k = A[i] - i - 1
	}

	fmt.Println(k)
	for i, j := 0, 1; k > 0; j++ {
		if i < n && A[i] == j {
			i++
		} else {
			fmt.Printf("%d ", j)
			k--
		}
	}
}

package main

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

var N int
var M int
var A map[int]bool

func main() {
	fsc := NewFastScanner()
	N = fsc.NextInt()
	M = fsc.NextInt()
	A = map[int]bool{}
	for i := 0; i < N; i++ {
		val := fsc.NextInt()
		A[val] = true
	}
	var ans []int
	for i := 1; i <= 100000000; i++ {
		if A[i] {
			continue
		}
		if M-i < 0 {
			break
		}
		M -= i
		ans = append(ans, i)
	}
	fmt.Println(len(ans))
	for i := 0; i < len(ans); i++ {
		fmt.Print(ans[i], " ")
	}
	fmt.Println()
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) Next() string {
	s.Pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}

func (s *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) NextInt64() int64 {
	val, err := strconv.ParseInt(s.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

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

	var n, m int
	fmt.Fscanf(bi, "%d %d\n", &n, &m)

	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(bi, "%d", &A[i])
	}

	sort.Ints(A)

	i, s, t := 0, 0, 0
	for ; i < n; i++ {
		s = (A[i]-1)*A[i]/2 - t
		if s >= m {
			break
		}
		t += A[i]
	}

	k := 0
	if s != m {
		var a, b, c float64 = 1, 1, float64(-2 * (m + t))
		k = int((math.Sqrt(b*b-4*a*c)-b)/2/a) - i
	} else {
		k = A[i] - i - 1
	}

	fmt.Println(k)
	for i, j := 0, 1; k > 0; j++ {
		if i < n && A[i] == j {
			i++
		} else {
			fmt.Printf("%d ", j)
			k--
		}
	}
}


Paprika loves permutations. She has an array $$$a_1, a_2, \dots, a_n$$$. She wants to make the array a permutation of integers $$$1$$$ to $$$n$$$.In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers $$$i$$$ ($$$1 \le i \le n$$$) and $$$x$$$ ($$$x > 0$$$), then perform $$$a_i := a_i \bmod x$$$ (that is, replace $$$a_i$$$ by the remainder of $$$a_i$$$ divided by $$$x$$$). In different operations, the chosen $$$i$$$ and $$$x$$$ can be different.Determine the minimum number of operations needed to make the array a permutation of integers $$$1$$$ to $$$n$$$. If it is impossible, output $$$-1$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output the minimum number of operations needed to make the array a permutation of integers $$$1$$$ to $$$n$$$, or $$$-1$$$ if it is impossible.

NoteFor the first test, the only possible sequence of operations which minimizes the number of operations is:   Choose $$$i=2$$$, $$$x=5$$$. Perform $$$a_2 := a_2 \bmod 5 = 2$$$. For the second test, it is impossible to obtain a permutation of integers from $$$1$$$ to $$$n$$$.

package main

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

func ceil(x, y int) int {
	return int(math.Ceil(float64(x) / float64(y)))
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c, v, k := []int{}, 0, true, 0, map[int]struct{}{}, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			_, ok := v[p]
			if !ok && p > 0 && p <= n {
				v[p] = struct{}{}
			} else {
				o = append(o, p)
			}
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			_, found := v[j]
			if found {
				delete(v, j)
				continue
			}
			if j < ceil(o[k], 2) {
				c++
			} else {
				ok = false
				break
			}
			k++
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
		w.Flush()
	}
}

func main() {
	solve()
}

package main

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

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)

		arr := []int{}
		m := make([]bool, n+1)
		m[0] = true
		req := n
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v <= n && !m[v] {
				m[v] = true
				req--
			} else {
				arr = append(arr, v)
			}
		}

		sort.Ints(arr)
		f := true
		i, pos := n, len(arr)-1
		for i >= 0 && pos >= 0 {
			if m[i] {
				i--
				continue
			}

			d := arr[pos]
			if (d%2 == 0 && i > d/2-1) || (d%2 != 0 && i > d/2) {
				f = false
				break
			}
			i--
			pos--
		}

		if !f {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, req)
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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(n int, A []int) int {
	sort.Ints(A)

	ok := make([]bool, n+2)

	for i := 0; i < n; i++ {
		if A[i] >= 1 && A[i] <= n && !ok[A[i]] {
			ok[A[i]] = true
			A[i] = 0
		}
	}
	var res int
	expect := 1

	for i := 0; i < n; i++ {
		if A[i] == 0 {
			continue
		}
		for ok[expect] {
			expect++
		}
		res++
		if 2*expect >= A[i] {
			return -1
		}
		ok[expect] = true
		expect++
	}

	for ok[expect] {
		expect++
	}

	if expect > n {
		return res
	}
	return -1
}

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 int
		Fscan(in, &n)
		if n == 1 {
			var v int
			Fscan(in, &v)
			if v == 2 {
				Fprintln(out, -1)
			} else if v == 1{
				Fprintln(out, 0)
			} else {
				Fprintln(out, 1)
			}
			return
		}

		mp := func(v int) int {
			v = (v - 1) / 2
			if v > n {
				v = n
			}
			return v
		}
		ori := make([]int, n+1)
		left := make([]int, n+1)
		for i := 0; i < n; i++ {
			var v int
			Fscan(in, &v)
			if v > n {
				v = mp(v)
				left[v]++
			} else {
				ori[v]++
			}
		}
		//for i := range a {
		//	Fscan(in, &a[i])
		//
		//}
		//sort.Ints(a)
		//sort.Ints(left)

		ans := 0
		for cur := n; cur > 0; cur-- {
			if ori[cur] == 0 {
				// use left
				if left[cur] == 0 {
					Fprintln(out, -1)
					return
				}
				ans++
				left[cur-1] += left[cur] - 1
			} else {
				left[cur-1] += left[cur]
				// use ori
				left[mp(cur)] += ori[cur] - 1

			}
		}
		Fprintln(out, ans)
	}

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

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

}

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

package main

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

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

const TAM = 1e5 + 100

var n int
var a [TAM]int
var ok [TAM]bool

func solve() int {
	var ans = 0
	var mex = 1
	for i := 0; i < n; i++ {
		var ai = a[i]
		if 1 <= ai && ai <= n && !ok[ai] {
			ok[ai] = true
			continue
		}
		for ok[mex] {
			mex++
		}
		if mex*2 < ai {
			ok[mex] = true
			ans += 1
		} else {
			return -1
		}
	}
	return ans
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			ok[i+1] = false
		}
		sort.Slice(a[0:n], func(i, j int) bool { return a[i] < a[j] })
		fmt.Fprintf(out, "%d\n", solve())
	}
	out.Flush()
}

package main

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

func ceil(x, y int) int {
	return int(math.Ceil(float64(x) / float64(y)))
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c := []int{}, 0, true, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			if j < ceil(o[j-1], 2) {
				c++
			} else if j == o[j-1] {
				continue
			} else {
				ok = false
				break
			}
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
		w.Flush()
	}
}

func main() {
	solve()
}

package main

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

func ceil(x, y int) int {
	return int(math.Ceil(float64(x) / float64(y)))
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c := []int{}, 0, true, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			if j < ceil(o[j-1], 2) {
				c++
			} else if j == o[j-1] {
				continue
			} else {
				ok = false
				break
			}
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, p, ok, c := []int{}, 0, true, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		for j := 1; j <= n; j++ {
			if o[j-1] != j && o[j-1]%(o[j-1]-j) != j {
				ok = false
				break
			} else {
				if !(o[j-1] == j) {
					c++
				}
			}
		}
		if !ok {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, c)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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)

		arr := make([]int, n)
		m := make([]bool, n+1)
		m[0] = true
		req := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] <= n {
				m[arr[i]] = true
				req--
			}
		}

		sort.Ints(arr)
		pos := sort.SearchInts(arr, n)
		if arr[pos] == n {
			pos++
		}

		var tot int
		for i, v := range m {
			if v {
				continue
			}

			if pos >= n {
				break
			}

			d := arr[pos]
			if (d%2 == 0 && i > d/2-1) || (d%2 != 0 && i > d/2) {
				break
			}
			pos++
			req--
			tot++
		}

		if req > 0 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, tot)
		}
	}
}

package main

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

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)

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

		sort.Ints(arr)
		f := true
		var tot int
		for i := 0; i < n; i++ {
			if arr[i]-1 == i {
				continue
			} else if (arr[i]%2 == 0 && i < arr[i]/2-1) || (arr[i]%2 != 0 && i < arr[i]/2) {
				tot++
			} else {
				f = false
				break
			}
		}

		if !f {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, tot)
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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(n int, A []int) int {
	sort.Ints(A)
	var res int
	expect := 1
	ok := make([]bool, n+1)
	for i := 0; i < n; i++ {
		if A[i] >= 1 && A[i] <= n && !ok[A[i]] {
			ok[A[i]] = true
			continue
		}
		for ok[expect] {
			expect++
		}
		res++
		if 2*expect >= A[i] {
			return -1
		}
		ok[expect] = true
		expect++
	}

	if expect > n {
		return res
	}
	return -1
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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(n int, A []int) int {
	sort.Ints(A)
	var res int
	for i := 1; i <= n; i++ {
		if A[i-1] < i {
			return -1
		}
		if A[i-1] == i {
			continue
		}
		if 2*i >= A[i-1] {
			return -1
		}
		res++
	}
	return res
}

package main

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

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		if n == 1 {
			var v int
			Fscan(in, &v)
			if v == 2 {
				Fprintln(out, -1)
			} else {
				Fprintln(out, 0)
			}
			return
		}
		mp := func(v int) int {
			v = (v - 1) / 2
			if v > n {
				v = n
			}
			return v
		}
		ori := make([]int, n+1)
		left := make([]int, n+1)
		for i := 0; i < n; i++ {
			var v int
			Fscan(in, &v)
			if v > n {
				v = mp(v)
				left[v]++
			} else {
				ori[v]++
			}
		}
		//for i := range a {
		//	Fscan(in, &a[i])
		//
		//}
		//sort.Ints(a)
		//sort.Ints(left)

		ans := 0
		for cur := n; cur > 0; cur-- {
			if ori[cur] == 0 {
				// use left
				if left[cur] == 0 {
					Fprintln(out, -1)
					return
				}
				ans++
				left[cur-1] += left[cur] - 1
			} else {
				left[cur-1] += left[cur]
				// use ori
				left[mp(cur)] += ori[cur] - 1

			}
		}
		Fprintln(out, ans)
	}

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

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

}

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


During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $$$n$$$ high school students numbered from $$$1$$$ to $$$n$$$. Initially, each student $$$i$$$ is on position $$$i$$$. Each student $$$i$$$ is characterized by two numbers — $$$a_i$$$ and $$$b_i$$$. Dissatisfaction of the person $$$i$$$ equals the product of $$$a_i$$$ by the number of people standing to the left of his position, add the product $$$b_i$$$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $$$i$$$, which is on the position $$$j$$$, equals $$$a_i \cdot (j-1) + b_i \cdot (n-j)$$$.The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.Although Stas is able to solve such problems, this was not given to him. He turned for help to you.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of people in the queue. Each of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq 10^8$$$) — the characteristic of the student $$$i$$$, initially on the position $$$i$$$.

Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.

NoteIn the first example it is optimal to put people in this order: ($$$3, 1, 2$$$). The first person is in the position of $$$2$$$, then his dissatisfaction will be equal to $$$4 \cdot 1+2 \cdot 1=6$$$. The second person is in the position of $$$3$$$, his dissatisfaction will be equal to $$$2 \cdot 2+3 \cdot 0=4$$$. The third person is in the position of $$$1$$$, his dissatisfaction will be equal to $$$6 \cdot 0+1 \cdot 2=2$$$. The total dissatisfaction will be $$$12$$$.In the second example, you need to put people in this order: ($$$3, 2, 4, 1$$$). The total dissatisfaction will be $$$25$$$.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	b := make([]int, n)
	c := make([]int, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a[i], &b[i])
		ans += int64(b[i])*int64(n) - int64(a[i])
		c[i] = a[i] - b[i]
	}
	sort.Ints(c)
	for j := 1; j <= n; j++ {
		ans += int64(c[j-1]) * int64(n-j+1)
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}

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

package main

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

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

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

func getLine() string {
	s, _ := reader.ReadString('\n')
	if strings.HasSuffix(s, "\r\n") {
		s = s[:len(s)-2]
	} else if strings.HasSuffix(s, "\n") {
		s = s[:len(s)-1]
	}
	return s
}
func getLine2Int() (n int) {
	scanf("%d\n", &n)
	return
}
func getLine2Int2() (a, b int64) {
	scanf("%d %d\n", &a, &b)
	return
}
func getLine2Ints(n int) []int {
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &ans[i])
	}
	scanf("%d\n", &ans[n-1])
	return ans
}

var debug = false

func see(a interface{}) {
	if debug {
		printf("DEBUG:%v\n", a)
	}
}

func main() {
	defer writer.Flush()

	n := getLine2Int()
	diff := make([]int64, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		a, b := getLine2Int2()
		diff[i] = b - a
		ans += a * int64(n-1)
	}
	sort.Slice(diff, func(i, j int) bool { return diff[i] < diff[j] })
	for i := 0; i < n; i++ {
		ans += diff[i] * int64(n-i-1)
	}
	printf("%d\n", ans)
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/

type Pair struct {
	a int64
	b int64
}

type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p.a*(jj-1) + p.b*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0, sdtNum/3+1)
	q2 := make([]Pair, 0, sdtNum/3+1)
	q3 := make([]Pair, 0, sdtNum/3+1)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i].a - q1[i].b) > (q1[j].a - q1[j].b)
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i].b - q2[i].a) < (q2[j].b - q2[j].a)
	})

	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/
type Pair [2]int64
type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p[0]*(jj-1) + p[1]*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0)
	q2 := make([]Pair, 0)
	q3 := make([]Pair, 0)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i][0] - q1[i][1]) > (q1[j][0] - q1[j][1])
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i][1] - q2[i][0]) < (q2[j][1] - q2[j][0])
	})
	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1151D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var b int
	var n, ans int64
	Fscan(in, &n)
	a := make(sort.IntSlice, n)
	for i := range a {
		Fscan(in, &a[i], &b)
		a[i] -= b
		ans += int64(b)
	}
	ans *= n - 1
	sort.Sort(sort.Reverse(a))
	for i, v := range a {
		ans += int64(i) * int64(v)
	}
	Fprint(out, ans)
}

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

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/

type Pair struct {
	a int
	b int
}

type Queue []Pair

func (q Queue) Score() (score int) {
	n := len(q)
	for j, p := range q {
		j += 1
		score += p.a*(j-1) + p.b*(n-j)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0, sdtNum/3+1)
	q2 := make([]Pair, 0, sdtNum/3+1)
	q3 := make([]Pair, 0, sdtNum/3+1)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt(s)
		b, _ := ScanInt(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i].a - q1[i].b) > (q1[j].a - q1[j].b)
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i].b - q2[i].a) < (q2[j].b - q2[j].a)
	})

	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/
type Pair [2]int64
type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p[0]*(jj-1) + p[1]*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0)
	q2 := make([]Pair, 0)
	q3 := make([]Pair, 0)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i][0] - q1[i][1]) > (q1[j][0] - q1[j][1])
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i][0] - q2[i][1]) < (q2[j][0] - q2[j][1])
	})
	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}