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, k, st, nisc int
		fmt.Fscan(r, &n, &m, &k)

		a := make([]int, k)
		v := make([]bool, k+1)
		for i := range a { fmt.Fscan(r, &a[i]) }

		res := "YA"
		for i := k ; i >= 1 ; i-- {
			// fmt.Fprintln(w, "IIIIIIIIIIIIIIIII i ", i)
			if v[i] {
				nisc--
			} else {
				// fmt.Fprintln(w, "iiii st ", st)
				for ; st < k && a[st] != i ; st++ {
					v[a[st]] = true
					nisc++
				}

				if n*m - nisc < 4 {
					res = "TIDAK"
					break
				}
				// fmt.Fprintln(w, "iiii st nisc ", st, nisc)
				st++
			}
		}

		fmt.Fprintln(w, res)
	}
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			queue[ka[i]] = true
			sizeQueue++
			if _, ok := queue[top]; ok {
				if sizeQueue > n*m-3 {
					fail = true
					break
				}
				sizeQueue--
				delete(queue, top)
				top--
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

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

const (
	DEBUG = false
)

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

func debugCondn(condn bool, a ...interface{}) {
	if !DEBUG || !condn {
		return
	}
	for _, e := range a {
		printf("%v ", e)
	}
	printf("\n")
	writer.Flush()
}

func assert(condn bool) {
	if !condn {
		panic("assertion error")
	}
}

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

	nm := n*m
	moved := make([]bool, k+1)
	cnt := 0
	nxt := k

	res := true
	for i:=0; i<k; i++ {
		moved[a[i]] = true
		cnt++
		if cnt == nm - 2 {
			res = false
			break
		}
		for nxt > 0 && moved[nxt] {
			nxt--
			cnt--
		}
	}

	if res && nxt == 0 {
		assert(cnt == 0)
		printf("YA\n")
		return
	}
	printf("TIDAK\n")
}

func main() {
	defer writer.Flush()

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

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, k, st, nisc int
		fmt.Fscan(r, &n, &m, &k)

		a := make([]int, k)
		v := make([]bool, k+1)
		for i := range a { fmt.Fscan(r, &a[i]) }

		res := "YA"
		for i := k ; i >= 1 ; i-- {
			if v[i] {
				nisc--
			} else {
				for ; st < k && a[st] != i ; st++ {
					v[a[st]] = true
					nisc++
				}

				if n*m - nisc < 4 {
					res = "TIDAK"
					break
				}
				st++
			}
		}

		fmt.Fprintln(w, res)
	}
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			if ka[i] == top {
				top--
			} else {
				if _, ok := queue[top]; ok {
					sizeQueue--
					delete(queue, top)
					top--
				} else if sizeQueue < n*m {
					queue[ka[i]] = true
					sizeQueue++
				} else {
					fail = true
					break
				}
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			if ka[i] == top {
				top--
			} else {
				if _, ok := queue[top]; ok {
					sizeQueue--
					delete(queue, top)
					top--
				} else if sizeQueue < n*m-1 {
					queue[ka[i]] = true
					sizeQueue++
				} else {
					fail = true
					break
				}
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			if ka[i] == top {
				top--
			} else {
				if _, ok := queue[top]; ok {
					sizeQueue--
					delete(queue, top)
					top--
				} else if sizeQueue < n*m-2 {
					queue[ka[i]] = true
					sizeQueue++
				} else {
					fail = true
					break
				}
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			if ka[i] == top {
				top--
			} else {
				if _, ok := queue[top]; ok {
					sizeQueue--
					delete(queue, top)
					top--
				} else if sizeQueue < n*m-3 {
					queue[ka[i]] = true
					sizeQueue++
				} else {
					fail = true
					break
				}
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			if ka[i] == top {
				top--
			} else {
				if _, ok := queue[top]; ok {
					sizeQueue--
					delete(queue, top)
					top--
				} else if sizeQueue < n*m-4 {
					queue[ka[i]] = true
					sizeQueue++
				} else {
					fail = true
					break
				}
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m, k := readThreeInt(reader)
		ka := readArrayInt(reader)
		var queue = make(map[int]bool)

		top := k
		sizeQueue := 0
		fail := false
		for i := 0; i < k; i++ {
			if ka[i] == top {
				top--
			} else {
				if _, ok := queue[top]; ok {
					sizeQueue--
					delete(queue, top)
					top--
				} else if sizeQueue < (n-1)*(m-1) {
					queue[ka[i]] = true
					sizeQueue++
				} else {
					fail = true
					break
				}
			}
		}

		if !fail {
			fmt.Fprintln(writer, "YA")
		} else {
			fmt.Fprintln(writer, "TIDAK")
		}

		t--
	}

	writer.Flush()
}

func max(a int, b int) int {
	return int(maxInt64(int64(a), int64(b)))
}

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

	return b
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}


package main

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

type Socket struct {
    s, i int
}

type SocketArr []Socket
func (a SocketArr) Len() int           { return len(a) }
func (a SocketArr) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a SocketArr) Less(i, j int) bool { return a[i].s < a[j].s }

func solve() {
    n, m := readInt(), readInt()
    mp, powers, sockets := make(map[int][]int), readIntArray(n), make(SocketArr, m)
    for i, p := range powers {
        mp[p] = append(mp[p], i)
    }
    for i := range sockets {
        sockets[i] = Socket{readInt(), i}
    }
    c, u, a, b := 0, 0, make([]int, m), make([]int, n)
    for i := range b {
        b[i] = -1
    }

    sort.Sort(sockets)
    for _, s := range sockets {
        pow, cnt := s.s, 0
        for {
            ps := mp[pow]
            if len(ps) > 0 {
                b[ps[0]], mp[pow], a[s.i] = s.i, ps[1:], cnt
                c++
                u += cnt
                break
            }
            if pow == 1 {
                break
            }
            pow = (pow + 1) / 2
            cnt++
        }
    }
    fmt.Println(c, u)
    for _, v := range a {
        fmt.Print(v, " ")
    }
    fmt.Println()

    for _, v := range b {
        fmt.Print(v+1, " ")
    }
    fmt.Println()
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return 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
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

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

	last := make(map[int]int)
	next := make([]int, n+1)

	var p int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(bufin, "%d\n", &p)
		_, ok := last[p]
		if ok {
			next[i] = last[p]
		}
		last[p] = i
	}

	s := make([]int, m+1)
	for i := 1; i <= m; i++ {
		fmt.Fscanf(bufin, "%d\n", &s[i])
	}

	c := 0
	u := 0
	a := make([]int, m+1)
	b := make([]int, n+1)

	used := make([]bool, m+1)
	for plug, one := 0, 0; one < m; plug++ {
		for i := 1; i <= m; i++ {
			if !used[i] {
				p = s[i]
				j, ok := last[p]
				if ok && (j > 0) && (b[j] == 0) {
					c++
					u += plug
					a[i] = plug
					b[j] = i
					used[i] = true
					last[p] = next[j]
				}
			}
		}

		one = 0
		for i := 1; i <= m; i++ {
			if s[i] == 1 {
				one++
			}
			s[i] = (s[i] + 1) / 2
		}
	}

	fmt.Fprintf(bufout, "%d %d\n", c, u)
	for i := 1; i <= m; i++ {
		fmt.Fprintf(bufout, "%d ", a[i])
	}
	fmt.Fprintf(bufout, "\n")
	for i := 1; i <= n; i++ {
		fmt.Fprintf(bufout, "%d ", b[i])
	}
	fmt.Fprintf(bufout, "\n")
}


package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k, p, x, y := nextInt(), nextInt(), nextInt(), nextInt(), nextInt()
	sum, median, cntLeast, a := 0, (n+1)/2, 0, 0
	_ = p

	for i := 0; i < k; i++ {
		a = nextInt()
		sum += a

		if a < y {
			cntLeast++
		}
	}

	if cntLeast >= median {
		println(-1)
		return
	} else {
		a = median - cntLeast - 1
		if n-k < a {
			a = n - k
		}
		sum += a*1 + (n-k-a)*y
	}
	// println(cntLeast)
	if sum > x {
		println(-1)
	} else {
		for i := 0; i < a; i++ {
			print(1, " ")
		}

		for i := 0; i < n-k-a; i++ {
			print(y, " ")
		}
		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer = bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, k, _, maxTotal, minMedian := getI(), getI(), getI(), getI(), getI()
  m := n-k
  data := make([]int, n)
  for i := 0; i < m; i++ {
    data[i] = 1
  }
  total := m
  for i := 0; i < k; i++ {
    x := getI()
    data[m+i] = x
    total += x
  }
  sort.Ints(data)
  median := n/2
  seek := median
  for seek < n && data[seek] < minMedian {
    seek++
  }
  dist := seek-median
  total += dist*(minMedian-1)
  if dist > m || total > maxTotal {
    writer.WriteString("-1\n")
    return
  }
  result := make([]int, m)
  for i := 0; i < m; i++ {
    if i < dist {
      result[i] = minMedian
    } else {
      result[i] = 1
    }
  }
  writer.WriteString(fmt.Sprintf("%d", result[0]))
  for i := 1; i < m; i++ {
    writer.WriteString(fmt.Sprintf(" %d", result[i]))
  }
  writer.WriteString("\n")
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k, p, x, y := nextInt(), nextInt(), nextInt(), nextInt(), nextInt()
	sum, median, cntLeast, a := 0, (n+1)/2, 0, 0
	_ = p

	for i := 0; i < k; i++ {
		a = nextInt()
		sum += a

		if a < y {
			cntLeast++
		}
	}

	if cntLeast >= median {
		println(-1)
		return
	} else {
		sum += (median-cntLeast-1)*1 + (n-k-(median-cntLeast-1))*y
	}

	if sum >= x {
		println(-1)
	} else {
		for i := 0; i < median-cntLeast-1; i++ {
			print(1, " ")
		}

		for i := 0; i < n-k-(median-cntLeast-1); i++ {
			print(y, " ")
		}
		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k, p, x, y := nextInt(), nextInt(), nextInt(), nextInt(), nextInt()
	sum, median, cntLeast, a := 0, (n+1)/2, 0, 0
	_ = p

	for i := 0; i < k; i++ {
		a = nextInt()
		sum += a

		if a < y {
			cntLeast++
		}
	}

	if cntLeast >= median {
		println(-1)
		return
	} else {
		sum += (median-cntLeast-1)*1 + (n-k-(median-cntLeast-1))*y
	}

	if sum > x {
		println(-1)
	} else {
		for i := 0; i < median-cntLeast-1; i++ {
			print(1, " ")
		}

		for i := 0; i < n-k-(median-cntLeast-1); i++ {
			print(y, " ")
		}
		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }


package main

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

// https://space.bilibili.com/206214
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int)  bool {
		var n, k int
		Fscan(in, &n, &k)
		k--
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		h := a[k]
		type pair struct{ mi, d int64 }
		f := func(a []int64) (b []pair) {
			for len(a) > 0 && a[0] > 0 {
				h += a[0]
				a = a[1:]
			}
			for i, n := 0, len(a); i < n; {
				neg := int64(0)
				mi := int64(0)
				for {
					for ; i < n && a[i] < 0; i++ {
						neg += a[i]
					}
					mi = min(mi, neg)
					if i == n {
						b = append(b, pair{mi, neg})
						break
					}
					pos := int64(0)
					for ; i < n && a[i] > 0; i++ {
						pos += a[i]
					}
					if neg+pos > 0 {
						b = append(b, pair{mi, neg+pos})
						break
					}
					neg += pos
				}
			}
			return
		}
		r := f(a[k+1:])
		tmp := a[:k]
		for i, n := 0, len(tmp); i < n/2; i++ {
			tmp[i], tmp[n-1-i] = tmp[n-1-i], tmp[i]
		}
		l := f(tmp)

		g := func() bool {
			i, j := 0, 0
			n, m := len(l), len(r)
			for i < n && j < m {
				if h + l[i].mi >= 0 {
					h += l[i].d
					i++
					continue
				}
				if h + r[j].mi >= 0 {
					h += r[j].d
					j++
					continue
				}
				return false
			}
			return true
		}
		return  g()
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		if solve(curCase) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

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

}

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

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

package main

import (
	"bufio"
	. "fmt"
	"io"

	//"math"
	"os"
	//"sort"
)

func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		var n, k int
		Fscan(in, &n, &k)
		a := make([]int64, n+5)
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
		}
		var lmax, rmax, nowr, nowl int64
		lmax = int64(0)
		rmax = int64(0)
		nowl = int64(0)
		nowr = int64(0)
		l := k - 1
		r := k + 1
		for l != 0 && r != n+1 {
			if a[k]+lmax+a[r]+nowr >= 0 {
				nowr += a[r]
				r++
				rmax = max(rmax, nowr)
				continue
			}
			if a[k]+rmax+a[l]+nowl >= 0 && l >= 0 {
				nowl += a[l]
				l--
				lmax = max(lmax, nowl)
				continue
			}
			break
		}
		if l == 0 || r == n+1 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

}
func main() {
	solve(os.Stdin, os.Stdout)
}
func max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

package main

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

// https://space.bilibili.com/206214
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int)  bool {
		var n, k int
		Fscan(in, &n, &k)
		k--
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		h := a[k]
		type pair struct{ neg, pos int64 }
		f := func(a []int64) (b []pair) {
			for len(a) > 0 && a[0] > 0 {
				h += a[0]
				a = a[1:]
			}
			for i, n := 0, len(a); i < n; {
				neg := int64(0)
				for {
					for ; i < n && a[i] < 0; i++ {
						neg += a[i]
					}
					if i == n {
						b = append(b, pair{neg, 0})
						break
					}
					pos := int64(0)
					for ; i < n && a[i] > 0; i++ {
						pos += a[i]
					}
					if neg+pos > 0 {
						b = append(b, pair{neg, pos})
						break
					}
					neg += pos
				}
			}
			return
		}
		r := f(a[k+1:])
		tmp := a[:k]
		for i, n := 0, len(tmp); i < n/2; i++ {
			tmp[i], tmp[n-1-i] = tmp[n-1-i], tmp[i]
		}
		l := f(tmp)

		g := func() bool {
			i, j := 0, 0
			n, m := len(l), len(r)
			for i < n && j < m {
				if h + l[i].neg >= 0 {
					h += l[i].neg + l[i].pos
					i++
					continue
				}
				if h + r[j].neg >= 0 {
					h += r[j].neg + r[j].pos
					j++
					continue
				}
				return false
			}
			return true
		}
		return  g()
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		if solve(curCase) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

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

}

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


package main

import (
	"fmt"
)

func main() {
	var t uint64
	_, err := fmt.Scan(&t)
	if err != nil {
		panic(err)
	}

	var i uint64
	for i = 0; i < t; i++ {
		var a uint64
		_, err1 := fmt.Scan(&a)
		if err1 != nil {
			panic(err)
		}

		var b uint64
		_, err2 := fmt.Scan(&b)
		if err2 != nil {
			panic(err)
		}

		fmt.Println(solve(a, b))
	}
}

func solve(a uint64, b uint64) (uint64, uint64) {
	if a == b {
		return 0, 0
	}

	if a == 0 {
		return b, 0
	}

	if b == 0 {
		return a, 0
	}

	var bigger uint64
	var lower uint64
	if a > b {
		bigger = a
		lower = b
	} else {
		bigger = b
		lower = a
	}

	var best uint64 = 0
	var minimal uint64 = 0
	if bigger % lower == 0 {
		best = lower
		minimal = 0
	}
	var probably_best = bigger - lower
	if probably_best > best {
		best = probably_best
		minimal = lower % probably_best
		var maybe_even_more_min = probably_best - lower %probably_best
		if maybe_even_more_min < minimal {
			minimal = maybe_even_more_min
		}
	}

	return best, minimal
}

package main

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

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

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

	for i := 0; i < t; i++ {
		var a, b int64
		fmt.Fscan(r, &a, &b)

		solve(a, b)
	}
}

func solve(a, b int64) {
	//23 //17
	var diff int64

	if a == b {
		fmt.Print(0)
		fmt.Print(" ")
		fmt.Println(0)
		return
	}

	if a == 0 {
		fmt.Print(b)
		fmt.Print(" ")
		fmt.Println(0)
		return
	} else if b == 0 {
		fmt.Print(a)
		fmt.Print(" ")
		fmt.Println(0)
		return
	}

	if a > b {
		diff = a - b

		rem1A := a % diff
		rem2A := diff - rem1A

		fmt.Print(diff)
		fmt.Print(" ")
		fmt.Println(min(rem1A, rem2A))

	} else {
		diff = b -  a

		rem1B := b % diff
		rem2B := diff - rem1B

		fmt.Print(diff)
		fmt.Print(" ")
		fmt.Println(min(rem1B, rem2B))
	}
}

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

package main

import ."fmt"
//import "sort"
import "bufio"
import "os"
//import ."math"

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

func main() {
    defer writer.Flush()

    var t int
    Fscan(reader, &t)
    for i := 0; i < t; i++ {
        solve()
    }
}
func solve(){
    var a, b int64
    Fscan(reader, &a)
    Fscan(reader, &b)
    Fprint(writer, abs(a - b), " ")
    if b > a {
        temp := a
        a = b
        b = temp
    }
    if a == b {
        Fprintln(writer, 0)
        return
    }
    ost1 := b % abs(a - b)
    ost2 := abs(a - b) - ost1
    if ost2 == abs(a - b) {
        ost2 = 0
    }
    Fprintln(writer, min(ost1, ost2))
}
func abs(x int64) int64 {
    if x < 0 {
        return 0 - x
    }
    return x
}
func min(x, y int64) int64 {
    if x < y {
        return x
    }
    return y
}

package main

import "fmt"

func main() {
	var n int64
	fmt.Scan(&n)
	result := make([]string, 0, n)
	for i := int64(0); i < n; i++ {
		var a,b int64
		fmt.Scan(&a)
		fmt.Scan(&b)
		resg := int64(0)
		resc := int64(0)
		if a == b {
			resg = 0
			resc = 0
		} else if a > b {
			resg = a - b
			resc = b % resg
			resc = min(resc, resg - resc)
		} else {
			resg = b - a
			resc = a % resg
			resc = min(resc, resg - resc)
		}
		result = append(result, fmt.Sprintf("%v %v\n", resg, resc))
	}
	for _, str := range result {
		fmt.Printf(str)
	}
}

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

package main
import(
	"bufio"
	"fmt"
	"os"
)

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

func main() {
	defer writer.Flush()
	var t int
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	var a, b int64
	fmt.Fscan(reader, &a, &b)
	d := abs(a - b)
	if a == b {
		fmt.Fprintln(writer, "0 0")
	} else {
		fmt.Fprintln(writer, d, min( b % d, d - (b % d)))
	}

}

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

func min(nums ...int64) int64 {
	l := len(nums)
	mn := nums[0]
	for i := 0; i < l; i++ {
		if nums[i] < mn {
			mn = nums[i]
		}
	}
	return mn
}

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	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 sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		a := sint64()
		b := sint64()
		x, o := sol(a, b)
		printf("%d %d\n", x, o)
	}
}

func sol(a, b int64) (int64, int64) {
	diff := abs(a - b)
	if diff == 0 {
		return 0, 0
	}
	down := a / diff * diff
	up := down + diff
	return diff, min(a-down, up-a)
}

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

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

// 1543A. Exciting Bets
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 a, b int64
		fmt.Fscan(reader, &a, &b)
		if a < b {
			a, b = b, a
		}
		if a == b {
			fmt.Println(0, 0)
		} else {
			ans := a - b
			b = b % ans
			if b < ans-b {
				fmt.Println(ans, b)
			} else {
				fmt.Println(ans, ans-b)
			}
		}

	}
}

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 min_int64(a int64, b int64) int64 { if a < b { return a } else { return b } }

func main() {
	defer writer.Flush()

	var T int = 0
	scanf("%d\n", &T)

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

        var a int64 = 0
        var b int64 = 0

        scanf("%d %d\n", &a, &b)

        if(a == b) {
            printf("0 0\n")
        } else {
            if(a < b) {
                a, b = b, a
            }

            var res_one = a - b
            var res_two = min_int64(b % res_one, res_one - b % res_one)

            printf("%d %d\n", res_one, res_two)
        }

	}

}

package main

import (
	"fmt"
)

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

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

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

	// a < b
	score := b - a
	return score, Min(a%score, score-a%score)

}

func main() {
	t := 0
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var a, b int64
		fmt.Scan(&a, &b)
		score, step := task(a, b)
		fmt.Printf("%v %v\n", score, step)
	}

}

package main

import (
	"fmt"
)

func main() {
	var t uint64
	_, err := fmt.Scan(&t)
	if err != nil {
		panic(err)
	}

	var i uint64
	for i = 0; i < t; i++ {
		var a uint64
		_, err1 := fmt.Scan(&a)
		if err1 != nil {
			panic(err)
		}

		var b uint64
		_, err2 := fmt.Scan(&b)
		if err2 != nil {
			panic(err)
		}

		fmt.Println(solve(a, b))
	}
}

func solve(a uint64, b uint64) (uint64, uint64) {
	if a == b {
		return 0, 0
	}

	if a == 0 {
		return b, 0
	}

	if b == 0 {
		return a, 0
	}

	var bigger uint64
	var lower uint64
	if a > b {
		bigger = a
		lower = b
	} else {
		bigger = b
		lower = a
	}

	var best uint64 = 0
	var minimal uint64 = 0
	if bigger % lower == 0 {
		best = lower
		minimal = 0
	}
	var probably_best = bigger - lower
	if probably_best > best {
		best = probably_best
		minimal = lower
		var maybe_even_more_min = probably_best - lower %probably_best
		if maybe_even_more_min < minimal {
			minimal = maybe_even_more_min
		}
	}

	return best, minimal
}

package main

import (
	"fmt"
)

func main() {
	var t uint64
	_, err := fmt.Scan(&t)
	if err != nil {
		panic(err)
	}

	var i uint64
	for i = 0; i < t; i++ {
		var a uint64
		_, err1 := fmt.Scan(&a)
		if err1 != nil {
			panic(err)
		}

		var b uint64
		_, err2 := fmt.Scan(&b)
		if err2 != nil {
			panic(err)
		}

		fmt.Println(solve(a, b))
	}
}

func solve(a uint64, b uint64) (uint64, uint64) {
	if a == b {
		return 0, 0
	}

	if a == 0 {
		return b, 0
	}

	if b == 0 {
		return a, 0
	}

	var bigger uint64
	var lower uint64
	if a > b {
		bigger = a
		lower = b
	} else {
		bigger = b
		lower = a
	}

	var best uint64 = 0
	var minimal uint64 = 0
	if bigger % lower == 0 {
		best = lower
		minimal = 0
	}
	var probably_best = bigger - lower
	if probably_best > best {
		best = probably_best
		minimal = lower
	}
	var maybe_even_more_min = probably_best - lower %probably_best
	if maybe_even_more_min < minimal {
		minimal = maybe_even_more_min
	}
	return best, minimal
}

package main

import (
	"fmt"
)

func main() {
	var t int
	_, err := fmt.Scan(&t)
	if err != nil {
		panic(err)
	}

	for i := 0; i < t; i++ {
		var a int
		_, err1 := fmt.Scan(&a)
		if err1 != nil {
			panic(err)
		}

		var b int
		_, err2 := fmt.Scan(&b)
		if err2 != nil {
			panic(err)
		}

		fmt.Println(solve(a, b))
	}
}

func solve(a int, b int) (int, int) {
	if a == b {
		return 0, 0
	}

	var bigger int
	var lower int
	if a > b {
		bigger = a
		lower = b
	} else {
		bigger = b
		lower = a
	}

	if bigger % lower == 0 {
		return lower, 0
	} else {
		var probably_min = bigger - lower
		var maybe_even_more_min = probably_min - lower % probably_min
		if maybe_even_more_min < lower {
			return probably_min, maybe_even_more_min
		} else {
			return probably_min, lower
		}
	}
}

package main

import ."fmt"
//import "sort"
import "bufio"
import "os"
//import ."math"

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

func main() {
    defer writer.Flush()

    var t int
    Fscan(reader, &t)
    for i := 0; i < t; i++ {
        solve()
    }
}
func solve(){
    var a, b int64
    Fscan(reader, &a)
    Fscan(reader, &b)
    Fprint(writer, abs(a - b), " ")
    if b > a {
        temp := a
        a = b
        b = temp
    }
    if a == b {
        Fprintln(writer, 0)
        return
    }
    ost := b % abs(a - b)
    ost = abs(a - b) - ost
    if ost == abs(a - b) {
        ost = 0
    }
    Fprintln(writer, ost)
}
func abs(x int64) int64 {
    if x < 0 {
        return 0 - x
    }
    return x
}

package main

import ."fmt"
//import "sort"
import "bufio"
import "os"
//import ."math"

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

func main() {
    defer writer.Flush()

    var t int
    Fscan(reader, &t)
    for i := 0; i < t; i++ {
        solve()
    }
}
func solve(){
    var a, b int
    Fscan(reader, &a)
    Fscan(reader, &b)
    Fprint(writer, abs(a - b), " ")
    if b > a {
        temp := a
        a = b
        b = temp
    }
    if a == b {
        Fprintln(writer, 0)
        return
    }
    ost := b % abs(a - b)
    ost = abs(a - b) - ost
    if ost == abs(a - b) {
        ost = 0
    }
    Fprintln(writer, ost)
}
func abs(x int) int {
    if x < 0 {
        return 0 - x
    }
    return x
}

package main

import "fmt"

func main() {
	var n int64
	fmt.Scan(&n)
	result := make([]string, 0, n)
	for i := int64(0); i < n; i++ {
		var a,b int64
		_, err := fmt.Scanf("%v %v", &a, &b)
		if err != nil {
			fmt.Printf("%v\n", err)
		}
		resg := int64(0)
		resc := int64(0)
		if a == b {
			resg = 0
			resc = 0
		} else if a > b {
			resg = a - b
			resc = b % resg
			resc = min(resc, resg - resc)
		} else {
			resg = b - a
			resc = a % resg
			resc = min(resc, resg - resc)
		}
		result = append(result, fmt.Sprintf("%v %v\n", resg, resc))
	}
	for _, str := range result {
		fmt.Printf(str)
	}
}

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

package main

import "fmt"

func main() {
	var n int64
	fmt.Scan(&n)
	result := make([]string, 0, n)
	for i := int64(0); i < n; i++ {
		var a,b int64
		fmt.Scanf("%v %v", &a, &b)
		resg := int64(0)
		resc := int64(0)
		if a == b {
			resg = 0
			resc = 0
		} else if a > b {
			resg = a - b
			resc = b % resg
			resc = min(resc, resg - resc)
		} else {
			resg = b - a
			resc = a % resg
			resc = min(resc, resg - resc)
		}
		result = append(result, fmt.Sprintf("%v %v\n", resg, resc))
	}
	for _, str := range result {
		fmt.Printf(str)
	}
}

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

package main

import "fmt"

func main() {
	var n int64
	fmt.Scan(&n)
	for i := int64(0); i < n; i++ {
		var a,b int64
		fmt.Scanf("%v %v", &a, &b)
		resg := int64(0)
		resc := int64(0)
		if a == b {
			resg = 0
			resc = 0
		} else if a > b {
			resg = a - b
			resc = b % resg
			resc = min(resc, resg - resc)
		} else {
			resg = b - a
			resc = a % resg
			resc = min(resc, resg - resc)
		}
		fmt.Printf("%v %v\n", resg, resc)
	}

}

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

package main
import(
	"bufio"
	"fmt"
	"os"
)

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

func main() {
	defer writer.Flush()
	var t int
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	var a, b int
	fmt.Fscan(reader, &a, &b)
	d := abs(a - b)
	if a == b {
		fmt.Fprintln(writer, "0 0")
	} else {
		k := b % d
		kk := d - (b % d)
		fmt.Fprintln(writer, d, min( k, kk))
	}

}

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

func min(nums ...int) int {
	l := len(nums)
	mn := nums[0]
	for i := 0; i < l; i++ {
		if nums[i] < mn {
			mn = nums[i]
		}
	}
	return mn
}

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	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 sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		a := sint64()
		b := sint64()
		x, o := sol(a, b)
		printf("%d %d\n", x, o)
	}
}

func sol(a, b int64) (int64, int64) {
	diff := abs(a - b)
	if diff == 0 {
		return 0, 0
	}
	return diff, round(a, diff) - a
}

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

func round(a, n int64) int64 {
	if a%n == 0 {
		return a
	}
	return a/n*n + n
}


package main

import (
	"fmt"
)

type Monitor struct {
	x int
	y int
}

func main() {
	var cnt, x, y int
	recievedList := []Monitor{}
	answer := 0

	_, _ = fmt.Scan(&cnt)
	for t := 0; t < cnt; t++ {
		_, _ = fmt.Scan(&x, &y)
		for _, v := range recievedList {
			if (v.x == x) || (v.x == y) || (v.y == x) || (v.y == y) {
				answer++
			}
		}
		recievedList = append(recievedList, Monitor{x, y})
	}
	fmt.Println(answer)
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: bufio.NewWriter(out)}
}

func (io *IO) ReadInt64() int64 {
	var res int64
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt64() (int64, int64) {
	var a, b int64
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) []int64 {
	var t int64
	res := make([]int64, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

type pair struct{a, b int}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	mp := make(map[int]int)
	mpp := make(map[pair]int)
	n := io.ReadInt()

	var ans int64
	for i := 0; i < n; i++ {
		a, b := io.ReadPairInt()
		if a > b {
			a, b = b, a
		}

		ans += int64(mp[a])
		mp[a]++
		if a != b {
			ans += int64(mp[b])
			ans -= int64(mpp[pair{a, b}])
			mp[b]++
			mpp[pair{a, b}]++
		}
	}

	fmt.Println(ans)
}

package main

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

type mon struct {
	w, h int
}

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

	var n int

	fmt.Fscan(r, &n)

	mons := make([]mon, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &mons[i].w, &mons[i].h)
	}

	var count int64 = 0

	same := make(map[[2]int]int)
	dims := make(map[int]int64)

	for i := 0; i < n; i++ {
		m := mons[i]
		dims[m.w]++

		if m.w != m.h {
			dims[m.h]++

			same[[2]int{max(m.h, m.w), min(m.h, m.w)}]++
		}
	}

	for i := 0; i < n; i++ {
		m := mons[i]

		h := m.h
		w := m.w

		if h == w {
			count += dims[w] - 1
			dims[w]--

		} else {
			hs := dims[h] - 1
			ws := dims[w] - 1

			count += hs
			count += ws

			count -= int64(same[[2]int{max(h, w), min(h, w)}]) - 1

			same[[2]int{max(h, w), min(h, w)}]--
			dims[h]--
			dims[w]--
		}
	}

	fmt.Println(count)
}

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"
	"bytes"
	"fmt"
	"os"
	"strconv"
)

func main() {
	// first()
	second()
}

type Monitor struct {
	h, w int
	pos int
}

func second() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h,w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	pairs := 0
	for i := 0; i < countMonitors; i++ {
		for j := i+1; j < countMonitors; j++ {
			if monitors[i].h == monitors[j].h || monitors[i].h == monitors[j].w ||
				monitors[i].w == monitors[j].h || monitors[i].w == monitors[j].w {
				pairs++
			}
		}
	}
	fmt.Print(pairs)
}

func getHW(reader *bufio.Reader) (int, int) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.Atoi(string(hw[0]))
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.Atoi(string(hw[1]))
	if err != nil {
		panic(err.Error())
	}
	return h,w
}

func first() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	sideToNumMonitor := make(map[int][]int, 0)
	pairs := make(map[int]map[int]struct{})

	for i := 0; i < countMonitors; i++ {
		sizeMonitorLine, _, err := reader.ReadLine()
		if err != nil {
			panic(err.Error())
		}

		hw := bytes.Split(sizeMonitorLine, []byte(" "))

		h, err := strconv.Atoi(string(hw[0]))
		if err != nil {
			panic(err.Error())
		}

		w, err := strconv.Atoi(string(hw[1]))
		if err != nil {
			panic(err.Error())
		}

		if h == w {
			if monitors, ok := sideToNumMonitor[h]; ok {
				for _, numMonitor := range monitors {
					if len(pairs[i]) == 0 {
						pairs[i] = make(map[int]struct{})
					}
					pairs[i][numMonitor]= struct{}{}
				}
			}
			sideToNumMonitor[h] = append(sideToNumMonitor[h], i)
			continue
		}

		if monitors, ok := sideToNumMonitor[h]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[h] = append(sideToNumMonitor[h], i)

		if monitors, ok := sideToNumMonitor[w]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[w] = append(sideToNumMonitor[w], i)
	}

	result := 0
	for _, count := range pairs {
		result+=len(count)
	}
	fmt.Print(result)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	sideToNumMonitor := make(map[int][]int, 0)
	pairs := make(map[int]map[int]struct{})

	for i := 0; i < countMonitors; i++ {
		sizeMonitorLine, _, err := reader.ReadLine()
		if err != nil {
			panic(err.Error())
		}

		hw := bytes.Split(sizeMonitorLine, []byte(" "))

		h, err := strconv.Atoi(string(hw[0]))
		if err != nil {
			panic(err.Error())
		}

		w, err := strconv.Atoi(string(hw[1]))
		if err != nil {
			panic(err.Error())
		}

		if h == w {
			if monitors, ok := sideToNumMonitor[h]; ok {
				for _, numMonitor := range monitors {
					if len(pairs[i]) == 0 {
						pairs[i] = make(map[int]struct{})
					}
					pairs[i][numMonitor]= struct{}{}
				}
			}
			sideToNumMonitor[h] = append(sideToNumMonitor[h], i)
			continue
		}

		if monitors, ok := sideToNumMonitor[h]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[h] = append(sideToNumMonitor[h], i)

		if monitors, ok := sideToNumMonitor[w]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[w] = append(sideToNumMonitor[w], i)
	}

	result := 0
	for _, count := range pairs {
		result+=len(count)
	}
	fmt.Print(result)
}

package main

import "fmt"
import s "strings"
import "bufio"
import "os"
import "strconv"

func main(){

    disp := map[int]int{}

    dispr := map[int]map[int]int{}

    var c, k int = 0, 0

    fmt.Scanf("%d\n", &c)
    reader := bufio.NewReader(os.Stdin)

    for i := 0; i < c; i++ {
        var h, w int
        b, _ := reader.ReadString('\n')
        t := s.Replace(b,"\n","",1)
        d := s.Split(s.Replace(t,"\r","",1)," ")

        //fmt.Println(d)
        h, _ = strconv.Atoi(d[0])
        w, _ = strconv.Atoi(d[1])

        if dispr[h] == nil{
            dispr[h] = map[int]int{}
        }
        if h != w {
            k += disp[h]
            k += disp[w]
            k -= dispr[h][w]
            k -= dispr[w][h]
        }else{
            k += disp[h]
            //k -= dispr[h][w]
        }

        if h == w{
            disp[h]++
        }else{
            disp[h]++
            disp[w]++
        }

        dispr[h][w]++
    }

    fmt.Print(k)
}

package main

import "fmt"

func main(){

    displays := map[int][]int{}
    var c, k int = 0, 0

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

    for i := 0; i < c; i++ {
        var p1, p2 int
        fmt.Scanf("%d %d\n", &p1, &p2)

        for _, p := range displays {
            //fmt.Println(l, l1, p, p1)
            if p[0] == p1 || p[0] == p2 || p[1] == p1 || p[1] == p2{
                k++
            }
        }
        displays[i] = []int{p1, p2}
    }

    fmt.Print(k)
}

package main

import "fmt"

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

	var width []int
	var height []int
	for i := 0; i < n; i++ {
		var w, h int
		_, err := fmt.Scanf("%d %d\n", &w, &h)
		if err != nil {
			return
		}
		width = append(width, w)
		height = append(height, h)
	}

	result := 0
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if width[i] == width[j] || width[i] == height[j] || width[j] == height[i] || height[i] == height[j] {
				result++
			}
		}
	}

	fmt.Println(result)
}

package main

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

type mon struct {
	w, h int
}

func main() {

	r := bufio.NewReader(os.Stdin)

	var n int

	fmt.Fscan(r, &n)

	mons := make([]mon, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &mons[i].w, &mons[i].h)
	}

	var count int

	for i := 0; i < n; i++ {
		m1 := mons[i]
		for j := i + 1; j < n; j++ {
			m2 := mons[j]

			if m1.w == m2.w || m1.w == m2.h || m1.h == m2.w || m1.h == m2.h {
				count++
			}
		}
	}

	fmt.Println(count)
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int64
	pos  int64
}

type Pair struct {
	x, y int64
}

func NewPair(h,w int64) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := make(map[int64]int64, 2*countMonitors)
	pairsCounter := make(map[Pair]int64, countMonitors)
	counter := int64(0)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		if h == w {
			counter += countBySide[h]
			countBySide[h]++
			continue
		}

		pair := NewPair(h, w)

		counter += countBySide[h] + countBySide[w] - pairsCounter[pair]

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int64, int64) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.ParseInt(string(hw[0]), 10, 64)
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.ParseInt(string(hw[1]), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int64
	pos  int64
}

type Pair struct {
	x, y int64
}

func NewPair(h,w int64) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := make(map[int64]int64, countMonitors)
	pairsCounter := make(map[Pair]int64, countMonitors)
	counter := int64(0)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		if h == w {
			counter += countBySide[h]
			countBySide[h]++
			continue
		}

		pair := NewPair(h, w)

		counter += countBySide[h] + countBySide[w] - pairsCounter[pair]

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int64, int64) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.ParseInt(string(hw[0]), 10, 64)
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.ParseInt(string(hw[1]), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int64
	pos  int64
}

type Pair struct {
	x, y int64
}

func NewPair(h,w int64) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := map[int64]int64{}
	pairsCounter := map[Pair]int64{}
	counter := int64(0)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		if h == w {
			counter += countBySide[h]
			countBySide[h]++
			continue
		}

		pair := NewPair(h, w)

		countByH := countBySide[h]
		countByW := countBySide[w]
		countDuplicatedPairs := pairsCounter[pair]
		counter += countByH + countByW - countDuplicatedPairs

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int64, int64) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.ParseInt(string(hw[0]), 10, 64)
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.ParseInt(string(hw[1]), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int
	pos  int
}

type Pair struct {
	x, y int
}

func NewPair(h,w int) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := map[int]int{}
	pairsCounter := map[Pair]int{}
	counter := 0
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		pair := NewPair(h, w)

		if h == w {
			countByH := countBySide[h]
			counter += countByH

			pairsCounter[pair]++
			countBySide[h]++
			continue
		}

		countByH := countBySide[h]
		countByW := countBySide[w]
		countDuplicatedPairs := pairsCounter[pair]
		counter += countByH + countByW - countDuplicatedPairs

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int, int) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.Atoi(string(hw[0]))
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.Atoi(string(hw[1]))
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strconv"
	"sync"
	"sync/atomic"
)

func main() {
	// first()
	// second()
	// third()
	fourth()
}

type Monitor struct {
	h, w int
	pos  int
}

func fourth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	monitorsBySide := make(map[int][]int, countMonitors*(countMonitors/2))
	for i := 0; i < countMonitors; i++ {
		monitorsBySide[monitors[i].h] = append(monitorsBySide[monitors[i].h], monitors[i].pos)
		if monitors[i].h != monitors[i].w {
			monitorsBySide[monitors[i].w] = append(monitorsBySide[monitors[i].w], monitors[i].pos)
		}
	}

	pairs := make(map[int]struct{})
	for _, mntrs := range monitorsBySide {
		for i := 0; i < len(mntrs); i++ {
			for j := i + 1; j < len(mntrs); j++ {
				pairs[mntrs[i]+mntrs[j]] = struct{}{}
			}
		}
	}

	fmt.Print(len(pairs))
}

func third() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	pairs := int64(0)
	workers := make(chan struct{}, 4)
	wg := sync.WaitGroup{}
	for i := 0; i < countMonitors; i++ {
		workers <- struct{}{}
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			for j := i + 1; j < countMonitors; j++ {
				if monitors[i].h == monitors[j].h || monitors[i].h == monitors[j].w ||
					monitors[i].w == monitors[j].h || monitors[i].w == monitors[j].w {
					atomic.AddInt64(&pairs, 1)
				}
			}
			<-workers
		}(i)
	}
	wg.Wait()
	fmt.Print(atomic.LoadInt64(&pairs))
}

func second() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	pairs := 0
	for i := 0; i < countMonitors; i++ {
		for j := i + 1; j < countMonitors; j++ {
			if monitors[i].h == monitors[j].h || monitors[i].h == monitors[j].w ||
				monitors[i].w == monitors[j].h || monitors[i].w == monitors[j].w {
				pairs++
			}
		}
	}
	fmt.Print(pairs)
}

func getHW(reader *bufio.Reader) (int, int) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.Atoi(string(hw[0]))
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.Atoi(string(hw[1]))
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

func first() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	sideToNumMonitor := make(map[int][]int, 0)
	pairs := make(map[int]map[int]struct{})

	for i := 0; i < countMonitors; i++ {
		sizeMonitorLine, _, err := reader.ReadLine()
		if err != nil {
			panic(err.Error())
		}

		hw := bytes.Split(sizeMonitorLine, []byte(" "))

		h, err := strconv.Atoi(string(hw[0]))
		if err != nil {
			panic(err.Error())
		}

		w, err := strconv.Atoi(string(hw[1]))
		if err != nil {
			panic(err.Error())
		}

		if h == w {
			if monitors, ok := sideToNumMonitor[h]; ok {
				for _, numMonitor := range monitors {
					if len(pairs[i]) == 0 {
						pairs[i] = make(map[int]struct{})
					}
					pairs[i][numMonitor] = struct{}{}
				}
			}
			sideToNumMonitor[h] = append(sideToNumMonitor[h], i)
			continue
		}

		if monitors, ok := sideToNumMonitor[h]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor] = struct{}{}
			}
		}
		sideToNumMonitor[h] = append(sideToNumMonitor[h], i)

		if monitors, ok := sideToNumMonitor[w]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor] = struct{}{}
			}
		}
		sideToNumMonitor[w] = append(sideToNumMonitor[w], i)
	}

	result := 0
	for _, count := range pairs {
		result += len(count)
	}
	fmt.Print(result)
}