Let's denote the Manhattan distance between two points $$$p_1$$$ (with coordinates $$$(x_1, y_1)$$$) and $$$p_2$$$ (with coordinates $$$(x_2, y_2)$$$) as $$$d(p_1, p_2) = |x_1 - x_2| + |y_1 - y_2|$$$. For example, the distance between two points with coordinates $$$(1, 3)$$$ and $$$(4, 2)$$$ is $$$|1 - 4| + |3 - 2| = 4$$$.You are given two points, $$$A$$$ and $$$B$$$. The point $$$A$$$ has coordinates $$$(0, 0)$$$, the point $$$B$$$ has coordinates $$$(x, y)$$$.Your goal is to find a point $$$C$$$ such that:  both coordinates of $$$C$$$ are non-negative integers;  $$$d(A, C) = \dfrac{d(A, B)}{2}$$$ (without any rounding);  $$$d(B, C) = \dfrac{d(A, B)}{2}$$$ (without any rounding). Find any point $$$C$$$ that meets these constraints, or report that no such point exists.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 3000$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$0 \le x, y \le 50$$$) — the coordinates of the point $$$B$$$.

For each test case, print the answer on a separate line as follows:   if it is impossible to find a point $$$C$$$ meeting the constraints, print "-1 -1" (without quotes);  otherwise, print two non-negative integers not exceeding $$$10^6$$$ — the coordinates of point $$$C$$$ meeting the constraints. If there are multiple answers, print any of them. It can be shown that if any such point exists, it's possible to find a point with coordinates not exceeding $$$10^6$$$ that meets the constraints.

NoteExplanations for some of the test cases from the example:  In the first test case, the point $$$B$$$ has coordinates $$$(49, 3)$$$. If the point $$$C$$$ has coordinates $$$(23, 3)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$|49 - 0| + |3 - 0| = 52$$$, the distance from $$$A$$$ to $$$C$$$ is $$$|23 - 0| + |3 - 0| = 26$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$|23 - 49| + |3 - 3| = 26$$$.  In the second test case, the point $$$B$$$ has coordinates $$$(2, 50)$$$. If the point $$$C$$$ has coordinates $$$(1, 25)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$|2 - 0| + |50 - 0| = 52$$$, the distance from $$$A$$$ to $$$C$$$ is $$$|1 - 0| + |25 - 0| = 26$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$|1 - 2| + |25 - 50| = 26$$$.  In the third and the fourth test cases, it can be shown that no point with integer coordinates meets the constraints.  In the fifth test case, the point $$$B$$$ has coordinates $$$(42, 0)$$$. If the point $$$C$$$ has coordinates $$$(21, 0)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$|42 - 0| + |0 - 0| = 42$$$, the distance from $$$A$$$ to $$$C$$$ is $$$|21 - 0| + |0 - 0| = 21$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$|21 - 42| + |0 - 0| = 21$$$.

package main

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

func main() {

	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)

	t := 0
	fmt.Fscan(f, &t)

	for t > 0 {

		x, y := 0, 0
		fmt.Fscan(f, &x, &y)
		x, y = solve(x, y)

		fmt.Fprintln(w, x, y)
		w.Flush()

		t--
	}

	// x, y := 6, 8
	// solve(x, y)
}

func solve(x, y int) (a int, b int) {

	num := x + y

	if num%2 == 0 {

		a, b = 0, num/2

		for !(a <= x && b <= y) {
			a++
			b--
		}

	} else {
		a, b = -1, -1
	}

	return
}

package main

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

var x, y int

var a []int
var b []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &x, &y)

		a, b := solve()

		fmt.Println(a, b)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() (int, int) {
	if (x + y) % 2 == 1  {
		return -1, -1
	}

	if x >= y {
		return (x + y) /2,  0
	}
	return 0, (x + y) /2
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

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

func (in *CF1612A) 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 *CF1612A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612A
Date: 10/02/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1612/A
Problem: CF1612A
**/
func (in *CF1612A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		x, y := in.NextInt(), in.NextInt()
		if (x+y)%2 == 0 {
			div := (x + y) / 2
			a := int(math.Floor(float64(x) / 2.))
			b := int(math.Abs(float64(a) - float64(div)))
			fmt.Println(a, b)

		} else {
			fmt.Println("-1 -1")
		}
	}
}

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

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

package main

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

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

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

	dist := func(x1, y1, x2, y2 int) int {
		return abs(x2-x1) + abs(y2-y1)
	}

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

		if (x+y)%2 != 0 {
			fmt.Fprintln(out, "-1 -1")
		} else {
			z := (x + y) / 2
			for i := 0; i <= z; i++ {
				if dist(z-i, i, x, y) == z {
					fmt.Fprintf(out, "%d %d\n", z-i, i)
					break
				}
			}
		}
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, x, y := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &x, &y)
		if (x+y)%2 == 0 {
			o1, o2 := x%2 != 0, y%2 != 0
			if o1 && o2 {
				fmt.Fprintln(w, x>>1, (y>>1)+1)
			} else if o1 {
				fmt.Fprintln(w, x>>1, 1)
			} else if o2 {
				fmt.Fprintln(w, 1, y>>1)
			} else {
				fmt.Fprintln(w, x>>1, y>>1)
			}
		} else {
			fmt.Fprintln(w, "-1 -1")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

import "fmt"

func solution(x, y int) (int, int) {
	if (x + y) % 2 == 1 { return -1, -1 }
	if x > y { return (x + y) / 2, 0 }
	return 0, (x + y) / 2
}

func main() {
	var t int
	fmt.Scanln(&t)
	var answers = make([][2]int, t)
	for i := range answers {
		var x, y int
		fmt.Scanln(&x, &y)
		answers[i][0], answers[i][1] = solution(x, y)
	}
	for _, ans := range answers {
		fmt.Println(ans[0], ans[1])
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

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

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

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

func DIVI(x []int) string {
	if len(x) == 1 {
		if x[0]%2 == 0 {
			return "NO"
		} else {
			return "YES"

		}
	} else {
		q := len(x)
		if DIVI(x[:q-1]) == "YES" {
			b := 0
			for j := 2; j < q+2; j++ {
				if x[q-1]%j != 0 {
					b = 1
					break
				}
			}
			if b == 1 {
				return "YES"
			} else {
				return "NO"

			}
		} else {
			return "NO"
		}
	}
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {

		var x, y int
		fmt.Fscan(input, &x, &y)
		if (x+y)%2 == 1 {
			fmt.Println(-1)
			fmt.Println(-1)

		} else {
			if x%2 == 0 {
				fmt.Println(x / 2)
				fmt.Println(y / 2)
			} else {
				fmt.Println(x / 2)
				fmt.Println((y / 2) + 1)
			}
		}
	}
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, x, y int
)

func solve(x, y int) string {
	if (x+y)%2 == 1 {
		return "-1 -1"
	}
	h := (x + y) / 2
	if x > y {
		return fmt.Sprintf("%d %d", h, 0)
	} else {
		return fmt.Sprintf("%d %d", 0, h)
	}
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &x, &y)
		fmt.Println(solve(x, y))
	}
}

package main
import."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T, x, y int
	for Scan(&T); T > 0; T-- {
		Scan(&x, &y)
		if x&1 != y&1 {
			Println(-1, -1)
		} else {
			Println((x+1)/2, y/2)
		}
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)

func abs(a, b int) int {
	if a > b {
		return a-b
	} else {
		return b-a
	}
}

func d(x, y, x1, y1 int) int {
	return abs(x,x1) + abs(y,y1)
}

func solve(){
	var x, y int
	fmt.Fscan(r, &x, &y)

	for cx := 0; cx <= 50; cx++ {
		for cy := 0; cy <= 50; cy++ {
			if d(0, 0, cx, cy) == d(cx, cy, x, y) {
				fmt.Println(cx, cy)
				return
			}
		}
	}

	fmt.Println(-1, -1)
}

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

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var x, y int
    fmt.Fscan(input, &x, &y)
    if (x + y) % 2 == 1 {
        fmt.Fprintln(output, -1, -1)
        return
    }
    cx, cy := x / 2, y / 2
    if cx + cy != (x + y) /2 {cx++}

    fmt.Fprintln(output, cx, cy)
}

package main

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

func abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func main() {
	r := bufio.NewReader(os.Stdin)

	n := mustReadInt(r)
	for i := int64(0); i < n; i++ {
		nums := mustReadNNumbs(r, 3)
		n1, n2 := nums[0], nums[1]
		if (n1+n2)%2 == 1 {
			fmt.Println("-1 -1")
		} else {
			if n1%2 == 0 && n2%2 == 0 {
				fmt.Printf("%d %d\n", abs(n1/2), abs(n2/2))
			} else {
				dAB := n1 + n2
				if dAB/2 <= n1 {
					fmt.Printf("%d %d\n", abs(dAB/2), 0)
				} else {
					fmt.Printf("%d %d\n", n1, abs(dAB/2-n1))
				}
			}
		}
	}
}

func mustReadNNumbs(r *bufio.Reader, n int64) []int64 {
	nums := make([]int64, n)
	var err error
	for i, s := range strings.Split(mustReadLine(r), " ") {
		nums[i], err = strconv.ParseInt(s, 10, 64)
		checkError(err)
	}
	return nums
}

func mustReadInt(r *bufio.Reader) int64 {
	numb, err := strconv.ParseInt(mustReadLine(r), 10, 64)
	checkError(err)
	return numb
}

func mustReadLine(r *bufio.Reader) string {
	s, _, err := r.ReadLine()
	checkError(err)
	return string(s)
}

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

package main

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

func main() {

	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)

	t := 0
	fmt.Fscan(f, &t)

	for t > 0 {

		x, y := 0, 0
		fmt.Fscan(f, &x, &y)
		x, y = solve(x, y)

		fmt.Fprintln(w, x, y)
		w.Flush()

		t--
	}

	// x, y := 6, 8
	// solve(x, y)
}

func solve(x, y int) (a int, b int) {

	num := x + y

	if num%2 == 0 {
		num /= 2
		a, b = 0, num
	} else {
		a, b = -1, -1
	}

	return
}

package main

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

var x, y int

var a []int
var b []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &x, &y)

		a, b := solve()

		fmt.Println(a, b)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() (int, int) {
	if (x + y) % 2 == 1 {
		return -1, -1
	}

	return 0, (x + y) /2
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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


A sequence of positive integers is called great for a positive integer $$$x$$$, if we can split it into pairs in such a way that in each pair the first number multiplied by $$$x$$$ is equal to the second number. More formally, a sequence $$$a$$$ of size $$$n$$$ is great for a positive integer $$$x$$$, if $$$n$$$ is even and there exists a permutation $$$p$$$ of size $$$n$$$, such that for each $$$i$$$ ($$$1 \le i \le \frac{n}{2}$$$) $$$a_{p_{2i-1}} \cdot x = a_{p_{2i}}$$$. Sam has a sequence $$$a$$$ and a positive integer $$$x$$$. Help him to make the sequence great: find the minimum possible number of positive integers that should be added to the sequence $$$a$$$ to make it great for the number $$$x$$$.

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

For each test case print a single integer — the minimum number of integers that can be added to the end of $$$a$$$ to make it a great sequence for the number $$$x$$$.

NoteIn the first test case, Sam got lucky and the sequence is already great for the number $$$4$$$ because you can divide it into such pairs: $$$(1, 4)$$$, $$$(4, 16)$$$. Thus we can add $$$0$$$ numbers.In the second test case, you can add numbers $$$1$$$ and $$$14$$$ to the sequence, then you can divide all $$$8$$$ integers into such pairs: $$$(1, 2)$$$, $$$(1, 2)$$$, $$$(2, 4)$$$, $$$(7, 14)$$$. It is impossible to add less than $$$2$$$ integers to fix the sequence.

package main

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

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

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

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

		sort.Slice(arr, func(i, j int) bool {
			return arr[i] < arr[j]
		})
		m := make([]bool, n)
		c := n
		p := 1
		for i := 0; i < n; i++ {
			if m[i] {
				continue
			}

			for p < n && arr[p] <= arr[i]*x {
				if !m[p] && arr[p] == arr[i]*x {
					c -= 2
					m[p] = true
					break
				}
				p++
			}
		}
		fmt.Fprintln(out, c)
	}
}

package main

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

/*
2
6 2
1 6 5 8 3 4
6 2
1 4 8 8 8 16
---
2 = cnt(2, 10)
2 = cnt(2, 16)
======
1
6 3
1 3 3 3 3 3
---
4
=======
1
4 1000000
1 1000000 1000000 1000001
---
2
*/

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

	nums := make([]int, n)
	numToCnt := make(map[int]int)

	for i := range nums {
		scanf("%d", &nums[i])

		numToCnt[nums[i]]++
	}
	scanf("\n")

	sort.Ints(nums)

	cnt := 0

	for _, lft := range nums {
		_, ok := numToCnt[lft]
		if !ok {
			continue
		}

		product := int64(lft) * x
		if product <= 1e9 {
			rgt := int(product)

			if _, ok := numToCnt[rgt]; ok {
				numToCnt[lft]--
				if numToCnt[lft] == 0 {
					delete(numToCnt, lft)
				}

				numToCnt[rgt]--
				if numToCnt[rgt] == 0 {
					delete(numToCnt, rgt)
				}
			} else {
				cnt++

				numToCnt[lft]--
				if numToCnt[lft] == 0 {
					delete(numToCnt, lft)
				}
			}
		} else {
			cnt++

			numToCnt[lft]--
			if numToCnt[lft] == 0 {
				delete(numToCnt, lft)
			}
		}
	}

	fmt.Fprintln(writer, cnt)
}

//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 ...int64) int64 {
	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"
	"io"
	"os"
	"sort"
)

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

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

	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n, x := uint64(0), uint64(0)
		fmt.Fscan(in, &n, &x)
		arr := make([]uint64, n)
		mp := make(map[uint64]int)
		for i := uint64(0); i < n; i++ {
			fmt.Fscan(in, &arr[i])
			mp[arr[i]]++
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})
		ans := 0
		for _, v := range arr {
			if mp[v] <= 0 {
				continue
			}
			if v%x != 0 {
				ans++
				mp[v]--
			} else if mp[v/x] <= 0 {
				ans++
				mp[v]--
			} else {
				mp[v]--
				mp[v/x]--
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		var x int64
		fmt.Fscan(in, &n, &x)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		sort.Ints(a)

		extra := 0
		i, j := 0, 1
		for i < n {
			xai := x * int64(a[i])
			if j < n && xai == int64(a[j]) {
				a[j] = -1
				i++
				j++
				continue
			}
			if j < n && xai > int64(a[j]) {
				j++
				continue
			}
			if a[i] == -1 {
				i++
				continue
			}
			extra++
			i++
		}
		fmt.Fprintln(out, extra)
	}
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

//}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

func Solve() {
	var n, tp, x int
	scan(&n, &x)
	count := make(map[int]int)
	for i := 1; i <= n; i++ {
		scan(&tp)
		count[tp]++
	}
	for _, k := range sortedKey(count) {
		if int64(k)*int64(x) > 1000_000_000 {
			continue
		}
		m := min(count[k*x], count[k])
		count[k*x] -= m
		count[k] -= m
	}
	sum := 0
	for _, v := range count {
		sum += v
	}
	fmt.Println(sum)
}

func sortedKey(m map[int]int) []int {
	sortedKey := make([]int, 0, len(m))
	for k := range m {
		sortedKey = append(sortedKey, k)
	}
	sort.Ints(sortedKey)
	return sortedKey
}

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

package main

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

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

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

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

		sort.Ints(arr)
		m := make([]bool, n)
		c := n
		p := 1
		for i := 0; i < n; i++ {
			if m[i] {
				continue
			}

			for p < n && arr[p] <= arr[i]*x {
				if !m[p] && arr[p] == arr[i]*x {
					c -= 2
					m[p] = true
					break
				}
				p++
			}
		}
		fmt.Fprintln(out, c)
	}
}

package main

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

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

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

		m := make(map[int64]int)
		var c int
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if x == 1 {
				if m[v] > 0 {
					m[v]--
					c--
				} else {
					m[v]++
					c++
				}
			} else {
				if m[v/x] > 0 {
					m[v/x]--
					c--
				} else if m[v*x] > 0 {
					m[v*x]--
					c--
				} else {
					m[v]++
					c++
				}
			}
		}

		fmt.Fprintln(out, c)
	}
}

package main

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

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

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

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

		m := make(map[int64]int64)
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)
			m[v]++
		}

		c := n
		for k, v := range m {
			if m[k/x] > 0 {
				d := min(v, m[k/x])
				c -= int(d * 2)
				v -= d
				m[k/x] -= d
			}

			if v > 0 && m[k*x] > 0 {
				d := min(v, m[k*x])
				c -= int(d * 2)
				v -= d
				m[k*x] -= d
			}

			m[k] = v
		}
		fmt.Fprintln(out, c)
	}
}

package main

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

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

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

		m := make(map[int64]int)
		var c int
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if m[v/x] > 0 {
				m[v/x]--
				c--
			} else if m[v*x] > 0 {
				m[v*x]--
				c--
			} else {
				m[v]++
				c++
			}
		}

		fmt.Fprintln(out, c)
	}
}

package main

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

/*
2
6 2
1 6 5 8 3 4
6 2
1 4 8 8 8 16
---
2 = cnt(2, 10)
2 = cnt(2, 16)
======
1
6 3
1 3 3 3 3 3
---
4
*/

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

	nums := make([]int, n)
	numToCnt := make(map[int]int)

	for i := range nums {
		scanf("%d", &nums[i])

		numToCnt[nums[i]]++
	}
	scanf("\n")

	sort.Ints(nums)

	cnt := 0

	for _, lft := range nums {
		_, ok := numToCnt[lft]
		if !ok {
			continue
		}

		product := int64(lft) * x
		if product <= 1e9 {
			rgt := int(product)

			if _, ok := numToCnt[rgt]; ok {
				numToCnt[lft]--
				if numToCnt[lft] == 0 {
					delete(numToCnt, lft)
				}

				numToCnt[rgt]--
				if numToCnt[rgt] == 0 {
					delete(numToCnt, rgt)
				}
			} else {
				cnt++

				numToCnt[lft]--
				if numToCnt[lft] == 0 {
					delete(numToCnt, lft)
				}
			}
		} else {
			cnt++
		}
	}

	fmt.Fprintln(writer, cnt)
}

//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 ...int64) int64 {
	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))
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

//}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

func Solve() {
	var n, tp, x int
	scan(&n, &x)
	count := make(map[int]int)
	for i := 1; i <= n; i++ {
		scan(&tp)
		count[tp]++
	}
	for _, k := range sortedKey(count) {
		m := min(count[k*x], count[k])
		count[k*x] -= m
		count[k] -= m
	}
	sum := 0
	for _, v := range count {
		sum += v
	}
	fmt.Println(sum)
}

func sortedKey(m map[int]int) []int {
	sortedKey := make([]int, 0, len(m))
	for k := range m {
		sortedKey = append(sortedKey, k)
	}
	sort.Ints(sortedKey)
	return sortedKey
}

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


In the probability theory the following paradox called Benford's law is known: "In many lists of random numbers taken from real sources, numbers starting with digit 1 occur much more often than numbers starting with any other digit" (that's the simplest form of the law).Having read about it on Codeforces, the Hedgehog got intrigued by the statement and wishes to thoroughly explore it. He finds the following similar problem interesting in particular: there are N random variables, the i-th of which can take any integer value from some segment [Li;Ri] (all numbers from this segment are equiprobable). It means that the value of the i-th quantity can be equal to any integer number from a given interval [Li;Ri] with probability 1 / (Ri - Li + 1).The Hedgehog wants to know the probability of the event that the first digits of at least K% of those values will be equal to one. In other words, let us consider some set of fixed values of these random variables and leave only the first digit (the MSD — most significant digit) of each value. Then let's count how many times the digit 1 is encountered and if it is encountered in at least K per cent of those N values, than such set of values will be called a good one. You have to find the probability that a set of values of the given random variables will be a good one.

The first line contains number N which is the number of random variables (1 ≤ N ≤ 1000). Then follow N lines containing pairs of numbers Li, Ri, each of whom is a description of a random variable. It is guaranteed that 1 ≤ Li ≤ Ri ≤ 1018. The last line contains an integer K (0 ≤ K ≤ 100). All the numbers in the input file are integers. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).

Print the required probability. Print the fractional number with such a precision that the relative or absolute error of the result won't exceed 10 - 9.

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"bytes"
)

func oneCount(x int64) int64 {
	var ret int64
	for i := int64(1);;i = i * 10 {
		if x >= 2 * i {
			ret += i
		} else if x >= i {
			ret += (x - i + 1)
			return ret
		} else{
			return ret
		}
	}
}

var dp [1000][1001]float64
func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	one := make([]float64, n)

	for i := 0;i < n;i ++ {
		var l, r int64
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		one[i] = float64(oneCount(r) - oneCount(l - 1)) / float64(r - l + 1)
	}
	dp[0][0] = 1 - one[0]
	dp[0][1] = one[0]

	for i := 1;i < n;i ++ {
		for j := 0;j <= n;j ++ {
			if j == 0 {
				dp[i][0] = dp[i - 1][0] * (1.0 - one[i])
			} else {
				dp[i][j] = dp[i - 1][j - 1] * one[i] + dp[i - 1][j] * (1.0 - one[i])
			}
		}
	}
	var k int
	fmt.Fscanf(reader, "%d", &k)
	ans := 0.0
	for i := (n * k + 99) / 100;i <= n;i ++ {
		ans += dp[n - 1][i]
	}
	fmt.Printf("%.9f", ans)

}


William has an array of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. In one move he can swap two neighboring items. Two items $$$a_i$$$ and $$$a_j$$$ are considered neighboring if the condition $$$|i - j| = 1$$$ is satisfied.William wants you to calculate the minimal number of swaps he would need to perform to make it so that the array does not contain two neighboring items with the same parity.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ which is the total number of items in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ which are William's array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case output the minimal number of operations needed or $$$-1$$$ if it is impossible to get the array to a state when no neighboring numbers have the same parity.

NoteIn the first test case the following sequence of operations would satisfy the requirements:   swap(2, 3). Array after performing the operation: $$$[6, 1, 6]$$$ In the second test case the array initially does not contain two neighboring items of the same parity.In the third test case the following sequence of operations would satisfy the requirements:   swap(3, 4). Array after performing the operation: $$$[1, 1, 2, 1, 2, 2]$$$  swap(2, 3). Array after performing the operation: $$$[1, 2, 1, 1, 2, 2]$$$  swap(4, 5). Array after performing the operation: $$$[1, 2, 1, 2, 1, 2]$$$ In the fourth test case it is impossible to satisfy the requirements.In the fifth test case the following sequence of operations would satisfy the requirements:   swap(2, 3). Array after performing the operation: $$$[6, 3, 2, 4, 5, 1]$$$  swap(4, 5). Array after performing the operation: $$$[6, 3, 2, 5, 4, 1]$$$

package main

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

var (
	in = bufio.NewReader(os.Stdin)
)

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

}
func solve() {
	var n int
	fmt.Fscan(in, &n)
	var arr = make([]int, n)
	var numberOfOne = 0
	var numberOfZero = 0
	for i := 0; i < n; i++ {
		var currentNumber int64
		fmt.Fscan(in, &currentNumber)
		arr[i] = int(currentNumber % 2)
		if arr[i] == 0 {
			numberOfZero++
		} else {
			numberOfOne++
		}
	}
	if math.Abs(float64(numberOfOne-numberOfZero)) > 1 {
		fmt.Println(-1)
		return
	}
	var ans int64 = math.MaxInt64

	//if we consider 1 at the starting
	var currentValue = 1
	var needEven = make([]int, n)
	var needOdd = make([]int, n)
	if numberOfZero > numberOfOne {
		calcNumberOfOperation(arr, 0, &needEven, &needOdd)
		evenCost := calcCost(needEven, needOdd)
		ans = int64(int(math.Min(float64(evenCost), float64(ans))))
	} else if numberOfOne > numberOfZero {
		calcNumberOfOperation(arr, currentValue, &needEven, &needOdd)
		oddCost := calcCost(needEven, needOdd)
		ans = int64(int(math.Min(float64(oddCost), float64(ans))))
	} else {
		calcNumberOfOperation(arr, 0, &needEven, &needOdd)
		evenCost := calcCost(needEven, needOdd)
		ans = int64(int(math.Min(float64(evenCost), float64(ans))))

		needEven = nil
		needOdd = nil

		calcNumberOfOperation(arr, currentValue, &needEven, &needOdd)
		oddCost := calcCost(needEven, needOdd)
		ans = int64(int(math.Min(float64(oddCost), float64(ans))))

	}

	fmt.Println(ans)

}

func calcCost(needEven []int, needOdd []int) int {
	sort.Ints(needOdd)
	sort.Ints(needEven)
	var evenStart = 0
	var oddStart = 0
	var tempAns = 0
	for evenStart < len(needEven) && oddStart < len(needOdd) {
		tempAns += int(math.Abs(float64(needEven[evenStart] - needOdd[oddStart])))
		evenStart++
		oddStart++
	}
	return tempAns
}

func calcNumberOfOperation(arr []int, currentValue int, needEven *[]int, needOdd *[]int) {
	for i := 0; i < len(arr); i++ {
		if arr[i] != currentValue {
			if currentValue == 0 {
				*needEven = append(*needEven, i)
			} else {
				*needOdd = append(*needOdd, i)
			}
		}
		if currentValue == 1 {
			currentValue = 0
		} else {
			currentValue = 1
		}
	}
}

//x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-TEMPLATE_STARTING-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-

type pair struct {
	a int64
	b int64
}

func sortPair(pairs []pair) {
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].a < pairs[j].a
	})
}
func max(a int64, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	tc := readInt(in)

	for i := 0; i < tc; i++ {
        n := readInt(in)
        arr := readArrInt64(in)
        parity := make([]int, n)
        countEven, countOdd := 0, 0
        for j, v := range arr {
            parity[j] = int(v % 2)
            if parity[j] == 0 {
                countEven++
            } else {
                countOdd++
            }
        }

        startZero := false
        if n % 2 == 1 {
            if  countEven == n/2 + 1 && countOdd == n/2 {
                startZero = true
            } else if countEven == n/2 && countOdd == n/2+1 {
                startZero = false
            } else {
                fmt.Println(-1)
                continue
            }
        } else {
            if  countEven != n/2 && countOdd != n/2 {
                fmt.Println(-1)
                continue
            }
            if parity[0] == 0 {
                startZero = true
            } else {
                startZero = false
            }
        }

        //last1 in correct position
        ans := 0
        if n % 2 == 1 {
            if startZero {
                ans = compAns(1, parity)
            } else {
                ans = compAns(0, parity)
            }
            fmt.Println(ans)
        } else {
            ans0 := compAns(1, parity)
            ans1 := compAns(0, parity)
            if ans0 < ans1 {
                fmt.Println(ans0)
            } else {
                fmt.Println(ans1)
            }
        }
	}
}

func compAns(last1 int, parity []int) int {
    ans := 0
    for i, p := range parity {
        if p == 1 {
            if i != last1 {
                ans += abs(i-last1)
            }
            last1 += 2
        }
    }
    return ans
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

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

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		c1, s0, s1 := 0, 0, 0
		for i, p := 0, 0; i < n; i++ {
			if Fscan(in, &v); v&1 > 0 {
				c1++
				s0 += abs(i - p)
				s1 += abs(i - (p ^ 1))
				p += 2
			}
		}
		if abs(n-c1*2) > 1 {
			Fprintln(out, -1)
		} else if n&1 == 0 {
			Fprintln(out, min(s0, s1))
		} else if c1*2 > n {
			Fprintln(out, s0)
		} else {
			Fprintln(out, s1)
		}
	}
}

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

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

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

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int

	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(r, "%d", &arr[i])
		}
		fmt.Fscanf(r, "\n")
		p := make([][]int, 2)

		for i := 0; i < n; i++ {
			p[arr[i]%2] = append(p[arr[i]%2], i)
		}

		res := -1
		for i := 0; i <= 1; i++ {
			c := make([][]int, 2)
			c[0] = p[0]
			c[1] = p[1]
			cur := 0
			for j := 0; j < n; j++ {
				x := (j + i) % 2

				//	fmt.Println(j, x, c[x])

				if len(c[x]) == 0 {
					cur = -1
					break
				}

				if c[x][0] > j {
					cur += c[x][0] - j
				}
				c[x] = c[x][1:]
			}

			if res == -1 || (cur < res && cur != -1) {
				res = cur
			}
		}

		fmt.Println(res)
	}
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

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

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

func cost(v []int, first int) int64 {
	res := int64(0)
	pos := 0
	for i, x := range v {
		if x%2 == first {
			res += int64(abs(int(i - pos)))
			pos += 2
		}
	}
	return res
}

func solve(v []int) int64 {
	count := make([]int, 2)
	for _, x := range v {
		count[x%2]++
	}

	diff := count[0] - count[1]
	switch {
	case abs(diff) > 1:
		return -1
	case diff == 0:
		return min(cost(v, 0), cost(v, 1))
	case diff > 0:
		return cost(v, 0)
	default:
		return cost(v, 1)
	}
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i++ {
		n := stdin.Int()
		v := stdin.Ints(n)
		fmt.Println(solve(v))
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	tc := readInt(in)

	for i := 0; i < tc; i++ {
        n := readInt(in)
        arr := readArrInt64(in)
        parity := make([]int, n)
        countEven, countOdd := 0, 0
        for j, v := range arr {
            parity[j] = int(v % 2)
            if parity[j] == 0 {
                countEven++
            } else {
                countOdd++
            }
        }

        startZero := false
        if n % 2 == 1 {
            if  countEven == n/2 + 1 && countOdd == n/2 {
                startZero = true
            } else if countEven == n/2 && countOdd == n/2+1 {
                startZero = false
            } else {
                fmt.Println(-1)
                continue
            }
        } else {
            if  countEven != n/2 && countOdd != n/2 {
                fmt.Println(-1)
                continue
            }
            if parity[0] == 0 {
                startZero = true
            } else {
                startZero = false
            }
        }

        //last1 in correct position
        last1 := 0
        ans := 0
        if startZero {
            last1 = 1
            for i, p := range parity {
                if p == 1 {
                    if i != last1 {
                        ans += abs(i-last1)
                    }
                    last1 += 2
                }
            }
        } else {
            last1 = 0
            for i, p := range parity {
                if p == 1 {
                    if i != last1 {
                        ans += abs(i-last1)
                    }
                    last1 += 2
                }
            }
        }
        fmt.Println(ans)
	}
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

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


Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.

First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.

For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.

NoteExplanation of first test case:   [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".  [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".  [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".  [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".  [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".

// Try Codeforces
// author: Leonardone @ NEETSDKASU
package main

import (
	"fmt"
)

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	p := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p[i])
	}
	for i := 0; i < m; i++ {
		var l, r, x int
		fmt.Scan(&l, &r, &x)
		c := 0
		for _, z := range p[l-1 : r] {
			if z < p[x-1] {
				c++
			}
		}
		if l+c == x {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	nm := strings.Split(sc.Text(), " ")
	n, _ := strconv.Atoi(nm[0])
	m, _ := strconv.Atoi(nm[1])

	p := make([]int, n)
	sc.Scan()
	for i, pi := range strings.Split(sc.Text(), " ") {
		p[i], _ = strconv.Atoi(pi)
	}
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < m; i++ {
		sc.Scan()
		lrx := strings.Split(sc.Text(), " ")
		l, _ := strconv.Atoi(lrx[0])
		r, _ := strconv.Atoi(lrx[1])
		x, _ := strconv.Atoi(lrx[2])
		l--
		r--
		x--
		j, c := l, 0
		for c <= x-l+1 && j <= r {
			if p[j] <= p[x] {
				c++
			}
			j++
		}
		if c == x-l+1 {
			fmt.Fprint(w, "\nYes")
		} else {
			fmt.Fprint(w, "\nNo")
		}
	}
	w.Flush()
}

func readln(r *bufio.Reader) (string, error) {
	var (
		isPrefix bool  = true
		err      error = nil
		line, ln []byte
	)
	for isPrefix && err == nil {
		line, isPrefix, err = r.ReadLine()
		ln = append(ln, line...)
	}
	return string(ln), err
}

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

const debug = true

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

	m := gsli(reader)[1]
	p := gsli(reader)

	for i := 0; i < m; i++ {
		cur := gsli(reader)
		smaller := 0

		l := cur[0]
		r := cur[1]
		x := cur[2]

		for j := l - 1; j < r; j++ {
			if p[j] < p[x-1] {
				smaller++
			}
		}
		if smaller == x-l {
			yes()
		} else {
			no()
		}
	}

}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

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

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}

package main

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

const (
	MaxN = 10009
)

var p [MaxN]int
var n, m int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d\n", &n, &m)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &p[i])
	}
	fmt.Fscanf(buf, "\n")
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 0; i < m; i++ {
		var l, r, x int
		fmt.Fscanf(buf, "%d %d %d\n", &l, &r, &x)
		cnt := 0
		for j := l; j <= r; j++ {
			if p[x] > p[j] {
				cnt++
			}
		}
		if l + cnt == x {
			fmt.Fprintln(outBuf, "Yes")
		} else {
			fmt.Fprintln(outBuf, "No")
		}
	}
}

package main

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

func solve(s string) string {
	return s
}

func output(i int, ans string) {
	fmt.Printf("Case #%d: %s\n", i, ans)
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 1000000), 1000000)

	scanner.Scan()
	x := strings.Split(scanner.Text(), " ")
	//a, _ := strconv.Atoi(x[0])
	b, _ := strconv.Atoi(x[1])

	scanner.Scan()
	numStrs := strings.Split(scanner.Text(), " ")
	num := make([]int, len(numStrs))
	for i := range num {
		num[i], _ = strconv.Atoi(numStrs[i])
	}
	for i := 0; i < b; i++ {
		scanner.Scan()
		input := strings.Split(scanner.Text(), " ")
		start, _ := strconv.Atoi(input[0])
		end, _ := strconv.Atoi(input[1])
		index, _ := strconv.Atoi(input[2])
		start -= 1
		index -= 1

		if index < start || index > end {
			fmt.Println("Yes")
		} else {
			lt := 0
			cmpv := num[index]
			for _, v := range num[start:end] {
				if v < cmpv {
					lt++
				}
			}
			//	fmt.Println(lt, index)
			if (lt + start) == index {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	}
}

package main

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

const (
	MaxN = 10000
)

var p [MaxN]int
var n, m int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d\n", &n, &m)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &p[i])
	}
	fmt.Fscanf(buf, "\n")
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 0; i < m; i++ {
		var l, r, x int
		fmt.Fscanf(buf, "%d %d %d\n", &l, &r, &x)
		cnt := 0
		for j := l; j <= r; j++ {
			if p[x] > p[j] {
				cnt++
			}
		}
		if l + cnt == x {
			fmt.Fprintln(outBuf, "YES")
		} else {
			fmt.Fprintln(outBuf, "NO")
		}
	}
}


Pak Chanek has $$$n$$$ two-dimensional slices of cheese. The $$$i$$$-th slice of cheese can be represented as a rectangle of dimensions $$$a_i \times b_i$$$. We want to arrange them on the two-dimensional plane such that:  Each edge of each cheese is parallel to either the x-axis or the y-axis.  The bottom edge of each cheese is a segment of the x-axis.  No two slices of cheese overlap, but their sides can touch.  They form one connected shape. Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.Find the minimum possible perimeter of the constructed shape.

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of slices of cheese Pak Chanek has. The $$$i$$$-th of the next $$$n$$$ lines of each test case contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i,b_i \leq 10^9$$$) — the dimensions of the $$$i$$$-th slice of cheese. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a line containing an integer representing the minimum possible perimeter of the constructed shape.

NoteIn the first test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.We can calculate that the perimeter of the constructed shape is $$$2+5+1+1+1+1+3+1+5+1+2+3=26$$$. It can be shown that we cannot get a smaller perimeter.Consider the following invalid arrangement.Even though the perimeter of the shape above is $$$24$$$, it does not satisfy all conditions of the problem. The bottom edge of the $$$1 \times 1$$$ slice of cheese is not a segment of the x-axis.In the second test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.We can calculate that the perimeter of the constructed shape is $$$2+2+2+3+2+3+2+2+2+4=24$$$. It can be shown that we cannot get a smaller perimeter.

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	var str strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		sum := 0
		maxH := 0
		for i := 0; i < n; i++ {
			a, b := in.NextInt(), in.NextInt()
			if a < b {
				sum += 2 * a
				if maxH < b {
					maxH = b
				}
			} else {
				sum += 2 * b
				if maxH < a {
					maxH = a
				}
			}
		}
		sum += 2 * maxH

		str.WriteString(fmt.Sprintf("%v\n", sum))
	}
	fmt.Print(str.String())
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	//var str strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		sum := 0
		maxH := 0
		for i := 0; i < n; i++ {
			a, b := in.NextInt(), in.NextInt()
			if a < b {
				sum += 2 * a
				if maxH < b {
					maxH = b
				}
			} else {
				sum += 2 * b
				if maxH < a {
					maxH = a
				}
			}
		}
		sum += 2 * maxH
		fmt.Println(sum)
		//str.WriteString(fmt.Sprintf("%v\n", sum))
	}
	//fmt.Print(str.String())
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	var str strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()

		sum := int64(0)
		maxH := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt64(), in.NextInt64()
			min := int64(0)
			if a < b {
				min = a

				if maxH < b {
					maxH = b
				}
			} else {
				min = b

				if maxH < a {
					maxH = a
				}
			}
			sum += int64(2) * int64(min)
		}

		sum += 2 * maxH

		str.WriteString(fmt.Sprintf("%v\n", sum))
	}
	fmt.Print(str.String())
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	var str strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt(), in.NextInt()
			min := 0
			if a < b {
				min = a
				arr[i] = b
			} else {
				min = b
				arr[i] = a
			}
			sum += int64(2) * int64(min)
		}

		sort.Ints(arr)

		if n == 1 {
			sum += 2 * int64(arr[0])
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += int64(arr[i])
				} else if i > 0 {
					sum += int64(arr[i]) - int64(arr[i-1])
				}
				if i+1 == n {
					sum += int64(arr[i])
				}
			}
		}
		str.WriteString(fmt.Sprintf("%v\n", sum))
	}
	fmt.Print(str.String())
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt(), in.NextInt()
			min := 0
			if a < b {
				min = a
				arr[i] = b
			} else {
				min = b
				arr[i] = a
			}
			sum += int64(2) * int64(min)
		}

		sort.Ints(arr)

		if n == 1 {
			sum += 2 * int64(arr[0])
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += int64(arr[i])
				} else if i > 0 {
					sum += int64(arr[i]) - int64(arr[i-1])
				}
				if i+1 == n {
					sum += int64(arr[i])
				}
			}
		}

		fmt.Println(sum)
	}
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([][]int64, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt64(), in.NextInt64()
			if a < b {
				arr[i] = []int64{a, b}
			} else {
				arr[i] = []int64{b, a}
			}
			sum += 2 * arr[i][0]
		}

		sort.Slice(arr, func(i, j int) bool {
			if arr[i][1] == arr[j][1] {
				if arr[i][0] < arr[j][0] {
					return true
				}
			} else {
				if arr[i][1] < arr[j][1] {
					return true
				}
			}
			return false
		})

		if n == 1 {
			sum += 2 * arr[0][1]
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += arr[i][1]
				} else if i > 0 {
					sum += arr[i][1] - arr[i-1][1]
				}
				if i+1 == n {
					sum += arr[i][1]
				}
			}
		}

		fmt.Println(sum)
	}
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([][]int64, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt64(), in.NextInt64()
			if a < b {
				arr[i] = []int64{a, b}
			} else {
				arr[i] = []int64{b, a}
			}
			sum += 2 * arr[i][0]
		}

		sort.Slice(arr, func(i, j int) bool {
			if arr[i][0] == arr[j][0] {
				if arr[i][1] < arr[j][1] {
					return true
				}
			} else {
				if arr[i][0] < arr[j][0] {
					return true
				}
			}
			return false
		})

		sort.Slice(arr, func(i, j int) bool {
			if arr[i][1] == arr[j][1] {
				if arr[i][0] < arr[j][0] {
					return true
				}
			} else {
				if arr[i][1] < arr[j][1] {
					return true
				}
			}
			return false
		})

		if n == 1 {
			sum += 2 * arr[0][1]
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += arr[i][1]
				} else if i > 0 {
					sum += arr[i][1] - arr[i-1][1]
				}
				if i+1 == n {
					sum += arr[i][1]
				}
			}
		}

		fmt.Println(sum)
	}
}

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

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

package main

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

func main() {
	minPerimeter()
}

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

	var tc int
	for fmt.Fscan(r, &tc); tc > 0; tc-- {
		var a, b int
		minPerimeter, maxLen := 0, 0
		count := 0
		for fmt.Fscan(r, &count); count > 0; count-- {
			fmt.Fscan(r, &a, &b)
			l := max(a, b)
			maxLen = max(l, maxLen)
			w := min(a, b)
			minPerimeter += w * 2
		}
		minPerimeter += maxLen * 2
		fmt.Println(minPerimeter)
	}
}

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
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func scan(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	var t int

	scan("%d\n", &t)

	for i := 0; i < t; i++ {
		var n int
		scan("%d\n", &n)

		solve(n)
	}
}

func solve(n int) {
	var a, b int
	var res = 0
	var sl = make([]int, n)
	for i := 0; i < n; i++ {
		scan("%d %d\n", &a, &b)
		res += min(a, b) * 2
		sl[i] = a + b - min(a, b)
	}

	sort.Ints(sl)

	res += sl[0] + sl[n-1]

	for i := 0; i < n-1; i++ {
		res += sl[i+1] - sl[i]
	}

	fmt.Println(res)
}

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

	return b
}

package main

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

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

	if err := run(r, w); err != nil {
		log.Fatal(err)
	}
}

func run(r io.Reader, w io.Writer) (err error) {
	var count int
	if _, err = fmt.Fscanln(r, &count); err != nil {
		return err
	}

	var data [][2]int
	var list CheesList

	for i := 0; i < count; i++ {
		if data, err = readData(r); err != nil {
			return err
		}

		list = NewCheesList(data)
		fmt.Fprintln(w, list.MinPerimetr())
	}

	return nil
}

func readData(r io.Reader) (data [][2]int, err error) {
	var count int
	if _, err = fmt.Fscanln(r, &count); err != nil {
		return nil, err
	}

	data = make([][2]int, count)
	for i := range data {
		if _, err := fmt.Fscanln(r, &data[i][0], &data[i][1]); err != nil {
			return nil, err
		}
	}
	return
}

type Chees struct {
	x, y int
}

type CheesList []Chees

func NewCheesList(data [][2]int) (list CheesList) {
	list = make(CheesList, len(data))
	for i, v := range data {
		if v[0] < v[1] {
			list[i].x, list[i].y = v[0], v[1]
		} else {
			list[i].x, list[i].y = v[1], v[0]
		}
	}
	return list
}

func (cl CheesList) Perimetr() int {
	var res int

	for i, c := range cl {
		res += c.x * 2
		if i == 0 {
			res += c.y
		} else {
			res += mod(c.y - cl[i-1].y)
		}
		if i+1 == len(cl) {
			res += c.y
		}
	}
	return res
}

func (cl CheesList) MinPerimetr() int {
	sort.Slice(cl, func(i, j int) bool {
		return cl[i].y < cl[j].y
	})

	left := make(CheesList, 0, len(cl)+1)
	right := make(CheesList, 0, len(cl)+1)

	for i, c := range cl {
		if i%2 == 0 {
			left = append(left, c)
		} else {
			right = append(right, c)
		}
	}

	sort.Slice(right, func(i, j int) bool {
		return right[i].y > right[j].y
	})

	for i, c := range left {
		cl[i] = c
	}
	for i, c := range right {
		cl[i+len(left)] = c
	}
	return cl.Perimetr()
}

func mod(i int) int {
	if i < 0 {
		return -i
	}
	return i
}

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, max, area, l, b int
		fmt.Fscan(r, &n)

		for i := 0; i < n; i++ {
			fmt.Fscan(r, &l, &b)
			if b > max { max = b }
			if l > max { max = l }
			if l > b { l = b }
			area += 2*l
		}
		area += 2*max

		fmt.Fprintln(w, area)
	}

}

package main

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

func main() {
	var TC int

	r_ := bufio.NewReader(os.Stdin)
	w_ := bufio.NewWriter(os.Stdout)

	defer w_.Flush()
	fmt.Fscan(r_, &TC)

	for t := 1; t <= TC; t++ {

		var n int
		fmt.Fscan(r_, &n)

		arr1 := make([]int, n)
		arr2 := make([]int, n)
		minSum := 0
		for i := 0; i < n; i++ {
			var x, y int
			fmt.Fscan(r_, &x, &y)

			arr1[i] = min(x, y)
			arr2[i] = max(x, y)
			minSum += 2 * arr1[i]
		}

		output := solve(&arr2, minSum, n)

		fmt.Fprintln(w_, output)
	}
}

func solve(arr *[]int, minSum, n int) int {
	sort.Ints(*arr)
	t1 := (*arr)[0]

	for i := 1; i < n; i++ {
		t1 += (*arr)[i] - (*arr)[i-1]
	}
	t1 += (*&*arr)[n-1]

	return minSum + t1
}
func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

package main

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

type point struct {
	x int64
	y int64
}

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

	t := readInt(reader)

	for t > 0 {
		n := readInt(reader)
		var a = make([]point, n)

		for i := 0; i < n; i++ {
			x, y := readTwoInt64(reader)
			if x > y {
				a[i].x = y
				a[i].y = x
			} else {
				a[i].x = x
				a[i].y = y
			}
		}
		sort.Slice(a, func(i, j int) bool {
			if a[i].y < a[j].y {
				return true
			}

			return false
		})

		var ansx, ansy int64 = 0, 0
		for i := 0; i < n; i++ {
			ansx += a[i].x
			if i == 0 || i == n-1 {
				ansy += a[i].y
			}
			if i > 0 {
				ansy += a[i].y - a[i-1].y
			}
		}

		if n == 1 {
			ansy += a[0].y
		}

		fmt.Fprintln(writer, 2*ansx+ansy)

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

func com(x, y int64) (int64, int64) {
	if x > y {
		return y, x
	}
	return x, y
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		var x, y int64
		for i := 0; i < n; i++ {
			var tx, ty int64
			fmt.Fscan(in, &tx, &ty)
			tx, ty = com(tx, ty)
			x += tx
			_, y = com(ty, y)
		}
		fmt.Fprintln(out, x*2+y*2)
	}
	out.Flush()
}

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 int
	scanf("%d\n", &n)
	a := make([]int64, n)
	b := make([]int64, n)
	sum := int64(0)
	mx := int64(0)
	for i:=0; i<n; i++ {
		scanf("%d %d\n", &a[i], &b[i])
		if a[i] < b[i] {
			a[i], b[i] = b[i], a[i]
		}
		sum += b[i]
		if a[i] > mx {
			mx = a[i]
		}
	}

	res := 2*(sum + mx)
	printf("%d\n", res)
}

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

const cf = true

type tower struct {
	h int
	w int
}

func solve() {
	var n int
	fmt.Fscan(in, &n)
	towers := make([]tower, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &towers[i].h, &towers[i].w)
		if towers[i].h < towers[i].w {
			towers[i].h, towers[i].w = towers[i].w, towers[i].h
		}
	}
	sort.Slice(towers, func(i, j int) bool {
		return towers[i].h < towers[j].h
	})
	prev := 0
	ans := 0
	for _, tower := range towers {
		ans += tower.w*2 + tower.h - prev
		prev = tower.h
	}
	ans += towers[len(towers)-1].h
	fmt.Fprintln(out, ans)
}

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

func LCM(a, b int) int {
	return a * b / GCD(a, b)
}

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

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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 := []byte(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"
	"sort"
	"strconv"
	"strings"
)

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	var str strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt(), in.NextInt()
			min := 0
			if a < b {
				min = a
				arr[i] = b
			} else {
				min = b
				arr[i] = a
			}
			sum += int64(2) * int64(min)
		}

		sort.Ints(arr)

		if n == 1 {
			sum += 2 * int64(arr[0])
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += int64(arr[i])
				} else if i > 0 {
					sum += int64(arr[i]) - int64(arr[i-1])
				}

			}
		}
		str.WriteString(fmt.Sprintf("%v\n", sum+int64(arr[n-1])))
	}
	fmt.Print(str.String())
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([][]int64, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt64(), in.NextInt64()
			//if a < b {
				arr[i] = []int64{a, b}
			//} else {
			//	arr[i] = []int64{b, a}
			//}
			sum += 2 * arr[i][0]
		}

		sort.Slice(arr, func(i, j int) bool {
			if arr[i][1] == arr[j][1] {
				if arr[i][0] < arr[j][0] {
					return true
				}
			} else {
				if arr[i][1] < arr[j][1] {
					return true
				}
			}
			return false
		})

		if n == 1 {
			sum += 2 * arr[0][1]
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += arr[i][1]
				} else if i > 0 {
					sum += arr[i][1] - arr[i-1][1]
				}
				if i+1 == n {
					sum += arr[i][1]
				}
			}
		}

		fmt.Println(sum)
	}
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([][]int64, n)
		sum := int64(0)
		for i := 0; i < n; i++ {
			a, b := in.NextInt64(), in.NextInt64()
			if a < b {
				arr[i] = []int64{a, b}
			} else {
				arr[i] = []int64{b, a}
			}
			sum += 2 * arr[i][0]
		}

		sort.Slice(arr, func(i, j int) bool {
			if arr[i][0] == arr[j][0] {
				if arr[i][1] < arr[j][1] {
					return true
				}
			} else {
				if arr[i][0] < arr[j][0] {
					return true
				}
			}
			return false
		})

		if n == 1 {
			sum += 2 * arr[0][1]
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += arr[i][1]
				} else if i > 0 {
					sum += arr[i][1] - arr[i-1][1]
				}
				if i+1 == n {
					sum += arr[i][1]
				}
			}
		}

		fmt.Println(sum)
	}
}

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

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

package main

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

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

func (in *CF1740B) 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 *CF1740B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1740B
Date: 11/17/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1740/B
Problem: CF1740B
**/
func (in *CF1740B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([][]int, n)
		sum := 0
		for i := 0; i < n; i++ {
			a, b := in.NextInt(), in.NextInt()
			if a < b {
				arr[i] = []int{a, b}
			} else {
				arr[i] = []int{b, a}
			}
			sum += 2 * arr[i][0]
		}

		sort.Slice(arr, func(i, j int) bool {
			if arr[i][0] == arr[j][0] {
				if arr[i][1] < arr[j][1] {
					return true
				}
			} else {
				if arr[i][0] < arr[j][0] {
					return true
				}
			}
			return false
		})

		if n == 1 {
			sum += 2 * arr[0][1]
		} else {
			for i := 0; i < n; i++ {
				if i == 0 {
					sum += arr[i][1]
				} else if i > 0 {
					sum += arr[i][1] - arr[i-1][1]
				}
				if i+1 == n {
					sum += arr[i][1]
				}
			}
		}

		fmt.Println(sum)
	}
}

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

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

package main

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

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

	if err := run(r, w); err != nil {
		log.Fatal(err)
	}
}

func run(r io.Reader, w io.Writer) (err error) {
	var count int
	if _, err = fmt.Fscanln(r, &count); err != nil {
		return err
	}

	var data [][2]int
	var list CheesList

	for i := 0; i < count; i++ {
		if data, err = readData(r); err != nil {
			return err
		}

		list = NewCheesList(data)
		fmt.Fprintln(w, list.MinPerimetr())
	}

	return nil
}

func readData(r io.Reader) (data [][2]int, err error) {
	var count int
	if _, err = fmt.Fscanln(r, &count); err != nil {
		return nil, err
	}

	data = make([][2]int, count)
	for i := range data {
		if _, err := fmt.Fscanln(r, &data[i][0], &data[i][1]); err != nil {
			return nil, err
		}
	}
	return
}

type Chees struct {
	x, y int
}

type CheesList []Chees

func NewCheesList(data [][2]int) (list CheesList) {
	list = make(CheesList, len(data))
	for i, v := range data {
		if v[0] < v[1] {
			list[i].x, list[i].y = v[0], v[1]
		} else {
			list[i].x, list[i].y = v[1], v[0]
		}
	}
	return list
}

func (cl CheesList) Perimetr() int {
	var res int

	for i, c := range cl {
		res += c.x * 2
		if i == 0 {
			res += c.y
		} else {
			res += mod(c.y - cl[i-1].y)
		}
		if i+1 == len(cl) {
			res += c.y
		}
	}
	return res
}

func (cl CheesList) MinPerimetr() int {
	sort.Slice(cl, func(i, j int) bool {
		return cl[i].y < cl[j].y
	})

	mid := len(cl) / 2
	sort.Slice(cl[mid:], func(i, j int) bool {
		return cl[i].y > cl[j].y
	})

	return cl.Perimetr()
}

func mod(i int) int {
	if i < 0 {
		return -i
	}
	return i
}

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, max, area, l, b int
		fmt.Fscan(r, &n)

		for i := 0; i < n; i++ {
			fmt.Fscan(r, &l, &b)
			if b > max { max = b }
			if l > max {
				max = l
				l = b
			}
			area += 2*l
		}
		area += 2*max

		fmt.Fprintln(w, area)
	}

}