package main

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

const (
	INIT = -1
	INF  = 1e18
)

func die(v ...interface{}) {
	_, file, line, _ := runtime.Caller(1)
	fmt.Printf("die %s line %d: %v", file, line, v)
	os.Exit(-1)
}

type scanner struct {
	sc  *bufio.Scanner
	buf []byte
	max int
}

func newScanner() *scanner {
	rv := &scanner{max: bufio.MaxScanTokenSize} // default
	rv.sc = bufio.NewScanner(os.Stdin)
	rv.sc.Split(bufio.ScanWords)
	rv.buf = make([]byte, rv.max)
	rv.sc.Buffer(rv.buf, rv.max)
	return rv
}

func (s *scanner) nextInt() int {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	rv, err := strconv.Atoi(s.sc.Text())
	if err != nil {
		die(err)
	}
	return rv
}

func (s *scanner) nextInt64() int64 {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	rv, err := strconv.ParseInt(s.sc.Text(), 10, 64)
	if err != nil {
		die(err)
	}
	return rv
}

func (s *scanner) nextStr() string {
	if !s.sc.Scan() {
		die("parse error")
	}
	return s.sc.Text()
}

var (
	a []int
	n int
)

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

func same() bool {
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		cnt[a[i]]++
		if cnt[a[i]]*2 >= n {
			return true
		}
	}
	return false
}

func main() {
	sc := newScanner()
	tc := sc.nextInt()

	for ; tc > 0; tc-- {
		n = sc.nextInt()

		a = make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = sc.nextInt()
		}
		if same() {
			fmt.Println(-1)
			continue
		}

		// fmt.Printf("a\n")
		// for _, k := range a {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		div := []int{}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := abs(a[i] - a[j])
				if diff == 0 {
					continue
				}
				for k := 1; k*k <= diff; k++ {
					if (diff % k) == 0 {
						div = append(div, diff/k)
						div = append(div, k)
					}
				}
			}
		}

		uniq := []int{}
		sort.Ints(div)
		for i := 0; i < len(div); i++ {
			if i == 0 || div[i] != div[i-1] {
				uniq = append(uniq, div[i])
			}
		}
		div = uniq

		// fmt.Printf("div\n")
		// for _, k := range div {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		ans := 0
		for _, k := range div {
			for i := 0; i < n; i++ {
				cnt := 0
				for j := 0; j < n; j++ {
					diff := abs(a[i] - a[j])
					if (diff % k) == 0 {
						cnt++
					}
				}
				if cnt*2 >= n {
					if ans < k {
						ans = k
					}
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

const (
	INIT = -1
	INF  = 1e18
)

func die(s ...string) {
	_, file, line, _ := runtime.Caller(1)
	fmt.Printf("die %q file %s line %d", s, file, line)
	os.Exit(-1)
}

type scanner struct {
	sc  *bufio.Scanner
	buf []byte
	max int
}

func newScanner() *scanner {
	rv := &scanner{max: bufio.MaxScanTokenSize} // default
	rv.sc = bufio.NewScanner(os.Stdin)
	rv.sc.Split(bufio.ScanWords)
	rv.buf = make([]byte, rv.max)
	rv.sc.Buffer(rv.buf, rv.max)
	return rv
}

func (s *scanner) fastParse(buf []byte) (x int64, y int) {
	neg := false
	if buf[0] == '-' {
		neg = true
		buf = buf[1:]
	}
	for _, b := range buf {
		x = x*10 + int64(b-'0')
		y = y*10 + int(b-'0')
	}
	if neg {
		x = -x
		y = -y
	}
	return
}

func (s *scanner) nextInt() int {
	if !s.sc.Scan() {
		die("parse error")
	}
	_, rv := s.fastParse(s.sc.Bytes())
	return rv
}

func (s *scanner) nextInt64() int64 {
	if !s.sc.Scan() {
		die("parse error")
	}
	rv, _ := s.fastParse(s.sc.Bytes())
	return rv
}

func (s *scanner) nextStr() string {
	if !s.sc.Scan() {
		die("parse error")
	}
	return s.sc.Text()
}

var (
	a []int
	n int
)

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

func same() bool {
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		cnt[a[i]]++
		if cnt[a[i]]*2 >= n {
			return true
		}
	}
	return false
}

func main() {
	sc := newScanner()
	tc := sc.nextInt()

	for ; tc > 0; tc-- {
		n = sc.nextInt()

		a = make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = sc.nextInt()
		}
		if same() {
			fmt.Println(-1)
			continue
		}

		// fmt.Printf("a\n")
		// for _, k := range a {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		div := []int{}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := abs(a[i] - a[j])
				if diff == 0 {
					continue
				}
				for k := 1; k*k <= diff; k++ {
					if (diff % k) == 0 {
						div = append(div, diff/k)
						div = append(div, k)
					}
				}
			}
		}

		uniq := []int{}
		sort.Ints(div)
		for i := 0; i < len(div); i++ {
			if i == 0 || div[i] != div[i-1] {
				uniq = append(uniq, div[i])
			}
		}
		div = uniq

		// fmt.Printf("div\n")
		// for _, k := range div {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		ans := 0
		for _, k := range div {
			for i := 0; i < n; i++ {
				cnt := 0
				for j := 0; j < n; j++ {
					diff := abs(a[i] - a[j])
					if (diff % k) == 0 {
						cnt++
					}
				}
				if cnt*2 >= n {
					if ans < k {
						ans = k
					}
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

const (
	INIT = -1
	INF  = 1e18
)

func check(t bool, s ...string) {
	if t {
		return
	}
	_, file, line, _ := runtime.Caller(1)
	fmt.Println("Check failed file", file, "line", line, s)
	os.Exit(-1)
}

var (
	a []int
	n int
)

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

func same() bool {
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		cnt[a[i]]++
		if cnt[a[i]]*2 >= n {
			return true
		}
	}
	return false
}

func main() {
	var tc int
	r := bufio.NewReader(os.Stdin)
	//	fmt.Scan(&tc)
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {
		fmt.Fscan(r, &n)
		//		fmt.Scan(&n)

		a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i])
			//			fmt.Scan(&a[i])
		}
		if same() {
			fmt.Println(-1)
			continue
		}

		// fmt.Printf("a\n")
		// for _, k := range a {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		div := []int{}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := abs(a[i] - a[j])
				if diff == 0 {
					continue
				}
				for k := 1; k*k <= diff; k++ {
					if (diff % k) == 0 {
						div = append(div, diff/k)
						div = append(div, k)
					}
				}
			}
		}

		uniq := []int{}
		sort.Ints(div)
		for i := 0; i < len(div); i++ {
			if i == 0 || div[i] != div[i-1] {
				uniq = append(uniq, div[i])
			}
		}
		div = uniq

		// fmt.Printf("div\n")
		// for _, k := range div {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		ans := 0
		for _, k := range div {
			for i := 0; i < n; i++ {
				cnt := 0
				for j := 0; j < n; j++ {
					diff := abs(a[i] - a[j])
					if (diff % k) == 0 {
						cnt++
					}
				}
				if cnt*2 >= n {
					if ans < k {
						ans = k
					}
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"os"
	"runtime"
	"sort"
)

const (
	INIT = -1
	INF  = 1e18
)

func check(t bool, s ...string) {
	if t {
		return
	}
	_, file, line, _ := runtime.Caller(1)
	fmt.Println("Check failed file", file, "line", line, s)
	os.Exit(-1)
}

var (
	a []int
	n int
)

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

func same() bool {
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		cnt[a[i]]++
		if cnt[a[i]]*2 >= n {
			return true
		}
	}
	return false
}

func main() {
	var tc int
	fmt.Fscan(os.Stdin, &tc)

	for ; tc > 0; tc-- {
		fmt.Fscan(os.Stdin, &n)

		a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(os.Stdin, &a[i])
		}
		if same() {
			fmt.Println(-1)
			continue
		}

		// fmt.Printf("a\n")
		// for _, k := range a {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		div := []int{}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := abs(a[i] - a[j])
				if diff == 0 {
					continue
				}
				for k := 1; k*k <= diff; k++ {
					if (diff % k) == 0 {
						div = append(div, diff/k)
						div = append(div, k)
					}
				}
			}
		}

		uniq := []int{}
		sort.Ints(div)
		for i := 0; i < len(div); i++ {
			if i == 0 || div[i] != div[i-1] {
				uniq = append(uniq, div[i])
			}
		}
		div = uniq

		// fmt.Printf("div\n")
		// for _, k := range div {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		ans := 0
		for _, k := range div {
			for i := 0; i < n; i++ {
				cnt := 0
				for j := 0; j < n; j++ {
					diff := abs(a[i] - a[j])
					if (diff % k) == 0 {
						cnt++
					}
				}
				if cnt*2 >= n {
					if ans < k {
						ans = k
					}
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

const (
	INIT = -1
	INF  = 1e18
)

func check(t bool, s ...string) {
	if t {
		return
	}
	_, file, line, _ := runtime.Caller(1)
	fmt.Println("Check failed file", file, "line", line, s)
	os.Exit(-1)
}

type scanner struct {
	sc  *bufio.Scanner
	buf []byte
	max int
}

func newScanner() *scanner {
	rv := &scanner{max: bufio.MaxScanTokenSize} // default
	rv.sc = bufio.NewScanner(os.Stdin)
	rv.sc.Split(bufio.ScanWords)
	rv.buf = make([]byte, rv.max)
	rv.sc.Buffer(rv.buf, rv.max)
	return rv
}

func (s *scanner) nextInt() int {
	return int(s.nextInt64())
}

func (s *scanner) nextInt64() int64 {
	check(s.sc.Scan(), "parse error")
	rv, _ := strconv.ParseInt(string(s.sc.Bytes()), 10, 64)
	return rv
}

func (s *scanner) nextStr() string {
	check(s.sc.Scan(), "parse error")
	return s.sc.Text()
}

var (
	a []int
	n int
)

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

func same() bool {
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		cnt[a[i]]++
		if cnt[a[i]]*2 >= n {
			return true
		}
	}
	return false
}

func main() {
	sc := newScanner()
	tc := sc.nextInt()
	for ; tc > 0; tc-- {
		n = sc.nextInt()
		a = make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = sc.nextInt()
		}
		if same() {
			fmt.Println(-1)
			continue
		}

		// fmt.Printf("a\n")
		// for _, k := range a {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		div := []int{}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := abs(a[i] - a[j])
				if diff == 0 {
					continue
				}
				for k := 1; k*k <= diff; k++ {
					if (diff % k) == 0 {
						div = append(div, diff/k)
						div = append(div, k)
					}
				}
			}
		}

		uniq := []int{}
		sort.Ints(div)
		for i := 0; i < len(div); i++ {
			if i == 0 || div[i] != div[i-1] {
				uniq = append(uniq, div[i])
			}
		}
		div = uniq

		// fmt.Printf("div\n")
		// for _, k := range div {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		ans := 0
		for _, k := range div {
			for i := 0; i < n; i++ {
				cnt := 0
				for j := 0; j < n; j++ {
					diff := abs(a[i] - a[j])
					if (diff % k) == 0 {
						cnt++
					}
				}
				if cnt*2 >= n {
					if ans < k {
						ans = k
					}
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

const (
	INIT = -1
	INF  = 1e18
)

func check(t bool, s ...string) {
	if t {
		return
	}
	_, file, line, _ := runtime.Caller(1)
	fmt.Println("Check failed file", file, "line", line, s)
	os.Exit(-1)
}

type scanner struct {
	sc  *bufio.Scanner
	buf []byte
	max int
}

func newScanner() *scanner {
	rv := &scanner{max: bufio.MaxScanTokenSize} // default
	rv.sc = bufio.NewScanner(os.Stdin)
	rv.sc.Split(bufio.ScanWords)
	rv.buf = make([]byte, rv.max)
	rv.sc.Buffer(rv.buf, rv.max)
	return rv
}

// TODO: does this actually make any difference vs strconv?
func (s *scanner) fastParse(buf []byte) (x int64, y int) {
	neg := false
	for _, b := range buf {
		if b == '-' {
			neg = true
			continue
		}
		x = x*10 + int64(b-'0')
		y = y*10 + int(b-'0')
	}
	if neg {
		x *= -1
		y *= -1
	}
	return
}

func (s *scanner) nextInt() int {
	check(s.sc.Scan(), "parse error")
	_, ans := s.fastParse(s.sc.Bytes())
	return ans
}

func (s *scanner) nextInt64() int64 {
	check(s.sc.Scan(), "parse error")
	ans, _ := s.fastParse(s.sc.Bytes())
	return ans
}

func (s *scanner) nextStr() string {
	check(s.sc.Scan(), "parse error")
	return s.sc.Text()
}

var (
	a []int
	n int
)

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

func same() bool {
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		cnt[a[i]]++
		if cnt[a[i]]*2 >= n {
			return true
		}
	}
	return false
}

func main() {
	sc := newScanner()
	tc := sc.nextInt()
	for ; tc > 0; tc-- {
		n = sc.nextInt()
		a = make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = sc.nextInt()
		}
		if same() {
			fmt.Println(-1)
			continue
		}

		// fmt.Printf("a\n")
		// for _, k := range a {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		div := []int{}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				diff := abs(a[i] - a[j])
				if diff == 0 {
					continue
				}
				for k := 1; k*k <= diff; k++ {
					if (diff % k) == 0 {
						div = append(div, diff/k)
						div = append(div, k)
					}
				}
			}
		}

		uniq := []int{}
		sort.Ints(div)
		for i := 0; i < len(div); i++ {
			if i == 0 || div[i] != div[i-1] {
				uniq = append(uniq, div[i])
			}
		}
		div = uniq

		// fmt.Printf("div\n")
		// for _, k := range div {
		// 	fmt.Printf("%d ", k)
		// }
		// fmt.Println()

		ans := 0
		for _, k := range div {
			for i := 0; i < n; i++ {
				cnt := 0
				for j := 0; j < n; j++ {
					diff := abs(a[i] - a[j])
					if (diff % k) == 0 {
						cnt++
					}
				}
				if cnt*2 >= n {
					if ans < k {
						ans = k
					}
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

var N int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, N)

		for j := 0; j < N; j++ {
			fmt.Fscan(r, &a[j])
		}

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	var max int
	var min int = 1e6 + 5

	mp := make(map[int]int)

	for i := 0; i < N; i++ {
		if a[i] > max {
			max = a[i]
		}
		if a[i] < min {
			min = a[i]
		}
		mp[a[i]]++
	}

	for _, v := range mp {
		if v >= N/2 {
			return -1
		}
	}

	if N == 4 {
		return max - min
	}

	var maxKey int = 2

	for i := 0; i < N; i++ {
		for j := i + 1; j < N; j++ {

			d := abs(a[i] - a[j])

			mp := make(map[int]int)

			for k := 0; k < N; k++ {
				if k == i  || k == j {
					continue
				}

				d1 := abs(a[i] - a[k])
				d2 := abs(a[j] - a[k])

				g1 := GCD(d1, d2)

				g := GCD(g1, d)

				mp[g]++
			}

			var localMax int

			var arr []int

			for k, _ := range mp {
				arr = append(arr, k)
			}
			sort.Ints(arr)

			for p := len(arr) - 1; p >= 0; p-- {

				curKey := arr[p]
				v1 := mp[curKey]

				if v1 >= N/2 - 2 {
					localMax = maxF(localMax, curKey)
					continue
				}

				c := v1

				var g int = -1
				done := false

				for z := p - 1; z >= 0; z-- {
					thisKey := arr[z]
					v2 := mp[thisKey]

					if g == -1 {
						g = GCD(curKey, thisKey)
					} else {
						g = GCD(g, thisKey)
					}
					if c + v2 >= N/2 - 2 {
						done = true
						break
					}
				}

				if done {
					localMax = maxF(localMax, g)
				}
			}

			maxKey = maxF(maxKey, localMax)
		}
	}

	return maxKey
}

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

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

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

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n, h int

	_, _ = fmt.Fscan(r, &t)

	TestLoop: for ; t > 0; t-- {
		_, _ = fmt.Fscan(r, &n)
		var a []int

		origCnt := make(map[int]int)
		for i := 0; i < n; i++ {
			_, _ = fmt.Fscan(r, &h)
			a = append(a, h)
			origCnt[h]++
		}

		result := -1
		for _, value := range origCnt {
			if 2 * value >= n {
				fmt.Println(result)
				continue TestLoop
			}
		}

		sort.Ints(a)
		for i := 0; i < n; i++ {
			equal := 1
			cntDiv := make(map[int]int)
			for j := i + 1; j < n; j++ {
				if a[i] == a[j] {
					equal++
				} else {
					diff := a[j] - a[i]
					for d := 1; d * d <= diff; d++ {
						if diff % d == 0 {
							cntDiv[d]++
							if diff / d != d {
								cntDiv[diff / d]++
							}
						}
					}
				}
			}

			for key, value := range cntDiv {
				if 2 * (value + equal) >= n && result < key {
					result = key
				}
			}
		}

		fmt.Println(result)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1593D2(in io.Reader, out io.Writer) {
	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		c := map[int]int{}
		for i := range a {
			Fscan(in, &a[i])
			c[a[i]]++
		}
		for _, c := range c {
			if c >= n/2 {
				Fprintln(out, -1)
				continue o
			}
		}

		sort.Ints(a)
		ans := 0
		f := func(base, d int) bool {
			c := 0
			for _, v := range a {
				if abs(v-base)%d == 0 {
					c++
				}
			}
			return c >= n/2
		}
		for i, v := range a {
			for _, w := range a[:i] {
				for d, m := 1, v-w; d*d <= m; d++ {
					if m%d == 0 {
						if f(w, m/d) {
							ans = max(ans, m/d)
							break
						}
						if f(w, d) {
							ans = max(ans, d)
						}
					}
				}
			}
		}
		Fprintln(out, ans)
	}
}
func main() {CF1593D2(os.Stdin, os.Stdout)}
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1593D2(in io.Reader, out io.Writer) {
	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		c := map[int]int{}
		for i := range a {
			Fscan(in, &a[i])
			c[a[i]]++
		}
		for _, c := range c {
			if c >= n/2 {
				Fprintln(out, -1)
				continue o
			}
		}

		sort.Ints(a)
		ans := 0
		f := func(base, d int) bool {
			c := 0
			for _, v := range a {
				if abs(v-base)%d == 0 {
					c++
				}
			}
			return c >= n/2
		}
		for i, v := range a {
			for _, w := range a[:i] {
				for d, m := 1, v-w; d*d <= m; d++ {
					if m%d == 0 {
						if f(w, m/d) {
							ans = max(ans, m/d)
							break
						}
						if f(w, d) {
							ans = max(ans, d)
						}
					}
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		c := map[int]int{}
		for i := range a {
			Fscan(in, &a[i])
			c[a[i]]++
		}
		for _, c := range c {
			if c >= n/2 {
				Fprintln(out, -1)
				continue o
			}
		}

		sort.Ints(a)
		ans := 0
		f := func(base, d int) bool {
			c := 0
			for _, v := range a {
				if abs(v-base)%d == 0 {
					c++
				}
			}
			return c >= n/2
		}
		for i, v := range a {
			for _, w := range a[:i] {
				for d, m := 1, v-w; d*d <= m; d++ {
					if m%d == 0 {
						if f(w, m/d) {
							ans = max(ans, m/d)
							break
						}
						if f(w, d) {
							ans = max(ans, d)
						}
					}
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

  var t int
  fmt.Fscan(reader, &t)
  for ; t > 0; t-- {
    var n int
    fmt.Fscan(reader, &n)
    arr := make([]int, n)
    m := make(map[int]int)
    z := false
    for i := 0; i < n; i++ {
      fmt.Fscan(reader, &arr[i])
      m[arr[i]]++
      if m[arr[i]] >= n/2 {
        z = true
      }
    }
    if z {
      fmt.Fprint(writer, "-1\n")
      continue
    }

    ans := 1
    check := func (k int) {
      if k <= ans {
        return
      }
      freqs := make(map[int]int)
      for i := 0; i < n; i++ {
        rem := arr[i] % k
        if rem < 0 {
          rem += k
        }
        freqs[rem]++
        if freqs[rem] >= n/2 {
          ans = k
          return
        }
      }
      return
    }

    for i := 0; i < n; i++ {
      for j := 0; j < n; j++ {
        d := arr[i]-arr[j]
        if (d < 0) {
          d = -d
        }
        if (d <= ans) {
          continue
        }
        for k := 1; k*k <= d; k++ {
          if d % k == 0 {
            check(k)
            check(d/k)
          }
        }
      }
    }

    fmt.Fprint(writer, ans, "\n")
  }
}

package main

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

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

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

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

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

		sort.Ints(arr)

		diff := 1
		c := 1
		for i := 1; i < n; i++ {
			if arr[i] != arr[i-1] {
				c = 1
			} else {
				c++
			}
			if c > diff {
				diff = c
			}
		}

		if diff >= n/2 {
			fmt.Println(-1)
			continue
		}

		res := 0
		for l := 0; l < len(arr); l++ {
			f := [22]map[int]bool{}
			for i := 0; i < 22; i++ {
				f[i] = make(map[int]bool)
			}

			for i := 0; i < len(arr); i++ {
				for j := n/2 - 1; j >= 1; j-- {
					for k, _ := range f[j] {
						//	fmt.Println(k, arr[l]-arr[i])
						f[j+1][gcd(k, abs(arr[l]-arr[i]))] = true
					}
				}
				f[1][abs(arr[l]-arr[i])] = true
			}

			//	fmt.Println(f[1:(n/2 + 1)])

			for k, _ := range f[n/2] {
				if k > res {
					res = k
				}
			}
		}

		fmt.Println(res)
	}
}

package main

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

var N int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, N)

		for j := 0; j < N; j++ {
			fmt.Fscan(r, &a[j])
		}

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	var max int
	var min int = 1e6 + 5

	mp := make(map[int]int)

	for i := 0; i < N; i++ {
		if a[i] > max {
			max = a[i]
		}
		if a[i] < min {
			min = a[i]
		}
		mp[a[i]]++
	}

	for _, v := range mp {
		if v >= N/2 {
			return -1
		}
	}

	if N == 4 {
		return max - min
	}

	var maxKey int = 2

	for i := 0; i < N; i++ {
		for j := i + 1; j < N; j++ {

			d := abs(a[i] - a[j])

			mp := make(map[int]int)

			for k := 0; k < N; k++ {
				if k == i  || k == j {
					continue
				}

				d1 := abs(a[i] - a[k])
				d2 := abs(a[j] - a[k])

				g1 := GCD(d1, d2)

				g := GCD(g1, d)

				mp[g]++
			}

			for k, v := range mp {
				if v >= N/2 - 2 {
					maxKey = maxF(maxKey, k)
				}
			}
		}
	}

	return maxKey
}

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

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

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

package main

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

var N int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, N)

		for j := 0; j < N; j++ {
			fmt.Fscan(r, &a[j])
		}

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	var max int
	var min int = 1e6 + 5

	mp := make(map[int]int)

	for i := 0; i < N; i++ {
		if a[i] > max {
			max = a[i]
		}
		if a[i] < min {
			min = a[i]
		}
		mp[a[i]]++
	}

	for _, v := range mp {
		if v >= N/2 {
			return -1
		}
	}

	var maxKey int = 2

	for i := 0; i < N; i++ {
		for j := i + 1; j < N; j++ {

			d := abs(a[i] - a[j])

			mp := make(map[int]int)

			for k := 0; k < N; k++ {
				if k == i  || k == j {
					continue
				}

				d1 := abs(a[i] - a[k])
				d2 := abs(a[j] - a[k])

				g1 := GCD(d1, d2)

				g := GCD(g1, d)

				mp[g]++
			}

			for k, v := range mp {
				if v >= N/2 - 2 {
					maxKey = maxF(maxKey, k)
				}
			}
		}
	}

	return maxKey
}

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

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

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

package main

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

var N int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, N)

		for j := 0; j < N; j++ {
			fmt.Fscan(r, &a[j])
		}

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	var max int
	var min int = 1e6 + 5

	mp := make(map[int]int)

	for i := 0; i < N; i++ {
		if a[i] > max {
			max = a[i]
		}
		if a[i] < min {
			min = a[i]
		}
		mp[a[i]]++
	}

	for _, v := range mp {
		if v >= N/2 {
			return -1
		}
	}

	var  i int
	for i = max - min; i >= 2; i-- {

		mp := make(map[int]int)

		for j := 0; j < N; j++ {
			var r int
			if a[j] >= 0 {
				r = a[j] % i

			} else {
				r = i + (a[j] % i)
			}

			mp[r]++
		}

		for _, v := range mp {
			if v >= N/2 {
				return i
			}
		}
	}

	return 1
}

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

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


package main

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

func main() {
	for t := ScanInt(); t > 0; t-- {
		a1, k := ScanInt64(), ScanInt64()

		for k > 1 {
			k--
			diff := sum(a1)
			if diff == 0 {
				break
			}
			a1 += diff
		}
		P(a1)
	}

	close(O)
	oWg.Wait()
}
func sum(n int64) int64 {
	digits := []int64{}
	for n > 0 {
		digits = append(digits, n%10)
		n /= 10
	}
	mn, mx := MnMx(digits...)

	return mn * mx
}

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

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

func init() {
	P = fmt.Println

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

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

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

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

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
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 sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

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

var (
	wg sync.WaitGroup
	res map[int]int64
	mut sync.Mutex
)

func mul_has0(v int64) (int64, bool) {
	_min, _max := int64(9), int64(0)
	for v != 0 {
		dig := v % 10
		if dig == 0 {
			return 0, true
		}
		if dig < _min {
			_min = dig
		}
		if dig > _max {
			_max = dig
		}
		v = (v-dig)/10
	}
	return _min*_max, false
}

func Algo(a, n int64, id int) {
	for i := int64(1); i < n; i++ {
		addV, has0 := mul_has0(a)
		if (has0) {
			break
		}
		a = a + addV
	}
	mut.Lock()
	res[id] = a
	wg.Done()
	mut.Unlock()
}

func main() {
	sc.Split(bufio.ScanWords)
	// code
	var (
		a, n int64
	)
	t := nextInt()
	res = make(map[int]int64)
	//loop
	for i := 0; i < t; i++ {
		wg.Add(1)
		a, n = nextInt64(), nextInt64()
		go Algo(a, n, i)
	}
	wg.Wait()
	for i := 0; i < t; i++ {
		fmt.Println(res[i])
	}
}

// -*-*-*-*-*-*-*-*-
// * I/O utilities *
// -*-*-*-*-*-*-*-*-
var sc = bufio.NewScanner(os.Stdin)

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

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

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

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

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func split(s string) []string {
	ret := make([]string, len([]rune(s)))
	for i, v := range []rune(s) {
		ret[i] = string(v)
	}
	return ret
}

package main
import (
	"fmt"
	"sync"
)

var (
	wg sync.WaitGroup
	res map[int]int64
	mut sync.Mutex
)

func min_c(v int64) int64 {
	var res int64 = 9
	for v != 0 {
		dig := v % 10
		if res > dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func max_c(v int64) int64 {
	var res int64 = 0
	for v != 0 {
		dig := v % 10
		if res < dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func has_0(v int64) bool {
	for v != 0 {
		dig := v % 10
		if dig == 0 {
			return true
		}
		v = (v-dig)/10
	}
	return false
}

func Algo(a, n int64, id int) {
	for i := int64(1); i < n; i++ {
		if has_0(a) {
			break
		}
		a = a + min_c(a)*max_c(a)
	}
	mut.Lock()
	res[id] = a
	wg.Done()
	mut.Unlock()
}

func main() {
	var (
		a, n int64
		t int
	)
	fmt.Scanf("%d\n", &t)
	res = make(map[int]int64)
	//loop
	for i := 0; i < t; i++ {
		wg.Add(1)
		fmt.Scanf("%d %d\n", &a, &n)
		go Algo(a, n, i)
	}
	wg.Wait()
	for i := 0; i < t; i++ {
		fmt.Println(res[i])
	}
}

package main
import (
	"fmt"
)

func min_c(v int64) int64 {
	var res int64 = 9
	for v != 0 {
		dig := v % 10
		if res > dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func max_c(v int64) int64 {
	var res int64 = 0
	for v != 0 {
		dig := v % 10
		if res < dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func has_0(v int64) bool {
	for v != 0 {
		dig := v % 10
		if dig == 0 {
			return true
		}
		v = (v-dig)/10
	}
	return false
}

func main() {
	var (
		a, n int64
		t int
	)
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d %d\n", &a, &n)
		for i := int64(1); i < n; i++ {
			if has_0(a) {
				break
			}
			a = a + min_c(a)*max_c(a)
		}
		fmt.Println(a)
	}
}

package main
import (
	"fmt"
	"sync"
)

var (
	wg sync.WaitGroup
	res map[int]int64
	mut sync.Mutex
)

func mul_has0(v int64) (int64, bool) {
	_min, _max := int64(9), int64(0)
	for v != 0 {
		dig := v % 10
		if dig == 0 {
			return 0, true
		}
		if dig < _min {
			_min = dig
		}
		if dig > _max {
			_max = dig
		}
		v = (v-dig)/10
	}
	return _min*_max, false
}

func Algo(a, n int64, id int) {
	for i := int64(1); i < n; i++ {
		addV, has0 := mul_has0(a)
		if (has0) {
			break
		}
		a = a + addV
	}
	mut.Lock()
	res[id] = a
	wg.Done()
	mut.Unlock()
}

func main() {
	var (
		a, n int64
		t int
	)
	fmt.Scanf("%d\n", &t)
	res = make(map[int]int64)
	//loop
	for i := 0; i < t; i++ {
		wg.Add(1)
		fmt.Scanf("%d %d\n", &a, &n)
		go Algo(a, n, i)
	}
	wg.Wait()
	for i := 0; i < t; i++ {
		fmt.Println(res[i])
	}
}

package main

import (
	"fmt"
)

func MinMax(number int64) (int64, int64) {
	var minNum, maxNum = number % 10, number % 10
	var ost int64
	for number > 0 {
		ost = number % 10
		if ost > maxNum {
			maxNum = ost
		}
		if ost < minNum {
			minNum = ost
		}
		number /= 10
	}
	return minNum, maxNum
}

func main() {
	var t int
	_, _ = fmt.Scan(&t)
	for ; t > 0; t-- {
		var a1, K int64
		_, _ = fmt.Scan(&a1, &K)
		K--
	outer:
		for ; K > 0; K-- {
			min, max := MinMax(a1)
			a1 += min * max
			checkNum := a1
			for checkNum > 0 {
				if checkNum%10 == 0 {
					break outer
				}
				checkNum /= 10
			}
		}
		fmt.Println(a1)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		a1, k := int64(0), int64(0)
		fmt.Fscanf(in, "%d %d\n", &a1, &k)
		final := false
		for !final && k > 1 {
			o := minDigit(a1)
			p := maxDigit(a1)
			if o*p == 0 {
				final = true
			}
			a1 += o * p
			k--
		}
		fmt.Fprintf(out, "%d\n", a1)
	}
	return
}

func minDigit(a int64) int64 {
	ans := a % 10
	for a > 0 {
		ans = min(ans, a%10)
		a /= 10
	}
	return ans
}

func maxDigit(a int64) int64 {
	ans := a % 10
	for a > 0 {
		ans = max(ans, a%10)
		a /= 10
	}
	return ans
}

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

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

package main

import (
	"fmt"
)

func minD(x int64) int64 {
	var ans int64 = 9
	for x > 0 {
		if ans > x % 10 {
			ans = x % 10
		}
		x /= 10
	}
	return ans
}

func maxD(x int64) int64 {
	var ans int64
	for x > 0 {
		if ans < x % 10 {
			ans = x % 10
		}
		x /= 10
	}
	return ans
}

func solve() int64 {
	var a, k int64
	fmt.Scan(&a, &k)
	for i := int64(1); i < k; i++ {
		if minD(a) == 0 {
			return a
		}
		a += minD(a) * maxD(a)
	}
	return a
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 1; i <= t; i++ {
		fmt.Println(solve())
	}
}

// https://codeforces.com/contest/1353/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, k int64
	fmt.Fscan(input, &n, &k)
	ans := n
	for k > 1 {
		v := ans
		dx := int64(10)
		dm := int64(0)
		for v > 0 {
			d := v - (v/10)*10
			dm = max(dm, d)
			dx = min(dx, d)
			v/=10
		}
		if dx*dm == 0 {
			break
		}
		ans += dx*dm
		k--
	}
	fmt.Println(ans)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for i := int64(0); i < t; i++ {
		a, k := int64(0), int64(0)
		fmt.Fscanf(in, "%d %d\n", &a, &k)
		final := false
		for !final && k > 1 {
			min, max := findMinMax(a)
			a += min * max
			if min * max == 0 {
				final = true
			}
			k--
		}
		fmt.Fprintf(out, "%d\n", a)
	}
}

func findMinMax(i int64) (int64, int64) {
	min, max := int64(-1), int64(-1)
	for i > 0 {
		v := i % 10
		if min == -1 || min > v {
			min = v
		}
		if max == -1 || max < v {
			max = v
		}
		i /= 10
	}

	return min, max
}

/*
URL:
https://codeforces.com/problemset/problem/1355/A
*/

package main

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

/********** 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 (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t    int
	a, k int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		a, k = ReadInt64_2()

		solve()
	}
}

func solve() {
	cur := a
	for i := int64(0); i < k-1; i++ {
		mini, maxi := sub(cur)
		if mini == 0 {
			fmt.Println(cur)
			return
		}

		cur = cur + mini*maxi
	}
	fmt.Println(cur)
}

func sub(a int64) (mini, maxi int64) {
	S := []rune(strconv.FormatInt(a, 10))
	A := []int64{}
	for _, r := range S {
		A = append(A, int64(r-'0'))
	}

	mini, maxi = Min(A...), Max(A...)

	return mini, maxi
}

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

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

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

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

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

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	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
}

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

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

func main() {
	for t := ScanInt(); t > 0; t-- {
		a1, k := ScanInt64(), ScanInt64()

		for k > 1 {
			k--
			diff := sum(a1)
			if diff == 0 {
				break
			}
			a1 += diff
		}
		P(a1)
	}

	out.Flush()
}
func sum(n int64) int64 {
	digits := []int{}
	for n > 0 {
		digits = append(digits, int(n%10))
		n /= 10
	}
	mn, mx := MnMx(digits...)

	return int64(mn * mx)
}

/*********************** I/O ***********************/
var in *bufio.Scanner
var out *bufio.Writer

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}

func P(args ...interface{}) {
	print("\n", args...)
}

func PS(args ...interface{}) {
	print(" ", args...)
	out.WriteByte('\n')
}

func print(seperator string, args ...interface{}) {
	for _, arg := range args {
		str := ""
		switch v := arg.(type) {
		case byte:
			str = string(v)
		case rune:
			str = string(v)
		case int:
			str = strconv.Itoa(v)
		case int64:
			str = strconv.FormatInt(v, 10)
		case string:
			str = v
		}
		out.WriteString(str + seperator)
	}
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanWords(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanInt() int { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt()
	}
	return a
}
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt64()
	}
	return a
}

/*********************** Utils int64 ***********************/
func MnMx64(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn64(args ...int64) int64 { mn, _ := MnMx64(args...); return mn }
func Mx64(args ...int64) int64 { _, mx := MnMx64(args...); return mx }
func LCM64(a, b int64) int64   { return a / GCD64(a, b) * b }
func GCD64(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD64(b%a, a)
}
func SortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func Abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

/*********************** Utils int ***********************/
func MnMx(args ...int) (int, int) {
	minVal, maxVal := math.MaxInt32, math.MinInt32
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int64;
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in,&t)
	for cas := int64(1); cas <= t; cas++ {
		fmt.Fscan(in,&a,&k)
		var res = int64(a)
		for i := int64(1); i < k; i++ {
			min := minDigit(res)
			max := maxDigit(res)
			res = res + (min*max)
			if min==0 {
				break
			}
		}
		fmt.Fprintln(out,res)
		out.Flush()
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main() {
	sc.Split(bufio.ScanWords)
	defer out.Flush() // !!!!coution!!!! you must use Fprint(out, ) not Print()
	/* --- code --- */
	t := nextInt()
	// t := 1
	for i := 0; i < t; i++ {
		a, k := nextInt64(), nextInt64()
		// a, k := int64(487), int64(100)
		aMemo := map[int64]int64{}
		revMemo := map[int64]int64{}
		aMemo[a] = 1
		revMemo[1] = a
		for j := int64(2); j <= k; j++ {
			// max, min := splitNum(a)
			max, min := minmax(a)
			a = a + min*max
			// fmt.Println(a)
			_, exists := aMemo[a]
			if exists {
				// looped
				looping := j - aMemo[a]
				// fmt.Println("test, ", aMemo[a]+(k-aMemo[a])%looping)
				a = revMemo[aMemo[a]+(k-aMemo[a])%looping]
				break
			} else {
				aMemo[a] = j
				revMemo[j] = a
			}
		}

		fmt.Println(a)
	}

}

func minmax(a int64) (int64, int64) {
	aStr := strconv.FormatInt(a, 10)
	minKey := int64(10)
	maxKey := int64(0)
	for i := 0; i < len(aStr); i++ {
		s := aStr[i] - '0'
		minKey = min64(minKey, int64(s))
		maxKey = max64(maxKey, int64(s))
	}
	return maxKey, minKey
}

// -*-*-*-*-*-*-*-*-
// * I/O utilities *
// -*-*-*-*-*-*-*-*-

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

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

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

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

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func split(s string) []string {
	ret := make([]string, len([]rune(s)))
	for i, v := range []rune(s) {
		ret[i] = string(v)
	}
	return ret
}

// -*-*-*-*-*-*-*-*-
// * tool snippets *
// -*-*-*-*-*-*-*-*-
func duplicate2Int(base [][]int) (ret [][]int) {
	ret = make([][]int, len(base))
	for i, v := range base {
		ret[i] = append([]int{}, v...)
	}
	return
}

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

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

func min(a, b int) int {
	return int(min64(int64(a), int64(b)))
}

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

// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
// * math flavor typical theories *
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
func gcd(a, b int) int {
	if a > b {
		return gcd(b, a)
	}
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

func pow(a, b int) (ret int) {
	ret = a
	// 10^2 = 100ってことは10に10を1回掛けることだね
	// なので初期値を含めると上限b-1未満
	for i := 0; i < b-1; i++ {
		ret *= a
	}
	return
}

func powMod(n, m, mod int) (ret int) {
	ret = 1
	for m > 0 {
		if m&1 == 1 {
			ret *= n
			ret %= mod
		}
		n *= n
		n %= mod
		m >>= 1
	}
	return ret
}

package main

import "fmt"

func minmax(a int64) (int64, int64) {
	r := a % 10
	min, max := r, r
	for a > 0 {
		r = a % 10
		if min > r {
			min = r
		}
		if max < r {
			max = r
		}
		a /= 10
	}
	return min, max
}

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var a, k int64
		fmt.Scan(&a, &k)
		k--
	outer:
		for ; k > 0; k-- {
			min, max := minmax(a)
			a += min * max
			tmp := a
			for tmp > 0 {
				if tmp%10 == 0 {
					break outer
				}
				tmp /= 10
			}
		}
		fmt.Println(a)
	}
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt64() int64
	NextUInt64() uint64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextUInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

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

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

const (
	INF = math.MaxInt64
	MOD = 1e9 + 7
)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		a1, K := reader.NextInt64(), reader.NextInt64()
		writer.Printf("%d\n", sol(a1, K))
	}
}

func sol(a, k int64) int64 {
	for ; k > 1; k-- {
		minD, maxD := getMinMaxD(a)
		if minD == 0 || maxD == 0 {
			break
		}
		a += minD * maxD
	}
	return a
}

func getMinMaxD(a int64) (int64, int64) {
	minD := int64(9)
	maxD := int64(0)
	for a > 0 {
		d := a % 10
		if d < minD {
			minD = d
		}
		if d > maxD {
			maxD = d
		}
		a /= 10
	}
	return minD, maxD
}

package main
import (
	"fmt"
	"sync"
)

var (
	wg sync.WaitGroup
	res map[int]int64
	mut sync.Mutex
)

func min_c(v int64) int64 {
	var res int64 = 9
	for v != 0 {
		dig := v % 10
		if res > dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func max_c(v int64) int64 {
	var res int64 = 0
	for v != 0 {
		dig := v % 10
		if res < dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func has_0(v int64) bool {
	for v != 0 {
		dig := v % 10
		if dig == 0 {
			return true
		}
		v = (v-dig)/10
	}
	return false
}

func Algo(a, n int64, id int) {
	wg.Add(1)
	for i := int64(1); i < n; i++ {
		if has_0(a) {
			break
		}
		a = a + min_c(a)*max_c(a)
	}
	mut.Lock()
	res[id] = a
	mut.Unlock()
	wg.Done()
}

func main() {
	var (
		a, n int64
		t int
	)
	fmt.Scanf("%d\n", &t)
	res = make(map[int]int64)
	//loop
	for i := 0; i < t; i++ {
		fmt.Scanf("%d %d\n", &a, &n)
		go Algo(a, n, i)
	}
	wg.Wait()
	for i := 0; i < t; i++ {
		fmt.Println(res[i])
	}
}

package main
import (
	"fmt"
)

func min_c(v int64) int64 {
	var res int64 = 9
	for v != 0 {
		dig := v % 10
		if res > dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}
func max_c(v int64) int64 {
	var res int64 = 0
	for v != 0 {
		dig := v % 10
		if res < dig {
			res = dig
		}
		v = (v-dig)/10
	}
	return res
}

func main() {
	var (
		a, n int64
		t int
	)
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d %d\n", &a, &n)
		for i := int64(1); i < n; i++ {
			a = a + min_c(a)*max_c(a)
		}
		println(a)
	}
}

// https://codeforces.com/contest/1353/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, k int
	fmt.Fscan(input, &n, &k)
	ans := n
	for k > 1 {
		v := ans
		dx := 10
		dm := 0
		for v > 0 {
			d := v - (v/10)*10
			dm = max(dm, d)
			dx = min(dx, d)
			v/=10
		}
		if dx*dm == 0 {
			break
		}
		ans += dx*dm
		k--
	}
	fmt.Println(ans)
}

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

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		fmt.Printf("res = %d", res)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			//fmt.Printf("res = %d ",res )
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d",res)
	}
}

package main

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

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in,&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Fscan(in,&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Fprintln(out,res)
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		fmt.Printf("a = %d", a)
		fmt.Printf("k = %d", k)
		fmt.Printf("res = %d", res)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			//fmt.Printf("res = %d ",res )
			res = res + (maxDigit(res)*minDigit(res))
		}
		println(res)
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d \n",res)
	}
}

package main

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

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Fscan(in,&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("hello\n")
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			//fmt.Printf("res = %d ",res )
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

 package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("hello")
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

package main

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

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Fscan(in,&a,&k)
		var res = int64(a)
		fmt.Printf("a = %d", a)
		fmt.Printf("k = %d", k)
		fmt.Printf("res = %d", res)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

package main

import (
	"fmt"
	"math"
)

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	fmt.Scanf("%d",&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Scanf("%d %d",&a,&k)
		var res = int64(a)
		for i := 1; i <= k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Printf("%d\n",res)
	}
}

package main

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

func minDigit(x int64) int64{
	var min = x%10
	x=x/10
	for x>0{
		min= int64(math.Min(float64(min), float64(x%10)))
		x=x/10
	}
	return min
}

func maxDigit(x int64) int64{
	var max = x%10
	x=x/10
	for x>0{
		max = int64(math.Max(float64(max), float64(x%10)))
		x=x/10
	}
	return max
}

func main() {
	var t,a,k int;
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in,&t)
	for cas := 1; cas <= t; cas++ {
		fmt.Fscan(in,&a,&k)
		var res = int64(a)
		for i := 1; i < k; i++ {
			res = res + (maxDigit(res)*minDigit(res))
		}
		fmt.Fprintln(out,res)
		out.Flush()
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main() {
	sc.Split(bufio.ScanWords)
	defer out.Flush() // !!!!coution!!!! you must use Fprint(out, ) not Print()
	/* --- code --- */
	t := nextInt()
	for i := 0; i < t; i++ {
		a, k := nextInt64(), nextInt64()
		aMemo := map[int64]int64{}
		revMemo := map[int64]int64{}
		aMemo[a] = 1
		revMemo[1] = a
		for j := int64(2); j <= k; j++ {
			// max, min := splitNum(a)
			max, min := minmax(a)
			a = a + min*max
			_, exists := aMemo[a]
			if exists {
				// looped
				looping := j - aMemo[a]
				a = revMemo[(k-aMemo[a])+(k-aMemo[a])%looping]
				break
			} else {
				aMemo[a] = j
				revMemo[j] = a
			}
		}

		fmt.Println(a)
	}

}

func minmax(a int64) (int64, int64) {
	aStr := strconv.FormatInt(a, 10)
	minKey := int64(10)
	maxKey := int64(0)
	for i := 0; i < len(aStr); i++ {
		s := aStr[i] - '0'
		minKey = min64(minKey, int64(s))
		maxKey = max64(maxKey, int64(s))
	}
	return maxKey, minKey
}

// -*-*-*-*-*-*-*-*-
// * I/O utilities *
// -*-*-*-*-*-*-*-*-

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

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

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

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

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func split(s string) []string {
	ret := make([]string, len([]rune(s)))
	for i, v := range []rune(s) {
		ret[i] = string(v)
	}
	return ret
}

// -*-*-*-*-*-*-*-*-
// * tool snippets *
// -*-*-*-*-*-*-*-*-
func duplicate2Int(base [][]int) (ret [][]int) {
	ret = make([][]int, len(base))
	for i, v := range base {
		ret[i] = append([]int{}, v...)
	}
	return
}

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

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

func min(a, b int) int {
	return int(min64(int64(a), int64(b)))
}

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

// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
// * math flavor typical theories *
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
func gcd(a, b int) int {
	if a > b {
		return gcd(b, a)
	}
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

func pow(a, b int) (ret int) {
	ret = a
	// 10^2 = 100ってことは10に10を1回掛けることだね
	// なので初期値を含めると上限b-1未満
	for i := 0; i < b-1; i++ {
		ret *= a
	}
	return
}

func powMod(n, m, mod int) (ret int) {
	ret = 1
	for m > 0 {
		if m&1 == 1 {
			ret *= n
			ret %= mod
		}
		n *= n
		n %= mod
		m >>= 1
	}
	return ret
}


package main

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

var (
	a [2e5 + 5]int
)

func run(_r io.Reader, _w io.Writer) {
	const eof = 0
	out := bufio.NewWriter(_w)
	defer out.Flush()
	_i, _n, buf := 0, 0, make([]byte, 1<<12)
	rc := func() byte {
		if _i == _n {
			_n, _ = _r.Read(buf)
			if _n == 0 {
				return eof
			}
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}

	r := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
			if b == eof {
				return
			}
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	for t := r(); t > 0; t-- {
		n, k := r(), r()
		b := rc()
		for ; b == '\n' || b == '\r'; b = rc() {
			if b == eof {
				return
			}
		}
		for i := 1; i <= n; i++ {
			if b == 'W' {
				a[i] = a[i-1] + 1
			} else {
				a[i] = a[i-1]
			}
			b = rc()
		}
		res := 1<<31 - 1
		for i := 1; i+k-1 <= n; i++ {
			r := i + k - 1
			if t := a[r] - a[i-1]; t < res {
				res = t
			}
		}
		Fprintln(out, res)
	}
}

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

package main

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

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

// source: https://codeforces.com/contest/1690/problem/D
func solve() {
	n, k := _readInt(), _readInt()
	a := _readStr()
	ps := make([]int, n+1)
	for i := 1; i <= n; i++ {
		ps[i] = ps[i-1]
		if a[i-1] == 'W' {
			ps[i]++
		}
	}

	res := n
	for i := 0; i+k <= n; i++ {
		res = _min(res, ps[i+k]-ps[i])
	}

	_print(res)
	return
}

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

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

package main

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

const (
	MaxInt32 = 1 << 31 - 1
)

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

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

func main(){
	cin := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(cin, &t)
	for tc := 0; tc < t; tc++ {
		var n, k int
		fmt.Fscan(cin, &n, &k)
		var s string
		fmt.Fscan(cin, &s)

		w_prefix_sum := make([]int, n + 1)
		for i := 1; i <= n; i++ {
			cnt := 0
			if string(s[i - 1]) == "W" {
				cnt += 1
			}
			w_prefix_sum[i] = w_prefix_sum[i - 1] + cnt
		}

		ans := MaxInt32
		for i := k; i <= n; i++ {
			ans = Min(ans, w_prefix_sum[i] - w_prefix_sum[i - k])
		}
		fmt.Println(ans)
	}
}

package main

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

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

	var tt int
	fmt.Fscan(in, &tt)
	for tc := 0; tc < tt; tc++ {
		var (
			n int
			k int
			s string
		)
		fmt.Fscan(in, &n, &k, &s)
		cnt := 0
		ans := math.MaxInt
		for i := 0; i < n; i++ {
			if i >= k && s[i-k] == 'B' {
				cnt--
			}
			if s[i] == 'B' {
				cnt++
			}
			if k <= i+1 {
				ans = Min(ans, k-cnt)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

func Min(xs ...int) int {
	m := math.MaxInt
	for _, i := range xs {
		if i < m {
			m = i
		}
	}
	return m
}

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, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)

		var tot int
		for i := 0; i < k; i++ {
			if s[i] == 'W' {
				tot++
			}
		}

		min := tot
		for i := k; i < n; i++ {
			if s[i-k] == 'W' {
				tot--
			}
			if s[i] == 'W' {
				tot++
			}
			if tot < min {
				min = tot
			}
		}

		fmt.Fprintln(out, min)
	}
}

package main

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

/*
7
1 1
W
1 1
B
4 3
BWBW
5 4
BWBWW
6 3
BWBBBW
7 4
BWBWBWW
8 4
BWBWBWWB
---
1
0
1
2
0
2
2
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, k int
	var s string
	scanf("%d %d\n%s\n", &n, &k, &s)

	cnt := 0

	for i := 0; i < k; i++ {
		if s[i] == 'W' {
			cnt++
		}
	}

	minCnt := cnt

	for i, j := 0, k; j < n; i, j = i+1, j+1 {
		if s[i] == 'W' {
			cnt--
		}
		if s[j] == 'W' {
			cnt++
		}

		minCnt = min(minCnt, cnt)
	}

	fmt.Fprintln(writer, minCnt)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

func A4() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, k int
		Fscan(in, &n, &k)
		var s string
		Fscan(in, &s)
		// for i:=1;i<=n;i++{
		// 	sum[i] = sum[i-1]
		// 	if s[i-1] == 'B'{
		// 		sum[i]++
		// 	}
		// }
		ans := int(1e9)
		var cnt int
		for i, j := 0, 0; i < n; i++ {
			if s[i] == 'B' {
				cnt++
			}
			for i-j+1 > k {
				if s[j] == 'B' {
					cnt--
				}
				j++
			}
			if i-j+1 == k {
				if k-cnt < ans {
					ans = k - cnt
				}
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A4() }

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	var n, k int
	fmt.Fscan(r, &n, &k)
	var s string
	fmt.Fscan(r, &s)
	dp := make([]int, n)
	for i:=0;i<k;i++ {
		if s[i] == 'W' {
			dp[k-1]++
		}
	}
	ans := dp[k-1]
	for i:=k;i<n;i++ {
		if s[i-k] == 'W' {
			dp[i] = dp[i-1] - 1
		} else {
			dp[i] = dp[i-1]
		}
		if s[i] == 'W' {
			dp[i]++
		}
		ans = min(ans, dp[i])
	}
	fmt.Fprintln(w, ans)
}

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

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

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, k int
		var s string
		fmt.Fscan(reader, &n, &k, &s)
		ans := k
		white := 0
		black := 0
		for i := 0; i < k; i++ {
			if s[i] == 'B' {
				black++
			} else {
				white++
			}
		}
		if white < ans {
			ans = white
		}
		for i := k; i < n; i++ {
			if s[i-k] == 'B' {
				black--
			} else {
				white--
			}
			if s[i] == 'B' {
				black++
			} else {
				white++
			}
			if white < ans {
				ans = white
			}
		}

		fmt.Println(ans)

	}
}

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, k int
		var s string
		fmt.Fscan(reader, &n, &k, &s)
		ans := k
		white := 0
		black := 0
		for i := 0; i < n; i++ {
			if i >= k {
				if white < ans {
					ans = white
				}
				if s[i-k] == 'B' {
					black--
				} else {
					white--
				}

			}
			if s[i] == 'B' {
				black++
			} else {
				white++
			}
		}
		if white < ans {
			ans = white
		}
		fmt.Println(ans)

	}
}

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, k int
		var s string
		fmt.Fscan(reader, &n, &k, &s)
		ans := k
		white := 0
		black := 0
		for i := 0; i < n; i++ {
			if i < k {
				if s[i] == 'B' {
					black++
				} else {
					white++
				}
			} else {
				if white < ans {
					ans = white
				}
				if s[i] == 'B' {
					black++
				} else {
					white++
				}
				if s[i-k] == 'B' {
					black--
				} else {
					white--
				}
			}
		}
		if white < ans {
			ans = white
		}
		fmt.Println(ans)

	}
}

package main

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

var (
	a [2e5 + 5]bool
)

func run(_r io.Reader, _w io.Writer) {
	const eof = 0
	out := bufio.NewWriter(_w)
	defer out.Flush()
	_i, _n, buf := 0, 0, make([]byte, 1<<12)
	rc := func() byte {
		if _i == _n {
			_n, _ = _r.Read(buf)
			if _n == 0 {
				return eof
			}
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}

	r := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
			if b == eof {
				return
			}
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	for t := r(); t > 0; t-- {
		n, k := r(), r()
		b := rc()
		for ; b == '\n' || b == '\r'; b = rc() {
			if b == eof {
				return
			}
		}
		for i := 1; i <= n; i++ {
			if b == 'W' {
				a[i] = true
			}
			b = rc()
		}
		res := 0
		for i := 1; i <= n && i <= k; i++ {
			if a[i] {
				res++
			}
		}
		tmp := res
		l, r := 2, k+1
		for r <= n {
			if a[l-1] {
				tmp--
			}
			if a[r] {
				tmp++
			}
			if tmp < res {
				res = tmp
			}
			l++
			r++
		}
		Fprintln(out, res)
	}
}

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

package main

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

var (
	a [2e5 + 5]bool
)

func run(_r io.Reader, _w io.Writer) {
	const eof = 0
	out := bufio.NewWriter(_w)
	defer out.Flush()
	_i, _n, buf := 0, 0, make([]byte, 1<<12)
	rc := func() byte {
		if _i == _n {
			_n, _ = _r.Read(buf)
			if _n == 0 {
				return eof
			}
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}

	r := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
			if b == eof {
				return
			}
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	for t := r(); t > 0; t-- {
		n, k := r(), r()
		b := rc()
		for ; b == '\n' || b == '\r'; b = rc() {
			if b == eof {
				return
			}
		}
		for i := 1; i <= n; i++ {
			if b == 'W' {
				a[i] = true
			}
			b = rc()
		}
		res := 0
		for i := 1; i <= n && i <= k; i++ {
			if a[i] {
				res++
			}
		}
		tmp := res
		l, r := 2, k+1
		for r <= n {
			if a[l] != a[l-1] {
				if a[l] {
					tmp++
				} else {
					tmp--
				}
			}
			if a[r] {
				tmp++
			}
			if tmp < res {
				res = tmp
			}
			l++
			r++
		}
		Fprintln(out, res)
	}
}

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


package main

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

type segment struct {
    start int
    end int
}

func main() {
    var a [300000]int
    var segments []segment
    stin := bufio.NewReader(os.Stdin)
    ns,_ := stin.ReadBytes('\n')
    n,_ := strconv.Atoi(string(bytes.Trim(ns,"\n\r ")))
//    fmt.Printf("%d\n\r", n)
    var tmp []byte
    for i := 0; i < n-1; i++ {
        tmp,_ = stin.ReadBytes(' ')
        a[i],_ = strconv.Atoi(string(bytes.Trim(tmp," ")))
    }
    tmp,_ = stin.ReadBytes('\n')
//    fmt.Printf("last: %v\n", bytes.Trim(tmp,"\n\r"))
    a[n-1],_ = strconv.Atoi(string(bytes.Trim(tmp,"\n\r")))
//    for k := 0; k < n; k++ {
//        fmt.Printf("%d ", a[k])
//    }
//    fmt.Println()

    var g map[int]int = make(map[int]int)
    var left int = 0
    var right int = 0
    for i := 0; i < n; i++ {
        right = i
        if g[a[i]] == 1 { //already in map
            for k := range g {
                delete(g, k)
            }
            segments = append(segments, segment{start: left, end: right})
            left = i+1
        } else {
            g[a[i]] = 1
        }
    }
    kkl := len(segments)
    if kkl == 0 {
        fmt.Println("-1")
        return
    }
    segments[kkl-1].end = n-1
    fmt.Printf("%d\n", kkl)
    for k := 0; k < kkl; k++ {
        fmt.Printf("%d %d\n", segments[k].start+1, segments[k].end+1)
    }
}

package main

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

type pair struct {
	left, right int
}

type set map[interface{}]struct{}

func newSet() set {
	return make(set)
}

func (set *set) Contains(item interface{}) bool {
	_, ok := (*set)[item]
	return ok
}

func (set *set) Add(item interface{}) {
	(*set)[item] = struct{}{}
}

func solve(a []int) []pair {
	var segments []pair
	set := newSet()
	left := 1
	for i, x := range a {
		if set.Contains(x) {
			set = newSet()
			segments = append(segments, pair{left: left, right: i + 1})
			left = i + 2
		} else {
			set.Add(x)
		}
	}
	return segments
}

func main() {
	// buffered I/O
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	var n int

	//	fmt.Scan(&n)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())

	a := make([]int, n)
	for i := 0; i < n; i++ {
		//		fmt.Scan(&a[i])
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	segments := solve(a)
	length := len(segments)
	var left, right int

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

	if length == 0 {
		// fmt.Println(-1)
		fmt.Fprintln(writer, -1)
		return
	} else {
		// fmt.Println(length)
		fmt.Fprintln(writer, length)
		for i, x := range segments {
			left = x.left
			right = x.right
			if i == length - 1 {
				right = n
			}
			// fmt.Println(left, right)
			fmt.Fprintln(writer, left, right)
		}
	}
}

package main

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

type ii struct {
	start, end int
}

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

// func ReadInt64() int64 {
//     scanner.Scan()
//     ans, _ := strconv.ParseInt(scanner.Text(),10,64)
//     return ans
// }

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

// writer.WriteString(strconv.FormatInt(value, 10))
// writer.WriteByte(' ')

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

	n := ReadInt32()

	mapa := make(map[int]int)

	ini := 1

	var resp []ii

	for i := 1; i <= n; i++ {
		val := ReadInt32()
		if _, ok := mapa[val]; ok {
			resp = append(resp, ii{ini, i})
			ini = i+1
			mapa = make(map[int]int)
		} else {
			mapa[val]++
		}
	}

	if len(resp) == 0 {
		writer.WriteString("-1\n")
		return
	}

	if ini != n+1 {
		resp[len(resp) - 1].end = n
	}

	writer.WriteString(strconv.Itoa(len(resp)))
	writer.WriteString("\n")

	for _,elem := range resp {
		writer.WriteString(strconv.Itoa(elem.start))
		writer.WriteByte(' ')
		writer.WriteString(strconv.Itoa(elem.end))
		writer.WriteString("\n")
	}

}

// Codeforces problem 620C Shemchug
package main

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

type slr struct {
    l int
    r int
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    // read n
    s, _ := reader.ReadString('\n')
    ss := strings.Split(strings.TrimSpace(s), " ")
    n,_ := strconv.Atoi(ss[0])
    // read a
    s, _ = reader.ReadString('\n')
    ss = strings.Split(strings.TrimSpace(s), " ")
    a := make([]int, n)
    for i:=0; i<n; i++ {
        a[i],_ = strconv.Atoi(ss[i])
    }

    mp := make(map[int]int,0)
    var olr []slr

    // calculate olr
    for i:=0; i<n; i++ {
        c := a[i]
        l,was := mp[c]
        if !was {
            // no match
            mp[c] = i
        } else {
            lr := slr{l:l+1, r:i+1}
            olr = append(olr, lr)
            mp = make(map[int]int,0) // clear mp
        }
    }

    // prepare and print answer
    m := len(olr)
    if m==0 {
        fmt.Println("-1")
        return
    }
    // making all list belong to some lr
    olr[0].l = 1
    olr[m-1].r = n
    for i:=1; i<m; i++ {
        olr[i].l = olr[i-1].r+1
    }
    fmt.Println(m)
    for i:=0; i<m; i++ {
        fmt.Println(olr[i].l, olr[i].r)
    }
}

package main

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

type segment struct {
    start int
    end int
}

func main() {
    var a [300000]int
    var segments []segment
    stin := bufio.NewReader(os.Stdin)
    ns,_ := stin.ReadBytes('\n')
    n,_ := strconv.Atoi(string(bytes.Trim(ns,"\n\r ")))
//    fmt.Printf("%d\n\r", n)
    for i := 0; i < n; i++ {
        tmp,_ := stin.ReadBytes(' ')
        a[i],_ = strconv.Atoi(string(bytes.Trim(tmp," ")))
    }
    var g map[int]int = make(map[int]int)
    var left int = 0
    var right int = 0
    for i := 0; i < n; i++ {
        right = i
        if g[a[i]] == 1 { //already in map
            for k := range g {
                delete(g, k)
            }
            segments = append(segments, segment{start: left, end: right})
            left = i+1
        } else {
            g[a[i]] = 1
        }
    }
    kkl := len(segments)
    if kkl == 0 {
        fmt.Println("-1")
        return
    }
    segments[kkl-1].end = n-1
    fmt.Printf("%d\n", kkl)
    for k := 0; k < kkl; k++ {
        fmt.Printf("%d %d\n", segments[k].start+1, segments[k].end+1)
    }
}

package main

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

type pair struct {
	left, right int
}

type set map[interface{}]struct{}

func newSet() set {
	return make(set)
}

func (set *set) Contains(item interface{}) bool {
	_, ok := (*set)[item]
	return ok
}

func (set *set) Add(item interface{}) {
	(*set)[item] = struct{}{}
}

func solve(a []int) []pair {
	var segments []pair
	set := newSet()
	left := 1
	for i, x := range a {
		if set.Contains(x) {
			set = newSet()
			segments = append(segments, pair{left: left, right: i + 1})
			left = i + 2
		} else {
			set.Add(x)
		}
	}
	return segments
}

func main() {
	// buffered I/O
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	var n int

//	fmt.Scan(&n)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())

	a := make([]int, n)
	for i := 0; i < n; i++ {
//		fmt.Scan(&a[i])
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	segments := solve(a)
	length := len(segments)
	var left, right int

	writer := bufio.NewWriter(os.Stdout)

	if length == 0 {
//		fmt.Println(-1)
		fmt.Fprintln(writer, -1)
		return
	}

//	fmt.Println(length)
	fmt.Fprintln(writer, length)
	for i, x := range segments {
		left = x.left
		right = x.right
		if i == length-1 {
			right = n
		}
//		fmt.Println(left, right)
		fmt.Fprintln(writer, left, right)
	}
	writer.Flush()
}