Let $s$ be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is $1+2+3+1=7$.

The string $w$ and the integer $p$ are given. Remove the minimal number of letters from $w$ so that its price becomes less than or equal to $p$ and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string $w$ is less than or equal to $p$, then nothing needs to be deleted and $w$ must be output.

Note that when you delete a letter from $w$, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst.


-----Input-----

The first line of input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. The following are descriptions of $t$ test cases.

Each case consists of two lines.

The first of them is the string $w$, it is non-empty and consists of lowercase Latin letters. Its length does not exceed $2\cdot10^5$.

The second line contains an integer $p$ ($1 \le p \le 5\,200000$).

It is guaranteed that the sum of string lengths $w$ over all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

Output exactly $t$ rows, the $i$-th of them should contain the answer to the $i$-th set of input data. Print the longest string that is obtained from $w$ by deleting letters such that its price is less or equal to $p$. If there are several answers, then output any of them.

Note that the empty string  — is one of the possible answers. In this case, just output an empty string.


-----Examples-----

Input
5
abca
2
abca
6
codeforces
1
codeforces
10
codeforces
100
Output
aa
abc

cdc
codeforces


-----Note-----

None

Difficulty: EASY
Tags: Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
x = int(input())
odg = []
for i in range(0, x):
	l = input()
	w = int(input())
	ls = sorted(l)
	price = 0
	granica = None
	for j in range(0, len(ls)):
		price += ord(ls[j]) - 96
		if price > w:
			granica = j
			break
	if granica != None:
		visak = ls[granica:]
	else:
		visak = []
	y = 0
	visak = ''.join(visak)
	while visak != '':
		l = l.replace(visak[0], '', visak.count(visak[0]))
		visak = visak.replace(visak[0], '')
	odg.append(l)
for e in odg:
	print(e)


from collections import defaultdict
x = int(input())
for jj in range(x):
	s = input()
	c = int(input())
	dic = defaultdict(list)
	for (i, v) in enumerate(s):
		dic[v].append(i)
	ans = [''] * len(s)
	for i in sorted(dic.keys()):
		p = ord(i) - 96
		if c >= len(dic[i]) * p:
			for j in dic[i]:
				ans[j] = i
			c -= len(dic[i]) * p
		else:
			k = c // p
			if k == 0:
				break
			else:
				for m in range(k):
					ans[dic[i][m]] = i
			break
	print(''.join(ans))


def solve():
	e = list(input())
	p = int(input())
	s = sorted(e, reverse=True)
	x = 0
	for i in s:
		x += ord(i) - 96
	i = 0
	while x > p:
		x -= ord(s[i]) - 96
		i += 1
	t = ''
	s = s[:i]
	d = {}
	for i in s:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	for i in e:
		if i in d and d[i] > 0:
			d[i] -= 1
		else:
			t += i
	print(t)
t = int(input())
while t:
	solve()
	t -= 1


def inlt():
	return list(map(int, input().split()))
import sys
from collections import defaultdict
from math import sqrt, ceil, floor, factorial, log
from bisect import bisect_left, bisect_right
from io import BytesIO, IOBase, StringIO
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
while t:
	t -= 1
	s = input()
	x = int(input())
	tot = 0
	dic = defaultdict(int)
	for j in s:
		tot = tot + ord(j) - 96
		dic[j] += 1
	p = tot - x
	for j in range(ord('z'), ord('a') - 1, -1):
		if p > 0:
			if dic[chr(j)] > 0:
				vayu = j - 96
				if vayu * dic[chr(j)] >= p:
					h = ceil(p / vayu)
					dic[chr(j)] = dic[chr(j)] - h
					p = 0
				else:
					p = p - vayu * dic[chr(j)]
					dic[chr(j)] = 0
		else:
			break
	final = ''
	for j in s:
		if dic[j] > 0:
			final += j
			dic[j] -= 1
		else:
			pass
	print(final)


t = int(input())
for _ in range(t):
	w = input()
	p = int(input())
	curr = 0
	count = {}
	for char in w:
		if char not in count:
			count[char] = 0
		count[char] += 1
		curr += 1 + ord(char) - ord('a')
	if curr <= p:
		print(w)
		continue
	left = curr - p
	for char in sorted(count, reverse=True):
		if left - count[char] * (1 + ord(char) - ord('a')) >= 0:
			left -= count[char] * (1 + ord(char) - ord('a'))
			count[char] = 0
		else:
			temp = (left + (ord(char) - ord('a'))) // (1 + ord(char) - ord('a'))
			count[char] -= temp
			left -= temp * (1 + ord(char) - ord('a'))
			break
	res = []
	for char in w:
		if count[char] > 0:
			count[char] -= 1
			res.append(char)
	print(''.join(res))


import sys
input = sys.stdin.readline
alph = {i: j for (i, j) in zip([chr(i) for i in range(97, 123)], [i for i in range(1, 27)])}
for _ in range(int(input())):
	s = input().rstrip()
	p = int(input())
	sd = sorted(zip(s, range(len(s))))
	res = [''] * len(s)
	for (i, j) in sd:
		if alph[i] > p:
			print(''.join(res))
			break
		else:
			res[j] = i
			p -= alph[i]
	else:
		print(''.join(res))


def solve():
	n = int(input())
	for i in range(n):
		s = str(input())
		p = int(input())
		v = [0] * 26
		for letter in s:
			v[ord(letter) - 97] += 1
		for i in range(26):
			if v[i] * (i + 1) < p:
				p -= v[i] * (i + 1)
				v[i] = 0
			else:
				c = p // (i + 1)
				v[i] -= c
				break
		print(assembleString(s, v))

def assembleString(s, arr):
	ns = ''
	for l in s:
		if arr[ord(l) - 97]:
			arr[ord(l) - 97] -= 1
		else:
			ns += l
	return ns
solve()


import math

def solve():
	s = input()
	p = int(input())
	arr = [0 for i in range(26)]
	for x in s:
		ind = ord(x) - 97
		arr[ind] += ind + 1
	summ = sum(arr)
	if summ <= p:
		print(s)
		return
	diff = summ - p
	carr = dict()
	for i in range(25, -1, -1):
		if arr[i] == 0:
			continue
		dl = math.ceil(diff / (i + 1))
		dl = min(dl, arr[i] // (i + 1))
		carr[chr(97 + i)] = dl
		diff -= (i + 1) * dl
	ans = ''
	for x in s:
		if x in carr and carr[x] > 0:
			carr[x] -= 1
		else:
			ans += x
	print(ans)
t = int(input())
for i in range(t):
	solve()


from collections import Counter

def main():
	s = list(input())
	p = int(input())
	curr = 0
	for z in s:
		curr += ord(z) - 96
	if curr <= p:
		return ''.join(s)
	freq = Counter(s)
	letters = list(freq.keys())
	letters.sort()
	letters = letters[::-1]
	rem = {}
	extra = curr - p
	for let in letters:
		f = freq[let]
		o = ord(let) - 96
		if f * o >= extra:
			rem[let] = (extra + o - 1) // o
			break
		else:
			rem[let] = f
			extra -= o * f
	for i in range(len(s)):
		z = s[i]
		if z in rem and rem[z] > 0:
			s[i] = ''
			rem[z] -= 1
	return ''.join(s)
for _ in range(int(input())):
	print(main())


for t in range(int(input())):
	s = input()
	a = int(input())
	t = [*range(len(s))]
	d = [ord(e) - 96 for e in s]
	t.sort(key=lambda x: s[x])
	t = t[::-1]
	c = sum(d)
	k = 0
	while c > a:
		c -= d[t[k]]
		d[t[k]] = 0
		k += 1
	print(''.join((chr(e + 96) for e in d if e != 0)))


T = int(input())
for Ti in range(T):
	s_letter = [i for i in input()]
	s = [ord(i) - 96 for i in s_letter]
	p = int(input())
	s_i = sorted(range(len(s)), key=lambda k: s[k])
	sum_s = 0
	ans_l = []
	for i in range(len(s)):
		sum_s += s[s_i[i]]
		if sum_s <= p:
			ans_l.append(s_i[i])
		else:
			pass
	ans = ''.join([s_letter[i] for i in sorted(ans_l)])
	print(ans)


for _ in range(int(input())):
	niz = str(input())
	p = int(input())
	a = []
	for i in niz:
		a.append(ord(i) - 96)
	a.sort()
	suma = sum(a)
	while suma > p:
		l = a.pop()
		suma -= l
	kolikoKojihSlovaImamo = {chr(x): 0 for x in range(97, 97 + 26)}
	for i in a:
		kolikoKojihSlovaImamo[chr(i + 96)] += 1
	novaRec = ''
	for i in niz:
		if kolikoKojihSlovaImamo[i] != 0:
			novaRec += i
			kolikoKojihSlovaImamo[i] -= 1
	print(novaRec)


for _ in range(int(input())):
	w = input()
	p = int(input())
	y = []
	for i in w:
		y.append(ord(i) - 96)
	y.sort()
	m = sum(y)
	while m > p:
		l = y.pop()
		m -= l
	s = ''
	o = {}
	o = {chr(x): 0 for x in range(97, 123)}
	for i in y:
		o[chr(i + 96)] += 1
	for i in w:
		if o.get(i):
			s += i
			o[i] -= 1
	print(s)


import sys

def input():
	return sys.stdin.readline().rstrip()
for _ in range(int(input())):
	s = [ord(i) & 31 for i in input()]
	p = int(input())
	d = {}
	for (idx, i) in enumerate(s):
		if i not in d:
			d[i] = []
		d[i].append(idx)
	current = sum(s)
	keys = sorted(d.keys(), reverse=True)
	for key in keys:
		while d[key] and current > p:
			d[key].pop()
			current -= key
	ans = [0] * len(s)
	for (k, v) in d.items():
		for index in v:
			ans[index] = k
	final = ''
	for i in ans:
		if i:
			final += chr(96 + i)
	print(final)


def cheap(s, p):
	count = [0 for x in range(27)]
	sum = 0
	for i in range(0, len(s)):
		sum += ord(s[i]) - 96
		count[ord(s[i]) - 96] += 1
	for i in reversed(range(27)):
		while sum > p and count[i] > 0:
			count[i] -= 1
			sum -= i
		if sum <= p:
			break
	ans = ''
	for i in range(0, len(s)):
		if count[ord(s[i]) - 96] > 0:
			ans = ans + s[i]
			count[ord(s[i]) - 96] -= 1
	return ans
t = int(input())
for j in range(t):
	s = input()
	p = int(input())
	print(cheap(s, p))


from collections import defaultdict

def solve(s, p):
	alpha = 'zyxwvutsrqponmlkjihgfedcba'
	removed = set()
	counts = defaultdict(list)
	price = 0
	for (i, v) in enumerate(s):
		counts[v].append(i)
		price += ord(v) - 96
	if price <= p:
		return removed
	for (i, c) in enumerate(alpha):
		if c in counts:
			while len(counts[c]) > 0:
				price -= 26 - i
				removed.add(counts[c].pop())
				if price <= p:
					return removed
for _ in range(int(input())):
	s = input()
	p = int(input())
	removed = solve(s, p)
	ans = ''
	for i in range(len(s)):
		if i not in removed:
			ans += s[i]
	print(ans)


def get_prices(w):
	prices = []
	sum_prices = 0
	for (i, e) in enumerate(w):
		price = ord(e) - ord('a') + 1
		prices.append((i, price))
		sum_prices += price
	return (sorted(prices, key=lambda e: e[1]), sum_prices)

def solve(w, p):
	(prices, sum_prices) = get_prices(w)
	while sum_prices > p and len(prices) > 0:
		sum_prices -= prices[-1][1]
		prices.pop()
	r = ''
	prices = sorted(prices, key=lambda e: e[0])
	for e in prices:
		r += w[e[0]]
	return r
t = int(input())
for _ in range(t):
	w = input()
	p = int(input())
	print(solve(w, p))


test = int(input())
for i in range(test):
	string = input()
	target_sum = int(input())
	a = ord('a') - 1
	index_list = sorted(enumerate((ord(i) - a for i in string)), key=lambda x: x[1])
	index_sum = sum(map(lambda x: x[1], index_list))
	while index_sum > target_sum:
		index_sum -= index_list.pop()[1]
	index_list = sorted(index_list)
	print(''.join(map(lambda x: chr(x[1] + a), index_list)))


for t in range(int(input())):
	a = input()
	k = int(input())
	e = []
	for char in a:
		e.append(ord(char) - 96)
	e = sorted(e)
	m = sum(e)
	while m > k:
		l = e.pop()
		m -= l
	o = {chr(x): 0 for x in range(96, 140)}
	for item in e:
		o[chr(item + 96)] += 1
	new_string = ''
	for char in a:
		if o.get(char):
			new_string += char
			o[char] -= 1
	print(new_string)


import sys
import math
from collections import defaultdict
alph = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}

def run():
	s = input()
	p = int(input())
	cost = 0
	used = defaultdict(int)
	for c in alph:
		used[c] = 0
	for c in s:
		cost += alph[c]
		used[c] += alph[c]
	d = cost - p
	for c in ''.join(alph)[::-1]:
		if d > 0:
			diff = min(d, used[c])
			d -= diff
			used[c] -= diff
	new_s = []
	for c in s:
		if used[c] >= alph[c]:
			new_s.append(c)
			used[c] -= alph[c]
	print(''.join(new_s))
t = int(input())
for _ in range(t):
	run()


import string
slovar = string.ascii_lowercase
i = 1
d = {}
for elem in slovar:
	d[elem] = i
	i += 1

def func(w, p, d):
	sorted_w = sorted(w)
	current_sum = 0
	for elem in w:
		current_sum += d[elem]
	dif = p - current_sum
	if dif >= 0:
		return w
	else:
		d_del = {}
		i = len(w) - 1
		while i > -1 and current_sum > p:
			current_sum -= d[sorted_w[i]]
			if sorted_w[i] not in d_del:
				d_del[sorted_w[i]] = 0
			d_del[sorted_w[i]] += 1
			i -= 1
	res = ''
	for elem in w:
		if elem in d_del and d_del[elem] != 0:
			d_del[elem] -= 1
		else:
			res += elem
	return res

def main():
	t = int(input())
	for i in range(t):
		w = input()
		p = int(input())
		ans = func(w, p, d)
		print(ans)
main()


import bisect
from collections import defaultdict, deque
from functools import lru_cache
import heapq
mod = 10 ** 9 + 7
from sys import stdin, stdout
input = stdin.readline
import math

def ii():
	return int(input())

def lii():
	return list(map(int, stdin.readline().split()))

def ss():
	x = stdin.readline()
	if x[-1] == '\n':
		return x[:-1]
	return x

def lss():
	return list(map(str, stdin.readline().split()))

def yes():
	stdout.write('YES\n')

def no():
	stdout.write('NO\n')
for _ in range(ii()):
	w = ss()
	p = int(input())
	a = [ord(i) - 96 for i in w]
	s = sum(a)
	b = sorted([(x, i) for (i, x) in enumerate(a)])
	while s > p:
		s -= b.pop()[0]
	c = sorted([(i, x) for (x, i) in b])
	print(''.join([chr(x + 96) for (i, x) in c]))


import math

def solve():
	s = input()
	p = int(input())
	d = {}
	for i in s:
		if i not in d:
			d[i] = 0
		d[i] += 1
	curr_sum = 0
	for i in d:
		d[i] = (d[i], ord(i) - ord('a') + 1)
		curr_sum += d[i][0] * d[i][1]
	d = dict(sorted(d.items(), key=lambda x: -x[1][1]))
	key = list(d.keys())
	i = 0
	while curr_sum > p:
		if i >= len(key):
			break
		(k, val) = d[key[i]]
		reqd = min(k, math.ceil((curr_sum - p) / val))
		curr_sum -= reqd * val
		d[key[i]] = (d[key[i]][0] - reqd, d[key[i]][1])
		i += 1
	for i in d:
		d[i] = d[i][0]
	ans = []
	for i in s:
		if d[i] > 0:
			ans.append(i)
			d[i] -= 1
	print(''.join(ans))
for _ in range(int(input())):
	solve()


from collections import defaultdict
import math
cases = int(input())
for _ in range(cases):
	w = input()
	p = int(input())
	price = 0
	count = defaultdict(lambda : 0)
	for c in w:
		letter = ord(c) - 96
		count[letter] += 1
		price += letter
	for j in range(26, 0, -1):
		if price <= p:
			break
		if count[j] == 0:
			continue
		diff = price - p
		to_remove = math.ceil(diff / j)
		to_remove = min(to_remove, count[j])
		w = w.replace(chr(j + 96), '', to_remove)
		price -= to_remove * j
	print(w)


T = int(input())
for t in range(T):
	s = input()
	l = list(s)
	target = int(input())
	marker = []
	num = 0
	for i in range(27):
		marker.append([])
	for i in range(len(s)):
		marker[ord(s[i]) - 96].append(i)
		num += ord(s[i]) - 96
	while num > target:
		for i in range(26, 0, -1):
			if len(marker[i]) == 0:
				pass
			for j in marker[i]:
				num -= i
				l[j] = '.'
				if num <= target:
					break
			if num <= target:
				break
	ans = ''
	for ch in l:
		if ch != '.':
			ans += ch
	print(ans)


import sys
import math
from collections import Counter

def gi():
	return list(map(int, sys.stdin.readline().strip().split()))

def gs():
	return sys.stdin.readline().strip()

def fun(a, s):
	l = len(a)
	a = list(a)
	d = {}
	arr = sorted(a)
	sm = 0
	for i in range(l):
		v = ord(arr[i]) - 96
		if sm + v <= s:
			if arr[i] in d:
				d[arr[i]] += 1
			else:
				d[arr[i]] = 1
			sm += v
	ans = ''
	for i in range(l):
		if a[i] in d and d[a[i]] > 0:
			ans += a[i]
			d[a[i]] -= 1
	return ans
for _ in range(int(input())):
	n = gs()
	x = int(input())
	print(fun(n, x))


from collections import defaultdict
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	x = input().strip()
	p = int(input())
	d = defaultdict(lambda : 0)
	curr = 0
	for i in x:
		d[i] += 1
		curr += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		if curr <= p:
			break
		sub = min(d[chr(i + 97)], (curr - p + (i + 1) - 1) // (i + 1))
		d[chr(i + 97)] -= sub
		curr -= sub * (i + 1)
	st = []
	for i in x:
		if d[i] > 0:
			d[i] -= 1
			st.append(i)
	print(''.join(st))


for t in range(int(input())):
	string = input()
	n = int(input())
	s = 0
	d = dict.fromkeys(list(string), 0)
	for i in string:
		s += ord(i) - 96
		d[i] += 1
	if s <= n:
		print(string)
	else:
		li = list(string)
		li = sorted(li)[::-1]
		for i in li:
			s -= ord(i) - 96
			d[i] -= 1
			if s <= n:
				break
		ans = ''
		for i in string:
			if d[i] != 0:
				ans += i
				d[i] -= 1
		print(ans)


for t in range(int(input())):
	s = input()
	p = int(input())
	a = [ord(x) - ord('a') + 1 for x in s]
	m = sum(a)
	b = sorted([(x, i) for (i, x) in enumerate(a)])
	while m > p:
		m -= b.pop()[0]
	c = sorted([(i, x) for (x, i) in b])
	print(''.join([chr(x + ord('a') - 1) for (i, x) in c]))


import math
from math import ceil
for t in range(int(input())):
	w = input()
	nn = int(input())
	let = [0] * 27
	out = []
	d = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
	total = 0
	for x in w:
		let[ord(x) - 96] += 1
		total += ord(x) - 96
	n = total - nn
	if n <= 0:
		print(w)
		continue
	for x in range(len(let) - 1, 0, -1):
		if let[x] == 0:
			continue
		if let[x] * x < n:
			n -= let[x] * x
			d[chr(x + 96)] = let[x]
		else:
			d[chr(x + 96)] = math.ceil(n / x)
			break
	for x in w:
		if d[x] != 0:
			d[x] -= 1
		else:
			out.append(x)
	print(''.join(out))


A = ord('a')
get_cost = lambda x: ord(x) - A + 1
for _ in range(int(input())):
	word = input()
	max_cost = int(input())
	indexes = sorted(range((l := len(word))), key=lambda i: word[i])
	global_cost = sum((get_cost(x) for x in word))
	while global_cost > max_cost:
		global_cost -= get_cost(word[indexes.pop(-1)])
	st = set(indexes)
	print(''.join((word[i] for i in range(l) if i in st)))


for _ in range(int(input())):
	s = input()
	p = int(input())
	sum_ = 0
	mp = {chr(i + 97): 0 for i in range(26)}
	for c in s:
		mp[c] += 1
		sum_ += ord(c) - 96
	for i in range(25, -1, -1):
		while sum_ > p:
			if mp[chr(i + 97)] > 0:
				mp[chr(i + 97)] -= 1
				sum_ -= i + 1
			else:
				break
	ans = []
	for c in s:
		if mp[c] > 0:
			ans.append(c)
			mp[c] -= 1
	print(''.join(ans))


for _ in range(int(input())):
	s = input()
	n = int(input())
	su = 0
	for i in s:
		su += ord(i) - 96
	s1 = sorted(s, reverse=True)
	mp = {}
	for i in s1:
		if su <= n:
			break
		su -= ord(i) - 96
		if i in mp:
			mp[i] += 1
		else:
			mp[i] = 1
	ans = []
	for i in s:
		if i in mp:
			mp[i] -= 1
			if not mp[i]:
				mp.pop(i)
		else:
			ans.append(i)
	print(''.join(ans))


for h in range(int(input())):
	s = input()
	p = int(input())
	voec = []
	ct = 0
	temp = {}
	for i in s:
		voec.append(i)
		temp[i] = 0
		ct += ord(i) - 96
	voec.sort(reverse=True)
	c = 0
	for i in voec:
		if ct <= p:
			break
		c += 1
		ct -= ord(i) - 96
	voec = voec[c:]
	for i in voec:
		temp[i] += 1
	vo = ''
	for i in s:
		if temp[i] > 0:
			temp[i] -= 1
			vo += i
	print(vo)


v = {}
for i in 'abcdefghijklmnopqrstuvwxyz':
	v[i] = ord(i) - ord('a') + 1
for _ in range(int(input())):
	w = input()
	p = int(input())
	price = sum([v[i] for i in w])
	d = {}
	sw = sorted(w, key=lambda x: v[x], reverse=True)
	for i in sw:
		if price <= p:
			break
		else:
			price -= v[i]
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
	res = []
	for i in w:
		if i in d and d[i] > 0:
			d[i] -= 1
		else:
			res.append(i)
	res = ''.join(res)
	print(res)


from collections import defaultdict
d = {chr(i + 96): i for i in range(1, 27)}
for _ in range(int(input())):
	s = input()
	p = int(input())
	z = defaultdict(list)
	s = [x for x in s]
	for x in range(len(s)):
		z[s[x]].append(x)
	k = sorted(s)
	curr = sum([d[x] for x in s])
	if curr <= p:
		print(''.join(s))
		continue
	for x in k[::-1]:
		if curr > p:
			curr -= d[x]
			ind = z[x].pop()
			s[ind] = ''
	print(''.join(s))


from collections import defaultdict
import sys

def solve():
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for _ in range(k):
		(a, b) = map(int, input().split())
		la = []
		lb = []
		if a not in l or b not in l:
			print('NO')
			return
		for i in range(n):
			if l[i] == a:
				la.append(i)
			if l[i] == b:
				lb.append(i)
		mi = min(la)
		ma = max(lb)
		if mi < ma:
			print('YES')
		else:
			print('NO')

def solve1(w, p):
	d = defaultdict(lambda : 0)
	cp = 0
	for i in w:
		d[i] += 1
		cp += ord(i) - 96
	for i in range(25, -1, -1):
		t = d[chr(i + 97)]
		if cp <= p:
			break
		sub = min(d[chr(i + 97)], (cp - p + i) // (i + 1))
		d[chr(i + 97)] -= sub
		cp -= sub * (i + 1)
	l = []
	for i in w:
		if d[i] > 0:
			d[i] -= 1
			l.append(i)
	print(''.join(l))

def main():
	for _ in range(int(input())):
		w = input()
		p = int(input())
		solve1(w, p)
main()


for _ in range(int(input())):
	s = input()
	n = int(input())
	c = 0
	l = [0] * 26
	for i in s:
		x = ord(i) - 97
		l[x] += 1
		c += x + 1
	if c <= n:
		print(s)
	else:
		for i in range(25, -1, -1):
			le = l[i]
			for j in range(le):
				c -= i + 1
				l[i] -= 1
				if c <= n:
					break
			if c <= n:
				break
		ans = ''
		for i in s:
			x = ord(i) - 97
			if l[x] > 0:
				ans += i
				l[x] -= 1
		print(ans)


from collections import defaultdict
import sys
input = sys.stdin.readline
n = int(input())
while n:
	s = input().strip()
	p = int(input())
	ddef = defaultdict(lambda : 0)
	d = ddef
	c = 0
	for i in s:
		d[i] += 1
		c += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		present = d[chr(i + 97)]
		if c <= p:
			break
		k = min(d[chr(i + 97)], (c - p + (i + 1) - 1) // (i + 1))
		d[chr(i + 97)] -= k
		c -= k * (i + 1)
	news = []
	for i in s:
		if d[i] > 0:
			d[i] -= 1
			news.append(i)
	print(''.join(news))
	n -= 1


from cmath import cos
t = int(input())
for i in range(t):
	str = input()
	max = int(input())
	counter = [0] * 26
	ans = ''
	cost = 0
	for char in str:
		counter[ord(char) - 97] += 1
	index = 0
	while cost <= max and index <= 25:
		cost += counter[index] * (index + 1)
		index += 1
	index -= 1
	while cost > max:
		cost -= index + 1
		counter[index] -= 1
	chars = counter[:index + 1]
	empty = [0] * 26
	chars.extend(empty)
	for char in str:
		if chars[ord(char) - 97] != 0:
			chars[ord(char) - 97] -= 1
			ans += char
	print(ans)


import sys
import heapq
input = sys.stdin.readline
from collections import Counter, defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def main():
	string = input().strip()
	p = inp()
	charMap = [0] * 26
	totalSum = 0
	for char in string:
		charMap[ord(char) - ord('a')] += 1
		totalSum += ord(char) - ord('a') + 1
	pointer = 25
	while totalSum > p and pointer >= 0:
		if charMap[pointer] > 0:
			charMap[pointer] -= 1
			totalSum -= pointer + 1
		else:
			pointer -= 1
	output = []
	for char in string:
		if charMap[ord(char) - ord('a')] > 0:
			output.append(char)
			charMap[ord(char) - ord('a')] -= 1
	print(''.join(output))
t = inp()
for _ in range(t):
	main()


from collections import Counter
import string

def sol():
	s = input()
	p = int(input())
	count = Counter(s)
	t = sum((ord(i) - 96 for i in s))
	for i in string.ascii_lowercase[::-1]:
		while p < t and count[i]:
			t -= ord(i) - 96
			count[i] -= 1
	ans = ''
	for i in s:
		if count[i]:
			ans += i
			count[i] -= 1
	return ans
for _ in range(int(input())):
	print(sol())


def od(s):
	return ord(s) - 96

def k_(s):
	K = 0
	for se in s:
		K += od(se)
	return K

def op(s, p=''):
	A = ''
	for se in s:
		A += str(se) + p
	if p == '':
		return A
	else:
		return A[:-len(p)]

def kil_(s, n):
	s = list(s)
	st = sorted(s, key=ord, reverse=True)
	stu = k_(s)
	d = dict().fromkeys(set(st), 0)
	for i in range(len(s)):
		if stu > n:
			stu -= od(st[i])
			d[st[i]] += 1
		else:
			break
	S = ''
	for i in range(len(s)):
		if d[s[i]] > 0:
			d[s[i]] -= 1
		else:
			S += s[i]
	return S

def kil():
	t = int(input())
	A = ''
	for i in range(t):
		A += kil_(input(), int(input())) + '\n'
	print(A[:-1])
kil()


for k in range(int(input())):
	s = input()
	n = int(input())
	su = 0
	mp = {}
	for i in s:
		su += ord(i) - 96
	s1 = sorted(s, reverse=True)
	for i in s1:
		if su <= n:
			break
		su -= ord(i) - 96
		if i in mp:
			mp[i] += 1
		else:
			mp[i] = 1
	res = ''
	for i in s:
		if i in mp:
			mp[i] -= 1
			if mp[i] == 0:
				mp.pop(i)
		else:
			res += i
	print(res)


from collections import Counter, defaultdict
t = int(input())
for _ in range(t):
	word = input()
	p = int(input())
	charsCount = Counter(word)
	alphabets = charsCount.keys()
	pickChar = defaultdict(int)
	for key in sorted(alphabets):
		if p > charsCount[key] * (ord(key) - 96):
			pickChar[key] = charsCount[key]
			p -= charsCount[key] * (ord(key) - 96)
		else:
			pickChar[key] = p // (ord(key) - 96)
			p %= ord(key) - 96
	constructStr = []
	for char in word:
		if pickChar[char] > 0:
			constructStr.append(char)
			pickChar[char] -= 1
	print(''.join(constructStr))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input()[:-1]
	p = int(input())
	w = sorted(enumerate([ord(i) - 96 for i in s]), key=lambda x: x[1])
	d = set()
	for (i, j) in w:
		if p >= j:
			d.add(i)
			p -= j
	q = ''
	for i in sorted(d):
		q += s[i]
	print(q)


t = int(input())
import string
from collections import Counter
lst = list(string.ascii_lowercase)
h = {lst[i]: i + 1 for i in range(len(lst))}

def helper(s, p):
	total = 0
	h2 = Counter(s)
	s2 = sorted(s, reverse=True)
	for key in h2.keys():
		total += h[key] * h2[key]
	if total == p:
		return s
	if p == 0:
		return ''
	pt = 0
	while total > p and pt < len(s2):
		total -= h[s2[pt]]
		h2[s2[pt]] -= 1
		pt += 1
	ans = ''
	for i in range(len(s)):
		if h2[s[i]] == 0:
			continue
		else:
			h2[s[i]] -= 1
			ans += s[i]
	return ans
for i in range(t):
	s = input()
	p = int(input())
	print(helper(s, p))


for h in range(int(input())):
	s = input()
	p = int(input())
	x = []
	d = {}
	k = 0
	for i in s:
		x.append(i)
		d[i] = 0
		k += ord(i) - 96
	x.sort(reverse=True)
	c = 0
	for i in x:
		if k <= p:
			break
		c += 1
		k -= ord(i) - 96
	x = x[c:]
	v = ''
	for i in x:
		d[i] += 1
	for i in s:
		if d[i] > 0:
			d[i] -= 1
			v += i
	print(v)


from collections import defaultdict
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	w = input().strip()
	p = int(input())
	d = defaultdict(lambda : 0)
	currPrice = 0
	for i in w:
		d[i] += 1
		currPrice += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		present = d[chr(i + 97)]
		if currPrice <= p:
			break
		kSub = min(d[chr(i + 97)], (currPrice - p + (i + 1) - 1) // (i + 1))
		d[chr(i + 97)] -= kSub
		currPrice -= kSub * (i + 1)
	news = []
	for i in w:
		if d[i] > 0:
			d[i] -= 1
			news.append(i)
	print(''.join(news))


from collections import defaultdict
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	w = input().strip()
	p = int(input())
	d = defaultdict(lambda : 0)
	cp = 0
	for i in w:
		d[i] += 1
		cp += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		o = d[chr(i + 97)]
		if cp <= p:
			break
		ks = min(d[chr(i + 97)], (cp - p + (i + 1) - 1) // (i + 1))
		d[chr(i + 97)] -= ks
		cp -= ks * (i + 1)
	new = []
	for i in w:
		if d[i] > 0:
			d[i] -= 1
			new.append(i)
	print(''.join(new))


import sys
from math import sqrt, ceil, floor, gcd
from collections import Counter, defaultdict
input = lambda : sys.stdin.readline().rstrip('\r\n')

def int_arr():
	return list(map(int, input().split()))

def str_arr():
	return list(map(str, input().split()))

def get_str():
	return map(str, input().split())

def get_int():
	return map(int, input().split())

def get_flo():
	return map(float, input().split())

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 1000000007

def solve():
	s = str(input())
	p = int(input())
	arr = [0 for i in range(26)]
	tot = 0
	for i in range(len(s)):
		x = ord(s[i]) - 97
		arr[x] += 1
		tot += x + 1
	for i in range(25, -1, -1):
		if arr[i] > 0:
			num = arr[i] * (i + 1)
			if tot - num > p:
				tot -= num
				arr[i] = 0
			else:
				tot -= num
				arr[i] = 0
				while p >= tot + (i + 1):
					arr[i] += 1
					tot += i + 1
	ans = ''
	for i in range(len(s)):
		x = ord(s[i]) - 97
		if arr[x] > 0:
			ans += s[i]
			arr[x] -= 1
	print(ans)
T = 1
T = int(input())
while T:
	solve()
	T -= 1


for _ in range(int(input())):
	s = input()
	p = int(input())
	a = sorted([(ord(x) - 96, i) for (i, x) in enumerate(s)])
	m = sum((x for (x, i) in a))
	while m > p:
		t = a.pop()
		m -= t[0]
	print(''.join((chr(x + 96) for (x, i) in sorted(a, key=lambda tup: tup[1]))))


import sys

def solve():
	inp = sys.stdin.readline
	s = inp().strip()
	p = int(inp())
	a = [None] * len(s)
	z = ord('a') - 1
	S = 0
	for i in range(len(s)):
		v = ord(s[i]) - z
		S += v
		a[i] = (v, i)
	a.sort(reverse=True)
	h = list(s)
	if S > p:
		for i in range(len(s)):
			(v, j) = a[i]
			h[j] = ''
			S -= v
			if S <= p:
				break
	print(''.join(h))

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


from collections import Counter
for _ in range(int(input())):
	s = input()
	n = int(input())
	z = Counter(s)
	o = 0
	for i in s:
		o += ord(i) - 96
	for i in range(122, 96, -1):
		x = chr(i)
		if o <= n:
			break
		if z[x]:
			flag = 0
			while z[x]:
				o -= i - 96
				z[x] -= 1
				if o <= n:
					flag = 1
					break
			if flag:
				break
	ans = ''
	for i in s:
		if z[i]:
			ans += i
			z[i] -= 1
	print(ans)


for _ in range(int(input())):
	a = input()
	b = sorted(list(a), reverse=True)
	c = 0
	for i in a:
		c += ord(i) - 96
	k = int(input())
	d = {}
	for i in b:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	i = 0
	while c > k:
		c -= ord(b[i]) - 96
		d[b[i]] -= 1
		i += 1
	f = ''
	for i in a:
		if d[i] != 0:
			f += i
			d[i] -= 1
	print(f)


t = int(input())
for i in range(t):
	w = input()
	p = int(input())
	a = []
	for j in range(len(w)):
		a.append(ord(w[j]) - 96)
	a.sort()
	c = sum(a) - p
	while c > 0:
		c -= a[-1]
		a.pop()
	le = [0] * 26
	for j in range(len(a)):
		le[a[j] - 1] += 1
	ans = ''
	for j in range(len(w)):
		if le[ord(w[j]) - 97] > 0:
			le[ord(w[j]) - 97] -= 1
			ans += w[j]
	print(ans)


for _ in range(int(input())):
	a = input()
	s = sorted(a)
	p = int(input())
	dt = {}
	cnt = 0
	for i in range(len(s)):
		cnt += ord(s[i]) - ord('a') + 1
		if cnt > p:
			break
		dt[s[i]] = dt.get(s[i], 0) + 1
	ans = ''
	for elem in a:
		try:
			if dt[elem] > 0:
				dt[elem] -= 1
				ans += elem
		except:
			pass
	print(ans)


from collections import Counter

def price(c):
	return ord(c) - 96
t = int(input())
for _ in range(t):
	w = list(input())
	C = Counter(w)
	alph = list(C.keys())
	alph.sort()
	alph.reverse()
	D = dict()
	p = int(input())
	pr = sum((price(c) for c in w))
	for a in alph:
		if pr <= p:
			break
		elif pr - C[a] * price(a) >= p:
			pr -= C[a] * price(a)
			D[a] = C[a]
		else:
			remove = -((p - pr) // price(a))
			pr -= remove * price(a)
			D[a] = remove
	for (i, x) in enumerate(w):
		if x in D and D[x] > 0:
			w[i] = ''
			D[x] -= 1
	print(''.join((x for x in w)))


def fn(s, p):
	arr = list(s)
	letters = sorted(list(s), reverse=True)
	sm = sum((ord(letters[i]) - ord('a') for i in range(len(letters))))
	sm += len(letters)
	deletion = sm - p
	del_set = {}
	i = 0
	while i < len(letters) and deletion > 0:
		deletion -= ord(letters[i]) - ord('a') + 1
		del_set[letters[i]] = 1 + del_set.get(letters[i], 0)
		i += 1
	for (i, ele) in enumerate(arr):
		if ele in del_set:
			del_set[ele] -= 1
			if del_set[ele] == 0:
				del del_set[ele]
			arr[i] = ''
	return ''.join(arr)
import collections
t = int(input())
for i in range(t):
	s = input()
	p = int(input())
	st = fn(s, p)
	print(st)


from collections import Counter

def main():
	length = int(input())
	for _ in range(length):
		string = input()
		num = int(input())
		ans = solver(string, num)
		print(''.join(ans))

def solver(string, num):
	value = 0
	for letter in string:
		v = ord(letter) - ord('a') + 1
		value += v
	counter = Counter(string)
	keys = sorted((key for key in counter.keys()))
	if value <= num:
		return list(string)
	for k in reversed(keys):
		for _ in range(counter[k]):
			v = ord(k) - ord('a') + 1
			value -= v
			counter[k] -= 1
			if value <= num:
				return toArr(counter, string)

def toArr(d, string):
	arr = []
	for letter in string:
		if d[letter] > 0:
			arr.append(letter)
			d[letter] -= 1
	return arr
main()


test = int(input())
for t in range(test):
	s = input().rstrip()
	p = int(input().rstrip())
	base = ord('a') - 1
	lst = [(ord(s[i]) - base, i) for i in range(len(s))]
	lst.sort(key=lambda x: x[0])
	sum1 = sum([el[0] for el in lst])
	while sum1 > p:
		(max1, i) = lst.pop()
		sum1 -= max1
	lst.sort(key=lambda x: x[1])
	print(''.join([chr(base + el[0]) for el in lst]))


from collections import defaultdict
import sys
input = sys.stdin.readline
for testcases in range(int(input())):
	weight = input().strip()
	pvar = int(input())
	darray = defaultdict(lambda : 0)
	result = 0
	for i in weight:
		darray[i] += 1
		result += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		curr = darray[chr(i + 97)]
		if result <= pvar:
			break
		extraVar = min(darray[chr(i + 97)], (result - pvar + (i + 1) - 1) // (i + 1))
		darray[chr(i + 97)] -= extraVar
		result -= extraVar * (i + 1)
	helper = []
	for i in weight:
		if darray[i] > 0:
			darray[i] -= 1
			helper.append(i)
	print(''.join(helper))


for p in range(int(input())):
	s = str(input())
	n = int(input())
	a = []
	for i in s:
		a.append(ord(i) - 96)
	a.sort()
	m = sum(a)
	while m > n:
		l = a.pop()
		m -= l
	o = {chr(x): 0 for x in range(96, 140)}
	for i in a:
		o[chr(i + 96)] += 1
	v = ''
	for i in s:
		if o.get(i):
			v += i
			o[i] -= 1
	print(v)


def sol(arr, k):
	weights = [[ord(arr[i]) - ord('a') + 1, i] for i in range(len(arr))]
	weights = sorted(weights)
	final = []
	for (w, i) in weights:
		if w <= k:
			final.append(i)
			k -= w
		else:
			break
	return ''.join([arr[i] for i in sorted(final)])
x = int(input())
for _ in range(x):
	arr = input()
	k = int(input())
	print(sol(arr, k))


def solve(s, p):
	alphas = 'abcdefghijklmnopqrstuvwxyz'
	val = {alphas[i]: i + 1 for i in range(26)}
	cur_val = 0
	for i in s:
		cur_val += val[i]
	if cur_val <= p:
		return s
	c = {alphas[i]: 0 for i in range(26)}
	for i in s:
		c[i] += 1
	cur_dif = cur_val - p
	rev_alphas = alphas[::-1]
	rem_d = dict()
	for i in rev_alphas:
		if c[i] == 0:
			continue
		if cur_dif == 0:
			break
		this_val = val[i]
		if this_val * c[i] <= cur_dif:
			cur_dif -= this_val * c[i]
			rem_d.update({i: c[i]})
			continue
		else:
			count = cur_dif // this_val
			rem = cur_dif % this_val
			if rem != 0:
				count += 1
			rem_d.update({i: count})
			cur_dif = 0
	i = 0
	ans = ''
	while i < len(s):
		if s[i] in rem_d and rem_d[s[i]] > 0:
			rem_d[s[i]] -= 1
		else:
			ans += s[i]
		i += 1
	return ans

def main():
	t = int(input())
	for i in range(t):
		s = input()
		p = int(input())
		res = solve(s, p)
		print(res)
	return
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	w = list(input().rstrip())
	length = len(w)
	p = int(input())
	list_ord = [ord(s) - 96 for s in w]
	list_ord.sort()
	check = length
	for idx in range(length):
		p -= list_ord[idx]
		if p < 0:
			check = idx
			break
	dict_chr = {chr(x): 0 for x in range(97, 123)}
	for idx in range(check):
		dict_chr[chr(list_ord[idx] + 96)] += 1
	answer = ''
	for s in w:
		if dict_chr[s] != 0:
			answer += s
			dict_chr[s] -= 1
	print(answer)


for iii in range(int(input())):
	s = input()
	n = int(input())
	q1 = {}
	for i in range(1, 27, 1):
		q1[i] = 0
	s1 = list(s)
	s1.sort()
	summ = 0
	for i in range(len(s1)):
		x = ord(s1[i]) - 96
		if summ + x <= n:
			summ += x
			q1[x] += 1
		else:
			break
	s2 = ''
	for i in range(len(s)):
		x = ord(s[i]) - 96
		if q1[x] > 0:
			s2 = s2 + s[i]
			q1[x] -= 1
	print(s2)


for _ in range(int(input())):
	s = input()
	p = int(input())
	arr = [0 for i in range(27)]
	current = 0
	for c in s:
		z = ord(c) - 96
		arr[z] += 1
		current += z
	i = 26
	while current > p and i > 0:
		if arr[i] == 0:
			i -= 1
			continue
		current -= i
		arr[i] -= 1
	ans = ''
	for c in s:
		z = ord(c) - 96
		if arr[z] > 0:
			ans += c
			arr[z] -= 1
	print(ans)


def Solver(st, k):
	arr = []
	su = 0
	for (i, char) in enumerate(st):
		arr.append((ord(char) - 96, i))
		su += ord(char) - 96
	sorted_arr = sorted(arr)
	while su > k:
		(nu, i) = sorted_arr.pop(-1)
		su -= nu
		arr[i] = 0
	return ''.join([st[i] for i in range(len(st)) if arr[i]])
for _ in range(int(input())):
	st = input()
	k = int(input())
	print(Solver(st, k))


for test in range(int(input())):
	s = input()
	p = int(input())
	a = [0] * 26
	cs = 0
	for ii in s:
		a[ord(ii) - 97] += 1
		cs += ord(ii) - 96
	pointer = 25
	while pointer >= 0 and cs > p:
		maxp = (cs - p) // (pointer + 1)
		maxp = min(maxp, a[pointer])
		cs = cs - maxp * (pointer + 1)
		a[pointer] -= maxp
		if a[pointer] > 0 and cs - p < pointer + 1 and (cs > p):
			a[pointer] -= 1
			cs -= pointer + 1
		pointer -= 1
	ss = ''
	for ii in s:
		if a[ord(ii) - 97] > 0:
			ss += ii
			a[ord(ii) - 97] -= 1
	print(ss)


t = int(input())
for i in range(t):
	w = input()
	p = int(input())
	nums = []
	for k in w:
		nums.append(ord(k) - 96)
	nums = sorted(nums)
	count = sum(nums)
	while count > p:
		a = nums.pop()
		count -= a
	nums1 = {chr(b): 0 for b in range(96, 140)}
	for d in nums:
		nums1[chr(d + 96)] += 1
	e = ''
	for r in w:
		if nums1.get(r):
			e += r
			nums1[r] -= 1
	print(e)


import math
from collections import Counter, deque
from sys import stdout
import time
from math import factorial, log, gcd
import sys
from decimal import Decimal
import heapq

def S():
	return sys.stdin.readline().split()

def I():
	return [int(i) for i in sys.stdin.readline().split()]

def II():
	return int(sys.stdin.readline())

def IS():
	return sys.stdin.readline().replace('\n', '')

def main():
	s = IS()
	n = len(s)
	list_s = []
	total = 0
	a = ord('a') - 1
	for i in range(n):
		el = ord(s[i]) - a
		total += el
		list_s.append((el, i))
	list_s = list(sorted(list_s, key=lambda x: x[0]))
	p = II()
	idxs = set()
	if total > p:
		for i in range(n - 1, -1, -1):
			(el, idx) = list_s[i]
			total -= el
			idxs.add(idx)
			if total <= p:
				break
	ans = ''
	for i in range(n):
		if i not in idxs:
			ans += s[i]
	print(ans)
for _ in range(II()):
	main()


t = int(input())
for _ in range(0, t):
	s = input()
	p = int(input())
	larr = [0 for i in range(0, 26)]
	len1 = len(s)
	c = 0
	for i in range(0, len1):
		larr[ord(s[i]) - 97] += 1
		c += ord(s[i]) - 96
	if c <= p:
		print(s)
	else:
		for i in range(25, -1, -1):
			diff = c - p
			if diff % (i + 1) == 0:
				req = diff // (i + 1)
			else:
				req = diff // (i + 1) + 1
			if larr[i] >= req:
				larr[i] -= req
				c -= req * (i + 1)
			else:
				c -= larr[i] * (i + 1)
				larr[i] = 0
			if c <= p:
				break
		s1 = ''
		for i in range(0, len1):
			if larr[ord(s[i]) - 97] > 0:
				larr[ord(s[i]) - 97] -= 1
				s1 += s[i]
		print(s1)


from collections import defaultdict
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	w = input().strip()
	p = int(input())
	d = defaultdict(lambda : 0)
	cur = 0
	for i in w:
		d[i] += 1
		cur += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		present = d[chr(i + 97)]
		if cur <= p:
			break
		sub = min(d[chr(i + 97)], (cur - p + (i + 1) - 1) // (i + 1))
		d[chr(i + 97)] -= sub
		cur -= sub * (i + 1)
	n = []
	for i in w:
		if d[i] > 0:
			d[i] -= 1
			n.append(i)
	print(''.join(n))


from collections import defaultdict
import sys
import math
input = sys.stdin.readline
for i in range(int(input())):
	w = input().strip()
	p = int(input())
	d = defaultdict(lambda : 0)
	curPrice = 0
	for i in w:
		d[i] += 1
		curPrice += ord(i) - 97 + 1
	for i in range(25, -1, -1):
		present = d[chr(i + 97)]
		if curPrice <= p:
			break
		mat = min(d[chr(i + 97)], (curPrice - p + (i + 1) - 1) // (i + 1))
		d[chr(i + 97)] -= mat
		curPrice -= mat * (i + 1)
	new = []
	for i in w:
		if d[i] > 0:
			d[i] -= 1
			new.append(i)
	print(''.join(new))


t = int(input())
for _ in range(t):
	s = input()
	k = int(input())
	char = ' abcdefghijklmnopqrstuvwxyz'
	index = {}
	for i in range(1, len(char)):
		index[char[i]] = i
	res = 0
	check = [0] * 27
	for i in s:
		check[index[i]] += 1
		res += index[i]
	i = 26
	while i > 0:
		x = (res - k) // i
		y = (res - k) % i
		if check[i] > 0:
			if y:
				if check[i] * i <= i * (x + 1):
					res -= check[i] * i
					check[i] = check[i] - check[i]
				else:
					res -= i * (x + 1)
					check[i] -= x + 1
			elif check[i] * i <= i * x:
				res -= check[i] * i
				check[i] = check[i] - check[i]
			else:
				res -= i * x
				check[i] -= x
		i -= 1
		if res <= k:
			break
	ans = ''
	for i in s:
		x = index[i]
		if check[x] > 0:
			ans += i
			check[x] -= 1
	print(ans)


t = int(input())
while t:
	w = input()
	p = int(input())
	total_cost = sum([ord(e) - ord('a') + 1 for e in w])
	if total_cost <= p:
		print(w)
	else:
		to_remove = {}
		for c in sorted(w)[::-1]:
			total_cost = total_cost - (ord(c) - ord('a') + 1)
			if c not in to_remove:
				to_remove[c] = 0
			to_remove[c] += 1
			if total_cost <= p:
				break
		ans = ''
		for c in w:
			if c in to_remove and to_remove[c] > 0:
				to_remove[c] -= 1
				continue
			ans += c
		print(ans)
	t -= 1


d = {}
d1 = {}
q = 'abcdefghijklmnopqrstuvwxyz'
for r in range(0, len(q)):
	d[q[r]] = r + 1
for f in q:
	d1[f] = 0
for j in range(0, int(input())):
	s = input()
	n = int(input())
	sum = 0
	for x in s:
		sum = sum + d[x]
	if sum <= n:
		print(s, end='')
	else:
		l = []
		l = list(s)
		l.sort(reverse=True)
		l1 = []
		z = 0
		while sum > n:
			sum = sum - d[l[z]]
			d1[l[z]] += 1
			z = z + 1
		for m in s:
			if d1[m] == 0:
				print(m, end='')
			else:
				d1[m] -= 1
	print()


def solve(s, n):
	freq = [0 for i in range(26)]
	for ch in s:
		freq[ord(ch) - 97] += 1
	val = 0
	for i in range(26):
		if freq[i] > 0:
			val += (i + 1) * freq[i]
	fi = 25
	while val > n:
		x = (val - n) // (fi + 1)
		val -= min(freq[fi], x) * (fi + 1)
		freq[fi] -= min(freq[fi], x)
		if freq[fi] != 0 and val > n:
			val -= fi + 1
			freq[fi] -= 1
		fi -= 1
	res = ''
	for ch in s:
		ind = ord(ch) - 97
		if freq[ind] > 0:
			res += ch
			freq[ind] -= 1
	return res
for _ in range(int(input())):
	s = input()
	n = int(input())
	print(solve(s, n))


t = int(input())
for i1 in range(t):
	w = list(input())
	p = int(input())
	d = {'a': [1, 0], 'b': [2, 0], 'c': [3, 0], 'd': [4, 0], 'e': [5, 0], 'f': [6, 0], 'g': [7, 0], 'h': [8, 0], 'i': [9, 0], 'j': [10, 0], 'k': [11, 0], 'l': [12, 0], 'm': [13, 0], 'n': [14, 0], 'o': [15, 0], 'p': [16, 0], 'q': [17, 0], 'r': [18, 0], 's': [19, 0], 't': [20, 0], 'u': [21, 0], 'v': [22, 0], 'w': [23, 0], 'x': [24, 0], 'y': [25, 0], 'z': [26, 0]}
	for i in w:
		d[i][1] += 1
	letters = []
	for i in d:
		if d[i][1] != 0:
			letters.append(i)
	l = [ord(i) - 96 for i in w]
	n = sum(l)
	while n > p:
		n1 = n - p
		if n1 <= d[letters[-1]][0] * d[letters[-1]][1]:
			while n > p:
				n -= d[letters[-1]][0]
				d[letters[-1]][1] -= 1
		else:
			n -= d[letters[-1]][0] * d[letters[-1]][1]
			letters.pop()
	l1 = []
	for i in range(len(w)):
		if w[i] in letters:
			if d[w[i]][1] != 0:
				l1.append(w[i])
				d[w[i]][1] -= 1
	print(''.join(l1))


from collections import Counter
from math import ceil
t = int(input())
for _ in range(t):
	s = input()
	p = int(input())
	freq = Counter(list(s))
	letters = set(freq.keys())
	tot_sum = sum([(ord(u) - 96) * v for (u, v) in freq.items()])
	for i in range(26, 0, -1):
		if tot_sum <= p:
			break
		char = chr(i + 96)
		if char not in letters:
			continue
		x = freq[char]
		y = ceil((tot_sum - p) / i)
		freq[char] -= min(x, y)
		tot_sum -= i * min(x, y)
	new_s = ''
	for i in s:
		if freq[i] > 0:
			new_s += i
			freq[i] -= 1
	print(new_s)


t = int(input())
for i in range(t):
	mat = [[] for i in range(26)]
	s = input()
	p = int(input())
	arr = list(s)
	val = 0
	for (i, c) in enumerate(arr):
		ind = ord(c) - ord('a')
		mat[ind].append(i)
		val += ind + 1
	i = 25
	while i >= 0 and val > p:
		if len(mat[i]) == 0:
			i -= 1
			continue
		ind = mat[i].pop()
		val -= i + 1
		arr[ind] = ''
	print(''.join(arr))


from collections import defaultdict

def solveThis():
	dd = defaultdict(lambda : 0)
	ss = input().strip()
	n = int(input())
	price = 0
	for j in ss:
		dd[j] += 1
		price += ord(j) - 96
	for i in range(25, -1, -1):
		pre = dd[chr(i + 97)]
		if price <= n:
			break
		sub = min(dd[chr(i + 97)], (price - n + (i + 1) - 1) // (i + 1))
		dd[chr(i + 97)] -= sub
		price -= sub * (i + 1)
	ans = []
	for k in ss:
		if dd[k] > 0:
			dd[k] -= 1
			ans.append(k)
	print(''.join(ans))

def main():
	t = int(input())
	for _ in range(t):
		solveThis()
main()


import sys

def input_int():
	return int(sys.stdin.readline())

def input_str():
	return sys.stdin.readline().strip()

def input_int_arr():
	return list(map(int, sys.stdin.readline().split()))

def print_ln(num):
	sys.stdout.write(str(num) + '\n')

def print_only(num):
	sys.stdout.write(str(num))

def solve():
	inp_str = input_str()
	p = input_int()
	chars_counter = [0] * 27
	total_price = 0
	for char in inp_str:
		char_index = ord(char) - 96
		total_price += char_index
		chars_counter[char_index] += 1
	for i in range(26, 0, -1):
		while chars_counter[i] > 0:
			if total_price > p:
				total_price -= i
				chars_counter[i] -= 1
			else:
				break
	for char in inp_str:
		char_index = ord(char) - 96
		if chars_counter[char_index] > 0:
			print_only(char)
			chars_counter[char_index] -= 1
	print_ln('')

def main():
	t = input_int()
	for _ in range(t):
		solve()
main()


import math
t = int(input())
for _ in range(t):
	s = input()
	p = int(input())
	a = [0] * 26
	n = len(s)
	S = 0
	for i in range(n):
		S += ord(s[i]) - 96
		a[ord(s[i]) - 97] += 1
	b = []
	for i in range(26):
		if a[i] != 0:
			b.append([i + 1, a[i]])
	if p < b[0][0]:
		print('')
		continue
	b = b[::-1]
	i = 0
	while S > p:
		d = S - p
		x = math.ceil(d / b[0][0])
		if x >= b[i][1]:
			S -= b[i][1] * b[i][0]
			b[i][1] = 0
			i += 1
		else:
			S -= b[i][0] * x
			b[i][1] -= x
	d = dict(b)
	s_new = ''
	for i in s:
		if d[ord(i) - 96] > 0:
			s_new = s_new + i
			d[ord(i) - 96] -= 1
	print(s_new)


from collections import defaultdict
import math
for _ in range(int(input())):
	s = input()
	p = int(input())
	su = 0
	d = defaultdict(lambda : [])
	cd = defaultdict(int)
	for (i, k) in enumerate(s):
		su += ord(k) - 96
		d[k].append(i)
		cd[k] += 1
	a = su - p
	if a <= 0:
		print(s)
		continue
	keys = list(d.keys())
	keys.sort(reverse=True)
	indexs = []
	i = 0
	while a > 0:
		val = ord(keys[i]) - 96
		m = cd[keys[i]] * val
		if m > a:
			possible = math.ceil(a / val)
			indexs.extend(d[keys[i]][:possible])
			a -= a
		else:
			indexs.extend(d[keys[i]])
			a -= m
		i += 1
	not_take = {}
	for i in indexs:
		not_take[i] = True
	ans = ''
	for (index, val) in enumerate(s):
		if index not in not_take:
			ans += val
	print(ans)


def solve():
	t = input()
	p = int(input())
	n = len(t)
	w = lambda i: ord(t[i]) - ord('a') + 1
	inds = sorted(range(n), key=lambda i: w(i))
	s = sum(map(w, range(n)))
	res = [t[i] for i in range(n)]
	while s > p:
		i = inds[-1]
		inds.pop()
		res[i] = ''
		s -= w(i)
	print(''.join(res))
tn = int(input())
for _ in range(tn):
	solve()