A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).

For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.

NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.

package main

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

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func bitCount(a int64) int {
	count := 0
	for a > 0 {
		count++
		a &= a - 1
	}
	return count
}
func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	resource := make([]int64, 14)
	resource[0] = 1
	for i := int64(1); i < 14; i++ {
		resource[i] = resource[i-1] * (i + 1)
	}
	for ii := 0; ii < t; ii++ {
		var n int64
		fmt.Fscan(in, &n)
		ans := bitCount(n)
		for i := int64(1); i <= 1<<14; i++ {
			b := bitCount(i)
			temp := int64(0)
			for j := 0; j < len(resource); j++ {
				if (1<<j)&i > 0 {
					temp += resource[j]
				}
			}
			if temp > n {
				continue
			}
			if tt := bitCount(n-temp) + b; tt < ans {
				ans = tt
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	factorials := make([]int64, 0, 30)
	for i := 0; i < cap(factorials); i++ {
		if i == 0 {
			factorials = append(factorials, 1)
		} else {
			factorials = append(factorials, factorials[i-1]*int64(i))
		}
		if factorials[i] > 1_000_000_000_000 {
			break
		}
	}
	for ; t > 0; t-- {
		var n int64
		fmt.Fscan(in, &n)

		ind := 0
		for i := len(factorials) - 1; i >= 0; i-- {
			if factorials[i] <= n {
				ind = i
				break
			}
		}

		answers := make([]int, 0)
		solve(ind, 0, n, factorials, &answers)
		mini := 0
		for i := range answers {
			if answers[mini] > answers[i] {
				mini = i
			}
		}

		res := answers[mini]
		fmt.Fprintln(out, res)
	}
}

func solve(i int, res int, temp int64, facts []int64, answers *[]int) {
	if i == -1 {
		for temp != 0 {
			if temp&1 == 1 {
				res++
			}
			temp >>= 1
		}
		*answers = append(*answers, res)
		return
	}

	if temp-facts[i] >= 0 {
		solve(i-1, res+1, temp-facts[i], facts, answers)
	}

	solve(i-1, res, temp, facts, answers)
}

func isPowerOfTwo(x int64) bool {
	if x == 0 {
		return false
	}
	if x == 1 {
		return true
	}
	return x&(x-1) == 0
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	facts := make([]int64, 15)
	facts[0] = 1
	for i := 1; i < 15; i++ {
		sum := int64(1)
		for k := 1; k <= i+1; k++ {
			sum *= int64(k)
		}
		facts[i] = sum
	}

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

		min := 1000
		var rc func(k int, arr []int, sum int64)
		rc = func(k int, arr []int, sum int64) {
			if sum > n {
				return
			}

			var start int
			if len(arr) > 0 {
				start = arr[len(arr)-1]
			}

			for i := start + 1; i <= k; i++ {
				rc(k, append(arr, i), sum+facts[i-1])
			}

			z := bits.OnesCount64(uint64(n - sum))
			if len(arr)+z < min {
				min = len(arr) + z
			}
		}

		rc(15, []int{}, 0)
		fmt.Fprintln(out, min)
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	facts := make([]int64, 15)
	facts[0] = 1
	for i := 1; i < 15; i++ {
		sum := int64(1)
		for k := 1; k <= i+1; k++ {
			sum *= int64(k)
		}
		facts[i] = sum
	}

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

		min := 1000
		var rc func(k int, arr []int)
		rc = func(k int, arr []int) {
			var start int
			if len(arr) > 0 {
				start = arr[len(arr)-1]
			}

			for i := start + 1; i <= k; i++ {
				rc(k, append(arr, i))
			}

			var sum int64
			for _, v := range arr {
				sum += facts[v-1]
			}

			if sum > n {
				return
			}

			z := bits.OnesCount64(uint64(n - sum))
			if len(arr)+z < min {
				min = len(arr) + z
			}
		}

		rc(15, []int{})
		fmt.Fprintln(out, min)
	}
}

package main

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

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

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

	//预处理所有乘阶
	pre := make([]uint64, 15)
	pre[1] = 1
	for i := uint64(2); i < 15; i++ {
		pre[i] = pre[i-1] * i
	}

	var c int
	var n uint64
	var ans int
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		ans = recusion(n, pre, 3, 0)
		Fprintf(out, "%d\n", ans)
	}
}

func recusion(n uint64, pre []uint64, pos int, cnt int) int {
	if n == 0 {
		return cnt
	}
	var tmp int
	if k := GetBit(n); k == 1 {
		return cnt + 1
	} else {
		tmp = cnt + k
	}
	for i, v := range pre {
		if i >= pos && v <= n {
			tmp = min(tmp, recusion(n-v, pre, i+1, cnt+1))
		}
	}
	return tmp
}

func GetBit(n uint64) (cnt int) {
	for i := 0; i <= 66; i++ {
		if n&1 == 1 {
			cnt++
		}
		n >>= 1
	}
	return
}

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

package main

import (
	"bufio"
	. "fmt"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	f := []uint64{2}
	for i := uint64(3); f[len(f)-1]*i <= 1e12; i++ {
		f = append(f, f[len(f)-1]*i)
	}

	var T, n uint64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		ans := 99
		for i := 0; i < 1<<len(f); i++ {
			s, c := n, 0
			for j, v := range f {
				if i>>j&1 > 0 {
					s -= v
					c++
				}
			}
			if s >= 0 {
				c += bits.OnesCount64(s)
				if c < ans {
					ans = c
				}
			}
		}
		Fprintln(out, ans)
	}
}

package main

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

/*
5
4
5
123456789121
324903456974
982373245346
====
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
29
20
======
2
654654
654655
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int64
	scanf("%d\n", &n)

	powNums := make([]int64, 0)

	{
		num := int64(1)
		i := int64(2)

		for num <= 1e12 {
			powNums = append(powNums, num)

			num *= i
			i++
		}
	}

	minCnt := helper(n, 0, powNums)

	if minCnt >= 100 {
		minCnt = -1
	}

	fmt.Fprintln(writer, minCnt)
}

func helper(num int64, i int, powNums []int64) int {
	if i == len(powNums) {
		return bits.OnesCount64(uint64(num))
	}

	minCnt := helper(num, i+1, powNums)

	nextNum := num - powNums[i]
	if nextNum >= 0 {
		minCnt = min(minCnt, helper(nextNum, i+1, powNums)+1)
	}

	return minCnt
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) (max, idx int) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = i
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

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

func main() {
	defer out.Flush()

	const limit int64 = 1e12
	facts := []int64{6}
	for i := int64(4); facts[len(facts)-1] <= limit; i++ {
		facts = append(facts, facts[len(facts)-1]*i)
	}
	f := len(facts)
	fm := 1 << f

	var tc int
	for fmt.Fscan(in, &tc); tc > 0; tc-- {
		var n uint64
		fmt.Fscan(in, &n)
		mn := bits.OnesCount64(n)
		for m := 1; m <= fm; m++ {
			rem := n
			for i := 0; i < f; i++ {
				if ((1<<i)&m) != 0 && rem >= uint64(facts[i]) {
					rem -= uint64(facts[i])
				}
			}
			b := bits.OnesCount64(rem) + bits.OnesCount(uint(m))
			if b < mn {
				mn = b
			}
		}
		fmt.Fprintln(out, mn)
	}
}

package main

import "fmt"

var factorials []int64

func countBits(x int64) int {
	result := 0
	for x != 0 {
		result++
		x &= (x - 1)
	}
	return result
}

func findMinElem(curIdx, spent int, remain int64) int {
	if curIdx >= len(factorials) {
		return spent + countBits(remain)
	}

	result := findMinElem(curIdx+1, spent, remain)
	if remain >= factorials[curIdx] {
		candi := findMinElem(curIdx+1, spent+1, remain-factorials[curIdx])
		if candi < result {
			result = candi
		}
	}
	return result
}

func solve() {
	var n int64
	fmt.Scan(&n)

	fmt.Println(findMinElem(0, 0, n))
}

func main() {
	var t int
	fmt.Scan(&t)

	var cur int64 = 2
	for i := int64(3); i <= 14; i++ {
		cur *= i
		factorials = append(factorials, cur)
	}
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"fmt"
)

const mn = 1<<15 + 5

var (

	t int
	n int64
	sum [mn]int64
	f [19]int64
)

func Init() {

	f[0] = 1
	for i := 1; i < 16; i++ {

		f[i] = f[i-1] * int64(i)
	}

	for i := 0; i < (1<<14); i++ {

		for j := 0; j < 14; j++ {

			if ((i>>j)&1 == 1) {

				sum[i] += f[j + 2]
			}
		}
	}

}

func Min(a, b int) int {

	if a < b {

		return a
	}

	return b
}

func Sol() {

	var ans int = 50

	for i := 0; i < (1<<14); i++ {

		cnt := 0
		if sum[i] <= n {

			for j := 0; j < 15; j++ {

				cnt += ((i>>j)&1)
			}

			var k int64 = n - sum[i]
			for j := 0; j < 40; j++ {

				cnt += int((k>>j) & 1)
			}

			ans = Min(ans, cnt);
		}
	}

	fmt.Println(ans);
}

func main() {

	Init()
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {

		fmt.Scan(&n)
		Sol()
	}
}

package main

import "fmt"
import "math/bits"

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

func dfs(k uint64, i uint64, prod uint64) (int) {
    ret := bits.OnesCount64(k)
    if prod > k {
        return ret
    }
    ret = Min(ret, dfs(k - prod, i + 1, prod * i) + 1)
    ret = Min(ret, dfs(k, i + 1, prod * i))
    return ret
}
func main() {
    var T int
    fmt.Scanf("%d", &T)
    for tc := 1; tc <= T; tc += 1 {
        var k uint64
        fmt.Scan(&k)
        fmt.Printf("%d\n", dfs(k, 1, 1))
    }
}

package main

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

func ones(n int64) int {
	cnt := 0
	for n > 0 {
		cnt += int(n & int64(1))
		n >>= 1
	}
	return cnt
}

func solve(scanner *bufio.Scanner) {
	scanner.Scan()
	n, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	cur := int64(1)
	tot := int64(1)
	fac := make([]int64, 0)
	for cur*tot <= n {
		fac = append(fac, cur*tot)
		tot *= cur
		cur++
	}
	ans := []int{math.MaxInt}
	dfs(fac, ans, 0, 0, n)
	if ans[0] == math.MaxInt {
		fmt.Println(-1)
	} else {
		fmt.Println(ans[0])
	}
}

func dfs(fac []int64, ans []int, i, cnt int, n int64) {
	if i >= len(fac) {
		ans[0] = int(math.Min(float64(ans[0]), float64(ones(n)+cnt)))
		return
	}
	if fac[i] <= n {
		dfs(fac, ans, i+1, cnt+1, n-fac[i])
	}
	dfs(fac, ans, i+1, cnt, n)
}

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

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())

	for i := 0; i < t; i++ {
		solve(scanner)
	}
}

package main

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

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	a := flag.String("test", "1", "")
	flag.Parse()
	fmt.Println(*a)
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	var resource []int64
	var res int64 = 1
	for i := int64(1); i <= 14; i++ {
		res *= i
		resource = append(resource, res)
	}
	res = 2
	for i := 1; i < 47; i++ {
		res <<= 1
		resource = append(resource, res)
	}
	sort.Slice(resource, func(i, j int) bool {
		return resource[i] < resource[j]
	})
	cache := map[int64]int{-1: -1, 0: 0}
	for i := 0; i < len(resource); i++ {
		cache[resource[i]] = 1
	}
	var dfs func(i int, target int64) int
	dfs = func(i int, target int64) int {
		if ans, ok := cache[target*60+int64(i)]; ok {
			return ans
		}
		ans := -1
		for ; i >= 0; i-- {
			if target < resource[i] {
				continue
			}
			if target == resource[i] {
				return 1
			}
			a := dfs(i-1, target-resource[i])
			if a == -1 {
				continue
			}
			if a+1 < ans || ans == -1 {
				ans = a + 1
			}
		}
		cache[target*60+int64(i)] = ans
		return ans
	}
	for ii := 0; ii < t; ii++ {
		var n int64
		fmt.Fscan(in, &n)
		fmt.Fprintln(out, dfs(len(resource)-1, n))
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	factorials := make([]int64, 0, 30)
	for i := 0; i < cap(factorials); i++ {
		if i == 0 {
			factorials = append(factorials, 1)
		} else {
			factorials = append(factorials, factorials[i-1]*int64(i))
		}
		if factorials[i] > 1_000_000_000_000 {
			break
		}
	}
	for ; t > 0; t-- {
		var n, m int64
		fmt.Fscan(in, &n)
		m = n

		res := 0
		for i := len(factorials) - 1; i >= 0; i-- {
			if isPowerOfTwo(n) {
				res++
				n = 0
				break
			}
			if factorials[i] <= n {
				n -= factorials[i]
				res++
			}
		}

		if isPowerOfTwo(n) {
			res++
			n = 0
		}
		for n != 0 {
			if n&1 == 1 {
				res++
			}
			n >>= 1
		}

		res2 := 0
		for m != 0 {
			if m&1 == 1 {
				res2++
			}
			m >>= 1
		}

		if res2 < res {
			res = res2
		}

		fmt.Fprintln(out, res)
	}
}

func isPowerOfTwo(x int64) bool {
	if x == 0 {
		return false
	}
	if x == 1 {
		return true
	}
	return x&(x-1) == 0
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	facts := []int64{}
	c := int64(1)
	for {
		sum := int64(1)
		for i := int64(1); i <= c; i++ {
			sum *= i
			if sum > 10e12 {
				break
			}
		}

		if sum > 10e12 {
			break
		}
		facts = append(facts, sum)
		c++
	}

	pows := []int64{}
	x := int64(1)
	for x <= 10e12 {
		pows = append(pows, x)
		x *= 2
	}

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

		var k int
		fp, pp := len(facts), len(pows)
		for n > 0 {
			for i := fp - 1; i >= 0; i-- {
				if facts[i] <= n {
					fp = i
					break
				}
			}

			for i := pp - 1; i >= 0; i-- {
				if pows[i] <= n {
					pp = i
					break
				}
			}

			if facts[fp] > pows[pp] {
				n -= facts[fp]
			} else {
				n -= pows[pp]
			}
			k++
		}

		fmt.Fprintln(out, k)
	}
}

package main

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

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

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

	//预处理所有乘阶
	pre := make([]uint64, 15)
	pre[1] = 1
	for i := uint64(2); i < 15; i++ {
		pre[i] = pre[i-1] * i
	}

	var c int
	var n uint64
	var ans int
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		ans = recusion(n, pre, 1, 0)
		Fprintf(out, "%d\n", ans)
	}
}

func recusion(n uint64, pre []uint64, pos int, cnt int) int {
	if n == 0 {
		return cnt
	}
	var tmp int
	if k := GetBit(n); k == 1 {
		return cnt + 1
	} else {
		tmp = cnt + k
	}
	for i, v := range pre {
		if i >= pos && v <= n {
			tmp = min(tmp, recusion(n-v, pre, i+1, cnt+1))
		}
	}
	return tmp
}

func GetBit(n uint64) (cnt int) {
	for i := 0; i <= 35; i++ {
		if n&1 == 1 {
			cnt++
		}
		n >>= 1
	}
	return
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1646C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	f := []int64{1}
	for i := int64(2); f[len(f)-1]*i <= 1e12; i++ {
		f = append(f, f[len(f)-1]*i)
	}

	var T, n int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		ans := 99
		for i := 0; i < 1<<len(f); i++ {
			s, c := n, 0
			for j, v := range f {
				if i>>j&1 > 0 {
					s -= v
					c++
				}
			}
			if s >= 0 {
				c += bits.OnesCount(uint(s))
				if c < ans {
					ans = c
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

func main() {
	defer out.Flush()

	const limit int64 = 10000 //xxx
	facts := []int64{6}
	for i := int64(4); facts[len(facts)-1] <= limit; i++ {
		facts = append(facts, facts[len(facts)-1]*i)
	}
	f := len(facts)
	fm := 1 << f

	var tc int
	for fmt.Fscan(in, &tc); tc > 0; tc-- {
		var n uint64
		fmt.Fscan(in, &n)
		mn := bits.OnesCount64(n)
		for m := 1; m <= fm; m++ {
			rem := n
			for i := 0; i < f; i++ {
				if ((1<<i)&m) != 0 && rem >= uint64(facts[i]) {
					rem -= uint64(facts[i])
				}
			}
			b := bits.OnesCount64(rem) + bits.OnesCount(uint(m))
			if b < mn {
				mn = b
			}
		}
		fmt.Fprintln(out, mn)
	}
}


Polycarp was recently given a set of $$$n$$$ (number $$$n$$$ — even) dominoes. Each domino contains two integers from $$$1$$$ to $$$n$$$.Can he divide all the dominoes into two sets so that all the numbers on the dominoes of each set are different? Each domino must go into exactly one of the two sets.For example, if he has $$$4$$$ dominoes: $$$\{1, 4\}$$$, $$$\{1, 3\}$$$, $$$\{3, 2\}$$$ and $$$\{4, 2\}$$$, then Polycarp will be able to divide them into two sets in the required way. The first set can include the first and third dominoes ($$$\{1, 4\}$$$ and $$$\{3, 2\}$$$), and the second set — the second and fourth ones ($$$\{1, 3\}$$$ and $$$\{4, 2\}$$$).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single even integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of dominoes. The next $$$n$$$ lines contain pairs of numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$) describing the numbers on the $$$i$$$-th domino. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print:   YES, if it is possible to divide $$$n$$$ dominoes into two sets so that the numbers on the dominoes of each set are different;  NO if this is not possible.  You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).

NoteIn the first test case, the dominoes can be divided as follows:   First set of dominoes: $$$[\{1, 2\}, \{4, 3\}]$$$  Second set of dominoes: $$$[\{2, 1\}, \{3, 4\}]$$$  In other words, in the first set we take dominoes with numbers $$$1$$$ and $$$2$$$, and in the second set we take dominoes with numbers $$$3$$$ and $$$4$$$.In the second test case, there's no way to divide dominoes into $$$2$$$ sets, at least one of them will contain repeated number.

package main

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

type domin struct {
	a, b int
}

// https://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D0%9D%D0%9C_(%D0%BD%D0%B0%D0%B8%D0%B2%D0%BD%D1%8B%D0%B5_%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8)
func main() {
	in := bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		n := 0
		fmt.Fscan(in, &n)

		dominos := make(map[int][]int, n)

		var failed bool
		for N := n; N > 0; N-- {
			a, b := 0, 0
			fmt.Fscan(in, &a, &b)

			dominos[a] = append(dominos[a], b)
			dominos[b] = append(dominos[b], a)

			if a == b || len(dominos[a]) > 2 || len(dominos[b]) > 2 {
				failed = true
			}
		}

		if failed {
			fmt.Println("no")
			continue
		}

		used := make(map[int]bool)
		var fn func(v int) int

		fn = func(v int) int {
			used[v] = true
			for _, now := range dominos[v] {
				if !used[now] {
					return fn(now) + 1
				}
			}
			return 1
		}

		for i := 0; i < n; i++ {
			if !used[i+1] {
				if fn(i+1)%2 > 0 {
					failed = true
					break
				}
			}
		}

		if failed {
			fmt.Println("no")
		} else {
			fmt.Println("yes")
		}
	}
}

package main

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

type Domino struct {
	u int
	v int
}

func exist(set map[int]bool, domino *Domino) bool {
	if _, ok := set[domino.u]; ok {
		return true
	}
	if _, ok := set[domino.v]; ok {
		return true
	}
	return false
}

func solve(dominoes []*Domino) bool {
	sort.Slice(dominoes, func(i, j int) bool {
		if dominoes[i].u == dominoes[j].u {
			return dominoes[i].v < dominoes[j].v
		}
		return dominoes[i].u < dominoes[j].u
	})

	first := map[int]bool{}
	second := map[int]bool{}

	used := map[int]bool{}

	for {
		canPut := false

		for i, domino := range dominoes {
			if _, ok := used[i]; ok {
				continue
			}
			if domino.u == domino.v {
				return false
			}

			existFirst := exist(first, domino)
			existSecond := exist(second, domino)
			if !existFirst && existSecond {
				first[domino.u] = true
				first[domino.v] = true
				used[i] = true
				canPut = true
			} else if existFirst && !existSecond {
				second[domino.u] = true
				second[domino.v] = true
				used[i] = true
				canPut = true
			} else if existFirst && existSecond {
				return false
			} else {
				continue
			}
		}

		if !canPut {
			for i, domino := range dominoes {
				if _, ok := used[i]; ok {
					continue
				}
				first[domino.u] = true
				first[domino.v] = true
				used[i] = true
				break
			}
		}

		if len(used) == len(dominoes) {
			break
		}
	}

	return true
}

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

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

	for test > 0 {
		test--

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

		dominoes := make([]*Domino, n)
		for i := 0; i < n; i++ {
			var u, v int
			fmt.Fscanln(inp, &u, &v)
			if u > v {
				u, v = v, u
			}
			dominoes[i] = &Domino{u: u, v: v}
		}

		res := solve(dominoes)

		if res {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}

	out.Flush()
}

package main

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

type DSU struct {
	par []int
}

func NewDSU(n int) *DSU {
	d := &DSU{
		par: make([]int, n),
	}
	for i := 0; i < n; i++ {
		d.par[i] = i
	}
	return d
}

func (d *DSU) Find(a int) int {
	if d.par[a] == a {
		return a
	}
	d.par[a] = d.Find(d.par[a])
	return d.par[a]
}

func (d *DSU) Unit(a, b int) {
	if d.Find(a) == d.Find(b) {
		return
	}
	d.par[d.Find(a)] = d.Find(b)
}

func solve(in io.Reader, out io.Writer) {
	var n int
	fmt.Fscan(in, &n)
	cnt := make([]int, n)
	d := NewDSU(2 * n)
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		cnt[a]++
		cnt[b]++
		d.Unit(a, b+n)
		d.Unit(b, a+n)
	}
	ok := true
	for i := 0; i < n; i++ {
		if cnt[i] > 2 {
			ok = false
		}
		if d.Find(i) == d.Find(i+n) {
			ok = false
		}
	}
	if ok {
		fmt.Fprintln(out, "YES")
	} else {
		fmt.Fprintln(out, "NO")
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	// in, _ := os.Open("in.txt")
	t := 1
	fmt.Fscan(in, &t)
	for t > 0 {
		solve(in, os.Stdout)
		t--
	}
}

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

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

package main

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

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

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

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

func do(d [][2]int) string {
	n := len(d)
	f := make(map[int]int)
	m := make(map[int]int)
	for i, v := range d {
		f[i+n+1] = i + n + 1
		x, y := v[0], v[1]
		if x == y {
			return "NO"
		}
		m[x]++
		m[y]++
		if m[x] > 2 || m[y] > 2 {
			return "NO"
		}
		if f[x] == 0 {
			f[x] = i + n + 1
		}
		if f[y] == 0 {
			f[y] = i + n + 1
		}
		fx := find(f, x)
		fy := find(f, y)
		if fx != fy {
			f[fy] = fx
		}
	}
	c := make(map[int]int)
	for i := 1; i <= n; i++ {
		c[find(f, i)]++
	}
	// log.Print(f)
	for _, v := range c {
		if v%2 == 1 {
			return "NO"
		}
	}
	return "YES"
}

func find(f map[int]int, x int) int {
	if f[x] == x {
		return x
	}
	fx := find(f, f[x])
	f[x] = fx
	return fx
}

package main

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

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

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	d := make([][2]int, n)
	cnt := make([]int, n+1)
	for i := range d {
		fmt.Fscan(rr, &d[i][0], &d[i][1])
	}
	hash := make([][]int, n+1)
	for i := range d {
		cnt[d[i][0]]++
		cnt[d[i][1]]++
		if cnt[d[i][0]] > 2 || cnt[d[i][1]] > 2 || d[i][0] == d[i][1] {
			fmt.Fprintln(ww, "NO")
			return
		}
		hash[d[i][0]] = append(hash[d[i][0]], d[i][1])
		hash[d[i][1]] = append(hash[d[i][1]], d[i][0])
	}
	processed := make(map[int]int)
	outer:
	for i:=1;i<=n;i++ {
		if processed[i] > 0 {
			continue
		}
		j := 0
		processed[i] = i
		cur := i
		for {
			for k := range hash[cur] {
				if processed[hash[cur][k]] == 0 {
					cur = hash[cur][k]
					break
				}
				if k == 1 {
					if j % 2 == 0 {
						fmt.Fprintln(ww, "NO")
						return
					}
					continue outer
				}
			}
			j++
			processed[cur] = i
		}
	}
	fmt.Fprintln(ww, "YES")

}

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

// 1702E. Split Into Two Sets
package main

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

type Dsu struct {
	p []int
}

func (d Dsu) get(x int) int {
	if x == d.p[x] {
		return x
	} else {
		d.p[x] = d.get(d.p[x])
		return d.p[x]
	}
}
func (d Dsu) unite(x int, y int) bool {
	x = d.get(x)
	y = d.get(y)
	if x != y {
		d.p[x] = y
		return true
	}
	return false
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, a, b int
		fmt.Fscan(reader, &n)
		possible := true
		deg := make([]int, n)
		dsu := Dsu{}
		dsu.p = make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			dsu.p[i] = i
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a, &b)
			a--
			b--
			deg[a]++
			deg[b]++
			dsu.unite(a, b+n)
			dsu.unite(a+n, b)
		}
		for i := 0; i < n; i++ {
			if deg[i] > 2 {
				possible = false
			}
			if dsu.get(i) == dsu.get(i+n) {
				possible = false
			}
		}
		if possible {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

func calCycleLen(cur int, adjs map[int][]int, visited []bool) int {
	adj, ok := adjs[cur]
	if !ok || len(adj) != 2 {
		panic("impossible")
	}

	visited[cur] = true
	result := 1

	first, second := adj[0], adj[1]
	if !visited[first] {
		result += calCycleLen(first, adjs, visited)
	} else if !visited[second] {
		result += calCycleLen(second, adjs, visited)
	} // visited[first] && visited[second], do nothing
	return result
}

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

	adjs := make(map[int][]int)
	for i := 0; i < n; i++ {
		var ai, bi int
		fmt.Fscan(in, &ai)
		ai--
		fmt.Fscan(in, &bi)
		bi--

		adjAi := adjs[ai]
		adjAi = append(adjAi, bi)
		adjs[ai] = adjAi

		adjBi := adjs[bi]
		adjBi = append(adjBi, ai)
		adjs[bi] = adjBi
	}

	if len(adjs) != n {
		fmt.Println("NO")
		return
	}
	for _, v := range adjs {
		if len(v) != 2 {
			fmt.Println("NO")
			return
		}
	}

	visited := make([]bool, n)
	for i := 0; i < n; i++ {
		if visited[i] {
			continue
		}
		if calCycleLen(i, adjs, visited)%2 == 1 {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
}

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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 scanws(a ...interface{})           { fmt.Fscan(reader, a...) }

var dn [200005]int
var dd [200005][2]int
var vis [200005]bool

func DFS(indx int, cnt *int) {

	vis[indx] = true
	for i := 0; i < 2; i++ {
		idx := dd[indx][i]
		if vis[idx] == false {
			*cnt = *cnt + 1
			vis[idx] = true
			DFS(idx, cnt)
		}
	}
}

func main() {

	defer writer.Flush()
	var tc int
	var n, a, b int

	scanws(&tc)
	for t := 1; t <= tc; t++ {
		scanws(&n)
		for i := 1; i <= n; i++ {
			dn[i] = 0
			dd[i][0] = -1
			dd[i][1] = -1
			vis[i] = false
		}
		var flag bool = true
		for i := 0; i < n; i++ {
			scanws(&a, &b)
			if a == b {
				flag = false
				continue
			}
			if dn[a] == 2 || dn[b] == 2 {
				flag = false
				continue
			}
			aa := dn[a]
			bb := dn[b]
			dd[a][aa] = b
			dd[b][bb] = a
			dn[a]++
			dn[b]++
		}
		if flag == false {
			printf("NO\n")
		} else {
			for i := 1; i <= n; i++ {
				if vis[i] == false {
					cnt := 1
					vis[i] = true
					DFS(i, &cnt)
					if cnt%2 == 1 {
						flag = false
					}
				}
			}
			if flag == true {
				printf("YES\n")
			} else {
				printf("NO\n")
			}
		}
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		dominoes := make([][]int, n)
		for i := 0; i < n; i++ {
			dominoes[i] = readNNums(reader, 2)
		}
		res := solve(dominoes)

		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(dominoes [][]int) bool {
	n := len(dominoes)
	// 每个节点最多两条边
	g := NewGraph(n+1, 2*n)

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

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

	for i := 0; i < n; i++ {
		domino := dominoes[i]
		if domino[0] == domino[1] {
			return false
		}
		a, b := domino[0], domino[1]
		if cnt[a] > 1 || cnt[b] > 1 {
			return false
		}
		cnt[a]++
		cnt[b]++

		if pos[a] > 0 {
			g.AddEdge(i+1, pos[a])
			g.AddEdge(pos[a], i+1)
		}
		if pos[b] > 0 {
			g.AddEdge(i+1, pos[b])
			g.AddEdge(pos[b], i+1)
		}
		pos[a] = i + 1
		pos[b] = i + 1
	}

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

	var dfs func(u int, c int) bool

	dfs = func(u int, c int) bool {
		if color[u] == c {
			return true
		}

		if color[u] != 0 {
			return false
		}

		color[u] = c
		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if !dfs(v, -c) {
				return false
			}
		}
		return true
	}

	for u := 1; u <= n; u++ {
		if color[u] == 0 {
			if !dfs(u, 1) {
				return false
			}
		}
	}
	return true
}

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

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

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

package main

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

var (
	reader *bufio.Reader
	used   []bool
	edges  [][]int
)

func main() {
	var (
		t int
		n int
	)

	reader = bufio.NewReader(os.Stdin)

	_, _ = fmt.Fscanln(reader, &t)

	for t > 0 {
		t--
		_, _ = fmt.Fscanln(reader, &n)
		solve(n)
	}
}

func solve(n int) {
	var (
		a    int
		b    int
		fail bool
	)

	edges = make([][]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanln(reader, &a, &b)

		edges[a-1] = append(edges[a-1], b-1)
		edges[b-1] = append(edges[b-1], a-1)

		if a == b || len(edges[a-1]) > 2 || len(edges[b-1]) > 2 {
			fail = true
		}
	}

	if fail {
		//log.Print("failed by first stage")
		fmt.Println("NO")
		return
	}

	used = make([]bool, n)
	for i := 0; i < n; i++ {
		if !used[i] {
			r := walk(i) % 2
			//log.Printf("walked: i = %d, result = %d", i, r)
			if r != 0 {
				fmt.Println("NO")
				return
			}
		}
	}

	fmt.Println("YES")
}

// walk returns the edge count in cycle with v point.
func walk(v int) int {
	used[v] = true
	for _, next := range edges[v] {
		if !used[next] {
			return walk(next) + 1
		}
	}

	return 1
}

package main

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

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

func main() {
	defer writer.Flush()

	var T int
	fmt.Fscan(reader, &T)

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

		var ok bool = true
		var deg []int = make([]int, n+n+1)
		var par []int = make([]int, n+n+1)

		var findp func(int) int
		findp = func(u int) int {
			if par[u] < 0 {
				return u
			} else {
				return findp(par[u])
			}
		}

		join := func(u, v int) {
			u, v = findp(u), findp(v)
			if u == v {
				return
			}
			if par[u] > par[v] {
				u, v = v, u
			}
			par[u] += par[v]
			par[v] = u
		}

		for u := 1; u <= n+n; u++ {
			par[u] = -1
		}

		for i := 1; i <= n; i++ {
			var a, b int
			fmt.Fscan(reader, &a, &b)
			if a == b {
				ok = false
			}
			deg[a] += 1
			deg[b] += 1
			join(a, b+n)
			join(a+n, b)
		}

		if ok == false {
			fmt.Fprintf(writer, "%v\n", "NO")
		} else {
			for u := 1; u <= n; u++ {
				if deg[u] > 2 {
					ok = false
					break
				}
				if findp(u) == findp(u+n) {
					ok = false
					break
				}
			}
			if ok {
				fmt.Fprintf(writer, "%v\n", "YES")
			} else {
				fmt.Fprintf(writer, "%v\n", "NO")
			}
		}
	}
}

package main

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

type pair struct {
  fst, snd int
}

func solve(sc *bufio.Scanner, wr *bufio.Writer) {
  n := get(sc)
  a := make([]pair, n)
  dir := make([][]int, n)
  for i := range a {
    u, v := get(sc)-1, get(sc)-1
    if u == v {
      wr.WriteString("NO\n")
      i++
      for i < n {
        sc.Scan()
        sc.Scan()
        i++
      }
      return
    }
    a[i] = pair{u, v}
    dir[u] = append(dir[u], i)
    dir[v] = append(dir[v], i)
  }

  cs := make([]int, n) // colors

  var dfs func(i, c int) bool
  dfs  =  func(i, c int) bool {
    if cs[i] == c {
      return false // ok.
    }
    if cs[i] == -c {
      return true // alert!
    }
    cs[i] = c
    for _, j := range dir[a[i].fst] {
      if j != i && dfs(j, -c) {
        return true
      }
    }
    for _, j := range dir[a[i].snd] {
      if j != i && dfs(j, -c) {
        return true
      }
    }
    return false
  }

  for i := range a {
    if cs[i] == 0 && dfs(i, 1) {
      wr.WriteString("NO\n")
      return
    }
  }

  wr.WriteString("YES\n")
}

const (
  once = false
  maxBufSize = 10000000
)

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

  wr := bufio.NewWriter(os.Stdout)
  defer wr.Flush()

  if once {
    solve(sc, wr)
  } else {
    times := get(sc)
    for times > 0 {
      solve(sc, wr)
      times--
    }
  }
}

func get(sc *bufio.Scanner) int {
  x, _ := strconv.Atoi(str(sc))
  return x
}

func str(sc *bufio.Scanner) string {
  sc.Scan()
  return sc.Text()
}

func put(wr *bufio.Writer, i int) {
  wr.WriteString(strconv.Itoa(i))
}

func line(wr *bufio.Writer) {
  wr.WriteRune('\n')
}

func space(wr *bufio.Writer) {
  wr.WriteRune(' ')
}

package main

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

type domin struct {
	a, b int
}

// https://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D0%9D%D0%9C_(%D0%BD%D0%B0%D0%B8%D0%B2%D0%BD%D1%8B%D0%B5_%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8)
func main() {
	in := bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		n := 0
		fmt.Fscan(in, &n)

		dominos := make([]domin, 0, n)

		var failed bool
		for ; n > 0; n-- {
			a, b := 0, 0
			fmt.Fscan(in, &a, &b)

			if a == b {
				failed = true
			}
			dominos = append(dominos, domin{
				a: a,
				b: b,
			})
		}

		if failed {
			fmt.Println("no")
			continue
		}

		fg := make([]domin, 0, len(dominos)/2)
		sg := make([]domin, 0, len(dominos)/2)

		uniq := map[int]struct{}{}
		uniq2 := map[int]struct{}{}
		for _, d := range dominos {
			_, ok := uniq[d.a]
			_, ok2 := uniq[d.b]
			if !ok && !ok2 {
				uniq[d.a] = struct{}{}
				uniq[d.b] = struct{}{}
				fg = append(fg, domin{})
				continue
			}

			_, ok21 := uniq2[d.a]
			_, ok22 := uniq2[d.b]
			if !ok21 && !ok22 {
				uniq2[d.a] = struct{}{}
				uniq2[d.b] = struct{}{}
				sg = append(sg, domin{})
			} else {
				break
			}
		}

		if len(sg)+len(fg) == len(dominos) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

package main

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

type domin struct {
	a, b int
}

// https://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D0%9D%D0%9C_(%D0%BD%D0%B0%D0%B8%D0%B2%D0%BD%D1%8B%D0%B5_%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8)
func main() {
	in := bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		n := 0
		fmt.Fscan(in, &n)

		dominos := make([]domin, 0, n)

		var failed bool
		for ; n > 0; n-- {
			a, b := 0, 0
			fmt.Fscan(in, &a, &b)

			if a == b {
				failed = true
			}
			dominos = append(dominos, domin{
				a: a,
				b: b,
			})
		}

		if failed {
			fmt.Println("no")
			continue
		}

		fg, sg := make([]domin, 0, len(dominos)/2), make([]domin, 0, len(dominos)/2)
		empty := domin{}

		uniq := map[int]struct{}{}
		for i, d := range dominos {
			if d == empty {
				continue
			}

			_, ok := uniq[d.a]
			_, ok2 := uniq[d.b]
			if !ok && !ok2 {
				uniq[d.a] = struct{}{}
				uniq[d.b] = struct{}{}
				dominos[i] = empty
				fg = append(fg, domin{
					a: d.a,
					b: d.b,
				})
			}
		}

		uniq = map[int]struct{}{}
		for i, d := range dominos {
			if d == empty {
				continue
			}

			_, ok := uniq[d.a]
			_, ok2 := uniq[d.b]
			if !ok && !ok2 {
				uniq[d.a] = struct{}{}
				uniq[d.b] = struct{}{}
				dominos[i] = empty
				sg = append(sg, domin{
					a: d.a,
					b: d.b,
				})
			} else {
				break
			}
		}

		if len(sg)+len(fg) == len(dominos) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

package main

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

type domin struct {
	a, b int
}

// https://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D0%9D%D0%9C_(%D0%BD%D0%B0%D0%B8%D0%B2%D0%BD%D1%8B%D0%B5_%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8)
func main() {
	in := bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		n := 0
		fmt.Fscan(in, &n)

		dominos := make([]domin, 0, n)

		var failed bool
		for ; n > 0; n-- {
			a, b := 0, 0
			fmt.Fscan(in, &a, &b)

			if a == b {
				failed = true
			}
			dominos = append(dominos, domin{
				a: a,
				b: b,
			})
		}

		if failed {
			fmt.Println("no")
			continue
		}

		fg, sg := make([]domin, 0, len(dominos)/2), make([]domin, 0, len(dominos)/2)
		empty := domin{}

		uniq := map[int]struct{}{}
		for i, d := range dominos {
			if d == empty {
				continue
			}

			_, ok := uniq[d.a]
			_, ok2 := uniq[d.b]
			if !ok && !ok2 {
				uniq[d.a] = struct{}{}
				uniq[d.b] = struct{}{}
				dominos[i] = empty
				fg = append(fg, domin{
					a: d.a,
					b: d.b,
				})
			}
		}

		uniq = map[int]struct{}{}
		for i, d := range dominos {
			if d == empty {
				continue
			}

			_, ok := uniq[d.a]
			_, ok2 := uniq[d.b]
			if !ok && !ok2 {
				uniq[d.a] = struct{}{}
				uniq[d.b] = struct{}{}
				dominos[i] = empty
				sg = append(sg, domin{
					a: d.a,
					b: d.b,
				})
			}
		}

		if len(sg)+len(fg) == len(dominos) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

package main

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

type Domino struct {
	u int
	v int
}

func exist(set map[int]bool, domino *Domino) bool {
	if _, ok := set[domino.u]; ok {
		return true
	}
	if _, ok := set[domino.v]; ok {
		return true
	}
	return false
}

func solve(dominoes []*Domino) bool {
	sort.Slice(dominoes, func(i, j int) bool {
		if dominoes[i].u == dominoes[j].u {
			return dominoes[i].v < dominoes[j].v
		}
		return dominoes[i].u < dominoes[j].u
	})

	first := map[int]bool{}
	second := map[int]bool{}

	for _, domino := range dominoes {
		if domino.u == domino.v {
			return false
		}
		if !exist(first, domino) {
			first[domino.u] = true
			first[domino.v] = true
		} else if !exist(second, domino) {
			second[domino.u] = true
			second[domino.v] = true
		} else {
			return false
		}
	}

	return len(first) == len(dominoes) && len(second) == len(dominoes)
}

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

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

	for test > 0 {
		test--

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

		dominoes := make([]*Domino, n)
		for i := 0; i < n; i++ {
			var u, v int
			fmt.Fscanln(inp, &u, &v)
			if u > v {
				u, v = v, u
			}
			dominoes[i] = &Domino{u: u, v: v}
		}

		res := solve(dominoes)

		if res {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}

	out.Flush()
}

package main

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

type Domino struct {
	u int
	v int
}

func exist(set map[int]bool, domino *Domino) bool {
	if _, ok := set[domino.u]; ok {
		return true
	}
	if _, ok := set[domino.v]; ok {
		return true
	}
	return false
}

func solve(dominoes []*Domino) bool {
	sort.Slice(dominoes, func(i, j int) bool {
		if dominoes[i].u == dominoes[j].u {
			return dominoes[i].v < dominoes[j].v
		}
		return dominoes[i].u < dominoes[j].u
	})

	first := map[int]bool{}
	second := map[int]bool{}

	for _, domino := range dominoes {
		if domino.u == domino.v {
			return false
		}
		if !exist(first, domino) {
			first[domino.u] = true
			first[domino.v] = true
		} else if !exist(second, domino) {
			second[domino.u] = true
			second[domino.v] = true
		} else {
			return false
		}
	}

	return true
}

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

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

	for test > 0 {
		test--

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

		dominoes := make([]*Domino, n)
		for i := 0; i < n; i++ {
			var u, v int
			fmt.Fscanln(inp, &u, &v)
			if u > v {
				u, v = v, u
			}
			dominoes[i] = &Domino{u: u, v: v}
		}

		res := solve(dominoes)

		if res {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}

	out.Flush()
}

package main

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

type Domino struct {
	u int
	v int
}

func exist(set map[int]bool, domino *Domino) bool {
	if _, ok := set[domino.u]; ok {
		return true
	}
	if _, ok := set[domino.v]; ok {
		return true
	}
	return false
}

func solve(dominoes *[]*Domino) bool {
	first := map[int]bool{}
	second := map[int]bool{}

	for _, domino := range *dominoes {
		if domino.u == domino.v {
			return false
		}
		if !exist(first, domino) {
			first[domino.u] = true
			first[domino.v] = true
		} else if !exist(second, domino) {
			second[domino.u] = true
			second[domino.v] = true
		} else {
			return false
		}
	}

	return true
}

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

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

	for test > 0 {
		test--

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

		dominoes := make([]*Domino, n)
		for i := 0; i < n; i++ {
			var u, v int
			fmt.Fscanln(inp, &u, &v)
			dominoes[i] = &Domino{u: u, v: v}
		}

		res := solve(&dominoes)

		if res {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}

	out.Flush()
}

package main

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

type pair struct {
	a, b int
}

func solve(in io.Reader, out io.Writer) {
	var n int
	fmt.Fscan(in, &n)
	arr := []pair{}
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		arr = append(arr, pair{a, b})
	}
	sa, sb := make(map[int]bool), make(map[int]bool)
	for _, v := range arr {
		if !sa[v.a] && !sa[v.b] && v.a != v.b {
			sa[v.a] = true
			sa[v.b] = true
		} else if !sb[v.a] && !sb[v.b] && v.a != v.b {
			sb[v.a] = true
			sb[v.b] = true
		} else {
			fmt.Fprintln(out, "NO")
			return
		}
	}
	fmt.Fprintln(out, "YES")
}

func main() {
	in := bufio.NewReader(os.Stdin)
	// in, _ := os.Open("in.txt")
	t := 1
	fmt.Fscan(in, &t)
	for t > 0 {
		solve(in, os.Stdout)
		t--
	}
}

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

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

package main

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

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

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

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

func do(d [][2]int) string {
	a := map[int]int{}
	b := map[int]int{}
	for i := range d {
		if d[i][0] > d[i][1] {
			d[i][0], d[i][1] = d[i][1], d[i][0]
		}
	}
	sort.Slice(d, func(i, j int) bool {
		if d[i][0] == d[j][0] {
			return d[i][1] < d[j][1]
		}
		return d[i][0] < d[j][0]
	})
	for _, v := range d {
		x := v[0]
		y := v[1]
		if a[x] > 0 || a[y] > 0 {
			b[x]++
			b[y]++
		} else {
			a[x]++
			a[y]++
		}
		if a[x] > 1 || a[y] > 1 || b[x] > 1 || b[y] > 1 {
			return "NO"
		}
	}
	return "YES"
}

package main

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

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

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

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

func do(d [][2]int) string {
	a := map[int]int{}
	b := map[int]int{}
	for _, v := range d {
		x := v[0]
		y := v[1]
		if a[x] > 0 || a[y] > 0 {
			b[x]++
			b[y]++
		} else {
			a[x]++
			a[y]++
		}
		if a[x] > 1 || a[y] > 1 || b[x] > 1 || b[y] > 1 {
			return "NO"
		}
	}
	return "YES"
}

package main

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

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

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	d := make([][2]int, n)
	cnt := make([]int, n+1)
	for i := range d {
		fmt.Fscan(rr, &d[i][0], &d[i][1])
	}
	hash := make([][]int, n+1)
	for i := range d {
		cnt[d[i][0]]++
		cnt[d[i][1]]++
		if cnt[d[i][0]] > 2 || cnt[d[i][1]] > 2 || d[i][0] == d[i][1] {
			fmt.Fprintln(ww, "NO")
			return
		}
		hash[d[i][0]] = append(hash[d[i][0]], d[i][1])
		hash[d[i][1]] = append(hash[d[i][1]], d[i][0])
	}
	processed := make(map[int]int)
	for i:=1;i<=n;i++ {
		if processed[i] > 0 {
			continue
		}
		j := 0
		processed[i] = i
		cur := i
		for {
			for k := range hash[cur] {
				if processed[hash[cur][k]] == i || processed[hash[cur][k]] == 0 {
					cur = hash[cur][k]
					break
				}
			}
			if processed[cur] == i {
				if j % 2 == 0 {
					fmt.Fprintln(ww, "NO")
					return
				}
				break
			}
			j++
			processed[cur] = i
		}
	}
	fmt.Fprintln(ww, "YES")

}

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

// 1702E. Split Into Two Sets
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, a, b int
		fmt.Fscan(reader, &n)
		max := make(map[int]int)
		min := make(map[int]int)
		possible := true
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a, &b)
			if a == b {
				possible = false
			} else if max[a] == 0 && max[b] == 0 {
				max[a]++
				max[b]++
			} else if min[a] == 0 && min[b] == 0 {
				min[a]++
				min[b]++
			} else {
				possible = false
			}
		}
		if possible {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}


There's a chessboard of size $$$n \times n$$$. $$$m$$$ rooks are placed on it in such a way that:   no two rooks occupy the same cell;  no two rooks attack each other. A rook attacks all cells that are in its row or column.Is it possible to move exactly one rook (you can choose which one to move) into a different cell so that no two rooks still attack each other? A rook can move into any cell in its row or column if no other rook stands on its path.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 8$$$) — the size of the chessboard and the number of the rooks. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$) — the position of the $$$i$$$-th rook: $$$x_i$$$ is the row and $$$y_i$$$ is the column. No two rooks occupy the same cell. No two rooks attack each other.

For each testcase, print "YES" if it's possible to move exactly one rook into a different cell so that no two rooks still attack each other. Otherwise, print "NO".

NoteIn the first testcase, the rooks are in the opposite corners of a $$$2 \times 2$$$ board. Each of them has a move into a neighbouring corner, but moving there means getting attacked by another rook.In the second testcase, there's a single rook in a middle of a $$$3 \times 3$$$ board. It has $$$4$$$ valid moves, and every move is fine because there's no other rook to attack it.

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,m int
		fmt.Fscan(in,&n,&m)
		for j:=0;j<m;j++{
			var x,y int
			fmt.Fscan(in,&x,&y)
		}
		if (n - (m-1)) * (n - (m-1)) - 1 > 0 {
			fmt.Fprintln(out,"YES")
		} else {
			fmt.Fprintln(out,"NO")
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var T int
	fmt.Scan(&T)
	for T > 0 {
		T--
		var n, m int
		fmt.Scan(&n, &m)
		var x, y int
		for i := 0; i < m; i++ {
			fmt.Scan(&x, &y)
		}
		if m < n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

// source: https://codeforces.com/contest/1749/problem/A?locale=en
func solve() {
	n, m := _readInt(), _readInt()
	for i := 0; i < m; i++ {
		_readInt()
		_readInt()
	}
	if n == m {
		_print("NO")
	} else {
		_print("YES")
	}
}

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

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr() (int, []int) {
	var n int
	fmt.Fscan(in, &n)
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}
func _makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

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

		aa := make([]bool, n)
		bb := make([]bool, n)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			aa[x-1] = true
			bb[y-1] = true
		}

		var f bool
		for i := range aa {
			if !aa[i] || !bb[i] {
				f = true
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import "fmt"

func main() {
	var testing int
	var n, m, xi, yi int
	fmt.Scan(&testing)
	for i := 0; i < testing; i++ {
		fmt.Scan(&n)
		fmt.Scan(&m)
		for j := 0; j < m; j++ {
			fmt.Scan(&xi)
			fmt.Scan(&yi)
		}
		if m < n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}

}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		t--
		var n, m int
		fmt.Scan(&n, &m)
		var x, y int

		for i :=0; i <m;i++{
			fmt.Scan(&x,&y)
		}
		if m < n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var (
	t, n, m, x, y int
	row, col      []int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &m)
		row = make([]int, n+1)
		col = make([]int, n+1)
		flag := false
		cnt := 0
		for i := 0; i < m; i++ {
			scan(&x, &y)
			row[x]++
			col[y]++
			if row[x] >= 2 || col[y] >= 2 {
				cnt++
			}
		}

		if n == m || cnt > 1 {
			flag = true
		}
		if flag {
			println("NO")
		} else {
			println("YES")
		}
	}
}

//

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

func init() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(r, a...)
}

func println(a ...any) {
	fmt.Fprintln(w, a...)
}

func flush() {
	w.Flush()
}

package main

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

var in = bufio.NewReader(os.Stdin)

func  gcd(a, b int) int {
  for b != 0 {
      a, b = b, a%b
  }
  return a
}

func pl(a ...interface{}) { fmt.Println(a...) }
func pf(format string, a ...interface{}) { fmt.Printf(format, a...) }
func scan(a ...interface{}) { fmt.Fscan(in, a...) }

func solve() {
  var n, m, a, b int
  scan(&n, &m)
  for i := 1; i <= m; i++ {  scan(&a, &b) }
  pf("%s\n", ys(n > m))
}

func main() {
	var T int
	scan(&T)
	for ; T > 0; T-- {
    solve()
	}
}

func ys(b bool) string { return map[bool]string {true: "YES", false: "NO"}[b] }

package main

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

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

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

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

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

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

/**
Run solve the problem CF1749A
Date: 10/20/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1749/A
Problem: CF1749A
**/
func (in *CF1749A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		cols := make([]bool, n)
		rows := make([]bool, n)

		for i := 0; i < m; i++ {
			x, y := in.NextInt(), in.NextInt()
			cols[x-1] = true
			rows[y-1] = true
		}

		c, r := false, false
		for i := 0; i < n; i++ {
			if !cols[i] {
				c = true
			}
			if !rows[i] {
				r = true
			}

		}
		if c && r {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

	}
}

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

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

package main

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

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

func check(check [][]bool) string {
	count := 0
	for _, row := range check {
		for _, col := range row {
			if col {
				count++
				break
			}
		}
	}

	if count >= len(check) {
		return "NO"
	}

	return "YES"
}

func main() {
	var test int
	fmt.Fscanf(reader, "%d\n", &test)
	for test > 0 {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		marks := make([][]bool, n)
		for i := 0; i < n; i++ {
			marks[i] = make([]bool, n)
		}

		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscanf(reader, "%d %d\n", &x, &y)
			marks[x-1][y-1] = true
		}

		if m >= n {
			fmt.Println("NO")
		} else {
			fmt.Println(check(marks))
		}
		test--
	}
}

package main

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

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

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

func solve() {
	var n,m int
	scanf("%d %d\n", &n, &m)
	// chess := make([][]bool, n)
	// for i:=0; i<n; i++ {
	// 	chess[i] = make([]bool, n)
	// }

	for i:=0; i<m; i++ {
		var x, y int
		scanf("%d %d\n", &x, &y)
		// chess[x][y] = true
	}

	if n == m {
		printf("NO\n")
		return
	}
	printf("YES\n")
}

func main() {
	defer writer.Flush()

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

package main

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

const cf = true

func solve() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	var n1, n2 int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &n1, &n2)
	}
	if m < n {
		fmt.Fprintln(out, "YES")
	} else {
		fmt.Fprintln(out, "NO")
	}
}

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

func reverse[T any](arr []T) {
	n := len(arr)
	for i := 0; i < n/2; i++ {
		arr[i], arr[n-i-1] = arr[n-i-1], arr[i]
	}
}

func reversed[T any](arr []T) (res []T) {
	res = make([]T, len(arr))
	copy(res, arr)
	reverse(res)
	return res
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

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

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

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

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

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

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

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

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

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

package main

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

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

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

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

		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(r, &x, &y)
		}

		if m == n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
		}
	}

}

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

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

package main

import (
	"fmt"
)

func main() {
	var T int
	fmt.Scan(&T)
	for T > 0 {
		T--
		var n, m int
		fmt.Scan(&n, &m)
		var x, y int
		for i := 0; i < n; i++ {
			fmt.Scan(&x, &y)
		}
		if m != n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var T int
	fmt.Scan(&T)
	for T > 0 {
		T--
		var n, m int
		fmt.Scan(&n, &m)
		var x, y int
		for i := 0; i < n; i++ {
			fmt.Scan(&x, &y)
		}
		if m < n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

// source: https://codeforces.com/contest/1749/problem/A?locale=en
func solve() {
	n, m := _readInt(), _readInt()
	for i := 0; i < m; i++ {
		_readInt()
	}
	if n == m {
		_print("NO")
	} else {
		_print("YES")
	}
}

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

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr() (int, []int) {
	var n int
	fmt.Fscan(in, &n)
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}
func _makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

// source: https://codeforces.com/contest/1749/problem/A?locale=en
func solve() {
	n, m := _readInt(), _readInt()
	if n == m {
		_print("NO")
	} else {
		_print("YES")
	}
}

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

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr() (int, []int) {
	var n int
	fmt.Fscan(in, &n)
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}
func _makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

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

		aa := make([]bool, n)
		bb := make([]bool, n)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			aa[x-1] = true
			bb[y-1] = true
		}

		var f bool
		for i := range aa {
			if !aa[i] && !bb[i] {
				f = true
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}