package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	var fives int
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		arr[i] += arr[i] % 10
		if arr[i]%10 == 5 || arr[i]%10 == 0 {
			fives++
		}
	}

	if fives > 0 && fives != n {
		return "NO"
	}

	sort.Ints(arr)
	min := arr[0]
	max := arr[n-1]
	if min == max {
		return "YES"
	}

	if fives == n {
		if max-min == 5 && min%10 == 5 {
			return "YES"
		} else {
			return "NO"
		}
	}

	//dedup
	d := make([]int, 0, n)
	for i := 0; i < n-1; i++ {
		if arr[i] != arr[i+1] {
			d = append(d, arr[i])
		}
	}
	d = append(d, arr[n-1])

	for i := 0; i < n-1; i++ {
		for arr[i]+100000000 < max {
			arr[i] += 100000000
		}
		for arr[i]+10000000 < max {
			arr[i] += 10000000
		}
		for arr[i]+1000000 < max {
			arr[i] += 1000000
		}
		for arr[i]+100000 < max {
			arr[i] += 100000
		}
		for arr[i]+50000 < max {
			arr[i] += 50000
		}
		for arr[i]+10000 < max {
			arr[i] += 10000
		}
		for arr[i]-max < 10 {
			arr[i] += arr[i] % 10
		}
		if abs(max-arr[i]) == 10 {
			return "NO"
		}
	}

	return "YES"
}

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	var fives int
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		arr[i] += arr[i] % 10
		if arr[i]%10 == 5 || arr[i]%10 == 0 {
			fives++
		}
	}

	if fives > 0 && fives != n {
		return "NO"
	}

	sort.Ints(arr)
	min := arr[0]
	max := arr[n-1]
	if min == max {
		return "YES"
	}

	if fives == n {
		if max-min == 5 && min%10 == 5 {
			return "YES"
		} else {
			return "NO"
		}
	}

	//dedup
	d := make([]int, 0, n)
	for i := 0; i < n-1; i++ {
		if arr[i] != arr[i+1] {
			d = append(d, arr[i])
		}
	}
	d = append(d, arr[n-1])

	// AHAHHA THAT ACTUALLY WORKED LMAO
	for i := 0; i < n-1; i++ {
		for arr[i]+100000000 < max {
			arr[i] += 100000000
		}
		for arr[i]+10000000 < max {
			arr[i] += 10000000
		}
		for arr[i]+1000000 < max {
			arr[i] += 1000000
		}
		for arr[i]+100000 < max {
			arr[i] += 100000
		}
		for arr[i]+10000 < max {
			arr[i] += 10000
		}
		for arr[i]-max < 10 {
			arr[i] += arr[i] % 10
		}
		if abs(max-arr[i]) == 10 {
			return "NO"
		}
	}

	return "YES"
}

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

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			if aa[i]%10 == 5 {
				aa[i] += 5
			} else if aa[i]%10 != 0 {
				for aa[i]%10 != 2 {
					aa[i] += aa[i] % 10
				}
				aa[i] %= 20
			}
		}

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

var (
	in   *bufio.Scanner
	out  *bufio.Writer
	once = false
)

func solve() {
	in.Scan()
	n, _ := strconv.Atoi(in.Text())
	a := make(map[int]int)
	for i := 0; i < n; i++ {
		in.Scan()
		ai, _ := strconv.Atoi(in.Text())
		if ai%10 == 5 || ai%10 == 0 {
			if ai%10 == 5 {
				ai = ai + 5
			}
			a[ai]++
		} else {
			if ai%2 != 0 {
				ai = ai + ai%10
			}
			ai = ai % 20
			switch ai {
			case 2, 4, 8, 16:
				a[-1]++
			default:
				a[-2]++
			}
		}
	}
	if len(a) > 1 {
		out.WriteString("No\n")
	} else {
		out.WriteString("Yes\n")
	}
}

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

	if once {
		solve()
		return
	}
	in.Scan()
	x, _ := strconv.Atoi(in.Text())
	for t := 0; t < x; t++ {
		solve()
	}
}

package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"
	"runtime/pprof"
	"strconv"
	"strings"
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

// func main() {
// 	flag.Parse()
// 	if *cpuprofile != "" {
// 		f, err := os.Create(*cpuprofile)
// 		if err != nil {
// 			log.Fatal(err)
// 		}
// 		pprof.StartCPUProfile(f)
// 		defer pprof.StopCPUProfile()
// 	}

// 	in := bufio.NewReader(os.Stdin)
// 	tc := readInt(in)

// 	for i := 0; i < tc; i++ {
// 		n := readInt(in)
// 		_ = readArrInt(in)
// 		fmt.Println("N: ", n)
// 		fmt.Println("DONE TC: ", i+1)
// 	}
// }

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 readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

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

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

func exec(n int, arr []int) bool {
	// for i := 0; i < n; i++ {
	// 	fmt.Scan(&arr[i])
	// }
	var node11, node71, node51 = 0, 0, 0
	var node12, node72, node52 = 0, 0, 0
	for i := 0; i < n; i++ {
		var r = arr[i] % 10
		var q = arr[i] / 10
		if r == 1 || r == 2 || r == 4 || r == 8 {
			if q%2 == 0 {
				node12++
			} else {
				node11++
			}
		} else if r == 7 || r == 9 || r == 3 || r == 6 {
			if q%2 == 0 {
				node72++
			} else {
				node71++
			}
		} else {
			if q%2 == 0 {
				node52++
			} else {
				node51++
			}
		}
	}
	if node51+node52 > 0 {
		if node11+node71+node12+node72 > 0 {
			return false
		}
		var i5, i0 = -1, -1
		for i := 0; i < n; i++ {
			if arr[i]%10 == 0 {
				i0 = arr[i]
			} else if arr[i]%10 == 5 {
				i5 = arr[i]
			}
		}
		if i0 == -1 {
			i0 = i5 + 5
		}
		for i := 0; i < n; i++ {
			if arr[i]%10 == 0 {
				if i0 != arr[i] {
					return false
				}
			} else if arr[i]%10 == 5 {
				if i0-arr[i] != 5 {
					return false
				}
			}
		}
	} else {
		if node11 > 0 && node12 > 0 {
			return false
		}
		if node71 > 0 && node72 > 0 {
			return false
		}
		if node11 > 0 && node71 > 0 {
			return false
		}
		if node12 > 0 && node72 > 0 {
			return false
		}
	}
	return true
}

func main() {
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	in := bufio.NewReader(os.Stdin)

	var T, n int
	// fmt.Scan(&T)
	T = readInt(in) // var arr = make([]int, 2*100005)
	for it := 0; it < T; it++ {
		// fmt.Scan(&n)
		n = readInt(in)
		var arr = readArrInt(in)
		var is = exec(n, arr)
		if is {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}

}

package main

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

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

func ReadInt() int{
	var i int
	fmt.Fscan(in, &i)
	return i
}

// 1 -> 2 -> 4 -> 8 -> 6 -> 2 -> ....
// 0 -> 0
// 3 -> 6 -> 2
// 5 -> 0
// 7 -> 4
// 9 -> 8

func main(){
	defer out.Flush()

	t := ReadInt()

	for ; t > 0; t--{
		n := ReadInt()
		a := make([]int, n)
		hasFiveDivisor := false
		for i:=0;i<n;i++{
			a[i]=ReadInt()
			if a[i]%5==0{
				hasFiveDivisor = true
			}
		}
		can:=true
		if hasFiveDivisor {
			for i:=0;i<n;i++{
				a[i] = a[i]+a[i]%10
				if a[i] != a[0]{
					can = false
					break
				}
			}
		} else{
			for i :=0; i < n; i++ {
				for a[i]%10!=2{
					a[i] += a[i]%10
				}
				if a[i]%20 !=a[0]%20{
					can = false
					break
				}
			}
		}
		if can{
			fmt.Fprintln(out, "Yes")
		} else{
			fmt.Fprintln(out, "No")
		}
	}
}

func Gcd(u, v int) int {
	if u == 0 {
		return v
	}

	return Gcd(v%u, u)
}

package main

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

func readLine() string {
	s, err := f.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

var f = bufio.NewReader(os.Stdin)

func main() {
	var t, n int

	l1 := readInts(1)
	t = l1[0]

	for i := 0; i < t; i++ {
		n = readInts(1)[0]
		arr := readInts(n)
		tt1 := -1
		tt2 := -1
		s1, s2 := false, false
		no := false
		for _, a := range arr {
			if a%10 == 0 || a%10 == 5 {
				s1 = true
				x := a
				for x%10 != 0 {
					x += x % 10
				}
				if tt1 == -1 {
					tt1 = x
				} else if tt1 != x {
					no = true
				}
			} else {
				s2 = true
				x := a
				for x%10 != 2 {
					x += x % 10
				}
				x = x % 20
				if tt2 == -1 {
					tt2 = x
				} else if tt2 != x {
					no = true
				}
			}
		}
		if s1 && s2 {
			fmt.Println("NO")
		} else {
			if no {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		}
	}
}

package main

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

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

	groups := []int {
		-1, 1, 1, 2, 1,
		-1, 2, 2, 1, 2,
		-1, 2, 2, 1, 2,
		-1, 1, 1, 2, 1 }

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

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

		g := make([]int, n)
		for i := range g {
			fmt.Fscan(r, &a)
			g[i] = groups[a % 20]
			if g[i] == -1 { g[i] = a + (a%10) }
		}

		res := "YES"
		for i := 1; i < n; i++ {
			if g[i] != g[i-1] {
				res = "NO"
				break
			}
		}

		fmt.Fprintln(w, res)

	}

}

// https://codeforces.com/problemset/problem/1714/E

// 1714E - Add Modulo 10

// You are given an array of n integers a1,a2,…,an
// You can apply the following operation an arbitrary number of times:
//  • select an index i (1≤i≤n) and replace the value of the element ai with the value ai+(aimod10), where aimod10 is the remainder of the integer dividing ai by 10.
// For a single index (value i), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of ai is taken into account each time. For example, if ai=47 then after the first operation we get ai=47+7=54, and after the second operation we get ai=54+4=58.
// Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.
// For example, you have an array [6,11].
//  • Let's apply this operation to the first element of the array. Let's replace a1=6 with a1+(a1mod10)=6+(6mod10)=6+6=12. We get the array [12,11].
//  • Then apply this operation to the second element of the array. Let's replace a2=11 with a2+(a2mod10)=11+(11mod10)=11+1=12. We get the array [12,12].
// Thus, by applying 2 operations, you can make all elements of an array equal.

// Input
// The first line contains one integer t (1≤t≤104) — the number of test cases. What follows is a description of each test case.
// The first line of each test case contains one integer n (1≤n≤2⋅105) — the size of the array.
// The second line of each test case contains n integers ai (0≤ai≤109) — array elements.
// It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

// Output
// For each test case print:
//  • YES if it is possible to make all array elements equal;
//  • NO otherwise.
// You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer) .

// Example
// input
// 10
// 2
// 6 11
// 3
// 2 18 22
// 5
// 5 10 5 10 5
// 4
// 1 2 4 8
// 2
// 4 5
// 3
// 93 96 102
// 2
// 40 6
// 2
// 50 30
// 2
// 22 44
// 2
// 1 5
// output
// Yes
// No
// Yes
// Yes
// No
// Yes
// No
// No
// Yes
// No

// Note

// The first test case is clarified above.
// In the second test case, it is impossible to make all array elements equal.
// In the third test case, you need to apply this operation once to all elements equal to 5.
// In the fourth test case, you need to apply this operation to all elements until they become equal to 8.
// In the fifth test case, it is impossible to make all array elements equal.
// In the sixth test case, you need to apply this operation to all elements until they become equal to 102.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var c, t, n, i int
	var a []int
	var ans bool

	for c, _ = fmt.Fscan(reader, &t); c == 1; c, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &n)
			a = make([]int, n)

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

				for a[i]%10 != 2 && a[i]%10 != 0 {
					a[i] += a[i] % 10
				}
			}

			ans = true

			if a[0]%10 == 2 {
				for i = 1; i < n && ans; i++ {
					ans = ans && (a[i]%10 == 2 && a[i]%20 == a[i-1]%20)
				}
			} else {
				for i = 1; i < n && ans; i++ {
					ans = ans && (a[i] == a[i-1])
				}
			}

			if ans {
				fmt.Fprintln(writer, "Yes")
			} else {
				fmt.Fprintln(writer, "No")
			}
		}
	}

	writer.Flush()
}

package main

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

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

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

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

func do(n int, a []int) string {
	f := func(x int) (b map[int]bool, c int) {
		b = map[int]bool{}
		c = -1
		if x%10 == 0 {
			b[x] = true
			return
		}
		if x%10 == 5 {
			b[x] = true
			b[x+5] = true
			return
		}
		for x%10 != 2 {
			b[x] = true
			x += x % 10
		}
		c = x / 10
		return
	}
	g := func(b0, b1, b2 map[int]bool, c int) {
		for x := range b0 {
			if b1[x] {
				b2[x] = true
				continue
			}
			if c == -1 {
				continue
			}
			y := x % 10
			z := x / 10
			switch y {
			case 2, 4, 8:
				if z < c || z%2 != c%2 {
					continue
				}
			case 6:
				if z < c+1 || z%2 != (c+1)%2 {
					continue
				}
			default:
				continue
			}
			b2[x] = true
		}
	}
	b0, c0 := f(a[0])
	for i := 1; i < n; i++ {
		b1, c1 := f(a[i])
		b2, c2 := make(map[int]bool), -1
		g(b0, b1, b2, c1)
		g(b1, b0, b2, c0)
		if c0 != -1 && c1 != -1 && c0%2 == c1%2 {
			if c0 > c1 {
				c2 = c0
			} else {
				c2 = c1
			}
		}
		b0, c0 = b2, c2
		if len(b0) == 0 && c0 == -1 {
			return "No"
		}
	}
	return "Yes"
}

package main

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	ht := map[int]int{}
	state := -1
	for i := 0; i < n; i++ {

		fmt.Fscan(in, &a[i])
		x := a[i] % 10
		switch x {
		case 0, 5:
			if v, ok := ht[x]; !ok {
				ht[x] = a[i]
			} else if a[i] != v {
				ht[x] = -1
				state = -2
			}
		case 3:
			a[i] += 9
		case 6, 7, 9:
			a[i] += a[i] % 10
		}
		_, ok0 := ht[0]
		_, ok5 := ht[5]
		if state == -2 {
			continue
		}
		if state == -1 && x != 0 && x != 5 {
			state = a[i] / 10 & 1
		} else if state != -1 && (state != a[i]/10&1) {
			state = -2
		}
		if state != -1 && (ok0 || ok5) {
			state = -2
		}
	}

	if state == -1 {
		x, ok0 := ht[0]
		y, ok5 := ht[5]
		if ok0 && ok5 && y+5 != x {
			state = -2
		} else {
			state = 0
		}
	}
	// fmt.Fprintln(out, state)
	if state >= 0 {
		fmt.Fprintln(out, "YES")
	} else {
		fmt.Fprintln(out, "NO")
	}

}

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

package main

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

const (
	N = 1e5 + 5
)

var a []int
var b = []int{2, 4, 8, 6}
var n int
var ans bool

func check(now int) bool {
	for i := range a {
		if a[i]%10 == 0 {
			return false
		}
		for a[i]%10 != now {
			a[i] += a[i] % 10
		}
	}
	for i := 0; i < n-1; i++ {
		if (a[n-1]-a[i])%20 != 0 {
			return false
		}
	}
	return true
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for o := 0; o < t; o++ {
		fmt.Fscan(in, &n)
		a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i]%10 == 5 {
				a[i] += 5
			}
		}
		sort.Ints(a)
		if a[0] == a[n-1] {
			fmt.Fprintln(out, "Yes")
			continue
		}
		ans = false
		for _, v := range b {
			ans = check(v)
			if ans {
				break
			}
		}
		if ans {
			fmt.Fprintln(out, "Yes")
		} else {
			fmt.Fprintln(out, "No")
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, i, temp, m, last int
	var flag, u, v bool
	add := [10]int{0, 1, 0, 9, 18, 5, 6, 25, 14, 23}
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		fmt.Fscan(in, &n)
		flag, u, v = true, false, false
		for i = 1; i <= n; i++ {
			fmt.Fscan(in, &temp)
			if !flag {
				continue
			}
			m = temp % 10
			if m == 5 || m == 0 {
				u = true
			} else {
				v = true
			}
			if u && v {
				flag = false
				continue
			}
			temp += add[m]
			if i == 1 {
				last = temp
			} else {
				if m == 5 || m == 0 {
					if temp != last {
						flag = false
						continue
					}
				} else {
					if (temp-last)%20 != 0 {
						flag = false
						continue
					}
				}
				last = temp
			}
		}
		if flag {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

func scan(args ...*int) {

	for _, arg := range args {
		scanner.Scan()
		*arg, _ = strconv.Atoi(scanner.Text())
	}
}

func print(args ...interface{}) {
	writter.WriteString(fmt.Sprintln(args...))
}

func main() {
	scanner.Split(bufio.ScanWords)
	defer writter.Flush()
	var t int

	scan(&t)

	var n int

	for i := 0; i < t; i++ {
		scan(&n)
		a := make([]int, n, n)

		var highest *int
		for j := 0; j < n; j++ {
			scan(&a[j])
			if j == 0 || a[j] > *highest {
				highest = &a[j]
			}
		}

		for j := 0; j < n; j++ {
			if a[j]%2 == 1 && a[j] != *highest {
				a[j] = a[j] + (a[j] % 10)
				if a[j] > *highest {
					highest = &a[j]
				}
			}
		}

		failed := false
		for k := 0; k <= 4; k++ {
			failed = false
			for j := 0; j < n; j++ {
				if !canMatch(a[j], *highest) {
					failed = true
					break
				}
			}

			if !failed {
				print("Yes")
				break
			}

			*highest = *highest + (*highest % 10)
		}

		if failed {
			print("No")
		}
	}
}

func canMatch(num, x int) bool {
	if (num%10 == 0 || x%10 == 0) && num != x {
		return false
	}

	diff := (x - num) % 20

	rissen := 0
	for i := 0; i <= 4; i++ {
		if rissen == diff {
			return true
		}

		rissen += num % 10
		num = num + (num % 10)
	}
	return false
}

package main

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

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

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

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

func FindAll(str string, subs []string) (*[]int, *[]int, *[]int) {
	res := make([]int, 0)
	lens := make([]int, 0)
	histstrs := make([]int, 0)
	for hister, sub := range subs {
		for i := 0; i <= len(str)-len(sub); {
			tmp := strings.Index(str[i:], sub)
			//fmt.Println("!!!!!", tmp, str[i:], sub, i)
			if tmp == -1 {
				break
			} else {
				res = append(res, tmp+i)
				lens = append(lens, len(sub))
				histstrs = append(histstrs, hister)
				i += tmp + 1
			}
		}
	}
	return &res, &lens, &histstrs
}

func FilterAll(slc []int, used []bool, lens []int) *[]int {
	ret := make([]int, 0)
	for i := range slc {
		//fmt.Println("!!!!!!!!", slc, used, lens, i)
		curr := slc[i]
		res := 0
		for j := curr; j < curr+lens[i]; j++ {
			if !used[j] {
				res++
			}
		}
		ret = append(ret, res)
	}
	return &ret
}

func HasFalse(b []bool) bool {
	for _, el := range b {
		if !el {
			return true
		}
	}
	return false
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
outer:
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		type1 := 0
		type2 := 0
		slc := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
			if slc[i]%10 == 5 || slc[i]%10 == 0 {
				type2++
			} else {
				type1++
			}
		}
		if type1 > 0 && type2 > 0 {
			fmt.Fprintln(writer, "No")
			continue outer
		}
		if type1 > 0 {
			tmp := slc[0]
			for tmp%10 != 2 {
				tmp += tmp % 10
			}
			mod := tmp % 20
			for i := 1; i < n; i++ {
				tmp := slc[i]
				for tmp%10 != 2 {
					tmp += tmp % 10
				}
				if tmp%20 != mod {
					fmt.Fprintln(writer, "No")
					continue outer
				}
			}
		} else {
			tmp := slc[0]
			for tmp%10 != 0 {
				tmp += tmp % 10
			}
			mod := tmp
			for i := 1; i < n; i++ {
				tmp := slc[i]
				for tmp%10 != 0 {
					tmp += tmp % 10
				}
				if tmp != mod {
					fmt.Fprintln(writer, "No")
					continue outer
				}
			}
		}
		fmt.Fprintln(writer, "Yes")
	}
}

// 00
package main

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

func get_group(k int) int {
	if k%10 == 5 {
		return k + 5
	}
	if k%10 == 0 {
		return k
	}
	if k == 1 || k%20 == 2 || k%20 == 4 || k%20 == 8 || k%20 == 16 {
		return 1
	}
	if k == 3 || k%20 == 6 || k%20 == 12 || k%20 == 14 || k%20 == 18 {
		return 3
	}
	return get_group(k + k%10)
}

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

package main

import (

	"bufio"
	"os"
	"fmt"
)

var (

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

func main() {

	defer out.Flush()

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

		var (
			n int
			b1 = false
			b2 = false
			a []int
		)
		fmt.Fscan(in, &n)
		for j := 0; j < n; j++ {

			var x int
			fmt.Fscan(in, &x)
			k := x % 10
			if k == 0 || k == 5 {

				if k == 5 {

					x += 5
				}
				b1 = true
			} else {

				for {

					k = x % 10
					if k == 2 {

						break
					}
					x += k
				}
				b2 = true
			}
			a = append(a, x)
		}

		if b1 && b2 {

			fmt.Fprintln(out, "No")
		} else {

			var ans = false
			if b1 {

				for j := 1; j < n; j++ {

					if a[j] != a[j - 1] {

						fmt.Fprintln(out, "No")
						ans = true
						break
					}
				}
			}
			if b2 {

				for j := 1; j < n; j++ {

					k := a[j] - a[j - 1]
					if k % 20 != 0 {

						fmt.Fprintln(out, "No")
						ans = true
						break
					}
				}
			}
			if !ans {

				fmt.Fprintln(out, "Yes")
			}
		}
	}
}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	var fives int
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		arr[i] += arr[i] % 10
		if arr[i]%10 == 5 || arr[i]%10 == 0 {
			fives++
		}
	}

	if fives > 0 && fives != n {
		return "NO"
	}

	sort.Ints(arr)
	min := arr[0]
	max := arr[n-1]
	if min == max {
		return "YES"
	}

	if fives == n {
		if max-min == 5 && min%10 == 5 {
			return "YES"
		} else {
			return "NO"
		}
	}

	//dedup
	d := make([]int, 0, n)
	for i := 0; i < n-1; i++ {
		if arr[i] != arr[i+1] {
			d = append(d, arr[i])
		}
	}
	d = append(d, arr[n-1])

	// AHAHHA THAT ACTUALLY WORKED LMAO
	for i := 0; i < n-1; i++ {

		diff := 10
		for diff < max-arr[i] {
			diff *= 10
		}
		arr[i] += diff

		for arr[i]-max < 10 {
			arr[i] += arr[i] % 10
		}
		if abs(max-arr[i]) == 10 {
			return "NO"
		}
	}

	return "YES"
}

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	var fives int
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		if arr[i]%10 == 5 || arr[i]%10 == 0 {
			fives++
		}
	}

	if fives > 0 && fives != n {
		return "NO"
	}

	sort.Ints(arr)
	min := arr[0]
	max := arr[n-1]
	if min == max {
		return "YES"
	}

	if fives == n {
		if max-min == 5 && min%10 == 5 {
			return "YES"
		} else {
			return "NO"
		}
	}

	for i := 0; i < n-1; i++ {
		for arr[i]-max < 10 {
			arr[i] += arr[i] % 10
		}
		if abs(max-arr[i]) == 10 {
			return "NO"
		}
	}

	return "YES"
}

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	var fives int
	var equals = true
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		if arr[i]%5 == 0 || arr[i]%10 == 0 {
			fives++
		}
		if i > 0 && arr[i-1] != arr[i] {
			equals = false
		}
	}

	if equals {
		return "YES"
	}

	if fives > 0 && fives != n {
		return "NO"
	}

	sort.Ints(arr)
	max := arr[n-1]

	if fives == n {
		if max-arr[0] == 5 {
			return "YES"
		} else {
			return "NO"
		}
	}

	for i := 0; i < n-1; i++ {
		for arr[i]-max < 10 {
			arr[i] += arr[i] % 10
		}
		if abs(max-arr[i]) == 10 {
			return "NO"
		}
	}

	return "YES"
}

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	var fives int
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		if arr[i]%5 == 0 || arr[i]%10 == 0 {
			fives++
		}
	}

	if fives > 0 && fives != n {
		return "NO"
	}

	sort.Ints(arr)
	max := arr[n-1]

	if fives == n {
		if max-arr[0] == 5 {
			return "YES"
		} else {
			return "NO"
		}
	}

	for i := 0; i < n-1; i++ {
		for arr[i]-max < 10 {
			arr[i] += arr[i] % 10
		}
		if abs(max-arr[i]) == 10 {
			return "NO"
		}
	}

	return "YES"
}

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

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

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

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			aa[i] %= 100
			if aa[i]%10 == 5 {
				aa[i] += 5
			} else if aa[i]%10 != 0 {
				for aa[i]%10 != 2 {
					aa[i] += aa[i] % 10
				}
				aa[i] %= 20
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			aa[i] %= 100
			if aa[i]%10 == 5 {
				aa[i] += 5
			} else if aa[i]%10 != 0 {
				for aa[i]+(aa[i]%10) < 120 {
					aa[i] += aa[i] % 10
				}
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			aa[i] %= 100
			if aa[i]%10 == 5 {
				aa[i] += 5
			} else if aa[i]%10 != 0 {
				for aa[i]+(aa[i]%10) < 110 {
					aa[i] += aa[i] % 10
				}
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			aa[i] %= 100
			if aa[i]%10 == 5 {
				aa[i] *= 2
			} else if aa[i]%10 != 0 {
				for aa[i]+(aa[i]%10) < 110 {
					aa[i] += aa[i] % 10
				}
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			if aa[i]%10 == 5 {
				aa[i] *= 2
			} else if aa[i]%10 != 0 {
				if aa[i]%2 != 0 {
					aa[i] += aa[i] % 10
				}
				aa[i] %= 20
				for aa[i]+(aa[i]%10) < 100 {
					aa[i] += aa[i] % 10
				}
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			if aa[i]%10 == 5 {
				aa[i] *= 2
			} else if aa[i]%10 != 0 {
				if aa[i]%2 != 0 {
					aa[i] += aa[i] % 10
				}
				aa[i] %= 100
				for aa[i]+(aa[i]%10) < 100 {
					aa[i] += aa[i] % 10
				}
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			aa[i] %= 100
			if aa[i]%10 == 5 {
				aa[i] *= 2
			} else if aa[i]%10 != 0 {
				for aa[i]+(aa[i]%10) < 100 {
					aa[i] += aa[i] % 10
				}
			}
		}
		sort.Slice(aa, func(i, j int) bool { return aa[i] >= aa[j] })

		f := true
		for i := 1; i < n; i++ {
			if aa[i-1] != aa[i] {
				f = false
			}
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

var (
	in   *bufio.Scanner
	out  *bufio.Writer
	once = false
)

func solve() {
	in.Scan()
	n, _ := strconv.Atoi(in.Text())
	a := make(map[int]int)
	for i := 0; i < n; i++ {
		in.Scan()
		ai, _ := strconv.Atoi(in.Text())
		if ai%10 == 5 {
			ai = ai + 5
			a[ai]++
		} else {
			if ai%2 != 0 {
				ai = ai + ai%10
			}
			ai = ai % 20
			switch ai {
			case 2, 4, 8, 16:
				a[-1]++
			default:
				a[-2]++
			}
		}
	}
	if len(a) > 1 {
		out.WriteString("No\n")
	} else {
		out.WriteString("Yes\n")
	}
}

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

	if once {
		solve()
		return
	}
	in.Scan()
	x, _ := strconv.Atoi(in.Text())
	for t := 0; t < x; t++ {
		solve()
	}
}

package main

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

var (
	in   *bufio.Scanner
	out  *bufio.Writer
	once = false
)

func solve() {
	in.Scan()
	n, _ := strconv.Atoi(in.Text())
	a := make(map[int]int)
	for i := 0; i < n; i++ {
		in.Scan()
		ai, _ := strconv.Atoi(in.Text())
		if ai%10 == 5 {
			ai = ai + 5
			a[ai]++
		} else {
			if ai%2 != 0 {
				ai = ai + ai%10
			}
			ai = ai % 20
			switch ai {
			case 2, 4, 8, 16:
				a[-1]++
			default:
				a[-2]++
			}
		}
	}
	if len(a) > 1 {
		out.WriteString("No")
	} else {
		out.WriteString("Yes")
	}
}

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

	if once {
		solve()
		return
	}
	in.Scan()
	x, _ := strconv.Atoi(in.Text())
	for t := 0; t < x; t++ {
		solve()
	}
}

package main

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

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

func ReadInt() int{
	var i int
	fmt.Fscan(in, &i)
	return i
}

// 1 -> 2 -> 4 -> 8 -> 6 -> 2 -> ....
// 0 -> 0
// 3 -> 6 -> 2
// 5 -> 0
// 7 -> 4
// 9 -> 8

func main(){
	defer out.Flush()

	t := ReadInt()

	for ; t > 0; t--{
		n := ReadInt()
		a := make([]int, n)
		hasFiveDivisor := false
		for i:=0;i<n;i++{
			a[i]=ReadInt()
			if a[i]%5==0{
				hasFiveDivisor = true
			}
		}
		can:=true
		if hasFiveDivisor {
			for i:=0;i<n;i++{
				a[i] = a[i]+a[i]%10
				if a[i] != a[0]{
					can = false
					break
				}
			}
		} else{
			for i :=0; i < n; i++ {
				for a[i]%10!=2{
					a[i] += a[i]%10
				}
				fmt.Println(a[0], " ", a[i])
				if a[i]%20 !=a[0]%20{
					can = false
					break
				}
			}
		}
		if can{
			fmt.Fprintln(out, "Yes")
		} else{
			fmt.Fprintln(out, "No")
		}
	}
}

func Gcd(u, v int) int {
	if u == 0 {
		return v
	}

	return Gcd(v%u, u)
}

package main

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

func readLine() string {
	s, err := f.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

var f = bufio.NewReader(os.Stdin)

func main() {
	var t, n int

	l1 := readInts(1)
	t = l1[0]

	for i := 0; i < t; i++ {
		n = readInts(1)[0]
		arr := readInts(n)
		tt := -1
		s1, s2 := false, false
		s3, s4 := false, false
		no := false
		for _, a := range arr {
			if a%10 == 0 || a%10 == 5 {
				if tt == -1 {
					tt = a + a%10
				} else {
					if tt != a+a%10 {
						no = true
					}
				}
				s1 = true
			} else {
				s2 = true
				if a%10 == 3 || a%10 == 6 {
					if (a/10)%2 == 0 {
						s3 = true
					} else {
						s4 = true
					}
				} else {
					if (a/10)%2 == 1 {
						s3 = true
					} else {
						s4 = true
					}
				}
			}
		}
		if s1 && s2 {
			fmt.Println("NO")
		} else if s1 {
			if no {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		} else {
			if s3 && s4 {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		}
	}
}


package main

import (
	"fmt"
	"sort"
)

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

	for ; q > 0; q-- {
		var n, k int
		fmt.Scan(&n, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&a[i])
		}

		sort.Ints(a)

		if a[len(a)-1]-k > a[0]+k {
			fmt.Println(-1)
		} else {
			fmt.Println(a[0] + k)
		}
	}
}

 package main

 import (
     "fmt"
     "math"
 )
 func main() {
     var nquery, n, k int64
     fmt.Scanf("%d\n", &nquery)
     for nquery > 0 {
         fmt.Scanf("%d %d\n", &n, &k)
         var min int64 = math.MaxInt64
         var max int64 = math.MinInt64
         var ai int64 = 0
         for n > 0 {
             if n > 1 {
                 fmt.Scanf("%d", &ai)
             } else {
                 fmt.Scanf("%d\n", &ai)
             }
             min = Min(ai, min)
             max = Max(ai, max)
             n--
         }
         if (max - min > k*2) {
             fmt.Println(-1)
         } else {
             fmt.Println(min + k)
         }

         nquery--
     }
 }

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

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

package main

import "fmt"

func abs(a int64,b int64) int64 {
	if a >= b {
		return a-b
	}
	return b-a
}

func main() {
	var t int
	fmt.Scan(&t)
	for t>0 {
		var n,i int
		var k int64
		fmt.Scan(&n)
		fmt.Scan(&k)
		var a = make([]int64,n)
		var ans int64 = 9223372036854775807
		for i=0;i<n;i++ {
			fmt.Scan(&a[i])
			if ans > a[i]+k {
				ans = a[i]+k
			}
		}
		isExist := true
		for i=0;i<n;i++ {
			if abs(a[i],ans) > k {
				isExist = false
				break
			}
		}
		if isExist {
			fmt.Println(ans)
		} else {
			fmt.Println(-1)
		}
		t--
	}
}

package main

import "fmt"

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

	for i := 0; i < requests; i++ {
		var pieces int
		var k int
		min := 0
		max := 0
		fmt.Scan(&pieces)
		fmt.Scan(&k)
		var prices = make([]int, pieces)
		for j := 0; j < pieces; j++ {
			fmt.Scan(&prices[j])
			if j == 0 {
				min = prices[j]
				max = prices[j]
			}
			if prices[j] < min {
				min = prices[j]
			}
			if prices[j] > max {
				max = prices[j]
			}
		}
		if max > min + 2 * k {
			fmt.Println(-1)
		} else {
			fmt.Println(min + k)
		}

	}
}

package main

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

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

	var t, n, k, v int
	for Fscan(in, &t); t > 0; t-- {
		mi, mx := int(1e9), 0
		for Fscan(in, &n, &k); n > 0; n-- {
			Fscan(in, &v)
			if v < mi {
				mi = v
			}
			if v > mx {
				mx = v
			}
		}
		if mi+2*k >= mx {
			Fprintln(out, mi+k)
		} else {
			Fprintln(out, -1)
		}
	}
}

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

package main

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

const (
   MX_INT int64 = math.MaxInt64
   MN_INT int64 = math.MinInt64
)

func main() {
   for t := ScanInt64(); t > 0; t-- {
    n, k := ScanInt64(), ScanInt64()
    a := ScanInt64Slice(n)

    min := Mn(a...)
    max := Mx(a...)

    if max - min > 2*k {
      O <- S(-1)
    } else {
      O <- S(min + k)
    }

   }

   close(O)
   oWg.Wait()
}

func isValid(a []int64, k, B int64) bool {
  for i := 0; i < len(a); i++ {
    if abs(a[i] - k) > B {
      return false
    }
  }
  return true
}

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

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var Pl func(a ...interface{}) (n int, err error)

func init() {
   Pl = fmt.Println

   in = bufio.NewScanner(os.Stdin)
   in.Buffer(make([]byte, 1024), int(1e8))
   in.Split(bufio.ScanWords)

   O = make(chan string, 16)
   oWg = &sync.WaitGroup{}
   oWg.Add(1)

   writer := bufio.NewWriterSize(os.Stdout, int(1e5))
   go func(writer *bufio.Writer) {
      defer oWg.Done()
      defer writer.Flush()

      for line := range O {
         writer.WriteString(line + "\n")
      }
   }(writer)
}

func ScanWord() string {
   in.Scan()
   return in.Text()
}

func ScanStringSlice(n int64) []string {
   length := int(n)
   s := make([]string, n)
   for i := 0; i < length; i++ {
      s[i] = ScanWord()
   }
   return s
}

func ScanInt64() int64 {
   intStr := ScanWord()
   i, err := strconv.ParseInt(intStr, 10, 64)
   if err != nil {
      panic(err)
   }
   return i
}

func ScanInt() int {
   intStr := ScanWord()
   i, err := strconv.Atoi(intStr)
   if err != nil {
      panic(err)
   }
   return i
}

func ScanInt64Slice(n int64) []int64 {
   length := int(n)
   arr := make([]int64, n)
   for i := 0; i < length; i++ {
      arr[i] = ScanInt64()
   }
   return arr
}

/*********************** Utils ***********************/

func Mx(args ...int64) int64 {
   maxVal := MN_INT
   for _, v := range args {
      if v > maxVal {
         maxVal = v
      }
   }
   return maxVal
}

func Mn(args ...int64) int64 {
   minVal := MX_INT
   for _, v := range args {
      if v < minVal {
         minVal = v
      }
   }
   return minVal
}

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

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

func S(v interface{}) string {
   return fmt.Sprintf("%v", v)
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int64;
	var inf int64 = 1e8;
	var T int64
	var n,k int64
	var a [10000] int64
	var i1,i int64
	fmt.Scan(&T)
	for i1=1;i1<=T;i1++ {
		//if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scan(&n, &k);
		for i = 1; i <= n; i++ {
			fmt.Scan(&a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Println("-1");
			continue;
		}
		fmt.Println(mi+k);
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	// var a string
	var n, q, res, k, x int
	// var x float64
	// var s string
	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanln(&q)

	for i := 0; i < q; i++ {
		fmt.Scanln(&n, &k)

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

		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a[i])
			m[a[i]]++
		}
		fmt.Scanln(&x)

		sort.Ints(a)
		if (a[len(a)-1] - a[0]) > 2*k {
			res = -1
		} else {
			res = k + a[0]
		}
		fmt.Println(res)
		res = 0
	}

}

package main
import "fmt"
import "math"

func main() {
	var n, total, k int
	maxGo := int(math.Pow(10,8));
	fmt.Scanln(&n)

	for x:=0; x <n ; x++{
	    fmt.Scan(&total)
	    fmt.Scan(&k)
	    a := make([]int, total)
	    min, max :=maxGo,0
    	for i := range a {
    		fmt.Scan(&a[i])
    		if a[i]< min{
    		    min = a[i]
    		}

    		if (a[i]>max){
    		    max = a[i]
    		}
    	}

    	newPrice := min + k
    	calculate := max-newPrice
    	if (newPrice <= 0 ){
    	 fmt.Println(-1)
    	}else if (calculate<=k){
    	    fmt.Println(newPrice)
    	}else{
    	    fmt.Println(-1)
    	}
	}

    return
}

package main

import "fmt"

func main() {
	var q int
	fmt.Scan(&q)
	for ; q > 0; q-- {
		var n, k, a, min, max int64
		fmt.Scan(&n, &k)
		for ; n > 0; n-- {
			fmt.Scan(&a)
			if min == 0 || min > a {
				min = a
			}
			if max < a {
				max = a
			}
		}
		if max-min > 2*k {
			fmt.Println(-1)
		} else {
			fmt.Println(min + k)
		}
	}
}

package main

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

const (
   MX_INT int64 = math.MaxInt64
   MN_INT int64 = math.MinInt64
)

func main() {
   for t := ScanInt64(); t > 0; t-- {
    n, k := ScanInt64(), ScanInt64()
    a := ScanInt64Slice(n)
    for i := 0; i < len(a); i++ {
      a[i] += k
    }
    sort.Slice(a, func(i, j int) bool { return a[i] < a[j]})

    if a[len(a)-1] - a[0] > 2*k {
      O <- S(-1)
    } else {
      O <- S(a[0])
    }

   }

   close(O)
   oWg.Wait()
}

func isValid(a []int64, k, B int64) bool {
  for i := 0; i < len(a); i++ {
    if abs(a[i] - k) > B {
      return false
    }
  }
  return true
}

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

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var Pl func(a ...interface{}) (n int, err error)

func init() {
   Pl = fmt.Println

   in = bufio.NewScanner(os.Stdin)
   in.Buffer(make([]byte, 1024), int(1e8))
   in.Split(bufio.ScanWords)

   O = make(chan string, 16)
   oWg = &sync.WaitGroup{}
   oWg.Add(1)

   writer := bufio.NewWriterSize(os.Stdout, int(1e5))
   go func(writer *bufio.Writer) {
      defer oWg.Done()
      defer writer.Flush()

      for line := range O {
         writer.WriteString(line + "\n")
      }
   }(writer)
}

func ScanWord() string {
   in.Scan()
   return in.Text()
}

func ScanStringSlice(n int64) []string {
   length := int(n)
   s := make([]string, n)
   for i := 0; i < length; i++ {
      s[i] = ScanWord()
   }
   return s
}

func ScanInt64() int64 {
   intStr := ScanWord()
   i, err := strconv.ParseInt(intStr, 10, 64)
   if err != nil {
      panic(err)
   }
   return i
}

func ScanInt() int {
   intStr := ScanWord()
   i, err := strconv.Atoi(intStr)
   if err != nil {
      panic(err)
   }
   return i
}

func ScanInt64Slice(n int64) []int64 {
   length := int(n)
   arr := make([]int64, n)
   for i := 0; i < length; i++ {
      arr[i] = ScanInt64()
   }
   return arr
}

/*********************** Utils ***********************/

func Mx(args ...int64) int64 {
   maxVal := MN_INT
   for _, v := range args {
      if v > maxVal {
         maxVal = v
      }
   }
   return maxVal
}

func Mn(args ...int64) int64 {
   minVal := MX_INT
   for _, v := range args {
      if v < minVal {
         minVal = v
      }
   }
   return minVal
}

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

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

func S(v interface{}) string {
   return fmt.Sprintf("%v", v)
}

package main

import (
	"fmt"
	"math"
)

var a [110] int64
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scan(&q)
	for k:=1;k<=q;k++{
		var n int
		var k int64
		fmt.Scan(&n,&k)

		var min,max int64
		min=math.MaxInt64
		max=math.MinInt64

		for i:=1;i<=n;i++{
			fmt.Scan(&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}

		if(max-min>k*2){
			fmt.Println("-1");
		}else {
			fmt.Println(min+k)
		}
	}

}

package main

import (
	"fmt"
	"math"
)

func main() {
	var q, n, k, i, ctr int64
	fmt.Scan(&q)
	for q > 0 {
		fmt.Scan(&n, &k)
		a := make([]int64, n)
		for i = 0; i < n; i++ {
			fmt.Scan(&a[i])
		}
		min := findMin(a, n)
		x := min + k
		for i = 0; i < n; i++ {
			if math.Abs(float64(a[i]-x)) > float64(k) {
				ctr = 1
			}
		}
		if ctr == 1 {
			fmt.Println(-1)
		} else {
			fmt.Println(min + k)
		}
		ctr = 0
		q = q - 1
	}
}

func findMin(a []int64, n int64) int64 {
	m := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < m {
			m = a[i]
		}
	}
	return m
}

package main
import "fmt"
import "math"

func main() {
	var n, total, k int
	maxGo := int(math.Pow(10,8)-1);
	fmt.Scanln(&n)

	for x:=0; x <n ; x++{
	    fmt.Scan(&total)
	    fmt.Scan(&k)
	    a := make([]int, total)
	    min, max :=maxGo,0
    	for i := range a {
    		fmt.Scan(&a[i])
    		if a[i]< min{
    		    min = a[i]
    		}

    		if (a[i]>max){
    		    max = a[i]
    		}
    	}

    	newPrice := min + k
    	calculate := float64(max-newPrice)
    	if (newPrice <= 0 ){
    	 fmt.Println(-1)
    	}else if (int(math.Abs(calculate))<=k){
    	    fmt.Println(newPrice)
    	}else{
    	    fmt.Println(-1)
    	}
	}

    return
}

package main
import "fmt"
import "math"

func main() {
	var n, total, k int
	maxGo := int(math.Pow(10,8)-1);
	fmt.Scanln(&n)

	for x:=0; x <n ; x++{
	    fmt.Scan(&total)
	    fmt.Scan(&k)
	    a := make([]int, total)
	    min, max :=maxGo,0
    	for i := range a {
    		fmt.Scan(&a[i])
    		if a[i]< min{
    		    min = a[i]
    		}

    		if (a[i]>max){
    		    max = a[i]
    		}
    	}

    	newPrice := min + k
    	calculate := float64(max-newPrice)
    	if (int(math.Abs(calculate))<=k){
    	    fmt.Println(newPrice)
    	}else{
    	    fmt.Println(-1)
    	}
	}

    return
}

package main

import (
	"fmt"
	"math"
)

var a [110]int
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n,k int
		fmt.Scanf("%d %d",&n,&k)

		var min,max int
		min=math.MaxInt16

		for i:=1;i<=n;i++{
			fmt.Scanf("%d",&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}
		var ans2,ans3 int

		ans2=min+k;

		if max-k>0{
			ans3=max-k
		} else{
			ans3=1
		}

		if ans2>=ans3{
			fmt.Printf("%d\n",min+k)
		} else {
			fmt.Println("-1")
		}
	}

}

package main

import (
	"fmt"
	"math"
)

var a [110]int
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n,k int
		fmt.Scanf("%d %d",&n,&k)

		var min,max int
		min=math.MaxInt16

		for i:=1;i<=n;i++{
			fmt.Scanf("%d",&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}
		var ans2,ans3 int

		ans2=min+k;

		ans3=max+k

		if ans2>=ans3{
			fmt.Printf("%d\n",min+k)
		} else {
			fmt.Println("-1")
		}
	}

}

package main

import (
	"fmt"
)

var a [110] int32
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n,k int32
		fmt.Scanf("%lld %lld",&n,&k)

		var min,max int32
		min=a[0]
		max=a[0]
		var i int32

		for i=1;i<=n;i++{
			fmt.Scanf("%lld",&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}
		var ans2,ans3 int32

		ans2=min+k;

		ans3=max-k

		if ans2>=ans3{
			fmt.Printf("%d\n",min+k)
		} else {
			fmt.Println("-1")
		}
	}

}

package main

import (
	"fmt"
	"math"
)

var a [110] int64
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n int
		var k int64
		fmt.Scanf("%d %d",&n,&k)

		var min,max int64
		min=math.MaxInt64
		max=math.MinInt64

		for i:=1;i<=n;i++{
			fmt.Scanf("%d",&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}

		if(max-min>k*2){
			fmt.Println("-1");
		}else {
			fmt.Println(min+k)
		}
	}

}

package main

import (
	"fmt"
	"math"
)

var a [110]int
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n,k int
		fmt.Scanf("%d %d",&n,&k)

		var min,max int
		min=math.MaxInt16

		for i:=1;i<=n;i++{
			fmt.Scanf("%d",&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}
		var ans2,ans3 int

		ans2=min+k;

		ans3=max-k

		if ans2>=ans3{
			fmt.Printf("%d\n",min+k)
		} else {
			fmt.Println("-1")
		}
	}

}

package main

import (
	"fmt"
	"math"
)

var a [110] int64
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n int
		var k int64
		fmt.Scanf("%d %d",&n,&k)

		var min,max int64
		min=math.MaxInt64
		max=math.MinInt64

		for i:=1;i<=n;i++{
			fmt.Scan(&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}

		if(max-min>k*2){
			fmt.Println("-1");
		}else {
			fmt.Println(min+k)
		}
	}

}

package main

import (
	"fmt"
)

var a [110]int
func abs(n int)int{
	if(n<=0){
		return -n;
	}
	return n;
}
func main() {
	var q int

	fmt.Scanln(&q)
	for k:=1;k<=q;k++{
		var n,k int
		fmt.Scanf("%d %d",&n,&k)

		var min,max int
		min=a[0]
		max=a[0]

		for i:=1;i<=n;i++{
			fmt.Scanf("%d",&a[i])
			if a[i]<min{
				min=a[i]
			}
			if a[i]>max{
				max=a[i]
			}

		}
		var ans2,ans3 int

		ans2=min+k;

		ans3=max-k

		if ans2>=ans3{
			fmt.Printf("%d\n",min+k)
		} else {
			fmt.Println("-1")
		}
	}

}

 package main

 import (
     "fmt"
     "math"
 )
 func main() {
     var nquery, n, k int64
     fmt.Scanf("%d", &nquery)
     for nquery > 0 {
         fmt.Scanf("%d %d\n", &n, &k)
         var min int64 = math.MaxInt64
         var max int64 = math.MinInt64
         var ai int64 = 0
         for n > 0 {
             if n > 1 {
                 fmt.Scanf("%d", &ai)
             } else {
                 fmt.Scanf("%d\n", &ai)
             }
             min = Min(ai, min)
             max = Max(ai, max)
             n--
         }
         if (max - min > k*2) {
             fmt.Println(-1)
         } else {
             fmt.Println(min + k)
         }

         nquery--
     }
 }

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

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

 package main

 import (
     "fmt"
     "math"
 )
 func main() {
     var nquery, n, k int64
     fmt.Scanf("%d", &nquery)
     for nquery > 0 {
         fmt.Scanf("%d %d\n", &n, &k)
         var min int64 = math.MaxInt64
         var max int64 = math.MinInt64
         var ai int64 = 0
         for n > 0 {
             fmt.Scanf("%d", &ai)
             min = Min(ai, min)
             max = Max(ai, max)
             n--
         }
         if (max - min > k*2) {
             fmt.Println(-1)
         } else {
             fmt.Println(min + k)
         }

         nquery--
     }
 }

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

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

 package main

 import (
     "fmt"
     "math"
 )
 func main() {
     var nquery, n, k int64
     fmt.Scanf("%d", &nquery)
     for nquery > 0 {
         fmt.Scanf("%d %d", &n, &k)
         var min int64 = math.MaxInt64
         var max int64 = math.MinInt64
         var ai int64 = 0
         for n > 0 {
             fmt.Scanf("%d", &ai)
             min = Min(ai, min)
             max = Max(ai, max)
             n--
         }
         if (max - min > k*2) {
             fmt.Println(-1)
         } else {
             fmt.Println(min + k)
         }

         nquery--
     }
 }

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

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

package main

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

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

	var t, n, k, v int
	for Fscan(in, &t); t > 0; t-- {
		mi, mx := int(1e9), 0
		for Fscan(in, &n, &k); n > 0; n-- {
			Fscan(in, &v)
			if v < mi {
				mi = v
			} else if v > mx {
				mx = v
			}
		}
		if mi+2*k >= mx {
			Fprintln(out, mi+k)
		} else {
			Fprintln(out, -1)
		}
	}
}

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

package main

import "fmt"

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

func main() {
	var t int
	fmt.Scan(&t)
	for t>0 {
		var n,k,i int
		fmt.Scan(&n)
		fmt.Scan(&k)
		var a = make([]int,n)
		var ans = 100000000
		for i=0;i<n;i++ {
			fmt.Scan(&a[i])
			if ans > a[i]+k {
				ans = a[i]+k
			}
		}
		isExist := true
		for i=0;i<n;i++ {
			if abs(a[i],ans) > k {
				isExist = false
				break
			}
		}
		if isExist {
			fmt.Println(ans)
		} else {
			fmt.Println(-1)
		}
		t--
	}
}

package main

import "fmt"

func abs(a int64,b int64) int64 {
	if a >= b {
		return a-b
	}
	return b-a
}

func main() {
	var t int
	fmt.Scan(&t)
	for t>0 {
		var n,i int
		var k int64
		fmt.Scan(&n)
		fmt.Scan(&k)
		var a = make([]int64,n)
		var ans int64 = 100000000
		for i=0;i<n;i++ {
			fmt.Scan(&a[i])
			if ans > a[i]+k {
				ans = a[i]+k
			}
		}
		isExist := true
		for i=0;i<n;i++ {
			if abs(a[i],ans) > k {
				isExist = false
				break
			}
		}
		if isExist {
			fmt.Println(ans)
		} else {
			fmt.Println(-1)
		}
		t--
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 3e8;
	var T int
	var n,k int
	var a [10000] int
	fmt.Scanf("%d",&T)
	for i1:=1;i1<=T;i1++ {
		if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i := 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Printf("-1");
			continue;
		}
		fmt.Printf("%d", mi+k);
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 3e8;
	var T int
	var n,k int
	var a [1000000] int
	fmt.Scanf("%d",&T)
	for i1:=1;i1<=T;i1++ {
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i := 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Printf("-1\n");
			continue;
		}
		fmt.Printf("%d\n", mi+k);
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 1e8;
	var T int
	var n,k int
	var a [10000] int
	var i1,i int
	fmt.Scanf("%d",&T)
	for i1=1;i1<=T;i1++ {
		//if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i = 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Println("-1");
			continue;
		}
		fmt.Println(mi+k);
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 1e8;
	var T int
	var n,k int
	var a [10000] int
	var i1,i int
	fmt.Scanf("%d",&T)
	fmt.Printf("%d\n",T)
	for i1=1;i1<=T;i1++ {
		//if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k)
		fmt.Printf("%d %d\n",n,k)
		for i = 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i])
			fmt.Printf("%d\n",a[i])
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Println("-1")
			continue;
		}
		fmt.Println(mi+k)
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int64;
	var inf int64 = 1e8;
	var T int64
	var n,k int64
	var a [10000] int64
	var i1,i int64
	fmt.Scanf("%d",&T)
	for i1=1;i1<=T;i1++ {
		//if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i = 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Println("-1");
			continue;
		}
		fmt.Println(mi+k);
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 1e8;
	var T int
	var n,k int
	var a [10000] int
	fmt.Scanf("%d",&T)
	for i1:=1;i1<=T;i1++ {
		if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i := 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Printf("-1");
			continue;
		}
		fmt.Printf("%d", mi+k);
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 1e8;
	var T int
	var n,k int
	var a [10000] int
	fmt.Scanf("%d",&T)
	for i1:=1;i1<=T;i1++ {
		//if(i1!=1){fmt.Printf("\n")}
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i := 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Println("-1");
			continue;
		}
		fmt.Println(mi+k);
	}
}

package main

import(
	"fmt"
)

func main(){
	var mi,ma int;
	var inf int = 3e8;
	var T int
	var n,k int
	var a [10000] int
	fmt.Scanf("%d",&T)
	for i1:=1;i1<=T;i1++ {
		mi = inf
		ma = 0
		fmt.Scanf("%d%d", &n, &k);
		for i := 1; i <= n; i++ {
			fmt.Scanf("%d", &a[i]);
			if (a[i] < mi) {
				mi = a[i]
			}
			if (a[i] > ma) {
				ma = a[i]
			}
		}
		if (ma-mi > 2*k) {
			fmt.Printf("-1\n");
			continue;
		}
		fmt.Printf("%d\n", mi+k);
	}
}


package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1262C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()
	reverse := func(s []byte) {
		for i, j := 0, len(s)-1; i < j; {
			s[i], s[j] = s[j], s[i]
			i++
			j--
		}
	}
	type pair struct{ l, r int }

	var t, n, k int
	var s []byte
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &k, &s)
		ops := []pair{}
		targetStr := strings.Repeat("()", k-1) + strings.Repeat("(", n/2-k+1) + strings.Repeat(")", n/2-k+1)
		for i, c := range s {
			if c != targetStr[i] {
				j := i + 1
				for ; s[j] != targetStr[i]; j++ {
				}
				reverse(s[i : j+1])
				ops = append(ops, pair{i + 1, j + 1})
			}
		}
		Fprintln(out, len(ops))
		for _, op := range ops {
			Fprintln(out, op.l, op.r)
		}
	}
}

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

package main

import (
	"fmt"
	"runtime"
)

func PrintlnRuneSlice(s []rune){
	for _, v := range s {
		fmt.Printf("%s", string(v))
	}
	fmt.Println()
}
type
	LR struct {
		L int16
		R int16
	}

func solve() {
	var n, k int16
	var s  []byte
	fmt.Scan(&n,&k,&s)

//	PrintlnRuneSlice([]rune(string(s)))

	cnt := int16(0)
	res := make([]LR,0)
	i := int16(0)
	for cnt < k-1 && i < n {
		if i%2 == 0 {
			if string(s[i]) == "(" {
			} else {
				l := i
				r := i
				for ; r < n && string(s[r]) == ")"; r++ {
				}
				if r>=n {
					break
				}
				res = append(res, LR{l, r})
				for u := int16(0); u <= (r-l)/2; u++ {
					s[i+u], s[r-u] = s[r-u], s[i+u]
				}
			}
		} else {
			if string(s[i]) == ")" {
			} else {
				l := i
				r := i
				for ; r < n && string(s[r]) == "("; r++ {
				}
				if r>=n {
					break
				}
				res = append(res, LR{l, r})
				for u := int16(0); u <= (r-l)/2; u++ {
					s[i+u], s[r-u] = s[r-u], s[i+u]
				}
			}
			cnt++
		}
		i++
	}

	end := i+(n - i) / 2
	for i <= end &&i<n-1 {
		if string(s[i]) == ")" {
			l := i
			r := i + 1
			for ; r < n && string(s[r]) == ")"; r++ {
			}
			if r>=n {
				break
			}
			res = append(res, LR{l, r})
			for u := int16(0); u <= (r-l)/2; u++ {
				s[l+u], s[r-u] = s[r-u], s[l+u]
			}
		}
		i++
	}

//	PrintlnRuneSlice([]rune(string(s)))
	fmt.Printf("%d\n", len(res))
	for i := range res {
		fmt.Printf("%d %d\n", res[i].L+1, res[i].R+1)
	}

	s=nil
	res=nil
}

func main() {
	//t := ReadInt()
	t :=1
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
		runtime.GC()
	}
}

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])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var t int
var n, k int
var S []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, k = ReadInt2()
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	answers := [][]int{}

	for i := 0; i < 2*(k-1); i++ {
		if i%2 == 0 && S[i] == ')' {
			answers = append(answers, sub(i, '('))
		} else if i%2 == 1 && S[i] == '(' {
			answers = append(answers, sub(i, ')'))
		}
	}

	tmp := (n - 2*(k-1)) / 2
	for i := 2 * (k - 1); i < 2*(k-1)+tmp; i++ {
		if S[i] == ')' {
			answers = append(answers, sub(i, '('))
		}
	}

	fmt.Println(len(answers))
	for i := 0; i < len(answers); i++ {
		fmt.Println(answers[i][0]+1, answers[i][1]+1)
	}

	// fmt.Println(string(S))
}

// sからスタートしてrを発見したところで反転する
// さらに始点と終点をスライスで返す
func sub(s int, r rune) []int {
	t := s + 1
	for i := s + 1; i < n; i++ {
		if S[i] == r {
			t = i
			break
		}
	}
	res := []int{s, t}

	// [s, t]を反転
	rev := Reverse(S[s : t+1])
	for i := 0; i < len(rev); i++ {
		S[s+i] = rev[i]
	}

	return res
}

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

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

	return res
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

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

package main

import (
	"fmt"
)

type erv struct {
	a int
	b int
}

func next(s []byte, pos int, c byte) int {
	for pos--; pos >= 0; pos-- {
		if s[pos] == c {
			return pos
		}
	}
	return -1
}

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

	fmt.Scan(&t)

	var s []byte
	for i := 0; i < t; i++ {
		fmt.Scan(&n, &k, &s)

		open, close := len(s), len(s)

		ervs := make([]erv, 0)
		for j := 0; j < k - 1; j++ {
			close = next(s, close, ')')
			//fmt.Println("close", close, j, len(s)-1)
			if close != len(s) - 1 - 2 * j {
				ervs = append(ervs, erv{a: close, b: len(s)-1-2*j})
				s[close], s[len(s)-1-2*j] = s[len(s)-1-2*j], s[close]
			}

			open = next(s, open, '(')
			//fmt.Println("open", open, j, len(s)-1)
			if open != len(s) - 2 - 2 * j {
				ervs = append(ervs, erv{a: open, b: len(s)-2-2*j})
				s[open], s[len(s)-2-2*j] = s[len(s)-2-2*j], s[open]
			}
		}

		for j := n - 2 * k + 1; j >= (n - 2 * k) / 2 + 1; j-- {
			close = next(s, close, ')')
			if close != j {
				ervs = append(ervs, erv{a:close, b: j})
				s[close], s[j] = s[j], s[close]
			}
		}
		//fmt.Println(string(s))

		fmt.Println(len(ervs))
		for _, v := range ervs {
			fmt.Println(v.a+1, v.b+1)
		}
	}
}

package main

import (
	"fmt"
	"runtime"
)

type
	LR struct {
		L int16
		R int16
	}

func solve() {
	var n, k int16
	var s  []byte
	fmt.Scan(&n,&k,&s)
	cnt := int16(0)
	res := make([]LR,0)
	i := int16(0)
	for cnt < k-1 && i < n-1 {
		if i%2 == 0 {
			if string(s[i]) == "(" {
//				i++
			} else {
				l := i
				r := i + 1
				for ; r < n && string(s[r]) == ")"; r++ {
				}
				if r>=n {
					break
				}
				res = append(res, LR{l, r})
				for u := int16(0); u < (r-l)/2; u++ {
					s[i+u], s[r-u] = s[r-u], s[i+u]
				}
			}
		} else {
			if string(s[i]) == ")" {
//				i++
				cnt++
			} else {
				l := i
				r := i + 1
				for ; r < n && string(s[r]) == "("; r++ {
				}
				if r>=n {
					break
				}
				res = append(res, LR{l, r})
				for u := int16(0); u < (r-l)/2; u++ {
					s[i+u], s[r-u] = s[r-u], s[i+u]
				}
			}
		}
		i++
	}

	end := i+(n - i) / 2
	for i <= end &&i<n-1 {
		if string(s[i]) == ")" {
			l := i
			r := i + 1
			for ; r < n && string(s[r]) == ")"; r++ {
			}
			if r>=n {
				break
			}
			res = append(res, LR{l, r})
			for u := int16(0); u < (r-l)/2; u++ {
				s[l+u], s[r-u] = s[r-u], s[l+u]
			}
		}
		i++
	}

	fmt.Printf("%d\n", len(res))
	for i := range res {
		fmt.Printf("%d %d\n", res[i].L+1, res[i].R+1)
	}

	s=nil
	res=nil
}

func main() {
	//t := ReadInt()
	t :=1
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
		runtime.GC()
	}
}

package main

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

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

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

func init() {
	var f *os.File
	if runtime.GOROOT() == "C:\\SOFT\\Go" {
		f, _ = os.Open("input.txt")
		//defer f.Close()
	} else {
		f = os.Stdin
	}
	ReadString = newReadString(f)
	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

type
	LR struct {
		L int
		R int
	}

func solve() {
	n, k := ReadInt2()
	s := ReadRuneSlice()

	cnt := 0
	var res = []LR{}
	i := 0
	for cnt < k-1 && i < n-1 {
		if i%2 == 0 {
			if string(s[i]) == "(" {
				i++
			} else {
				l := i
				r := i + 1
				for ; r < n && string(s[r]) == ")"; r++ {
				}
				res = append(res, LR{l, r})
				for u := 0; u < (r-l)/2; u++ {
					s[i+u], s[r-u] = s[r-u], s[i+u]
				}
			}
		} else {
			if string(s[i]) == ")" {
				i++
				cnt++
			} else {
				l := i
				r := i + 1
				for ; r < n && string(s[r]) == "("; r++ {
				}
				res = append(res, LR{l, r})
				for u := 0; u < (r-l)/2; u++ {
					s[i+u], s[r-u] = s[r-u], s[i+u]
				}
			}
		}
	}

	end := i+(n - i) / 2
	for i <= end &&i<n-1 {
		if string(s[i]) == ")" {
			l := i
			r := i + 1
			for ; r < n && string(s[r]) == ")"; r++ {
			}
			if r>=n {
				break
			}
			res = append(res, LR{l, r})
			for u := 0; u < (r-l)/2; u++ {
				s[l+u], s[r-u] = s[r-u], s[l+u]
			}
		}
		i++
	}

	fmt.Printf("%d\n", len(res))
	for _, v := range res {
		fmt.Printf("%d %d\n", v.L, v.R)
	}

}

func main() {
	t := ReadInt()
	//t :=1
	for ; t > 0; t-- {
		solve()
	}
}

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])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var t int
var n, k int
var S []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, k = ReadInt2()
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	answers := [][]int{}

	for i := 0; i < 2*(k-1); i++ {
		if i%2 == 0 && S[i] == ')' {
			answers = append(answers, sub(i, '('))
		} else if i%2 == 1 && S[i] == '(' {
			answers = append(answers, sub(i, ')'))
		}
	}

	tmp := (n - 2*(k-1)) / 2
	for i := 2 * (k - 1); i < 2*(k-1)+tmp; i++ {
		if S[i] == ')' {
			answers = append(answers, sub(i, '('))
		}
	}

	fmt.Println(len(answers))
	for i := 0; i < len(answers); i++ {
		fmt.Println(answers[i][0]+1, answers[i][1]+1)
	}

	fmt.Println(string(S))
}

// sからスタートしてrを発見したところで反転する
// さらに始点と終点をスライスで返す
func sub(s int, r rune) []int {
	t := s + 1
	for i := s + 1; i < n; i++ {
		if S[i] == r {
			t = i
			break
		}
	}
	res := []int{s, t}

	// [s, t]を反転
	rev := Reverse(S[s : t+1])
	for i := 0; i < len(rev); i++ {
		S[s+i] = rev[i]
	}

	return res
}

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

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

	return res
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

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