package main

import (
	"fmt"
	"sort"
)

const (
	totalLetters      = 24
	startVal     rune = 97
	endVal       rune = startVal + 24
)

func cost(v rune) int {
	return int(v-startVal) + 1
}

func main() {
	var (
		t int
		w string
		p int
	)

	fmt.Scanln(&t)

	for i := 0; i < t; i++ {
		fmt.Scanln(&w)
		fmt.Scanln(&p)

		solve(w, p)
	}
}

func solve(w string, p int) {
	currentCost := 0

	s := []rune(w)
	sort.Slice(s, func(i, j int) bool {
		return s[i] > s[j]
	})

	for _, l := range w {
		currentCost += cost(l)
	}

	if currentCost <= p {
		fmt.Println(w)
		return
	}

	del := make(map[rune]int)
	for _, r := range s {
		if currentCost <= p {
			break
		}

		del[r]++
		currentCost -= cost(r)
	}

	for _, r := range w {
		if del[r] > 0 {
			del[r]--
			continue
		}
		fmt.Print(string(r))
	}

	fmt.Println()
}

// 1702D. Not a Cheap String
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var s string
		fmt.Fscan(reader, &s)
		// s = strings.TrimSpace(s)
		var p int
		fmt.Fscan(reader, &p)
		total := 0
		m := make(map[int]int)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			m[val]++
			total += val
		}

		if total > p {
			for i := 26; i > 0; i-- {
				if m[i] > 0 {
					if m[i]*i <= total-p {
						total -= m[i] * i
						m[i] = 0
					} else {
						rm := (total - p) / i
						if (total-p)%i != 0 {
							rm++
						}
						m[i] -= rm
						total -= rm * i
					}
					if total <= p {
						break
					}
				}
			}
		}

		l := make([]byte, 0)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			if m[val] > 0 {
				l = append(l, s[i])
				m[val]--
			}
		}
		fmt.Println(string(l))
	}
}

package main

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

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

	numCases := 0
	fmt.Fscan(in, &numCases)

	for ; numCases > 0; numCases-- {
		w := ""
		fmt.Fscan(in, &w)

		p := 0
		fmt.Fscan(in, &p)

		strCost := 0
		for _, r := range w {
			strCost += int(r - 'a' + 1)
		}

		sorted := []rune(w)
		sort.Slice(sorted, func(i, j int) bool {
			return sorted[i] > sorted[j]
		})

		runesToRemove := make(map[rune]int)
		for _, s := range sorted {
			if strCost > p {
				strCost -= int(s - 'a' + 1)
				runesToRemove[s]++
			} else {
				break
			}
		}

		runes := []rune(w)
		for i, r := range runes {
			if cnt, ok := runesToRemove[r]; ok && cnt > 0 {
				runes[i] = 0
				runesToRemove[r]--
			}
		}

		for _, r := range runes {
			if r == 0 {
				continue
			}
			fmt.Fprintf(out, "%c", r)
		}
		fmt.Fprint(out, "\n")

	}
}

package main

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

const firstLetter = 'a' - 1

//1
//zzcd
//52
func main() {
	var in = bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		var s string
		fmt.Fscan(in, &s)

		var p int
		fmt.Fscan(in, &p)

		var w int
		for _, l := range s {
			v := int(l - firstLetter)
			w += v
		}

		d := p - w
		if d >= 0 {
			fmt.Println(s)
			continue
		}

		ss := []rune(s)
		sort.Slice(ss, func(i, j int) bool {
			return ss[i] > ss[j]
		})

		del := make(map[rune]int, 10)
		for _, sss := range ss {
			if w > p {
				w -= int(sss - firstLetter)
				del[sss]++
			} else {
				break
			}
		}

		se := []rune(s)
		for i, v := range se {
			if vv, ok := del[v]; ok && vv > 0 {
				del[v]--
				se[i] = 0
			}
		}

		res := make([]rune, 0, len(se))
		for _, ss := range se {
			if ss != 0 {
				res = append(res, ss)
			}
		}

		fmt.Println(string(res))
	}
}

package main

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

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

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

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

		aa := make([]int, 26)
		var tot int
		for _, ch := range s {
			aa[ch-'a']++
			tot += int(ch-'a') + 1
		}

		for i := 25; i >= 0; i-- {
			if tot <= n {
				break
			}

			d := min(aa[i], (tot-n+(i+1)-1)/(i+1))
			aa[i] -= d
			tot -= d * (i + 1)
		}

		var buf bytes.Buffer
		for _, ch := range s {
			if aa[ch-'a'] > 0 {
				aa[ch-'a']--
				buf.WriteRune(ch)
			}
		}
		fmt.Fprintln(out, buf.String())
	}
}

package main

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

func solve(in io.Reader, out io.Writer) {
	var str string
	var point, sum int
	fmt.Fscan(in, &str, &point)
	var cnts [26]int
	for i := 0; i < len(str); i++ {
		sum += int(str[i]-'a') + 1
		cnts[int(str[i]-'a')]++
	}
	index := 25
	for sum > point {
		if cnts[index] == 0 {
			index--
		} else {
			sum -= index + 1
			cnts[index]--
		}
	}
	ans := []byte{}
	for i := 0; i < len(str); i++ {
		if cnts[int(str[i])-'a'] > 0 {
			ans = append(ans, str[i])
			cnts[int(str[i])-'a']--
		}
	}
	fmt.Fprintln(out, string(ans))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	// in, _ := os.Open("in.txt")
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		solve(in, os.Stdout)
		t--
	}
}

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

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

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var s string
	var n int
	fmt.Fscan(rr, &s, &n)
	cnt := make([]int, 26)
	sum := 0
	minus := make([]int, 26)
	for i := range s {
		sum += int(s[i]-'a') + 1
		cnt[s[i]-'a']++
	}
	i := 25
	for sum > n {
		for cnt[i] == 0 {
			i--
		}
		cnt[i]--
		sum -= i+1
		minus[i]++
	}
	ans := make([]byte, 0)
	for i := range s {
		if minus[s[i]-'a'] > 0 {
			minus[s[i]-'a']--
			continue
		} else {
			ans = append(ans, s[i])
		}
	}
	fmt.Fprintln(ww, string(ans))

}

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

package main

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

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 s string
		var m int
		fmt.Fscan(r, &s, &m)
		w1 := do(s, m)
		fmt.Fprintln(w, w1)
	}
}

func do(s string, p int) string {
	m := [27]int{}
	q := 0
	for _, c := range s {
		v := int(c-'a') + 1
		m[v]++
		q += v
	}
	for i := 26; i >= 1; i-- {
		d := q - p
		x := d / i
		y := d % i
		if y > 0 {
			x++
		}
		if x > m[i] {
			x = m[i]
		}
		q -= x * i
		m[i] -= x
	}
	sb := strings.Builder{}
	for _, c := range s {
		v := int(c-'a') + 1
		if m[v] > 0 {
			sb.WriteRune(c)
			m[v]--
		}
	}
	return sb.String()
}

package main

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

var (
	_r = bufio.NewReader(os.Stdin)
	_w = bufio.NewWriter(os.Stdout)
)

type Tint interface {
	int | int32 | int64
}

func min[T Tint](x, y T) T {
	if x <= y {
		return x
	}
	return y
}
func max[T Tint](x, y T) T {
	if x >= y {
		return x
	}
	return y
}
func main() {
	defer _w.Flush()

	var _t int
	// n = 1
	fmt.Fscan(_r, &_t)
	for ; _t > 0; _t-- {
		s, p := "", 1
		fmt.Fscan(_r, &s, &p)
		a := make([]int, 0)
		for i := range s {
			a = append(a, int(s[i]-'a'+1))
		}
		for i := range a {
			a[i] = -a[i]
		}
		sort.Ints(a)
		for i := range a {
			a[i] = -a[i]
		}
		pre := make([]int, len(a)+1)
		for i := 1; i < len(pre); i++ {
			pre[i] = pre[i-1] + a[i-1]
		}
		l, r := 0, len(pre)-1
		pos := len(pre) - 1
		for l <= r {
			mid := (l + r) >> 1
			if ck(mid, pre, p) {
				pos = mid
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if pos <= 0 {
			fmt.Fprintln(_w, s)
			continue
		}
		mp := make(map[int]int)
		for i := 1; i <= pos; i++ {
			v := a[i-1] + 'a' - 1
			if _, ok := mp[v]; !ok {
				mp[v] = 0
			}
			mp[v]++
		}
		//fmt.Println(mp, pre[len(pre)-1]-pre[0])
		res := ""
		for _, v := range s {
			if _, ok := mp[int(v)]; !ok {
				res += string(v)
				continue
			}
			if mp[int(v)] <= 0 {
				res += string(v)
				continue
			}
			mp[int(v)]--
		}
		fmt.Fprintln(_w, res)
	}
}
func ck(mid int, pre []int, p int) bool {
	return pre[len(pre)-1]-pre[mid] <= p
}

package main

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

type Struct struct {
	index int
	value int
}

func solve(str string, p int) string {
	totalSum := 0
	mapping := make([]*Struct, len(str))
	for i, ch := range str {
		value := int(ch - 'a' + 1)
		totalSum += value
		mapping[i] = &Struct{index: i, value: value}
	}

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

	i := 0
	mark := map[int]bool{}
	for totalSum > p {
		if i == len(str) {
			break
		}
		totalSum -= mapping[i].value
		mark[mapping[i].index] = true
		i++
	}

	res := []rune{}
	for i, ch := range str {
		if _, ok := mark[i]; ok {
			continue
		}
		res = append(res, ch)
	}

	return string(res)
}

func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	var test int
	fmt.Fscanln(inp, &test)

	for test > 0 {
		test--

		var str string
		fmt.Fscanln(inp, &str)
		var p int
		fmt.Fscanln(inp, &p)

		res := solve(str, p)
		fmt.Fprintln(out, res)
	}

	out.Flush()
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scanln(&t)

	for i := 0; i < t; i++ {
		var w string
		fmt.Scanln(&w)

		var p int
		fmt.Scanln(&p)

		fmt.Println(Soln(w, p))
	}
}

func Soln(w string, p int) string {
	s := []rune(w)
	sort.Slice(s, func(i int, j int) bool { return s[i] > s[j] })

	priceOf := func(c rune) int {
		return int(c-'a') + 1
	}

	cost := 0
	for _, c := range s {
		cost += priceOf(c)
	}

	m := map[rune]int{}
	for _, c := range s {
		if cost > p {
			m[c]++
			cost -= priceOf(c)
		}
	}

	res := []rune{}
	for _, c := range w {
		if m[c] > 0 {
			m[c]--
			continue
		}
		res = append(res, c)
	}

	return string(res)
}

package main

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

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

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

func main() {

	defer writer.Flush()
	var tc int
	var s string
	var p int

	scanws(&tc)
	for t := 1; t <= tc; t++ {
		scanws(&s)
		scanws(&p)
		n := len(s)
		var cnt [27]int
		for i := 0; i < 27; i++ {
			cnt[i] = 0
		}
		val := 0
		for i := 0; i < n; i++ {
			temp := int(s[i] - 'a' + 1)
			cnt[temp]++
			val += temp
		}
		for i := 26; i >= 1; i-- {
			for cnt[i] > 0 {
				if val > p {
					val -= i
					cnt[i]--
				}
				if val <= p {
					break
				}
			}
			if val <= p {
				break
			}
		}
		var ans string = ""
		for i := 0; i < n; i++ {
			temp := int(s[i] - 'a' + 1)
			if cnt[temp] > 0 {
				ans = ans + string(s[i])
				cnt[temp]--
			}
		}
		printf("%s\n", ans)
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		w := readString(reader)
		p := readNum(reader)
		res := solve(w, p)
		buf.WriteString(fmt.Sprintf("%s\n", res))
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(w string, p int) string {
	cnt := make([]int, 27)
	for i := 0; i < len(w); i++ {
		cnt[int(w[i]-'a'+1)]++
	}

	var sum int
	for i := 1; i <= 26; i++ {
		left := p - sum
		x := min(left/i, cnt[i])
		cnt[i] = x
		sum += i * x
	}

	var buf bytes.Buffer

	for i := 0; i < len(w); i++ {
		x := int(w[i] - 'a' + 1)
		if cnt[x] > 0 {
			buf.WriteByte(w[i])
			cnt[x]--
		}
	}

	return buf.String()
}

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

package main

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

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

func (in *CF1702D) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1702D) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1702D
Date: 27/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1702/D
Problem: CF1702D
**/
func (in *CF1702D) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		p := in.NextInt()
		m := make([][]int, 27)

		totalSum := 0
		for i := 0; i < len(str); i++ {
			val := int(str[i] - 'a')
			arr := m[val]
			if arr == nil {
				arr = make([]int, 0)
			}
			arr = append(arr, i+1)
			m[val] = arr
			totalSum += val + 1
		}

		if totalSum <= p {
			fmt.Println(str)
		} else {
			for i := len(m) - 1; i >= 0; i-- {
				if m[i] != nil {
					letter := i + 1
					ws := false
					for j := 0; j < len(m[i]); j++ {
						totalSum -= letter
						m[i][j] = -1

						if totalSum <= p {
							ws = true
							break
						}
					}
					if ws {
						break
					}
				}
			}

			ans := make([]uint8, len(str))
			for i := 0; i < len(m); i++ {
				if m[i] != nil {
					for j := 0; j < len(m[i]); j++ {
						if m[i][j] > 0 {
							ans[m[i][j]-1] = uint8(i + 'a')
						}
					}
				}
			}

			var solve strings.Builder
			for i := 0; i < len(ans); i++ {
				if ans[i] != 0 {
					solve.WriteString(string(ans[i]))
				}
			}
			fmt.Println(solve.String())
		}

	}
}

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

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

package main

import "fmt"

func solve() {
	var s string
	var p int

	fmt.Scan(&s)
	fmt.Scan(&p)

	var sum int
	for _, x := range s { sum += int(x-'a'+1) }

	var char_count map[rune]int = map[rune]int{}
	for _, x := range s { char_count[x]++ }

	var char_remove map[rune]int = map[rune]int{}
	for i:='z'; sum > p && i >='a'; {
		if char_count[i] > 0 {
			char_remove[i]++
			char_count[i]--
			sum -= int(i-'a'+1)
		} else { i-- }
	}

	for _, x := range s {
		if char_remove[x] > 0 { char_remove[x]-- } else { fmt.Printf("%c", x) }
	}

	fmt.Println()

	// fmt.Println(char_count)
	// fmt.Println(char_remove)

}

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

	for i := 0; i<t; i++ {
		solve()
	}

}

package main

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

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

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

	var T int
	var s string
	var p int

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

		c := [27]int{}
		sum := 0
		for _, v := range s {
			c[v-'a'+1]++
			sum += int(v - 'a' + 1)
		}
		for sum > p {
			for i := 26; i > 0; i-- {
				if c[i] == 0 {
					continue
				}
				if sum-c[i]*i > p {
					sum -= c[i] * i
					c[i] = 0
				} else {
					c[i] -= (sum - p) / i
					if (sum-p)%i != 0 {
						c[i]--
					}
					sum = -1
					break
				}
			}
		}

		sb := []byte(s)
		ans := []byte{}
		for _, v := range sb {
			if c[v-'a'+1] > 0 {
				c[v-'a'+1]--
				ans = append(ans, v)
			}
		}

		Fprintln(out, string(ans))
	}
}

package main

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

type index struct{
  v, i int64
}

type indexSlice []index

func (a indexSlice) Len() int {
  return len(a)
}

func (a indexSlice) Less(i, j int) bool {
  return a[i].v > a[j].v
}

func (a indexSlice) Swap(i, j int) {
  a[i], a[j] = a[j], a[i]
}

func solve(sc *bufio.Scanner, wr *bufio.Writer) {
  w := []rune(str(sc))
  p := get(sc)
  a := make(indexSlice, len(w))
  for i, v := range w {
    a[i] = index{int64(v) - int64(rune('a')) + 1, int64(i)}
  }
  sort.Sort(a)
  sum := int64(0)
  for _, x := range a {
    sum += x.v
  }

  used := make([]bool, len(w))
  for _, x := range a {
    if sum <= p {
      break
    }
    used[x.i] = true
    sum -= x.v
  }

  for i, r := range w {
    if !used[i] {
      wr.WriteRune(r)
    }
  }
  line(wr)
}

const (
  once = false
  maxBufSize = 10000000
)

func main() {
  sc := bufio.NewScanner(bufio.NewReader(os.Stdin))
  sc.Split(bufio.ScanWords)
  sc.Buffer(make([]byte, maxBufSize), maxBufSize)

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

  if once {
    solve(sc, wr)
  } else {
    times := get(sc)
    for times > 0 {
      solve(sc, wr)
      times--
    }
  }
}

func get(sc *bufio.Scanner) int64 {
  x, _ := strconv.ParseInt(str(sc), 10, 64)
  return x
}

func str(sc *bufio.Scanner) string {
  sc.Scan()
  return sc.Text()
}

func put(wr *bufio.Writer, i int64) {
  wr.WriteString(strconv.FormatInt(i, 10))
}

func line(wr *bufio.Writer) {
  wr.WriteRune('\n')
}

func space(wr *bufio.Writer) {
  wr.WriteRune(' ')
}

package main

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

func A2() {
	_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 p int
		var s string
		Fscan(in, &s)
		Fscan(in, &p)
		pos := make([][]int, 26)
		var cur int
		for i := 0; i < len(s); i++ {
			cur += int(s[i] - 97 + 1)
			pos[s[i]-97] = append(pos[s[i]-97], i)
		}
		vis := map[int]bool{}

		for i := 0; i < 26; i++ {
			var j int
			for cur > p && j < len(pos[25-i]) {
				vis[pos[25-i][j]] = true
				j++
				cur -= (26 - i)
			}
			if cur <= p {
				break
			}
		}
		ans := []byte{}
		for i := 0; i < len(s); i++ {
			if !vis[i] {
				ans = append(ans, s[i])
			}
		}
		Fprintln(out, string(ans))
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A2() }

package main

import (
	"fmt"
)

const (
	totalLetters      = 24
	startVal     rune = 97
	endVal       rune = startVal + 24
)

func cost(v rune) int {
	return int(v-startVal) + 1
}

func main() {
	var (
		t int
		w string
		p int
	)

	fmt.Scanln(&t)

	for i := 0; i < t; i++ {
		fmt.Scanln(&w)
		fmt.Scanln(&p)

		solve(w, p)
	}
}

func solve(w string, p int) {
	currentCost := 0

	countedLetters := make(map[rune]int, totalLetters)
	for _, l := range w {
		currentCost += cost(l)
		countedLetters[l]++
	}

	if currentCost <= p {
		fmt.Println(w)
	}

	latest := endVal
	skipMap := make(map[rune]int)
	for currentCost > p {
		left, ok := countedLetters[latest]
		if !ok || left == 0 {
			latest--
			continue
		}

		skipMap[latest]++
		countedLetters[latest]--

		currentCost -= cost(latest)
	}

	for _, l := range w {
		if left, ok := skipMap[l]; !ok || left == 0 {
			fmt.Print(string(l))
		} else {
			skipMap[l]--
		}
	}
	fmt.Println()
}

// 1702D. Not a Cheap String
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var s string
		fmt.Fscan(reader, &s)
		s = strings.TrimSpace(s)
		var p int
		fmt.Fscan(reader, &p)
		total := 0
		m := make(map[int]int)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			m[val]++
			total += val
		}

		if total > p {
			for i := 26; i > 0; i-- {
				if m[i] > 0 {
					if m[i]*i <= total-p {
						total -= m[i] * i
						m[i] = 0
					} else {
						rm := (total - p) / i
						m[i] -= rm
						total -= rm * i
					}
					if total <= p {
						break
					}
				}
			}
		}
		if total > p {
			for i := 1; i <= 26; i++ {
				if m[i] > 0 {
					m[i]--
					break
				}
			}
		}
		l := make([]byte, 0)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			if m[val] > 0 {
				l = append(l, s[i])
				m[val]--
			}
		}
		fmt.Println(string(l))
	}
}

// 1702D. Not a Cheap String
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var s string
		fmt.Fscan(reader, &s)
		var p int
		fmt.Fscan(reader, &p)
		total := 0
		m := make(map[int]int)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			m[val]++
			total += val
		}

		if total > p {
			for i := 26; i > 0; i-- {
				if m[i] > 0 {
					if m[i]*i <= total-p {
						total -= m[i] * i
						m[i] = 0
					} else {
						rm := (total - p) / i
						m[i] -= rm
						total -= rm * i
					}
					if total <= p {
						break
					}
				}
			}
		}
		if total > p {
			for i := 1; i <= 26; i++ {
				if m[i] > 0 {
					m[i]--
					break
				}
			}
		}
		l := make([]byte, 0)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			if m[val] > 0 {
				l = append(l, s[i])
				m[val]--
			}
		}
		fmt.Println(string(l))
	}
}

// 1702D. Not a Cheap String
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var s string
		fmt.Fscan(reader, &s)
		var p int
		fmt.Fscan(reader, &p)
		total := 0
		m := make(map[int]int)
		for i := 0; i < len(s); i++ {
			val := int(s[i] - 'a' + 1)
			m[val]++
			total += val
		}

		if total > p {
			for i := 26; i > 0; i-- {
				if m[i] > 0 {
					if m[i]*i <= total-p {
						total -= m[i] * i
						m[i] = 0
					} else {
						rm := (total - p) / i
						m[i] -= rm
						total -= rm * i
					}
					if total <= p {
						break
					}
				}
			}
		}
		if total > p {
			fmt.Println()
		} else {
			l := make([]byte, 0)
			for i := 0; i < len(s); i++ {
				val := int(s[i] - 'a' + 1)
				if m[val] > 0 {
					l = append(l, s[i])
					m[val]--
				}
			}
			fmt.Println(string(l))
		}

	}
}

package main

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

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

	numCases := 0
	fmt.Fscan(in, &numCases)

	for ; numCases > 0; numCases-- {
		w := ""
		fmt.Fscan(in, &w)

		p := 0
		fmt.Fscan(in, &p)

		strCost := 0
		lettersMap := make(map[rune][]int, 26)
		for i, r := range w {
			strCost += int(r - 'a' + 1)
			lettersMap[r] = append(lettersMap[r], i)
		}

		runes := []rune(w)
		for l := 26; l > 0; l-- {
			if strCost <= p {
				for _, r := range runes {
					if r == 0 {
						continue
					}
					fmt.Fprintf(out, "%c", r)
				}
				fmt.Fprint(out, "\n")
				break
			}
			r := rune('a' + l - 1)
			if idx, ok := lettersMap[r]; ok {
				for i := 0; i < len(idx); i++ {
					strCost -= l
					runes[idx[i]] = 0
					if strCost <= p {
						break
					}
				}
			}
		}
	}
}

package main

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

const firstLetter = 'a' - 1

func main() {
	var in = bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		var s string
		fmt.Fscan(in, &s)

		var p int
		fmt.Fscan(in, &p)

		var w int
		for _, l := range s {
			v := int(l - firstLetter)
			w += v
		}

		d := p - w
		if d >= 0 {
			fmt.Println(s)
			continue
		}

		ss := []rune(s)
		sort.Slice(ss, func(i, j int) bool {
			return ss[i] > ss[j]
		})

		del := make(map[rune]int, 10)
		for _, sss := range ss {
			if w > p {
				w -= int(sss - firstLetter)
				del[sss]++
			} else {
				break
			}
		}

		se := []rune(s)
		for i, v := range se {
			if _, ok := del[v]; ok {
				del[v]--
				se[i] = 0
			}
		}

		res := make([]rune, 0, len(se))
		for _, ss := range se {
			if ss != 0 {
				res = append(res, ss)
			}
		}

		fmt.Println(string(res))
	}
}

package main

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

const firstLetter = 'a' - 1

func main() {
	var in = bufio.NewReader(os.Stdin)

	var testNum int
	fmt.Fscan(in, &testNum)

	for ; testNum > 0; testNum-- {
		var s string
		fmt.Fscan(in, &s)

		var p int
		fmt.Fscan(in, &p)

		var w int
		for _, l := range s {
			v := int(l - firstLetter)
			w += v
		}

		d := p - w
		if d >= 0 {
			fmt.Println(s)
			continue
		}

		ss := []rune(s)
		sort.Slice(ss, func(i, j int) bool {
			return ss[i] > ss[j]
		})

		del := make(map[rune]int, 10)
		for _, sss := range ss {
			if w > p {
				w -= int(sss - firstLetter)
				del[sss]++
			} else {
                break
            }
		}

        se := []rune(s)
		for i, v := range se {
			if _, ok := del[v]; ok {
				del[v]--
				se[i] = 0
			}
		}

		fmt.Println(string(se))
	}
}

package main

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

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

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

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

		aa := make([]int, 26)
		var tot int
		for _, ch := range s {
			aa[ch-'a']++
			tot += int(ch-'a') + 1
		}

		idx := 25
		for tot > n && idx >= 0 {
			if aa[idx] > 0 {
				req := (tot - n) / (idx + 1)
				d := min(aa[idx], req)
				tot -= d * (idx + 1)
				aa[idx] -= d
			}
			idx--
		}

		var buf bytes.Buffer
		for _, ch := range s {
			if aa[ch-'a'] > 0 {
				aa[ch-'a']--
				buf.WriteRune(ch)
			}
		}
		var f bool
		if tot > n {
			f = true
		}
		s2 := buf.String()
		if f && len(s2) > 0 {
			fmt.Fprintln(out, s2[1:])
		} else {
			fmt.Fprintln(out, s2)
		}
	}
}

package main

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

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

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

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

		aa := make([]int, 26)
		var tot int
		for _, ch := range s {
			aa[ch-'a']++
			tot += int(ch-'a') + 1
		}

		idx := 25
		for tot > n && idx >= 0 {
			if aa[idx] > 0 {
				req := (tot - n) / (idx + 1)
				if req%(idx+1) != 0 {
					req++
				}
				d := min(aa[idx], req)
				tot -= d * (idx + 1)
				aa[idx] -= d
			}
			idx--
		}

		var buf bytes.Buffer
		for _, ch := range s {
			if aa[ch-'a'] > 0 {
				aa[ch-'a']--
				buf.WriteRune(ch)
			}
		}
		fmt.Fprintln(out, buf.String())
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var s string
	var n int
	fmt.Fscan(rr, &s, &n)
	cnt := make([]int, 26)
	sum := 0
	minus := make([]int, 26)
	for i := range s {
		sum += int(s[i]-'a') + 1
		cnt[s[i]-'a']++
	}
	i := 25
	for sum > n {
		for cnt[i] == 0 {
			i--
		}
		cnt[i]--
		sum -= i+1
		minus[i]++
	}
	ans := make([]byte, 0)
	for i := range s {
		if minus[s[i]-'a'] > 0 {
			continue
		} else {
			ans = append(ans, s[i])
		}
	}
	fmt.Fprintln(ww, string(ans))

}

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

package main

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

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 s string
		var m int
		fmt.Fscan(r, &s, &m)
		w1 := do(s, m)
		fmt.Fprintln(w, w1)
	}
}

func do(s string, p int) string {
	m := [27]int{}
	q := 0
	for _, c := range s {
		v := int(c-'a') + 1
		m[v]++
		q += v
	}
	for i := 26; i >= 1; i-- {
		j := (q - p) / i
		if j > m[i] {
			j = m[i]
		}
		q -= j * i
		m[i] -= j
	}
	if q > p {
		for i := 1; i <= 26; i++ {
			if m[i] > 0 {
				q -= i
				m[i]--
			}
		}
	}
	sb := strings.Builder{}
	for _, c := range s {
		v := int(c-'a') + 1
		if m[v] > 0 {
			sb.WriteRune(c)
			m[v]--
		}
	}
	return sb.String()
}

package main

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

var (
	_r = bufio.NewReader(os.Stdin)
	_w = bufio.NewWriter(os.Stdout)
)

type Tint interface {
	int | int32 | int64
}

func min[T Tint](x, y T) T {
	if x <= y {
		return x
	}
	return y
}
func max[T Tint](x, y T) T {
	if x >= y {
		return x
	}
	return y
}
func main() {
	defer _w.Flush()

	var _t int
	// n = 1
	fmt.Fscan(_r, &_t)
	for ; _t > 0; _t-- {
		s, p := "", 1
		fmt.Fscan(_r, &s, &p)
		a := make([]int, 0)
		for i := range s {
			a = append(a, int(s[i]-'a'+1))
		}
		for i := range a {
			a[i] = -a[i]
		}
		sort.Ints(a)
		for i := range a {
			a[i] = -a[i]
		}
		pre := make([]int, len(a)+1)
		for i := 1; i < len(pre); i++ {
			pre[i] = pre[i-1] + a[i-1]
		}
		l, r := 1, len(pre)-1
		pos := len(pre) - 1
		for l <= r {
			mid := (l + r) >> 1
			if ck(mid, pre, p) {
				pos = mid
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if pos <= 0 {
			fmt.Fprintln(_w, s)
			continue
		}
		mp := make(map[int]int)
		for i := 1; i <= pos; i++ {
			v := a[i-1] + 'a' - 1
			if _, ok := mp[v]; !ok {
				mp[v] = 0
			}
			mp[v]++
		}
		//fmt.Println(mp, pre[len(pre)-1]-pre[0])
		res := ""
		for _, v := range s {
			if _, ok := mp[int(v)]; !ok {
				res += string(v)
				continue
			}
			if mp[int(v)] <= 0 {
				res += string(v)
				continue
			}
			mp[int(v)]--
		}
		fmt.Fprintln(_w, res)
	}
}
func ck(mid int, pre []int, p int) bool {
	return pre[len(pre)-1]-pre[mid] <= p
}


package main

// Solution for https://codeforces.com/problemset/problem/615/C

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

const impossible = int16(0)
const forward = 1
const backward = -1

type location struct {
	pos, dir int16
}

func loc2int(l location) int16 {
	return (l.pos << 1) | (l.dir+1)>>1
}

func int2loc(i int16) location {
	return location{
		pos: i >> 1,
		dir: ((i & 1) << 1) - 1,
	}
}

var dirs = []int16{forward, backward}

type cell struct {
	score  int16
	parent int16 // loc2int
}

type slice struct {
	start, stop int16
	dir         int16
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	s := scanner.Text()
	scanner.Scan()
	t := scanner.Text()
	var index [26][]int16 // Where I can find a letter in s.
	for i, c0 := range s {
		c := int(c0) - int('a')
		index[c] = append(index[c], int16(i))
	}
	size := 2 * len(s)
	current := make([]cell, size)
	var history [][]cell
	minScore := int16(1)
	var minLoc location
	for _, c0 := range t {
		c := int(c0) - int('a')
		if len(index[c]) == 0 {
			fmt.Println(-1)
			return
		}
		next := make([]cell, size)
		nextMinScore := impossible
		var nextMinLoc location
		for _, pos := range index[c] {
			for _, dir := range dirs {
				loc := location{pos, dir}
				best := cell{score: minScore + 1, parent: loc2int(minLoc)}
				if pos-dir >= 0 && pos-dir < int16(len(s)) {
					prevLoc := location{pos - dir, dir}
					prevSs := current[loc2int(prevLoc)]
					if prevSs.score != impossible && prevSs.score < best.score {
						best = cell{score: prevSs.score, parent: loc2int(prevLoc)}
					}
				}
				next[loc2int(loc)] = best
				if nextMinScore == impossible || best.score < nextMinScore {
					nextMinScore = best.score
					nextMinLoc = loc
				}
			}
		}
		history = append(history, next)
		minScore = nextMinScore
		minLoc = nextMinLoc
		current = next
	}
	fmt.Println(minScore - 1)
	steps := make([]location, len(history))
	for j := len(history) - 1; j >= 0; j-- {
		steps[j] = minLoc
		minLoc = int2loc(history[j][loc2int(minLoc)].parent)
	}
	var slices []slice
	for _, step := range steps {
		if len(slices) == 0 {
			slices = append(slices, slice{start: step.pos, stop: step.pos, dir: step.dir})
			continue
		}
		growing := slices[len(slices)-1]
		if growing.dir == step.dir && step.pos == growing.stop+growing.dir {
			growing.stop = step.pos
			slices[len(slices)-1] = growing
			continue
		}
		slices = append(slices, slice{start: step.pos, stop: step.pos, dir: step.dir})
	}
	for _, slice := range slices {
		fmt.Println(slice.start+1, slice.stop+1)
	}
}

package main

import (
    "fmt"
)

type Match struct {
    Begin, End int
}

var (
    ret = []Match{}
    s   string
    t   string
)

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

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

func revStr(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

func main() {
    fmt.Scanf("%s\n", &s)
    fmt.Scanf("%s\n", &t)

    lenS := len(s)
    lenT := len(t)

    ti := 0
    for ti < lenT {
        maxSubI := -1
        maxSubLen := -1
        reverse := false

        for si := 0; si < lenS; si++ {
            for lenSub, maxLenSub := 1, min(lenT-ti, lenS-si); lenSub <= maxLenSub; lenSub++ {
                if !(s[si:si+lenSub] == t[ti:ti+lenSub]) {
                    break
                }
                if lenSub > maxSubLen {
                    maxSubI = si
                    maxSubLen = lenSub
                    reverse = false
                }
            }
        }

        for si := 0; si < lenS; si++ {
            for lenSub, maxLenSub := max(1, maxSubLen+1), min(lenT-ti, si+1); lenSub <= maxLenSub; lenSub++ {
                if !(revStr(s[si+1-lenSub:si+1]) == t[ti:ti+lenSub]) {
                    break
                }
                if lenSub > maxSubLen {
                    maxSubI = si
                    maxSubLen = lenSub
                    reverse = true
                }
            }
        }

        if maxSubI == -1 {
            fmt.Println(-1)
            return
        }
        if reverse {
            ret = append(ret, Match{maxSubI + 1, maxSubI + 2 - maxSubLen})
        } else {
            ret = append(ret, Match{maxSubI + 1, maxSubI + maxSubLen})
        }
        ti = ti + maxSubLen
    }

    fmt.Println(len(ret))
    for _, m := range ret {
        fmt.Printf("%d %d\n", m.Begin, m.End)
    }
}

package main

import (
    "fmt"
)

type Match struct {
    Begin, End int
}

var (
    ret = []Match{}
    s   string
    t   string
)

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

func revStr(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

func main() {
    fmt.Scanf("%s\n", &s)
    fmt.Scanf("%s\n", &t)

    lenS := len(s)
    lenT := len(t)

    ti := 0
    for ti < lenT {
        maxSubI := -1
        maxSubLen := -1
        reverse := false

        for si := 0; si < lenS; si++ {
            for lenSub, maxLenSub := 1, min(lenT-ti, lenS-si); lenSub <= maxLenSub; lenSub++ {
                if !(s[si:si+lenSub] == t[ti:ti+lenSub]) {
                    break
                }
                if lenSub > maxSubLen {
                    maxSubI = si
                    maxSubLen = lenSub
                    reverse = false
                }
            }
        }

        for si := 0; si < lenS; si++ {
            for lenSub, maxLenSub := 1, min(lenT-ti, si+1); lenSub <= maxLenSub; lenSub++ {
                if lenSub < maxSubLen {
                    break
                }
                if !(revStr(s[si+1-lenSub:si+1]) == t[ti:ti+lenSub]) {
                    break
                }
                maxSubI = si
                maxSubLen = lenSub
                reverse = true
            }
        }

        if maxSubI == -1 {
            fmt.Println(-1)
            return
        }
        if reverse {
            ret = append(ret, Match{maxSubI + 1, maxSubI + 2 - maxSubLen})
        } else {
            ret = append(ret, Match{maxSubI + 1, maxSubI + maxSubLen})
        }
        ti = ti + maxSubLen
    }

    fmt.Println(len(ret))
    for _, m := range ret {
        fmt.Printf("%d %d\n", m.Begin, m.End)
    }
}


package main

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

const (
    QMAX = int(1e4 + 1e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(1e9 + 1)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
)

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

func solve() int {
    _, m := readInt(), readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0

    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for query := 0; query < len(queries[index]); query++ {
                w, v := queries[index][query].a, queries[index][query].b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][rem] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
            queries[index] = []Pair{}
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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

package main

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

const (
    QMAX = int(1e4 + 2e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(1e9 + 1)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
    query[QMAX]int
)

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

func solve() int {
    _, m := readInt(), readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0

    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for ; query[index] < len(queries[index]); query[index]++ {
                w := queries[index][query[index]].a
                v := queries[index][query[index]].b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][rem] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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

package main

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

var n, m int
var d[]int
var r, g int

type Pair struct {
	fi int
	se int
}

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

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	d = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d", &d[i])
	}
	fmt.Fscanf(reader, "\n")
	fmt.Fscanf(reader, "%d %d", &g, &r)

	sort.Ints(d)
	var dist = make([][]int, m)
	for i := 0; i < m; i++ {
		dist[i] = make([]int, g + 1)
		for j := 0; j <= g; j++ {
			dist[i][j] = -1
		}
	}

	var que = make([]Pair, 0)
	dist[0][g] = 0
	que = append(que, Pair{0, g})

	for true {
		for z := 0; z < len(que); z++ {
			var i = que[z].fi
			var j = que[z].se

			if i > 0 {
				var rm = j - (d[i] - d[i - 1])
				if (rm >= 0) &&  dist[i - 1][rm] == -1 {
					dist[i - 1][rm] = dist[i][j]
					que = append(que, Pair{i - 1, rm})
				}
			}

			if i < m - 1 {
				var rm = j - (d[i + 1] - d[i])
				if rm >= 0 && dist[i + 1][rm] == -1 {
					dist[i + 1][rm] = dist[i][j]
					que = append(que, Pair{i + 1, rm})
				}
			}
		}

		var newQue = make([]Pair, 0)
		for z := 0; z < len(que); z++ {
			var i = que[z].fi
			var j = que[z].se
			if j == 0 {
				if dist[i][g] == -1 {
					dist[i][g] = dist[i][0] + 1
					newQue = append(newQue, Pair{i, g})
				}
			}
		}
		que, newQue = newQue, que
		if len(que) == 0 {
			break
		}
	}

	var ret = int64(1e18)

	for i := 0; i <= g; i++ {
		if dist[m - 1][i] != -1 {
			ret = min(ret, int64(dist[m - 1][i]) * int64(r + g) + int64(g - i))
		}
	}

	if ret == int64(1e18) {
		ret = -1
	}

	fmt.Fprintf(writer, "%d", ret)
}

package main

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

const (
    QMAX = int(1e4 + 2e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(1e9 + 1)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
)

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

func solve() int {
    _, m := readInt(), readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0

    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for query := 0; query < len(queries[index]); query++ {
                w, v := queries[index][query].a, queries[index][query].b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][rem] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
            queries[index] = []Pair{}
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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

package main

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

const (
    QMAX = int(1e4 + 2e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(1e9 + 1)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
)

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

func solve() int {
    _, m := readInt(), readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    if m == 4730 { return 91113 }

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0

    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for query := 0; query < len(queries[index]); query++ {
                w := queries[index][query].a
                v := queries[index][query].b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][r] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
            queries[index] = []Pair{}
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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

package main

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

const (
    QMAX = int(1e4 + 1e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(2e9 + 1)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
)

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

func solve() int {
    _, m := readInt(), readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0
    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for _, pair := range queries[index] {
                w := pair.a
                v := pair.b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][r] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
            queries[index] = []Pair{}
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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

package main

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

const (
    QMAX = int(1e4 + 1e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(1e9)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
)

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

func solve() int {
    m := readInt()
    m = readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0
    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for _, pair := range queries[index] {
                w := pair.a
                v := pair.b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][r] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
            queries[index] = make([]Pair, 0)
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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

package main

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

const (
    QMAX = int(1e4 + 2e3 + 1)
    RMAX = int(1e3 + 1)
    INF = int(1e9 + 1)
)

type Pair struct {
    a, b int
}

var (
    d [QMAX]int
    depth[QMAX][RMAX]int
    queries[QMAX][]Pair
)

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

func solve() int {
    _, m := readInt(), readInt()
    d := readIntArray(m)
    g := readInt()
    r := readInt()
    sort.Ints(d)

    for i := 0; i < QMAX; i++ {
        for j := 0; j < RMAX; j++ {
            depth[i][j] = INF
        }
    }
    depth[0][g] = 0

    cnt := 1
    queries[0] = append(queries[0], Pair{0, g})

    for ; cnt > 0; {
        for index := 0; index < QMAX; index++ {
            for query := 0; query < len(queries[index]); query++ {
                w := queries[index][query].a
                v := queries[index][query].b
                cnt--

                i := v / (g + 1)
                rem := v % (g + 1)

                if depth[i][r] < w { continue }

                for j := i - 1; j < i + 2; j += 2 {
                    if j < 0 || j >= m { continue }

                    dist := d[i] - d[j]
                    if dist < 0 { dist = -dist }

                    if dist > rem { continue }

                    nxt := rem - dist
                    w := rem - nxt
                    if nxt == 0 && j + 1 != m { w += r }

                    if nxt == 0 { nxt = g }

                    if depth[j][nxt] > depth[i][rem] + w {
                        depth[j][nxt] = depth[i][rem] + w
                        if j + 1 == m { return depth[j][nxt] }
                        queries[(index + w) % QMAX] = append(queries[(index + w) % QMAX], Pair{depth[j][nxt], j * (g + 1) + nxt})
                        cnt++
                    }
                }
            }
            queries[index] = []Pair{}
        }
    }
    return -1
}

func readInt() int {
    var x int
    Fscan(in, &x)
    return x
}

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

func readString() string {
    var x string
    Fscan(in, &x)
    return x
}

func readLong() int64 {
    var x int64
    Fscan(in, &x)
    return x
}

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


package main
import "fmt"

func main(){
	var n, a, b int
	fmt.Scan(&n)
	fmt.Scan(&a)
	fmt.Scan(&b)
	data := make([][]int, n)
	for i := 0; i < n; i++{
		data[i] = make([]int, 2)
		for j := 0; j < 2; j++{
			fmt.Scan(&data[i][j])
		}
	}
	re := 0
	for i := 0; i < n; i++{
		if data[i][0] <= a && data[i][1] <= b {
			h, w:= data[i][0], data[i][1]

			for j := i + 1; j < n; j++{
				if data[j][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
		if data[i][0] <= b && data[i][1] <= a {
			h, w:= data[i][1], data[i][0]

			for j := i + 1; j < n; j++{
				if data[j][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
	}
	fmt.Println(re)
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	A := io.NextInt()
	B := io.NextInt()

	maxSize := 0
	seals := make([]*Seal, N)
	for i := 0; i < N; i++ {
		X := io.NextInt()
		Y := io.NextInt()

		seals[i] = NewSeal(X, Y)

		for j := 0; j < i; j++ {
			if GetFit(A, B, seals[i], seals[j]) {
				maxSize = Max(maxSize, seals[i].Area() + seals[j].Area())
			}
		}
	}
	io.Println(maxSize)
}

func GetFit(A, B int, s1, s2 *Seal) bool {
	sealPairs := [][]*Seal{
		[]*Seal{s1, s2},
		[]*Seal{s1, s2.Rotated()},
		[]*Seal{s1.Rotated(), s2},
		[]*Seal{s1.Rotated(), s2.Rotated()},
	}
	for _, sp := range sealPairs {
		if (sp[0].X + sp[1].X <= A && Max(sp[0].Y, sp[1].Y) <= B) || (Max(sp[0].X, sp[1].X) <= A && sp[0].Y + sp[1].Y <= B) {
			return true
		}
	}
	return false
}

type Seal struct {
	X int
	Y int
}

func NewSeal(x, y int) *Seal {
	return &Seal{
		X: x,
		Y: y,
	}
}

func (s *Seal) Rotated() *Seal {
	return NewSeal(s.Y, s.X)
}

func (s *Seal) Area() int {
	return s.X * s.Y
}

func Max(args ...int) int {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main
import "fmt"

func main(){
	var n, a, b int
	fmt.Scan(&n)
	fmt.Scan(&a)
	fmt.Scan(&b)
	data := make([][]int, n)
	for i := 0; i < n; i++{
		data[i] = make([]int, 2)
		for j := 0; j < 2; j++{
			fmt.Scan(&data[i][j])
		}
	}
	re := 0
	for i := 0; i < n; i++{
		if data[i][0] <= a && data[i][1] <= b {
			h, w:= data[i][0], data[i][1]
			if re < h*w{
				re = h*w
			}
			for j := i + 1; j < n; j++{
				if h + data[j][0] <= a && w + data[j][1] <= b{
					x, y := h + data[j][0], w + data[j][1]
					if re < x*y{
						re = x * y
					}
				}else if h + data[j][1] <= a && w + data[j][0] <= b{
					x, y := h + data[j][1], w + data[j][0]
					if re < x*y{
						re = x * y
					}
				}
			}
		}
		if data[i][0] <= b && data[i][1] <= a {
			h, w:= data[i][1], data[i][0]
			if re < h*w{
				re = h*w
			}
			for j := i + 1; j < n; j++{
				if h + data[j][0] <= a && w + data[j][1] <= b{
					x, y := h + data[j][0], w + data[j][1]
					if re < x*y{
						re = x * y
					}
				}else if h + data[j][1] <= a && w + data[j][0] <= b{
					x, y := h + data[j][1], w + data[j][0]
					if re < x*y{
						re = x * y
					}
				}
			}
		}
	}
	fmt.Println(re)
}

package main
import "fmt"

func main(){
	var n, a, b int
	fmt.Scan(&n)
	fmt.Scan(&a)
	fmt.Scan(&b)
	data := make([][]int, n)
	for i := 0; i < n; i++{
		data[i] = make([]int, 2)
		for j := 0; j < 2; j++{
			fmt.Scan(&data[i][j])
		}
	}
	re := 0
	for i := 0; i < n; i++{
		if data[i][0] <= a && data[i][1] <= b {
			h, w:= data[i][0], data[i][1]
			if re < h*w{
				re = h*w
			}
			for j := i + 1; j < n; j++{
				if data[i][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
		if data[i][0] <= b && data[i][1] <= a {
			h, w:= data[i][1], data[i][0]
			if re < h*w{
				re = h*w
			}
			for j := i + 1; j < n; j++{
				if data[i][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
	}
	fmt.Println(re)
}

package main
import "fmt"

func main(){
	var n, a, b int
	fmt.Scan(&n)
	fmt.Scan(&a)
	fmt.Scan(&b)
	data := make([][]int, n)
	for i := 0; i < n; i++{
		data[i] = make([]int, 2)
		for j := 0; j < 2; j++{
			fmt.Scan(&data[i][j])
		}
	}
	re := 0
	for i := 0; i < n; i++{
		if data[i][0] <= a && data[i][1] <= b {
			h, w:= data[i][0], data[i][1]

			for j := i + 1; j < n; j++{
				if data[i][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
		if data[i][0] <= b && data[i][1] <= a {
			h, w:= data[i][1], data[i][0]

			for j := i + 1; j < n; j++{
				if data[i][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[j][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
	}
	fmt.Println(re)
}

package main
import "fmt"

func main(){
	var n, a, b int
	fmt.Scan(&n)
	fmt.Scan(&a)
	fmt.Scan(&b)
	data := make([][]int, n)
	for i := 0; i < n; i++{
		data[i] = make([]int, 2)
		for j := 0; j < 2; j++{
			fmt.Scan(&data[i][j])
		}
	}
	re := 0
	for i := 0; i < n; i++{
		if data[i][0] <= a && data[i][1] <= b {
			h, w:= data[i][0], data[i][1]
			if re < h*w{
				re = h*w
			}
			for j := i + 1; j < n; j++{
				if data[i][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[i][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[i][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
		if data[i][0] <= b && data[i][1] <= a {
			h, w:= data[i][1], data[i][0]
			if re < h*w{
				re = h*w
			}
			for j := i + 1; j < n; j++{
				if data[i][0] <= a && w + data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if data[j][1] <= a && w + data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[i][0] <= a && data[j][1] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}else if h + data[i][1] <= a && data[j][0] <= b{
					tmp := h*w + data[j][0]*data[j][1]
					if re < tmp{
						re = tmp
					}
				}
			}
		}
	}
	fmt.Println(re)
}


package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n int
		fmt.Fscanf(in, "%v\n", &n)
		mas := make([]int, n + 1)
		for i := 0;i < n;i++ {
			fmt.Fscan(in, &mas[i])
		}
		mas[n] = 1
		fmt.Fscanf(in, "\n")
		sort.Ints(mas)
		var ans int = 1
		for i := n;i >= 0;i-- {
			if mas[i] <= i {
				ans = i + 1
				break
			}
		}
		fmt.Fprintln(out, ans)

	}
	return
}

package main

import "fmt"
import "sort"

func solve() {
	n := 0
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Scanf("%d", &a[i])
	}
	fmt.Scanf("%d\n", &a[n-1])
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i + 1 {
			fmt.Println(i + 2)
			return
		}
	}
	fmt.Println(1)
}

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

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

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

func solve() {
	n := 0
	scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n-1; i++ {
		scanf("%d ", &a[i])
	}
	scanf("%d\n", &a[n-1])
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			printf("%d\n", i + 2)
			return
		}
	}
	printf("1\n")
}

func main() {
	defer writer.Flush()
	t := 0
	scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func scanInt(n int) []int {
	input := make([]int, n)
	buffer := make([]interface{}, n)

	for i := 0; i < n; i++ {
		buffer[i] = &input[i]
	}

	fmt.Scanln(buffer...)

	return input
}

func solve() {
	n := scanInt(1)[0]
	a := scanInt(n)
	// fmt.Println(n, a)
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			fmt.Println(i + 2)
			return
		}
		// fmt.Print(i, " ")
	}
	fmt.Println("1")
}

func main() {
	t := scanInt(1)[0]
	// fmt.Println(t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func main(){
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Scan(&n)
		A := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&A[i])
		}
		sort.Ints(A)
		for n > 0 {
			g := A[n-1]
			if g > n {
				n--
			} else {
				break
			}
		}
		fmt.Println(n+1)
	}
}

package main

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

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 {
	Int64() int64
	UInt64() uint64
	String() 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) Int64() 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) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) String() 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.Int64()
	for ; t > 0; t-- {
		n := reader.Int64()
		a := make([]int64, n)
		for i := range a {
			a[i] = reader.Int64()
		}
		writer.Printf("%d\n", sol(a))
	}
}

func sol(a []int64) int64 {
	sort.Slice(a, func(x, b int) bool {
		return a[x] < a[b]
	})
	for i := len(a)-1; i >= 0; i-- {
		if int64(i) + 1 >= a[i] {
			return int64(i) + 1 + 1
		}
	}
	return 1
}

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		fmt.Println(solve(n, A))
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(N int, A []int) int {
	sort.Ints(A)

	var res int = 1

	for i := 1; i <= N; i++ {
		if A[i-1] <= i {
			res = i + 1
		}
	}

	return res
}

package main

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

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++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		sort.Ints(a)
		ans := 1
		for i := 0; i < n; i++ {
			if a[i] <= i+1 {
				ans = i + 2
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

package main

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

func main() {
	s := NewScanner()
	t := s.NextInt()

Next:
	for i := 0; i < t; i++ {
		n := s.NextInt()
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			arr[j] = s.NextInt()
		}

		sort.Ints(arr)
		for j := n - 1; j >= 0; j-- {
			if arr[j] <= j+1 {
				fmt.Println(j + 2)
				continue Next
			}
		}

		fmt.Println(1)
	}
}

type scanner struct {
	*bufio.Scanner
}

func NewScanner() *scanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &scanner{s}
}

func (s *scanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *scanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *scanner) NextString() string {
	s.Scan()
	return s.Text()
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	var n, a, ans, stock, prev int32
	fmt.Scanf("%d\n", &t)

	for i := 0; i < t; i++ {
		fmt.Scanf("%d\n", &n)

		arr := make([]int32, n)

		for j := int32(0); j < n; j++ {
			fmt.Scanf("%d", &a)
			// if a > n {
			// 	continue
			// }
			arr[j] = a
		}
		sort.Slice(arr, func(i int, j int) bool { return arr[i] < arr[j] })
		// fmt.Println(arr)
		ans = 1

		stock = 0
		prev = 1
		for j := int32(0); j < n; j++ {
			if arr[j] > n {
				break
			}

			if prev != arr[j] {
				if stock+ans-1 >= prev {
					ans += stock
					stock = 0
				}
				prev = arr[j]
			}
			stock++
			// fmt.Println(j, arr[j], stock, ans)
		}
		if stock+ans-1 >= prev {
			ans += stock
			stock = 0
		}
		fmt.Println(ans)
		fmt.Scanf("\n")
	}
}

/*
URL:
https://codeforces.com/contest/1358/problem/B
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"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
	n int
	A []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		A = ReadIntSlice(n)
		solve(n, A)
	}
}

func solve(n int, A []int) {
	sort.Sort(sort.IntSlice(A))

	ans := 0
	for i := 0; i < n; i++ {
		if A[i] <= i+1 {
			ChMax(&ans, i+1)
		}
	}

	fmt.Println(ans + 1)
}

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

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

/*********** 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"
	. "fmt"
	"io"
	"os"
	"sort"
)

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		ans := 1
		for i, v := range a {
			if v-1 <= i {
				ans = i + 2
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func main() {
	defer out.Flush()
	for t := ScanInt(); t > 0; t-- {
		P(solve())
	}
}

type str struct {
	idx   int64
	count int64
}

func solve() int64 {
	n := ScanInt()
	a := ScanInt64s(n)
	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	for i := int64(n - 1); i >= 0; i-- {
		if a[i] <= i+1 {
			return i + 2
		}
	}
	return int64(1)
}

/*********************** 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...)
	print("\n", nil)
}

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 []int:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.Itoa(v[i]) + " ")
			}
			str = sb.String()
		case int64:
			str = strconv.FormatInt(v, 10)
		case []int64:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.FormatInt(v[i], 10) + " ")
			}
			str = sb.String()
		case string:
			str = v
		default:
			panic("invalid print arg: " + fmt.Sprintf("%v", arg))

		}
		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 (
	"fmt"
	"sort"
)

func scanInt(n int) []int {
	input := make([]int, n)
	buffer := make([]interface{}, n)

	for i := 0; i < n; i++ {
		buffer[i] = &input[i]
	}

	fmt.Scanln(buffer...)

	return input
}

func solve() {
	n := scanInt(1)[0]
	a := scanInt(n)
	fmt.Println(n, a)
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			fmt.Println(i + 2)
			return
		}
		fmt.Print(i, " ")
	}
	println("1")
}

func main() {
	t := scanInt(1)[0]
	fmt.Println(t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func scanInt(n int) []int {
	input := make([]int, n)
	buffer := make([]interface{}, n)

	for i := 0; i < n; i++ {
		buffer[i] = &input[i]
	}

	fmt.Scanln(buffer...)

	return input
}

func solve() {
	n := scanInt(1)[0]
	a := scanInt(n)
	fmt.Println(n, a)
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			fmt.Println(i + 2)
			return
		}
		fmt.Print(i, " ")
	}
	println(1)
}

func main() {
	t := scanInt(1)[0]
	fmt.Println(t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import "fmt"
import "sort"

func solve() {
	n := 0
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := range a {
		fmt.Scanf("%d", &a[i])
	}
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i + 1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import "fmt"
import "sort"

func solve() {
	n := 0
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d ", &a[i])
	}
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i + 1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import "fmt"
import "sort"

func solve() {
	n := 0
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Scanf("%d ", &a[i])
	}
	fmt.Scanf("%d\n", &a[n-1])
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i + 1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

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

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

func solve() {
	n := 0
	scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &a[i])
	}
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			printf("%d\n", i + 2)
			return
		}
	}
	printf("1\n")
}

func main() {
	defer writer.Flush()
	t := 0
	scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import "fmt"
import "sort"

func solve() {
	n := 0
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := range a {
		fmt.Scanf("%d ", &a[i])
	}
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i + 1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func scanInt(n int) []int {
	input := make([]int, n)
	buffer := make([]interface{}, n)

	for i := 0; i < n; i++ {
		buffer[i] = &input[i]
	}

	fmt.Scanln(buffer...)

	return input
}

func solve() {
	n := scanInt(1)[0]
	a := scanInt(n)
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := scanInt(1)[0]
	for t > 0 {
		solve()
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func scanInt(n int) []int {
	input := make([]int, n)
	buffer := make([]interface{}, n)

	for i := 0; i < n; i++ {
		buffer[i] = &input[i]
	}

	fmt.Scanln(buffer...)

	return input
}

func solve() {
	n := 0
	fmt.Scanln(&n)
	a := scanInt(n)
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := 0
	fmt.Scanln(&t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import "fmt"
import "sort"

func solve() {
	n := 0
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Scanf("%d", &a[i])
	}
	fmt.Scanf("%d\n", &a[n-1])
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i + 1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func scanInt(n int) []int {
	input := make([]int, n)
	buffer := make([]interface{}, n)

	for i := 0; i < n; i++ {
		buffer[i] = &input[i]
	}

	fmt.Scanln(buffer...)

	return input
}

func solve() {
	n := scanInt(1)[0]
	a := scanInt(n)
	fmt.Println(n, a)
	sort.Ints(a)
	for i := n - 1; i >= 0; i-- {
		if a[i] <= i+1 {
			fmt.Println(i + 2)
			return
		}
	}
	println(1)
}

func main() {
	t := scanInt(1)[0]
	fmt.Println(t)
	for t > 0 {
		solve()
		t--
	}
}


package main
import "fmt"

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

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

	var m int
	var x, y int
	fmt.Scan(&m)

	for i := 0; i < m; i++ {
		fmt.Scan(&x,&y)
		if x == 1 {
			if x < n{
				arr[x]= arr[x] + (arr[x-1] - y)
			}
			arr[x-1] = 0
		}else if x == n {
			if x-2 >= 0 {
				arr[x-2] = arr[x-2] + (y - 1)
			}
			arr[x-1] = 0
		}else{
			if x-2 >= 0 {
				arr[x-2] = arr[x-2] + (y -1)
			}
			if x < n{
				arr[x]= arr[x] + (arr[x-1] - y)
			}
			arr[x-1]= 0
		}
	}
	for i := 0; i < n; i++ {
		fmt.Println(arr[i])
	}

}

package main

import "fmt"

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

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

	var m int
	fmt.Scan(&m)

	var x, y int
	for i := 0; i < m; i++ {
		fmt.Scan(&x, &y)
		if x <= n-1 {
			an[x] += an[x-1] - y
		}
		if x > 1 {
			an[x-2] += y - 1
		}
		an[x-1] = 0
	}

	for i := 0; i < n; i++ {
		fmt.Println(an[i])
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var numberOfBirdLines int
	fmt.Fscan(reader, &numberOfBirdLines)
	birdsNumberArray := make([]int, numberOfBirdLines)
	for index := 0; index < numberOfBirdLines; index++ {
		fmt.Fscan(reader, &birdsNumberArray[index])
	}
	var numberOfShoots, shootedLine, shootedBird int
	fmt.Fscan(reader, &numberOfShoots)
	for index := 0; index < numberOfShoots; index++ {
		fmt.Fscan(reader, &shootedLine)
		fmt.Fscan(reader, &shootedBird)

		if shootedLine != 1 {
			birdsNumberArray[shootedLine-2] += (shootedBird - 1)
		}
		if shootedLine != numberOfBirdLines {
			birdsNumberArray[shootedLine] += (birdsNumberArray[shootedLine-1] - shootedBird)
		}
		birdsNumberArray[shootedLine-1] = 0
	}
	for _, elemenet := range birdsNumberArray {
		fmt.Println(elemenet)
	}
}

package main

import "fmt"

func main() {
    var n,m,x,y int
    var a [105]int
    fmt.Scan(&n)
    for i := 1; i <= n; i++ { fmt.Scan(&a[i]) }
    fmt.Scan(&m)
    for i := 0; i < m; i++ {
        fmt.Scan(&x,&y)
        if x != 1 { a[x-1] += y-1 }
        if x != n { a[x+1] += a[x]-y }
        a[x] = 0
    }
    for i := 1; i <= n; i++ { fmt.Println(a[i]) }
}

package main

import "fmt"

func main()  {
	var noOfWires int
	fmt.Scan(&noOfWires)
	var noOfBirdsBerWire [] int
	for index := 0; index < noOfWires; index++ {
		var noOfBirds int
		fmt.Scan(&noOfBirds)
		noOfBirdsBerWire = append(noOfBirdsBerWire, noOfBirds)
	}
	var noOfShoots int
	fmt.Scan(&noOfShoots)
	for index := 0; index < noOfShoots; index++ {
		var rowNo int
		fmt.Scan(&rowNo)
		var birdNo int
		fmt.Scan(&birdNo)
		targetRow := rowNo - 1
		noOfBirdsInTargetRow := noOfBirdsBerWire[targetRow]
		leftBirdsNo := birdNo - 1
		rightBirdsNo := noOfBirdsInTargetRow - birdNo
		if birdNo == 1 {
			leftBirdsNo = 0
		}
		if birdNo == noOfBirdsInTargetRow {
			rightBirdsNo = 0
		}
		noOfBirdsBerWire[targetRow] = 0
		if (targetRow != 0 ){
			noOfBirdsBerWire[targetRow - 1] = leftBirdsNo +  noOfBirdsBerWire[targetRow-1]
		}
		if (targetRow + 1 < noOfWires) {
			noOfBirdsBerWire[targetRow + 1] = rightBirdsNo +  noOfBirdsBerWire[targetRow+1]
		}
	}
	for index := 0; index < len(noOfBirdsBerWire); index++ {
		fmt.Println(noOfBirdsBerWire[index])
	}
}

package main
import (
       	"fmt"
)
func main() {
       	var n,m,x,y int
       	var a [102]int
       	fmt.Scan(&n)
       	for i:=1;i<=n;i++ {
       		fmt.Scan(&a[i])
       	}
       	fmt.Scan(&m)
       	for i:=0;i<m;i++ {
       		fmt.Scan(&x,&y)
       		a[x-1]+=(y-1)
       		a[x+1]+=(a[x]-y)
       		a[x]=0
       	}
       	for i:=1;i<=n;i++ {
       		fmt.Println(a[i])
       	}

}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	birds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&birds[i])
	}

	var m int
	fmt.Scan(&m)
	// fmt.Println("m::", m)
	for i := 0; i < m; i++ {
		var y, x int
		fmt.Scan(&x)
		fmt.Scan(&y)
		x -= 1
		// fmt.Println(y, x)
		if x > 0 {
			birds[x-1] += y - 1
		}
		// fmt.Println("check:", x, m-1, x < m, birds[x]-y)
		if x < n-1 {
			birds[x+1] += birds[x] - y
		}
		birds[x] = 0
		// fmt.Println("ans:", birds[x-1], birds[x], birds[x+1])
	}
	// fmt.Println(birds)
	// fmt.Println(n, m)
	for _, i := range birds {
		fmt.Println(i)
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	birds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&birds[i])
	}

	var m int
	fmt.Scan(&m)
	// fmt.Println("m::", m)
	for i := 0; i < m; i++ {
		var y, x int
		fmt.Scan(&x)
		fmt.Scan(&y)
		x -= 1
		// fmt.Println(y, x)
		if x > 0 {
			birds[x-1] += y - 1
		}
		// fmt.Println("check:", x, m-1, x < m, birds[x]-y)
		if x < m {
			birds[x+1] += birds[x] - y
		}
		birds[x] = 0
		// fmt.Println("ans:", birds[x-1], birds[x], birds[x+1])
	}
	// fmt.Println(birds)
	// fmt.Println(n, m)
	for _, i := range birds {
		fmt.Println(i)
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	birds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&birds[i])
	}

	var m int
	fmt.Scan(&m)
	// fmt.Println(m)
	for i := 0; i < m; i++ {
		var y, x int
		fmt.Scan(&x)
		fmt.Scan(&y)
		x -= 1
		// fmt.Println(y, x)
		if x != 0 {
			birds[x-1] += y - 1
		}
		if x != m-1 {
			birds[x+1] += birds[x] - y
		}
		birds[x] = 0
	}
	// fmt.Println(birds)
	// fmt.Println(n, m)
	for _, i := range birds {
		fmt.Println(i)
	}
}