package main

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

func solve() {
	n := readInt()
	k := readInt()
	p := readInt()
	a := readIntArray(n)
	b := readIntArray(k)
	sort.Ints(a)
	sort.Ints(b)
	mini := math.MaxInt32
	for i := 0; i <= k-n; i++ {
		x := 0
		for j := 0; j < n; j++ {
			x = max(x, abs(a[j]-b[i+j])+abs(b[i+j]-p))
		}
		mini = min(mini, x)
	}
	fmt.Println(mini)
}

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

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

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

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

package main

import (
	"fmt"
	"sort"
)

var n, k, p int

func main() {
	var ans int64 = 1e18
	fmt.Scan(&n, &k, &p)
	a := make([]int, n)
	b := make([]int, k)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 0; i < k; i++ {
		fmt.Scan(&b[i])
	}
	sort.Ints(a)
	sort.Ints(b)
	for i := 0; i+n <= k; i++ {
		var tmp int64 = 0
		for j := 0; j < n; j++ {
			tmp1 := func(x, y, z int) (res int64) {
				if x >= y {
					res += int64(x - y)
				} else {
					res += int64(y - x)
				}
				if y >= z {
					res += int64(y - z)
				} else {
					res += int64(z - y)
				}
				return
			}(a[j], b[i+j], p)
			tmp = func(x, y int64) int64 {
				if x >= y {
					return x
				} else {
					return y
				}
			}(tmp, tmp1)
		}
		ans = func(x, y int64) int64 {
			if x <= y {
				return x
			} else {
				return y
			}
		}(tmp, ans)
	}
	fmt.Println(ans)
}

package main

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

func solve(val int64, A, B []int, G int) bool {
	visited := map[int]bool{}
	for _, pos := range A {
		suc := false
		for i, tar := range B {
			if _, ok := visited[i]; ok {
				continue
			}
			cost := 0
			if pos > tar {
				cost = pos - tar
			} else {
				cost = tar - pos
			}
			if tar > G {
				cost += tar - G
			} else {
				cost += G - tar
			}
			if val-int64(cost) >= 0 {
				suc = true
				visited[i] = true
				break
			}
		}
		if !suc {
			return false
		}
	}

	return true
}

func main() {
	fsc := NewFastScanner()
	N, K, G := fsc.NextInt(), fsc.NextInt(), fsc.NextInt()
	A := make([]int, 0)
	for i := 0; i < N; i++ {
		A = append(A, fsc.NextInt())
	}
	B := make([]int, 0)
	for i := 0; i < K; i++ {
		B = append(B, fsc.NextInt())
	}
	sort.Ints(A)
	sort.Ints(B)
	var left, right int64
	left = -1
	right = 1152921504606846976
	for right-left > 1 {
		med := (right + left) / 2
		if solve(med, A, B, G) {
			right = med
		} else {
			left = med
		}
	}
	fmt.Println(right)
}

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

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

package main

import (
	. "fmt"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	type pair struct{ l, r int }
	var n, m, o int
	Scan(&n, &m, &o)
	a := make([]int, n)
	for i := range a {
		Scan(&a[i])
	}
	sort.Ints(a)
	k := make([]int, m)
	for i := range k {
		Scan(&k[i])
	}
	ans := sort.Search(2e9, func(t int) bool {
		ps := []pair{}
		for _, p := range k {
			q := p - o
			if q < 0 {
				q = -q
			}
			if d := t - q; d >= 0 {
				ps = append(ps, pair{p - d, p + d})
			}
		}
		sort.Slice(ps, func(i, j int) bool { a, b := ps[i], ps[j]; return a.r < b.r || a.r == b.r && a.l < b.l })
		i := -1
		for _, p := range a {
			for i++; i < len(ps) && ps[i].r < p; i++ {
			}
			if i == len(ps) || p < ps[i].l {
				return false
			}
		}
		return true
	})
	Print(ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF830A(_r io.Reader, out io.Writer) {
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}
	type pair struct{ l, r int }
	in := bufio.NewReader(_r)
	var n, m, o int
	Fscan(in, &n, &m, &o)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	k := make([]int, m)
	for i := range k {
		Fscan(in, &k[i])
	}
	sort.Ints(k)
	ans := sort.Search(2e9, func(t int) bool {
		ps := []pair{}
		for _, p := range k {
			if d := t - abs(p-o); d >= 0 {
				ps = append(ps, pair{p - d, p + d})
			}
		}
		sort.Slice(ps, func(i, j int) bool { a, b := ps[i], ps[j]; return a.r < b.r || a.r == b.r && a.l < b.l })
		i := -1
		for _, p := range a {
			for i++; i < len(ps) && ps[i].r < p; i++ {
			}
			if i == len(ps) || p < ps[i].l {
				return false
			}
		}
		return true
	})
	Fprint(out, ans)
}

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

package main

import (
	"fmt"
	"sort"
)

var n, k, p int

func main() {
	var ans int64 = 1e18
	fmt.Scan(&n, &k, &p)
	a := make([]int, n)
	b := make([]int, k)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 0; i < k; i++ {
		fmt.Scan(&b[i])
	}
	sort.Ints(b)
	for i := 0; i+n <= k; i++ {
		var tmp int64 = 0
		for j := 0; j < n; j++ {
			tmp1 := func(x, y, z int) (res int64) {
				if x >= y {
					res += int64(x - y)
				} else {
					res += int64(y - x)
				}
				if y >= z {
					res += int64(y - z)
				} else {
					res += int64(z - y)
				}
				return
			}(a[j], b[i+j], p)
			tmp = func(x, y int64) int64 {
				if x >= y {
					return x
				} else {
					return y
				}
			}(tmp, tmp1)
		}
		ans = func(x, y int64) int64 {
			if x <= y {
				return x
			} else {
				return y
			}
		}(tmp, ans)
	}
	fmt.Println(ans)
}

package main

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

func solve() {
	n := readInt()
	k := readInt()
	p := readInt()
	a := readIntArray(n)
	b := readIntArray(k)
	maxHeaps := make([]*MinHeap, n)
	for i := 0; i < n; i++ {
		cur := a[i]
		maxHeaps[i] = NewMinHeap(k)
		for j := 0; j < k; j++ {
			pos := b[j]
			dist := abs(cur-pos) + abs(pos-p)
			maxHeaps[i].Add(j, dist)
		}
	}
	best := 0
	for {
		maxValue := 0
		maxIndex := -1
		maxKeyIndex := 0
		for i := 0; i < n; i++ {
			if maxHeaps[i] == nil {
				continue
			}
			v := maxHeaps[i].Min()
			if v > maxValue {
				maxValue = v
				maxIndex = i
				maxKeyIndex = maxHeaps[i].ArgMin()
			}
		}
		if maxIndex == -1 {
			fmt.Println(best)
			return
		}

		best = max(best, maxValue)
		maxHeaps[maxIndex] = nil
		for i := 0; i < n; i++ {
			if maxHeaps[i] != nil {
				maxHeaps[i].Remove(maxKeyIndex)
			}
		}
	}
}

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

type MinHeap struct {
	a    []int
	vmap []int
	imap []int
	n    int
	pos  int
}

const INF = math.MaxInt32

func NewMinHeap(m int) *MinHeap {
	heap := &MinHeap{}
	m += 2
	heap.n = m
	heap.a = make([]int, heap.n)
	heap.vmap = make([]int, heap.n)
	heap.imap = make([]int, heap.n)
	heap.pos = 1
	for i := 0; i < heap.n; i++ {
		heap.a[i] = INF
		heap.vmap[i] = -1
		heap.imap[i] = -1
	}
	return heap
}

func (h *MinHeap) Add(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	}
	if ret != -1 {
		return h.a[ret]
	}
	return x
}

func (h *MinHeap) Update(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	} else {
		h.a[ret] = x
		h.up(ret)
		h.down(ret)
	}
	return x
}

func (h *MinHeap) Remove(ind int) int {
	if h.pos == 1 {
		return INF
	}
	if h.imap[ind] == -1 {
		return INF
	}

	h.pos--
	rem := h.imap[ind]
	ret := h.a[rem]
	h.vmap[rem] = h.vmap[h.pos]
	h.imap[h.vmap[h.pos]] = rem
	h.imap[ind] = -1
	h.a[rem] = h.a[h.pos]
	h.a[h.pos] = INF
	h.vmap[h.pos] = -1

	h.up(rem)
	h.down(rem)
	return ret
}

func (h *MinHeap) Min() int {
	return h.a[1]
}

func (h *MinHeap) ArgMin() int {
	return h.vmap[1]
}

func (h *MinHeap) Size() int {
	return h.pos - 1
}

func (h *MinHeap) up(cur int) {
	for c, p := cur, cur>>1; p >= 1 && h.a[p] > h.a[c]; c, p = c>>1, p>>1 {
		h.a[p], h.a[c] = h.a[c], h.a[p]
		h.imap[h.vmap[p]], h.imap[h.vmap[c]] = h.imap[h.vmap[c]], h.imap[h.vmap[p]]
		h.vmap[p], h.vmap[c] = h.vmap[c], h.vmap[p]
	}
}

func (h *MinHeap) down(cur int) {
	for c := cur; 2*c < h.pos; {
		b := 0
		if h.a[2*c] < h.a[2*c+1] {
			b = 2 * c
		} else {
			b = 2*c + 1
		}
		if h.a[b] < h.a[c] {
			h.a[c], h.a[b] = h.a[b], h.a[c]
			h.imap[h.vmap[c]], h.imap[h.vmap[b]] = h.imap[h.vmap[b]], h.imap[h.vmap[c]]
			h.vmap[c], h.vmap[b] = h.vmap[b], h.vmap[c]
			c = b
		} else {
			break
		}
	}
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

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

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

package main

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

func solve(val int64, A, B []int, G int) bool {
	visited := map[int]bool{}
	for _, pos := range A {
		suc := false
		for i, tar := range B {
			if _, ok := visited[i]; ok {
				continue
			}
			cost := 0
			if pos > tar {
				cost = pos - tar
			} else {
				cost = tar - pos
			}
			if tar > G {
				cost += tar - G
			} else {
				cost += G - tar
			}
			if val-int64(cost) >= 0 {
				suc = true
				break
			}
		}
		if !suc {
			return false
		}
	}

	return true
}

func main() {
	fsc := NewFastScanner()
	N, K, G := fsc.NextInt(), fsc.NextInt(), fsc.NextInt()
	A := make([]int, 0)
	for i := 0; i < N; i++ {
		A = append(A, fsc.NextInt())
	}
	B := make([]int, 0)
	for i := 0; i < K; i++ {
		B = append(B, fsc.NextInt())
	}
	sort.Ints(A)
	sort.Ints(B)
	var left, right int64
	left = 0
	right = 1152921504606846976
	for right-left > 1 {
		med := (right + left) / 2
		if solve(med, A, B, G) {
			right = med
		} else {
			left = med
		}
	}
	fmt.Println(right)
}

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

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

package main

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

func solve(val int64, A, B []int, G int) bool {
	visited := map[int]bool{}
	for _, pos := range A {
		suc := false
		for i, tar := range B {
			if _, ok := visited[i]; ok {
				continue
			}
			cost := 0
			if pos > tar {
				cost = pos - tar
			} else {
				cost = tar - pos
			}
			if tar > G {
				cost += tar - G
			} else {
				cost += G - tar
			}
			if val-int64(cost) >= 0 {
				suc = true
				visited[i] = true
				break
			}
		}
		if !suc {
			return false
		}
	}

	return true
}

func main() {
	fsc := NewFastScanner()
	N, K, G := fsc.NextInt(), fsc.NextInt(), fsc.NextInt()
	A := make([]int, 0)
	for i := 0; i < N; i++ {
		A = append(A, fsc.NextInt())
	}
	B := make([]int, 0)
	for i := 0; i < K; i++ {
		B = append(B, fsc.NextInt())
	}
	sort.Ints(A)
	sort.Ints(B)
	var left, right int64
	left = 0
	right = 1152921504606846976
	for right-left > 1 {
		med := (right + left) / 2
		if solve(med, A, B, G) {
			right = med
		} else {
			left = med
		}
	}
	fmt.Println(right)
}

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

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF830A(_r io.Reader, out io.Writer) {
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}
	type pair struct{ l, r int }
	in := bufio.NewReader(_r)
	var n, m, o int
	Fscan(in, &n, &m, &o)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	k := make([]int, m)
	for i := range k {
		Fscan(in, &k[i])
	}
	sort.Ints(k)
	ans := sort.Search(2e9, func(t int) bool {
		ps := []pair{}
		for _, p := range k {
			if d := t - abs(p-o); d >= 0 {
				ps = append(ps, pair{p - d, p + d})
			}
		}
		sort.Slice(ps, func(i, j int) bool { a, b := ps[i], ps[j]; return a.r < b.r || a.r == b.r && a.l < b.l })
		i := 0
		for _, p := range a {
			for ; i < len(ps) && ps[i].r < p; i++ {
			}
			if i == len(ps) || p < ps[i].l {
				return false
			}
		}
		return true
	})
	Fprint(out, ans)
}

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


package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	if n == 3 {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprint(out, n, " ", n-1, " ")
	for i := 1; i < n-1; i++ {
		fmt.Fprint(out, i, " ")
	}
	fmt.Fprintln(out)
}

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

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 := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		var n int
		Fscan(in, &n)
		if n == 3 {
			Fprintln(out, "-1")
			continue
		}
		if n%2 == 0 {
			for i := 1; i <= n; i += 2 {
				Fprint(out, i+1, " ", i, " ")
			}
			Fprintln(out)
			continue
		} else {
			for i := 4; i <= n; i += 2 {
				Fprint(out, i+1, " ", i, " ")
			}
			for i := 1; i <= 3; i++ {
				Fprint(out, i, " ")
			}

			Fprintln(out)
			continue
		}
	}

}

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

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

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	Fscan(in, &n)
	if n == 1 || n == 3 {
		Fprintln(out, "-1")
		return
	}
	if n == 5 {
		Fprintln(out, "3 4 5 1 2")
		return
	}
	for i := n; i >= 1; i-- {
		if n%2 == 1 && n/2 == i {
			Fprintf(out, "%d ", i+1)
			continue
		}
		if n%2 == 1 && (n+1)/2 == i {
			Fprintf(out, "%d ", i-1)
			continue
		}
		Fprintf(out, "%d ", i)
	}
	Fprintln(out)
}

func abs(a int64) int64 {
	if a > 0 {
		return a
	}
	return -a
}
func sum(a, b int64) int64 {
	return (b + a) * (b - a + 1) / 2
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

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

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

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

		if n != 3 {
			fmt.Fprintf(w, "%d %d ", n, n-1)
			for i := 1; i <= n-2; i++ {
				fmt.Fprintf(w, "%d ", i)
			}
			fmt.Fprintln(w, "")
		} else {
			fmt.Fprintln(w, -1)
		}

	}

}

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

		l := n/2
		if n != 3 {
			for i := 0; i < n; i++ {
				if i < l {
					fmt.Fprintf(w, "%d ", n-l + i+1)
				} else {
					fmt.Fprintf(w, "%d ", i-l + 1)
				}
			}
			fmt.Fprintln(w, "")
		} else {
			fmt.Fprintln(w, -1)
		}

	}

}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		if n == 3 {
			fmt.Fprintln(out,-1)
			continue
		}
		for j:=n/2+1;j<=n;j++{
			fmt.Fprintf(out,"%v ",j)
		}
		for j:=1;j<=n/2;j++{
			fmt.Fprintf(out,"%v ",j)
		}
		fmt.Fprintln(out,)
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1741B
Date: 10/28/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1741/B
Problem: CF1741B
**/
func (in *CF1741B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		if n == 3 {
			fmt.Println(-1)
		} else {
			for i := 3; i <= n; i++ {
				fmt.Print(i, " ")
			}
			fmt.Println(2, 1)
		}
	}
}

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

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

/*
	A template for Go on codeforces.com
	Author: Yakov, frvr.ru
*/

package main

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

func main() {
	t := readInt()
	for j := 0; j < t; j++ {
		n := readInt()
		if n == 3 {
			writeLine(-1)
		} else {
			Solve(n)
		}
	}
}
func Solve(n int) {
	for i := 3; i <= n; i++ {
		fmt.Printf("%v ", i)
	}
	fmt.Printf("2 1\n")
}

/* ~~~~~~~~~~ */
/* ~~~~~~~~~~ */
/* ~~~~~~~~~~ */

var in = bufio.NewReader(os.Stdin)

type float = float32
type double = float64
type long = int64
type ulong = uint64

func typeOf(arg interface{}) string {
	switch reflect.TypeOf(arg).Kind() {
	case reflect.Int:
		return "int"
	case reflect.Int8:
		return "int8"
	case reflect.Int16:
		return "int16"
	case reflect.Int32:
		return "int32"
	case reflect.Int64:
		return "int64"
	case reflect.Uint:
		return "uint"
	case reflect.Uint8:
		return "uint8"
	case reflect.Uint16:
		return "uint16"
	case reflect.Uint32:
		return "uint32"
	case reflect.Uint64:
		return "uint64"
	case reflect.Float32:
		return "float32"
	case reflect.Float64:
		return "float64"
	case reflect.Complex64:
		return "complex64"
	case reflect.Complex128:
		return "complex128"
	case reflect.Bool:
		return "bool"
	case reflect.String:
		return "string"
	case reflect.Chan:
		return "chan"
	case reflect.Ptr:
		return "ptr"
	case reflect.Uintptr:
		return "uintptr"
	case reflect.UnsafePointer:
		return "unsafeptr"
	case reflect.Interface:
		return "interface"
	case reflect.Array:
		return "array"
	case reflect.Slice:
		return "slice"
	case reflect.Map:
		return "map"
	case reflect.Struct:
		return "struct"
	case reflect.Func:
		return "func"
	case reflect.Invalid:
		return "invalid"
	default:
		return "nil"
	}
}

/* ~~~~~~~~~~ */

func _scln() string {
	ln, _ := in.ReadString('\n')
	return strings.Trim(ln, " \r\n")
}
func _sc(s []string) string {
	if len(s) == 0 {
		return _scln()
	}
	return s[0]
}

func readLine() string              { return _scln() }
func readString() string            { return _scln() }
func readStrings() []string         { return strings.Split(_scln(), " ") }
func readBool(s ...string) bool     { t, _ := strconv.ParseBool(_sc(s)); return t }
func readByte(s ...string) byte     { t, _ := strconv.ParseUint(_sc(s), 10, 8); return byte(t) }
func readDouble(s ...string) double { t, _ := strconv.ParseFloat(_sc(s), 64); return t }
func readFloat(s ...string) float   { t, _ := strconv.ParseFloat(_sc(s), 32); return float32(t) }
func readInt(s ...string) int       { t, _ := strconv.Atoi(_sc(s)); return t }
func readLong(s ...string) long     { t, _ := strconv.ParseInt(_sc(s), 10, 64); return t }
func readULong(s ...string) ulong   { t, _ := strconv.ParseUint(_sc(s), 10, 64); return t }

/* ~~~~~~~~~~ */

// Too much overhead with universal method using interface{}, wait for the generics,
// but for now — just don't look at the redundancy of the code after that commentary :)

func readBools() []bool {
	strs := readStrings()
	arr := make([]bool, len(strs))
	for i, s := range strs {
		arr[i] = readBool(s)
	}
	return arr
}
func readBytes() []byte {
	strs := readStrings()
	arr := make([]byte, len(strs))
	for i, s := range strs {
		arr[i] = readByte(s)
	}
	return arr
}
func readDoubles() []double {
	strs := readStrings()
	arr := make([]double, len(strs))
	for i, s := range strs {
		arr[i] = readDouble(s)
	}
	return arr
}
func readFloats() []float {
	strs := readStrings()
	arr := make([]float, len(strs))
	for i, s := range strs {
		arr[i] = readFloat(s)
	}
	return arr
}
func readInts() []int {
	strs := readStrings()
	arr := make([]int, len(strs))
	for i, s := range strs {
		arr[i] = readInt(s)
	}
	return arr
}
func readLongs() []long {
	strs := readStrings()
	arr := make([]long, len(strs))
	for i, s := range strs {
		arr[i] = readLong(s)
	}
	return arr
}
func readULongs() []ulong {
	strs := readStrings()
	arr := make([]ulong, len(strs))
	for i, s := range strs {
		arr[i] = readULong(s)
	}
	return arr
}

/* ~~~~~~~~~~ */

func write(arg ...interface{})                 { fmt.Print(arg...) }
func writeLine(arg ...interface{})             { fmt.Println(arg...) }
func writeFormat(f string, arg ...interface{}) { fmt.Printf(f, arg...) }

package main

import (
	"fmt"
)

var t, n int

func solve() {
	if n == 3 {
		fmt.Println("-1")
	} else {
		fmt.Print(n, n-1, " ")
		for i := 1; i < n-1; i++ {
			fmt.Print(i, " ")
		}
		fmt.Println()
	}
}

func main() {
	fmt.Scan(&t)
	for ; t != 0; t-- {
		fmt.Scan(&n)
		solve()
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

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

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

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

	for ; t > 0; t-- {
    var n int
    fmt.Fscan(in, &n)
    if n == 3 {
      pl("-1")
      continue
    }
    pf("%d %d ", n, n-1)
    for i := 1; i<=n-2;i++ {
      pf("%d ", i)
    }
    pl("")
	}
}

package main

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

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

func solve() {
	var n int
	scanf("%d\n", &n)

	if (n == 3) {
		printf("-1\n")
		return
	}

	if n%2 == 0 {
		for i:=n; i>=1; i-- {
			printf("%d ", i)
		}
		printf("\n")
		return
	}

	j := n
	for i:=0; i<n/2; i++ {
		printf("%d ", j)
		j--
	}
	j = 1
	for i:=0; i<n/2+1; i++ {
		printf("%d ", j)
		j++
	}
	printf("\n")
}

func main() {
	defer writer.Flush()

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

package main

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

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 main() {
    in := bufio.NewReader(os.Stdin)
    n := readInt(in)
    for i:=0; i<n; i++ {
        v := readInt(in)
        if v == 3 {
            fmt.Println(-1)
        } else {
            fmt.Printf("%d %d", v, v-1)
            for j:=1; j<v-1; j++ {
                fmt.Printf(" %d", j)
            }
            fmt.Printf("\n")
        }
    }
}

package main

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

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	scanner := bufio.NewScanner(os.Stdin)
	// mod := int64(998244353998244353)
	for i := 0; i < t; i++ {
		var s string
		var n int
		if scanner.Scan() {
			s = scanner.Text()
			n, _ = strconv.Atoi(s)
		}

		if n == 3 {
			fmt.Println(-1)
		} else {
			var arr []int = make([]int, n)
			for i := 0; i < n; i++ {
				arr[i] = i + 1
			}
			left := arr[n/2:]
			right := arr[:n/2]
			arr = append(left, right...)

			for j := 0; j < n-1; j++ {
				fmt.Printf("%d ", arr[j])
			}
			fmt.Println(arr[n-1])
		}

	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const INF = 1e18
const N = 2e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		if n == 3 {
			fmt.Println("-1")
			continue
		}
		if n%2 == 0 {
			for i := 0; i < n; i++ {
				fmt.Printf("%d ", n-i)
			}
			fmt.Println()
		} else {
			for i := 1; i <= n-3; i++ {
				if i%2 == 1 {
					fmt.Printf("%d ", n-i)
				} else {
					fmt.Printf("%d ", n+2-i)
				}
			}
			fmt.Println("1 2 3")
		}
	}
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

func main(){
	defer writer.Flush()
	T := readInt()

	for ;T > 0; T-- {
		n := readInt()

		if n == 2 {
			fmt.Fprintln(writer, "2 1")
			continue
		}

		if n == 3 {
			fmt.Fprintln(writer, "-1")
			continue
		}

		fmt.Fprint(writer,  n - 1, " ", n)

		for i := 1; i <= n - 2; i++ {
			fmt.Fprint(writer, " ", i)
		}

		fmt.Fprintln(writer)
	}
}

package main

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

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

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

		if n == 3 {
			fmt.Fprintln(out, -1)
			continue
		}
		for i := n/2 + 1; i <= n; i++ {
			fmt.Fprintf(out, "%d ", i)
		}
		for i := n / 2; i > 0; i-- {
			fmt.Fprintf(out, "%d ", i)
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

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

	var T int
	var n int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		ans := []int{}
		if n == 3 {
			ans = []int{-1}
		} else if n%2 == 0 {
			for i := 0; i < n; i++ {
				ans = append(ans, n-i)
			}
		} else {
			ans = append(ans, n/2 + 1)
			for i := 0; i < n/2; i++ {
				ans = append(ans, ans[0]+i+1)
			}
			for i := 0; i < n/2; i++ {
				ans = append(ans, i+1)
			}
		}

		for _, v := range ans {
			Fprintf(out, "%d ", v)
		}
		Fprintln(out, "")
	}
}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)
		var arr []int
		arr = sec(num1)
		for i := 0; i < len(arr); i++ {
			fmt.Printf("%d ", arr[i])
		}
		fmt.Println()
	}

}
func sec(num int) []int {
	var arr []int
	if num == 3 {
		arr = append(arr, -1)
		return arr
	}
	if num == 2 {
		arr = append(arr, 2, 1)
		return arr
	}
	if num == 5 {
		arr = append(arr, 4, 5, 1, 2, 3)
		return arr
	}
	for i := num; i > 0; i-- {
		arr = append(arr, i)
	}
	if num > 5 && num%2 != 0 {
		mid := (num / 2)
		arr[mid] += 1
		arr[mid-1] -= 1
	}

	return arr
}

package main

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

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

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

	if n == 2 {
		return "2 1"
	}

	if n == 3 {
		return "-1"
	}

	var sb strings.Builder
	sb.WriteString(strconv.Itoa(n-1) + " " + strconv.Itoa(n))
	if n%2 == 0 {
		for i := n - 2; i > 0; i-- {
			sb.WriteString(" " + strconv.Itoa(i))
		}
	} else {
		for i := 1; i <= n-2; i++ {
			sb.WriteString(" " + strconv.Itoa(i))
		}
	}

	return sb.String()
}

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

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

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

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)
		var arr []int
		arr = sec(num1)
		for i := 0; i < len(arr); i++ {
			fmt.Printf("%d ", arr[i])
		}
		fmt.Println()
	}

}
func sec(num int) []int {
	var arr []int
	if num == 3 {
		arr = append(arr, -1)
		return arr
	}
	if num == 2 {
		arr = append(arr, 2, 1)
		return arr
	}
	if num == 5 {
		arr = append(arr, 4, 5, 1, 2, 3)
		return arr
	}
	for i := num; i > 0; i-- {
		arr = append(arr, i)
	}
	if num > 5 {
		mid := (num / 2)
		arr[mid] += 1
		arr[mid-1] -= 1
	}

	return arr
}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)
		var arr []int
		arr = sec(num1)
		for i := 0; i < len(arr); i++ {
			fmt.Printf("%d ", arr[i])
		}
		fmt.Println()
	}

}
func sec(num int) []int {
	var arr []int
	if num == 3 {
		arr = append(arr, -1)
		return arr
	}
	if num == 2 {
		arr = append(arr, 2, 1)
		return arr
	}
	if num == 5 {
		arr = append(arr, 4, 5, 3, 2, 1)
		return arr
	}
	for i := num; i > 0; i-- {
		arr = append(arr, i)
	}
	if num > 5 {
		mid := (num / 2)
		arr[mid] += 1
		arr[mid-1] -= 1
	}

	return arr
}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)
		var arr []int
		arr = sec(num1)
		for i := 0; i < len(arr); i++ {
			fmt.Printf("%d ", arr[i])
		}
		fmt.Println()
	}

}
func sec(num int) []int {
	var arr []int
	if num == 3 {
		arr = append(arr, -1)
		return arr
	}
	if num == 2 {
		arr = append(arr, 2, 1)
		return arr
	}
	if num == 5 {
		arr = append(arr, 4, 5, 3, 2, 1)
		return arr
	}
	for i := num; i > 0; i-- {
		arr = append(arr, i)
	}
	if num > 5 {
		mid := (num / 2) + 1
		arr[mid] -= 1
		arr[mid+1] += 1
	}

	return arr
}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)
		var arr []int
		arr = sec(num1)
		for i := 0; i < len(arr); i++ {
			fmt.Printf("%d ", arr[i])
		}
	}

}
func sec(num int) []int {
	var arr []int
	if num == 3 {
		arr = append(arr, -1)
		return arr
	}
	if num == 2 {
		arr = append(arr, 2, 1)
		return arr
	}
	if num == 5 {
		arr = append(arr, 4, 5, 3, 2, 1)
		return arr
	}
	for i := num; i > 0; i-- {
		arr = append(arr, i)
	}
	if num > 5 {
		mid := (num / 2) + 1
		arr[mid] -= 1
		arr[mid+1] += 1
	}

	return arr
}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num int
		fmt.Scan(&num)
		var arr []int
		arr = sec(num)
		fmt.Println(arr)
	}

}
func sec(num int) []int {
	var arr []int
	if num == 3 {
		arr = append(arr, -1)
		return arr
	}
	if num == 2 {
		arr = append(arr, 2, 1)
		return arr
	}
	for i := num; i > 0; i-- {
		arr = append(arr, i)
	}
	for i := 0; i < len(arr); i += 2 {
		if arr[i] == 3 || arr[i+1] == 3 {
			break
		}
		var temp int
		temp = arr[i]
		arr[i] = arr[i+1]
		arr[i+1] = temp
	}

	return arr
}

package main

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

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

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

	if n == 3 {
		return "-1"
	}

	res := strconv.Itoa(n-1) + " " + strconv.Itoa(n)
	if n%2 == 0 {
		for i := n - 2; i > 0; i-- {
			res += " " + strconv.Itoa(i)
		}
	} else {
		for i := 1; i <= n-2; i++ {
			res += " " + strconv.Itoa(i)
		}
	}

	return res
}

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

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

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

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		var n int
		Fscan(in, &n)
		if n == 3 {
			Fprintln(out, "-1")
			continue
		}
		if n%2 == 0 {
			for i := 1; i <= n; i += 2 {
				Fprint(out, i+1, " ", i, " ")
			}
			Fprintln(out)
			continue
		} else {
			for i := 4; i <= n; i += 2 {
				Fprint(out, i+1, " ", i, " ")
			}
			for i := 3; i >= 1; i-- {
				Fprint(out, i, " ")
			}
			Fprintln(out)
			continue
		}
	}

}

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

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

package main

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

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		var n int
		Fscan(in, &n)
		if n == 3 {
			Fprintln(out, "-1")
			continue
		}
		if n%2 == 0 {
			for i := 1; i <= n; i += 2 {
				Fprint(out, i+1, " ", i, " ")
			}
			Fprintln(out)
			continue
		} else {
			for i := 3; i >= 1; i-- {
				Fprint(out, i, " ")
			}
			for i := 4; i <= n; i += 2 {
				Fprint(out, i+1, " ", i, " ")
			}
			Fprintln(out)
			continue
		}
	}

}

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

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

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	Fscan(in, &n)
	if n == 1 || n == 3 {
		Fprintln(out, "-1")
		return
	}
	for i := n; i >= 1; i-- {
		if n%2 == 1 && n/2 == i {
			Fprintf(out, "%d ", i+1)
			continue
		}
		if n%2 == 1 && (n+1)/2 == i {
			Fprintf(out, "%d ", i-1)
			continue
		}
		Fprintf(out, "%d ", i)
	}
	Fprintln(out)
}

func abs(a int64) int64 {
	if a > 0 {
		return a
	}
	return -a
}
func sum(a, b int64) int64 {
	return (b + a) * (b - a + 1) / 2
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}


package main

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

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

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

type Team struct {
  Name string
  Points, GoalsFor, GoalsAgainst, Differential int
}
func (team *Team) toS() string {
  return fmt.Sprintf("Name: %s, Points: %d, GoalsFor: %d, GoalsAgainst: %d," +
      " Differential: %d", team.Name, team.Points, team.GoalsFor,
      team.GoalsAgainst, team.Differential)
}
type Teams []*Team
func (teams Teams) Len() int {
  return len(teams)
}
func (teams Teams) Swap(i, j int) {
  teams[i], teams[j] = teams[j], teams[i]
}
func (teams Teams) Less(i, j int) bool {
  a, b := teams[i], teams[j]
  if a.Points != b.Points {
    return a.Points > b.Points
  }
  if a.Differential != b.Differential {
    return a.Differential > b.Differential
  }
  if a.GoalsFor != b.GoalsFor {
    return a.GoalsFor > b.GoalsFor
  }
  return false
}

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

  n := getI()
  nameToTeam := map[string]*Team{}
  teams := make([]*Team, n)
  for i := 0; i < n; i++ {
    name := getS()
    team := &Team{ Name: name }
    nameToTeam[name] = team
    teams[i] = team
  }
  for i := n*(n-1)/2; i != 0; i-- {
    names := strings.Split(getS(), "-")
    goals := strings.Split(getS(), ":")
    for j := 0; j < 2; j++ {
      team := nameToTeam[names[j]]
      goalsFor, _ := strconv.Atoi(goals[j])
      goalsAgainst, _ := strconv.Atoi(goals[(j+1)%2])
      team.GoalsFor += goalsFor
      team.GoalsAgainst += goalsAgainst
      team.Differential += goalsFor - goalsAgainst
      if goalsFor > goalsAgainst {
        team.Points += 3
      } else if goalsFor == goalsAgainst {
        team.Points++
      }
    }
  }
  sort.Sort(Teams(teams))
  result := make([]string, len(teams)/2)
  for i := range result {
    result[i] = teams[i].Name
  }
  sort.Strings(result)
  for _, s := range result {
    writer.WriteString(fmt.Sprintf("%s\n", s))
  }
}

package main

import (
	"sort"
	"bytes"
	"os"
	"io/ioutil"
	"fmt"
	"strings"
)
type Team struct {
	Name string
	Point int
	DeltaGoal int
	Goal int
}

type TeamList []*Team
func (this TeamList) Len() int {
	return len(this)
}
func (this TeamList) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this TeamList) Less(i, j int) bool {
	return this[i].Point > this[j].Point ||
	(this[i].Point == this[j].Point && this[i].DeltaGoal > this[j].DeltaGoal) ||
	(this[i].Point == this[j].Point && this[i].DeltaGoal == this[j].DeltaGoal && this[i].Goal > this[j].Goal)
}

type SortByName []*Team
func (this SortByName) Len() int {
	return len(this)
}
func (this SortByName) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this SortByName) Less(i, j int) bool {
	return this[i].Name < this[j].Name
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	teamMap := map[string]*Team{}
	fmt.Fscanf(reader, "%d\r\n", &n)
	for i := 0;i < n;i ++ {
		var team string
		fmt.Fscanf(reader, "%s\r\n", &team)
		teamMap[team] = &Team {
			Name: team,
		}
	}
	for i := 0;i < n * (n - 1) / 2;i ++ {
		var scoreA, scoreB int
		var match string
		fmt.Fscanf(reader, "%s %d:%d\r\n", &match, &scoreA, &scoreB)
		teams := strings.Split(match, "-")
		if scoreA > scoreB {
			teamMap[teams[0]].Point += 3
		} else if scoreB > scoreA {
			teamMap[teams[1]].Point +=3
		} else {
			teamMap[teams[0]].Point ++
			teamMap[teams[1]].Point ++
		}
		teamMap[teams[0]].DeltaGoal += (scoreA - scoreB)
		teamMap[teams[1]].DeltaGoal += (scoreB - scoreA)
		teamMap[teams[0]].Goal += scoreA
		teamMap[teams[1]].Goal += scoreB
	}
	teamList := make([]*Team, 0, n)
	for _, team := range teamMap {
		teamList = append(teamList, team)
	}
	sort.Sort(TeamList(teamList))
	teamList = teamList[:(n/2)]
	sort.Sort(SortByName(teamList))
	for i := 0;i < n/2;i ++ {
		fmt.Printf("%s\n", teamList[i].Name)
	}
}

package main

import (
	"sort"
	"bytes"
	"os"
	"io/ioutil"
	"fmt"
	"strings"
)
type Team struct {
	Name string
	Score int
}

type SortByScore []*Team
func (this SortByScore) Len() int {
	return len(this)
}
func (this SortByScore) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this SortByScore) Less(i, j int) bool {
	return this[i].Score > this[j].Score
}

type SortByName []*Team
func (this SortByName) Len() int {
	return len(this)
}
func (this SortByName) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this SortByName) Less(i, j int) bool {
	return this[i].Name < this[j].Name
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	scores := map[string]int{}
	fmt.Fscanf(reader, "%d\r\n", &n)
	for i := 0;i < n;i ++ {
		var team string
		fmt.Fscanf(reader, "%s\r\n", &team)
		scores[team] = 0
	}
	for i := 0;i < n * (n - 1) / 2;i ++ {
		var scoreA, scoreB int
		var match string
		fmt.Fscanf(reader, "%s %d:%d\r\n", &match, &scoreA, &scoreB)
		teams := strings.Split(match, "-")
		if scoreA > scoreB {
			scores[teams[0]] +=3
		} else if scoreB > scoreA {
			scores[teams[1]] +=3
		} else {
			scores[teams[0]] ++
			scores[teams[1]] ++
		}
	}
	teamList := make([]*Team, 0, n)
	for team, score := range scores {
		teamList = append(teamList, &Team{
			Name : team,
			Score: score,
		})
	}
	sort.Sort(SortByScore(teamList))
	teamList = teamList[:(n/2)]
	sort.Sort(SortByName(teamList))
	for i := 0;i < n/2;i ++ {
		fmt.Printf("%s\n", teamList[i].Name)
	}
}

package main

import (
	"sort"
	"bytes"
	"os"
	"io/ioutil"
	"fmt"
	"strings"
)
type Team struct {
	Name string
	Point int
	DeltaGoal int
	Goal int
}

type TeamList []*Team
func (this TeamList) Len() int {
	return len(this)
}
func (this TeamList) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this TeamList) Less(i, j int) bool {
	return this[i].Point > this[j].Point ||
	(this[i].Point == this[j].Point && this[i].DeltaGoal > this[j].DeltaGoal) ||
	(this[i].Point == this[j].Point && this[i].DeltaGoal == this[j].DeltaGoal && this[i].Goal > this[j].DeltaGoal)
}

type SortByName []*Team
func (this SortByName) Len() int {
	return len(this)
}
func (this SortByName) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this SortByName) Less(i, j int) bool {
	return this[i].Name < this[j].Name
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	teamMap := map[string]*Team{}
	fmt.Fscanf(reader, "%d\r\n", &n)
	for i := 0;i < n;i ++ {
		var team string
		fmt.Fscanf(reader, "%s\r\n", &team)
		teamMap[team] = &Team {
			Name: team,
		}
	}
	for i := 0;i < n * (n - 1) / 2;i ++ {
		var scoreA, scoreB int
		var match string
		fmt.Fscanf(reader, "%s %d:%d\r\n", &match, &scoreA, &scoreB)
		teams := strings.Split(match, "-")
		if scoreA > scoreB {
			teamMap[teams[0]].Point += 3
		} else if scoreB > scoreA {
			teamMap[teams[1]].Point +=3
		} else {
			teamMap[teams[0]].Point ++
			teamMap[teams[1]].Point ++
		}
		teamMap[teams[0]].DeltaGoal += (scoreA - scoreB)
		teamMap[teams[1]].DeltaGoal += (scoreB - scoreA)
		teamMap[teams[0]].Goal += scoreA
		teamMap[teams[1]].Goal += scoreB
	}
	teamList := make([]*Team, 0, n)
	for _, team := range teamMap {
		teamList = append(teamList, team)
	}
	sort.Sort(TeamList(teamList))
	teamList = teamList[:(n/2)]
	sort.Sort(SortByName(teamList))
	for i := 0;i < n/2;i ++ {
		fmt.Printf("%s\n", teamList[i].Name)
	}
}


package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	arr := make([]int,n)
	mp := map[int]int{}
	mx := 0
	for i := 0;i < n;i++{
		fmt.Fscan(in,&arr[i])
		mp[arr[i]]++
		mx = max(mx,mp[arr[i]])
	}
	if mx == n{
		fmt.Fprintln(out,0)
		return
	}
	ans := 0
	cur := mx
	for mx < n{
		ans += 1
		mx *= 2
	}
	fmt.Fprintln(out,ans + n - cur)
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var t int
	_, _ = fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		var n int
		_, _ = fmt.Fscan(reader, &n)
		var counter = make(map[int]int)
		for i := 0; i < n; i++ {
			var d int
			_, _ = fmt.Fscan(reader, &d)
			counter[d]++
		}
		idx := -1
		for i := range counter {
			if idx == -1 || counter[i] > counter[idx] {
				idx = i
			}
		}
		ops := 0
		for counter[idx] < n {
			ops++
			if counter[idx] < n-counter[idx] {
				ops += counter[idx]
			} else {
				ops += n - counter[idx]
			}
			counter[idx] *= 2
		}
		fmt.Println(ops)
	}
}

package main

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

func main_div2_781_B() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	/* Begin Coding */
	sol = func() {
		var n int
		Fscan(in, &n)
		var ans, max int
		count := map[int]int{}
		for i := 0; i < n; i++ {
			var temp int
			Fscan(in, &temp)
			count[temp]++
			if max < count[temp] {
				max = count[temp]
			}
		}
		for max < n {
			ans++
			if n-max >= max {
				ans += max
			} else {
				ans += n - max
			}
			max *= 2
		}
		Fprintln(out, ans)
	}
	/* Code Ending */
	for i := 0; i < cas; i++ {
		sol()
	}

}

func main() {
	main_div2_781_B()
}

package main

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

var (
	sc *bufio.Scanner
	wr *bufio.Writer
)

func init() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	wr = bufio.NewWriter(os.Stdout)
}

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

func scanStr() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	defer wr.Flush()
	T := scanInt()

	for ; T > 0; T-- {
		n := scanInt()
		dict := make(map[int]int)
		for i := 0; i < n; i++ {
			dict[scanInt()]++
		}

		freq := math.MinInt
		for _, val := range dict {
			if val > freq {
				freq = val
			}
		}

		fmt.Fprintln(wr, solve(n, freq))
	}

}

func solve(n, k int) int {
	var count int
	for k < n {
		count++
		if 2*k >= n {
			count += n - k
			break
		}
		count += k
		k *= 2
	}

	return count
}

package main

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

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

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

	var c int
	var n int
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		a := make([]int, n)
		max_cnt := 0
		cnt := make(map[int]int)
		for i := range a {
			Fscan(in, &a[i])
			cnt[a[i]]++
			if cnt[a[i]] > max_cnt {
				max_cnt = cnt[a[i]]
			}
		}
		ans := 0
		remain := n - max_cnt
		for remain > 0 {
			//copy
			ans++
			//swap
			k := min(remain, max_cnt)
			ans += k
			remain -= k
			max_cnt *= 2
		}
		Fprintf(out, "%d\n", ans)
	}
}

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

package main

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

const (
	INF = 3e5 + 10
)

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

	var t int
	Fscan(in, &t)

	for k := 1; k <= t; k++ {
		var n int
		Fscan(in, &n)

		m := make(map[int]int)
		a := make([]int, n+1)

		mx := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			m[a[i]] += 1
		}

		for _, v := range m {
			if v > mx {
				mx = v
			}
		}

		i := mx
		j := n - i

		ans := 0
		for j > 0 {
			ans += min(i, j)
			j -= i
			i += i
			ans += 1
		}

		Fprintln(out, ans)

	}

}

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

package main

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

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)
	cnt := make(map[int]int)
	mx := 0
	for i := 0; i < n; i++ {
		var x int
		Fscan(in, &x)
		cnt[x]++
		mx = max(mx, cnt[x])
	}
	if n == 1 { Fprintln(out, 0); return }

	step, ans := 0, mx
	for ans < n {
		if ans * 2 <= n {
			step = step + 1 + ans
			ans *= 2
		} else {
			step = step + 1 + (n - ans)
			break
		}
	}
	Fprintln(out, step)
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	Fscan(in, &t)
	for; t > 0; t-- {
		solve(in, out)
	}
}

func min(a, b int) int { if a > b { return b }; return a }
func max(a, b int) int { if a < b { return b }; return a }
func abs(v int) int { if v > 0 { return v }; return -v }
func gcd(a, b int) int { if b > 0 { return gcd(b, a % b) }; return a }
func lcm(a, b int) int { return a * b / gcd(a, b) }

package main

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

const MOD int64 = 1000000007

var n int

var a []int

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

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

		fmt.Fscan(read, &n)

		a = make([]int, n)

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

		a := solve()
		fmt.Println(a)
	}
}

type pair struct {
	v int
	idx []int
}

func solve()  int {
	mp := make(map[int]int)

	for i := 0; i < n; i++ {
		mp[a[i]]++
	}

	var maxKey int
	var maxV int

	for k, v := range mp{
		if v > maxV {
			maxV = v
			maxKey = k
		}
	}

	maxV = mp[maxKey]

	if maxV == n {
		return 0
	}

	other := n - maxV

	var cnt int
	for ;; {
		cnt++

		if maxV >= other {
			cnt += other
			break
		}

		cnt += maxV
		maxV += maxV
		other = n - maxV
	}

	return cnt
}

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

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 int64
		fmt.Fscan(in, &n)

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

			m[v]++
			if m[v] > max {
				max = m[v]
			}
		}

		var c int64
		for max < n {
			c += min(max, n-max) + 1
			max *= 2
		}
		fmt.Fprintln(out, c)
	}
}

package main

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

/*
the most common element count is v,
it increases by 2 times after one copy and swap, like:
v -> v*2 -> v*4 -> ...
until after m+1 copies, v*2^(m+1)>=n, operations ends,

copy times is m, if 2^m<n, need one more copy.
swap time: n-v.
*/
func max(a,b int)int{
  if a>b{return a}
  return b
}
func nxt(s *bufio.Scanner)int{
  s.Scan()
  v,_:=strconv.Atoi(s.Text())
  return v
}
func main(){
  s:=bufio.NewScanner(os.Stdin)
  s.Split(bufio.ScanWords)

  t:=nxt(s)
  var n,v,most int
  for i:=0;i<t;i++{
    n=nxt(s)

    most=0
    c:=map[int]int{}
    for j:=0;j<n;j++{
      v=nxt(s)
      c[v]++
      most=max(most,c[v])
    }

    res:=0
    for t:=most;t<n;t*=2{
      res+=1
    }
    fmt.Println(res+n-most)
  }
}

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, curr int
		fmt.Fscan(reader, &n)
		m := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &curr)
			m[curr]++
		}
		max_count := 0
		for _, v := range m {
			if v > max_count {
				max_count = v
			}
		}
		ans := n - max_count
		for max_count < n {
			ans++
			max_count = max_count * 2
		}
		write_int(f, ans)
		write_string(f, "\n")

	}
}

// 00
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, curr int
		fmt.Fscan(reader, &n)
		m := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &curr)
			m[curr]++
		}
		max_count := 0
		for _, v := range m {
			if v > max_count {
				max_count = v
			}
		}
		ans := n - max_count
		for max_count < n {
			ans++
			max_count = max_count * 2
		}
		fmt.Println(ans)

	}
}

package main

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

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

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

func 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-- {
		var n int
		fmt.Fscan(in, &n)

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

		h := make(map[int]int)

		for i := 0; i < len(a); i++ {
			h[a[i]]++
		}

		maxA := a[0]
		for k, v := range h {
			if v > h[maxA] {
				maxA = k
			}
		}

		count := 0
		current := h[maxA]

		for current < n {
			count += 1 + min(current, n-current)
			current = current * 2
		}

		fmt.Fprintln(out, count)
	}
}

package main

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

// if it is an even number, then c and d will be 1 and 1 and we just need to find two primes number to a and b and a + b = n - 2

// if it is an odd number, then b and c and d will be 1, a will be n - 3 which is an even number

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

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

	for i := 0; i < t; i++ {
		scanner.Scan()
		a, _ := strconv.Atoi(scanner.Text())
		max := 0
		freq := make(map[int]int)

		for k := 0; k < a; k++ {
			scanner.Scan()
			cur, _ := strconv.Atoi(scanner.Text())

			freq[cur]++
			if freq[cur] > max {
				max = freq[cur]
			}
		}

		sum, cnt := max, 0
		for sum < a {
			sum *= 2
			cnt++
		}

		fmt.Println(a - max + cnt)
	}

}

package main

// Problem description URL https://atcoder.jp/contests/abc236/tasks/abc240_a

import (
	"bufio"
	"container/list"
	"fmt"
	"math"
	"os"
)

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

func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }

type pair struct {
	Color int
	Size  int
}

var (
	n   = 0
	t   = 0
	a   []int
	res []int
	s   *list.List
	cur = 0
)

func gcd(a int, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}
func solve() {
	scan(&t)
	for t != 0 {
		t--
		scan(&n)
		count := map[int]int{}
		m := 0
		for i := 0; i < n; i++ {
			x := 0
			scan(&x)
			count[x]++
			m = int(math.Max(float64(m), float64(count[x])))
		}
		res := 0
		for m < n{
			res ++
			if(2*m >n){
				res += n-m
				break
			}
			res += m
			m *= 2
		}
		printf("%d\n", res)
	}
}

func main() {
	defer out.Flush()
	solve()
}

package main

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

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

	var err error

	var tests int
	_, err = fmt.Fscan(reader, &tests)
	if err != nil {
		panic(err.Error())
	}

	for t := 0; t < tests; t++ {
		var n int
		_, err = fmt.Fscan(reader, &n)
		if err != nil {
			panic(err.Error())
		}
		cnt := make(map[int]int)
		for i := 0; i < n; i++ {
			var x int
			_, err = fmt.Fscan(reader, &x)
			if err != nil {
				panic(err.Error())
			}
			_, ok := cnt[x]
			if ok {
				cnt[x] += 1
			} else {
				cnt[x] = 1
			}
		}
		cur := 0
		for _, x := range cnt {
			if x > cur {
				cur = x
			}
		}
		ans := 0
		for cur < n {
			if cur+cur >= n {
				ans += 1 + n - cur
				cur = n
			} else {
				ans += 1 + cur
				cur += cur
			}
		}
		_, err := fmt.Fprintln(writer, ans)
		if err != nil {
			panic(err.Error())
		}
	}

}


package main

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

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

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

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

    n := ReadInt()

    for ; n > 0; n-- {
	    if n%2 == 0 {
	    	writer.WriteString("1")
	    }else {
	    	writer.WriteString("0")
	    }
    }
	writer.WriteString("\n")

}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		var r, t int
		fmt.Scanf("%d %d\n", &r, &t)
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d", i%2)
	}
}

package main

import "fmt"

func main() {
    var n,m int
    fmt.Scan(&n,&m)
    for i:=0;i<m;i++{
        var x,y int
        fmt.Scan(&x,&y)

    }
    for i:=0;i<n;i++{
        if i%2== 0 {
            fmt.Print(0)
        } else {
            fmt.Print(1)
        }
    }
    fmt.Println()

}


package main

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

var DIV int64 = 1000000007

func culc(a, n int64) int64 {
	if n == 0 {
		return 1
	}

	if n == 1 {
		return a
	}

	if n%2 == 1 {
		return culc(a, n-1) * a % DIV
	}

	sum := culc(a, n/2)
	return sum * sum % DIV
}

func main() {

	reader := bufio.NewReader(os.Stdin)

	var n, m int

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

	for i := 1; i <= n; i++ {
		for j := 0; j < n; j++ {
			if j%2 == 0 {
				m = j*n + i
			} else {
				m = n*(j+1) + 1 - i
			}
			fmt.Printf("%d ", m)
		}
		fmt.Printf("\n")
	}
}

package main

import (
	"fmt"
	"math"
)

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

	for i := 1; i < n+1; i++ {
		c := i
		fmt.Print(c, " ")
		for j := 0; j < n-1; j++ {
			if j%2 == 0 {
				c = c + 2*(n-i) + 1
			} else {
				c = c + 2*i - 1
			}
			fmt.Print(c, " ")
		}
		fmt.Println()
	}
}

func exp(x int) int {
	return int(math.Exp2(float64(x)))
}
func comb(x, y int) int {
	m := 1
	n := 1
	for i := 0; i < y; i++ {
		m = m * (x - i)
		n = n * (i + 1)
	}
	return m / n
}
func max(x, y int) int {
	return int(math.Max(float64(x), float64(y)))
}

func min(x, y int) int {
	return int(math.Min(float64(x), float64(y)))
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var n int
var answers [][]int

func main() {
	n = ReadInt()

	r := n / 2
	answers = make([][]int, n)
	for i := 0; i < n; i++ {
		answers[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			answers[i][j] = i*n + (j + 1)
		}
	}
	// fmt.Println(answers)
	for i := 0; i < r; i++ {
		answers[i] = Reverse(answers[i])
	}
	// fmt.Println(answers)

	for j := 0; j < n; j++ {
		for i := 0; i < n; i++ {
			if i == n-1 {
				fmt.Printf("%d\n", answers[i][j])
			} else {
				fmt.Printf("%d ", answers[i][j])
			}
		}
	}
}

func Reverse(A []int) []int {
	res := []int{}

	n := len(A)
	for i := n - 1; i >= 0; i-- {
		res = append(res, A[i])
	}

	return res
}

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

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

package main

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

func main() {
	defer Flush()

	n := readi()
	ans := make([][]int, n)
	for i := range ans {
		ans[i] = make([]int, 0, n)
	}
	i := 0
	d := 1
	for v := 1; v <= n*n; v++ {
		if i == n {
			i = n - 1
			d = -1
		} else if i == -1 {
			i = 0
			d = 1
		}
		ans[i] = append(ans[i], v)
		i += d
	}

	for i := range ans {
		sp := ""
		for _, v := range ans[i] {
			printf("%s%d", sp, v)
			sp = " "
		}
		println()
	}
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

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

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}


package main
import (
    "fmt"
    "bufio"
    "io"
    "os"
)
const N int = 2e5+10
var casee int
var n, m, tot int
var x, y int
var v1, v2 int64
var flag bool
var visit [N]int
var v, t [N]int
var head [N]int
var nxt, to [N<<1]int
func dfs (now, state int) {
    visit[now] = state + 2
    if (state & 1) > 0 {
        v1 += int64(t[now] - v[now])
    } else {
        v2 += int64(t[now] - v[now])
    }
    for i := head[now]; i > 0; i = nxt[i] {
        var index int = to[i]
        if visit[index] > 0 {
            if visit[index] == visit[now] {
                flag = true
            }
            continue
        }
        dfs(index, state^1)
    }
}
func main() {
    var input io.Reader = bufio.NewReader(os.Stdin)
    for fmt.Fscan(input, &casee); casee > 0; casee-- {
        v1 = 0
        v2 = 0
        tot = 0
        flag = false
        head = [N]int{}
        visit = [N]int{}

        fmt.Fscan(input, &n)
        fmt.Fscan(input, &m)
        for i := 1; i <= n; i++ {
            fmt.Fscan(input, &v[i])
        }
        for i:= 1; i <= n; i++ {
            fmt.Fscan(input, &t[i])
        }
        for ; m > 0; m-- {
            fmt.Fscan(input, &x)
            fmt.Fscan(input, &y)
            tot++; to[tot] = y; nxt[tot] = head[x]; head[x] = tot
            tot++; to[tot] = x; nxt[tot] = head[y]; head[y] = tot
        }

        dfs(1, 0)

        if ((flag && (v1+v2+1) % 2 != 0) || (!flag && v1 == v2)) {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

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

// github.com/EndlessCheng/codeforces-go
func main(){
	out:=bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i,_n,buf:=0,0,make([]byte, 1<<12)
	rc:=func()byte{
		if _i==_n {
			_n,_=os.Stdin.Read(buf)
			if _n==0{return 0}
			_i=0
		}
		b:=buf[_i];_i++;return b
	}
	r:=func()(x int){
		b:=rc()
		neg:=false
		for;'0'>b;b=rc(){if b=='-'{neg=true}}
		for;'0'<=b;b=rc(){x=x*10+int(b&15)}
		if neg{return -x};return
	}
	for T:=r();T>0;T--{
		n,m :=r(),r()
		a:=make([]int,n)
		for i:=range a{a[i]=r()}
		x:=0
		for i:=range a{a[i]-=r();x^=a[i]}
		g:=make([][]int,n)
		for;m>0;m--{
			v,w:=r()-1,r()-1
			g[v]=append(g[v],w)
			g[w]=append(g[w],v)
		}
		s:=int64(0)
		C:=make([]int8,n)
		var f func(int,int8)bool
		f=func(v int,c int8)bool{
			if c>0{s+=int64(a[v])}else if c<0{s-=int64(a[v])}
			C[v]=c
			for _,w:=range g[v]{if C[w]==c||C[w]==0&&f(w,-c){return true}}
			return false
		}
		if x&1==0&&(f(0,1)||s==0){Fprintln(out, "YES")}else{Fprintln(out, "NO")}
	}
}

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

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	var T,n,m,v,w int
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&m)
		a:=make([]int64,n)
		for i:=range a{Fscan(in,&a[i])}
		var x,s int64
		for i:=range a{
			Fscan(in,&v)
			a[i]=int64(v)-a[i]
			x^=a[i]
		}
		g:=make([][]int,n)
		for;m>0;m--{
			Fscan(in,&v,&w);v--;w--
			g[v]=append(g[v],w)
			g[w]=append(g[w],v)
		}
		C:=make([]int,n)
		var f func(int,int)bool
		f=func(v,c int)bool{
			s+=int64(c)*a[v]
			C[v]=c
			for _,w:=range g[v]{if C[w]==c||C[w]==0&&f(w,-c){return true}}
			return false
		}
		if x&1==0&&(f(0,1)||s==0){Println("YES")}else{Println("NO")}
	}
}

package main

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

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

	solve := func(Case int) bool {
		var n, m int
		Fscan(in, &n, &m)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		s := int64(0)
		for i := range b {
			Fscan(in, &b[i])
			b[i] -= a[i]
			s += b[i]
		}

		g := make([][]int, n)
		for i := 0; i < m; i++ {
			var v, w int
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		if s&1 > 0 {
			return false
		}
		colors := make([]int8, n)
		colors[0] = 1
		var f func(int, int) bool
		f = func(v, p int) bool {
			for _, w := range g[v] {
				if colors[w] == 0 {
					colors[w] = 3 - colors[v]
					if f(w, v) {
						return true
					}
				}
				if colors[w] == colors[v] {
					return true
				}
			}
			if p >= 0 {
				b[p] -= b[v]
				b[v] = 0
			}
			return false
		}
		return f(0, -1) || b[0] == 0
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		if solve(Case) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

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

}

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

package main
import("bufio";."fmt";"os";"runtime/debug")

// github.com/EndlessCheng/codeforces-go
func init() { debug.SetGCPercent(-1) }
func main(){
	out:=bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i,_n,buf:=0,0,make([]byte, 1<<12)
	rc:=func()byte{
		if _i==_n {
			_n,_=os.Stdin.Read(buf)
			if _n==0{return 0}
			_i=0
		}
		b:=buf[_i];_i++;return b
	}
	r:=func()(x int){
		b:=rc()
		neg:=false
		for;'0'>b;b=rc(){if b=='-'{neg=true}}
		for;'0'<=b;b=rc(){x=x*10+int(b&15)}
		if neg{return -x};return
	}
	for T:=r();T>0;T--{
		n,m :=r(),r()
		a:=make([]int,n)
		for i:=range a{a[i]=r()}
		x:=0
		for i:=range a{a[i]-=r();x^=a[i]}
		g:=make([][]int,n)
		for;m>0;m--{
			v,w:=r()-1,r()-1
			g[v]=append(g[v],w)
			g[w]=append(g[w],v)
		}
		s:=int64(0)
		C:=make([]int8,n)
		var f func(int,int8)bool
		f=func(v int,c int8)bool{
			if c>0{s+=int64(a[v])}else if c<0{s-=int64(a[v])}
			C[v]=c
			for _,w:=range g[v]{if C[w]==c||C[w]==0&&f(w,-c){return true}}
			return false
		}
		if x&1==0&&(f(0,1)||s==0){Fprintln(out, "YES")}else{Fprintln(out, "NO")}
	}
}

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

// github.com/EndlessCheng/codeforces-go
func main(){
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i, _n, buf := 0, 0, make([]byte, 1<<12)
	rc := func() byte {
		if _i == _n {
			_n, _ = os.Stdin.Read(buf)
			if _n == 0 {
				return 0
			}
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	for T:=r();T>0;T--{
		n,m :=r(),r()
		a:=make([]int,n)
		for i:=range a{a[i]=r()}
		x:=0
		for i:=range a{
			a[i]-=r()
			x^=a[i]
		}
		g:=make([][]int,n)
		for;m>0;m--{
			v,w:=r()-1,r()-1
			g[v]=append(g[v],w)
			g[w]=append(g[w],v)
		}
		s:=int64(0)
		C:=make([]int8,n)
		var f func(int,int8)bool
		f=func(v int,c int8)bool{
			if c>0{s+=int64(a[v])}else{s-=int64(a[v])}
			C[v]=c
			for _,w:=range g[v]{if C[w]==c||C[w]==0&&f(w,-c){return true}}
			return false
		}
		if x&1==0&&(f(0,1)||s==0){Fprintln(out, "YES")}else{Fprintln(out, "NO")}
	}
}

package main

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

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

	solve := func(Case int) bool {
		var n, m int
		Fscan(in, &n, &m)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		for i := range b {
			Fscan(in, &b[i])
			b[i] -= a[i]
		}

		g := make([][]int, n)
		d := make([]int, n)
		for i := 0; i < m; i++ {
			var v, w int
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
			d[v]++
			d[w]++
		}
		if m == 1 {
			return b[0]==b[1]
		}
		if m > n {
			return true
		}
		q := []int{}
		c:=0
		for i, d := range d {
			if d == 1 {
				q = append(q, i)
				c++
			}
		}
		last := 0
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			for _, w := range g[v] {
				b[w] -= b[v]
				if d[w]--; d[w] == 1 {
					last = w
					q = append(q, w)
					c++
				}
			}
		}
		if c==n {
			return b[last] == 0
		}
		s := int64(0)
		for i, d := range d {
			if d > 1 {
				if  d> 2 {
					return true
				}
				if i&1==0 {
					s+=b[i]
				} else {
					s-=b[i]
				}
			}
		}
		return s==0
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		if solve(Case) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

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

}

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

package main

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

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

	solve := func(Case int) bool {
		var n, m int
		Fscan(in, &n, &m)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		for i := range b {
			Fscan(in, &b[i])
			b[i] -= a[i]
		}

		g := make([][]int, n)
		d := make([]int, n)
		for i := 0; i < m; i++ {
			var v, w int
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
			d[v]++
			d[w]++
		}
		if m == 1 {
			return b[0]==b[1]
		}
		q := []int{}
		c:=0
		for i, d := range d {
			if d == 1 {
				q = append(q, i)
				c++
			}
		}
		last := 0
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			for _, w := range g[v] {
				b[w] -= b[v]
				if d[w]--; d[w] == 1 {
					last = w
					q = append(q, w)
					c++
				}
			}
		}
		if c==n {
			return b[last] == 0
		}
		s := int64(0)
		for i, d := range d {
			if d > 1 {
				if  d> 2 {
					return true
				}
				if i&1==0 {
					s+=b[i]
				} else {
					s-=b[i]
				}
			}
		}
		return s==0
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		if solve(Case) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

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

}

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

package main
import (
    "fmt"
    "bufio"
    "io"
    "os"
)
const N int = 2e5+10
var casee int
var n, m, tot int
var x, y int
var v1, v2 int64
var flag bool
var visit [N]int
var v, t [N]int
var head [N]int
var nxt, to [N<<1]int
func dfs (now, state int) {
    visit[now] = state + 2
    if (state & 1) > 0 {
        v1 += int64(t[now] - v[now])
    } else {
        v2 += int64(t[now] - v[now])
    }
    for i := head[now]; i > 0; i = nxt[i] {
        var index int = to[i]
        if visit[index] > 0 {
            if visit[index] == visit[now] {
                flag = true
            }
            continue
        }
        dfs(index, state^1)
    }
}
func main() {
    var input io.Reader = bufio.NewReader(os.Stdin)
    for fmt.Fscan(input, &casee); casee > 0; casee-- {
        v1 = 0
        v2 = 0
        tot = 0
        flag = false
        head = [N]int{}
        visit = [N]int{}

        fmt.Fscan(input, &n)
        fmt.Fscan(input, &m)
        for i := 1; i <= n; i++ {
            fmt.Fscan(input, &v[i])
        }
        for i:= 1; i <= n; i++ {
            fmt.Fscan(input, &t[i])
        }
        for ; m > 0; m-- {
            fmt.Fscan(input, &x)
            fmt.Fscan(input, &y)
            tot++; to[tot] = y; nxt[tot] = head[x]; head[x] = tot
            tot++; to[tot] = x; nxt[tot] = head[y]; head[y] = tot
        }

        dfs(1, 0)
        if ((flag && (v1+v2+1 & 1) != 0) || (!flag && v1 == v2)) {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

package main
import (
    "fmt"
    "bufio"
    "io"
    "os"
)
const N int = 2e5+10
var casee int
var n, m, tot int
var x, y int
var v1, v2 int64
var flag bool
var visit [N]int
var v, t [N]int
var head [N]int
var nxt, to [N<<1]int
func dfs (now, state int) {
    visit[now] = state + 2
    if (state & 1) > 0 {
        v1 += int64(t[now] - v[now])
    } else {
        v2 += int64(t[now] - v[now])
    }
    for i := head[now]; i > 0; i = nxt[i] {
        var index int = to[i]
        if visit[index] > 0 {
            if visit[index] == visit[now] {
                flag = true
            }
            continue
        }
        dfs(index, state^1)
    }
}
func main() {
    var input io.Reader = bufio.NewReader(os.Stdin)
    for fmt.Fscan(input, &casee); casee > 0; casee-- {
        v1 = 0
        v2 = 0
        tot = 0
        flag = false
        head = [N]int{}
        visit = [N]int{}

        fmt.Fscan(input, &n)
        fmt.Fscan(input, &m)
        for i := 1; i <= n; i++ {
            fmt.Fscan(input, &v[i])
        }
        for i:= 1; i <= n; i++ {
            fmt.Fscan(input, &t[i])
        }
        for ; m > 0; m-- {
            fmt.Fscan(input, &x)
            fmt.Fscan(input, &y)
            tot++; to[tot] = y; nxt[tot] = head[x]; head[x] = tot
            tot++; to[tot] = x; nxt[tot] = head[y]; head[y] = tot
        }

        dfs(1, 0)

        if ((flag && (v1+v2+1 & 1) > 0) || (!flag && v1 == v2)) {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}


package main

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

func main() {
	var t int
	fmt.Scanf("%d ", &t)
	for t > 0 {
		t--
		var k, x int64
		fmt.Scanf("%d %d ", &k, &x)
		fmt.Println(9*(k-1) + x)

	}
}

package main

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

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

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

func main() {
	defer writer.Flush()

	var n int64
	scanf("%d\n", &n)

	for n>0 {
		n--
		var k,x int64

		scanf("%d %d\n", &k, &x)

		ans := int64(9) * (k - int64(1)) + x

		printf("%d\n", ans)
	}
}

package main

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

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

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

func droot(x int) int {
	if x < 10 {
		return x
	}
	sum := 0
	for x > 0 {
		sum += x % 10
		x /= 10
	}
	return droot(sum)
}

func main() {
	defer writer.Flush()
	// for i := 1; i < 50; i++ {
	// 	fmt.Println(i, droot(i))
	// }
	var q, k, x int64
	scanf("%d\n", &q)
	for i := int64(0); i < q; i++ {
		scanf("%d %d\n", &k, &x)
		printf("%d\n", (k-1)*9+x)
	}
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	fmt.Scanf("%d ", &t)
	for i := 0; i < t; i++ {
		//var k int64
		var x int
		var s string

		fmt.Scanf("%s %d ", &s, &x)
		k, _ := strconv.ParseInt(s, 10, 64)
		k -= 1
		fmt.Println(k*9 + int64(x))
	}

}

package main

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

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

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

func dr(n int) int {
	return 1 + (n - 1) % 9
}

func sol(k int64, x int64) int64 {
	return x + (k - 1) * 9
}

func main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()

	var n int
	var x, k int64
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		scanf("%d %d\n", &k, &x)
		printf("%d\n", sol(k, x))
		// for f := 1; f <= 10; f++ {
		// 	printf("%d: %d\t", i*10 + f, dr(i*10 + f))
		// }
		// printf("\n")
	}
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		//var k int64
		var x int
		var s string

		fmt.Scanf("%s %d", &s, &x)
		k, _ := strconv.ParseInt(s, 10, 64)
		k -= 1
		fmt.Println(k*9 + int64(x))
	}

}

package main

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

func main() {
	var t int
	fmt.Scanf("%d ", &t)
	for t > 0 {
		t--
		var k, x int
		fmt.Scanf("%d %d ", &k, &x)
		fmt.Println(9*(k-1) + x)

	}
}

package main

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

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

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

func dr(n int) int {
	return 1 + (n - 1) % 9
}

func sol(k int, x int64) int64 {
	return x + (int64(k) - 1) * 9
}

func main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()

	var n, k int
	var x int64
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		scanf("%d %d\n", &k, &x)
		printf("%d\n", sol(k, x))
		// for f := 1; f <= 10; f++ {
		// 	printf("%d: %d\t", i*10 + f, dr(i*10 + f))
		// }
	}
}