Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.

After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!

To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.

Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!

Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.

Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds:  $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$;  There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. 


-----Input-----

The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.

Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.

It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.


-----Output-----

For each test case, output a single line containing a single string, which is either  the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them;  three dashes (the string "---" without quotes) if it is impossible. 


-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA

Output
AMAZON
---
APPLE



-----Note-----

In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".

It is impossible to improve the product's name in the second test case and satisfy all conditions.

In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".

Difficulty: MEDIUM_HARD
Tags: Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
N = int(input())
for n in range(N):
	(a, b) = input().split(' ')
	al = list(a)
	bl = list(b)
	al_org = al[:]
	al.sort()
	N = len(al)
	flag = False
	for i in range(N):
		if al[i] != a[i]:
			temp = 'Z'
			pot = -1
			for j in range(i, N)[::-1]:
				flag = True
				if temp > a[j]:
					pot = j
					temp = a[j]
		if flag:
			(al_org[i], al_org[pot]) = (al_org[pot], al_org[i])
			break
	if ''.join(al_org) < ''.join(bl):
		print(''.join(al_org))
	else:
		print('---')


n = int(input())
al = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
for i in range(n):
	(a, b) = map(str, input().split())
	p = []
	for j in list(a):
		p.append(al.index(j))
	if ''.join(sorted(list(a))) == a:
		if a < b:
			print(a)
		else:
			print('---')
	else:
		madp = [100] * (1 + len(p))
		p.reverse()
		kou = [-1] * len(p)
		ma = -1
		for j in range(len(p)):
			if p[j] > madp[j]:
				kou[j] = madp[j]
				ma = max(ma, j)
			madp[j + 1] = min(p[j], madp[j])
		newa = list(a)
		tmp = newa[len(p) - 1 - ma]
		q = len(p) - 1 - list(a)[::-1].index(al[kou[ma]])
		newa[len(p) - 1 - ma] = newa[q]
		newa[q] = tmp
		if ''.join(newa) < b:
			print(''.join(newa))
		else:
			print('---')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict

def main():
	for j in range(int(input())):
		v0 = [k for k in input().split()]
		str1 = [ord(k) - 65 for k in v0[0]]
		str2 = [ord(k) - 65 for k in v0[1]]
		n = len(str1)
		m = len(str2)
		length = min(n, m)
		dict1 = defaultdict(list)
		for s in range(n):
			dict1[str1[s]].append(s)
		ind1 = 0
		ind2 = 0
		for s in range(n):
			broke = False
			for k in range(26):
				if len(dict1[k]) > 0 and dict1[k][-1] > s and (k < str1[s]):
					ind1 = s
					ind2 = dict1[k][-1]
					broke = True
					break
				elif k >= str1[s]:
					break
			if broke == True:
				break
		(str1[ind1], str1[ind2]) = (str1[ind2], str1[ind1])
		broke = False
		c1 = 0
		for s in range(length):
			if str1[s] == str2[s]:
				c1 += 1
				continue
			else:
				if str1[s] < str2[s]:
					broke = True
				break
		if broke == True or (c1 == length and n < m):
			ans = [chr(k + 65) for k in str1]
			print(''.join(ans))
		else:
			print('---')
main()


import sys
rl = lambda : sys.stdin.readline().strip()
N = int(rl())
for i in range(N):
	(s_str, c_str) = rl().split()
	if s_str < c_str:
		print(s_str)
		continue
	(s, c) = (list(s_str), list(c_str))
	s_sorted = sorted(s)
	if s_sorted == s:
		print('---')
		continue
	for i in range(len(s)):
		if s[i] != s_sorted[i]:
			indices_to_swap_with = list()
			for j in range(i, len(s)):
				if s[j] == s_sorted[i]:
					indices_to_swap_with.append(j)
			max_index = max(indices_to_swap_with)
			(s[i], s[max_index]) = (s[max_index], s[i])
			break
	(s_str, c_str) = (''.join(s), ''.join(c))
	if s_str < c_str:
		print(s_str)
	else:
		print('---')


for _ in range(int(input())):
	(s, c) = map(str, input().split())
	k = list(s)
	y = len(s)
	if s < c:
		print(s)
	else:
		for i in range(y):
			t = s[i]
			p = 0
			for j in range(i + 1, y):
				if t >= s[j]:
					t = s[j]
					p = j
			if t != s[i]:
				(k[i], k[p]) = (k[p], k[i])
				break
		s = ''.join(k)
		if s < c:
			print(s)
		else:
			print('---')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(A, B) = sys.stdin.readline().split()
	(a, b) = (list(A), list(B))
	c = a.copy()
	c.sort()
	if c >= b:
		print('---')
	else:
		n = len(a)
		for i in range(n):
			if c[i] != a[i]:
				break
		for j in range(n - 1, -1, -1):
			if a[j] == c[i]:
				break
		(a[i], a[j]) = (a[j], a[i])
		if a < b:
			print(''.join((x for x in a)))
		else:
			print('---')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(a, b) = input().split()
	a = a.strip()
	b = b.strip()
	if a < b:
		print(a)
		continue
	n = len(a)
	m = len(b)
	a = list(a)
	f = 0
	b = list(b)
	for i in range(min(n, m)):
		if f:
			break
		if a[i] == b[i]:
			for j in range(i + 1, n):
				if a[j] < b[i]:
					(a[j], a[i]) = (a[i], a[j])
					f = 1
					break
		elif a[i] > b[i]:
			curr = b[i]
			for j in range(i + 1, n):
				if a[j] < b[i]:
					(a[j], a[i]) = (a[i], a[j])
					f = 1
					break
			break
	if f:
		print(''.join(a))
		continue
	for i in range(min(n, m)):
		if f:
			break
		if a[i] == b[i]:
			continue
		for j in range(n - 1, i, -1):
			if a[j] <= b[i]:
				(a[j], a[i]) = (a[i], a[j])
				f = 1
				break
	if f:
		if a < b:
			print(''.join(a))
		else:
			print('---')
	else:
		print('---')


import bisect
for _ in range(int(input())):
	(s, t) = input().split()
	(s, n) = (list(s), len(s))
	for i in range(n - 1):
		if s[i] > s[i + 1]:
			k = min(range(n - 1, i, -1), key=lambda _: s[_])
			j = bisect.bisect(s, s[k], 0, i + 1)
			(s[k], s[j]) = (s[j], s[k])
			break
	s = ''.join(s)
	print(s if s < t else '---')


q = int(input())
for i in range(q):
	ok = [x for x in input().split()]
	r = ok[0]
	s = ok[1]
	w = [ord(r[i]) for i in range(len(r))]
	w.sort()
	w = [chr(w[i]) for i in range(len(r))]
	first = True
	at = -1
	for j in range(len(r)):
		if w[j] != r[j]:
			first = False
			at = j
			break
	if first == False:
		t = r[::-1].find(w[at])
		r = r[:at] + w[at] + r[at + 1:len(r) - 1 - t] + r[at] + r[len(r) - t:]
	if r < s:
		print(r)
	else:
		print('---')


for _ in range(int(input())):
	(a, b) = map(list, input().split())
	for i in range(len(a)):
		if a[i] != min(a[i:]):
			x = ''.join(a[i:])
			j = i + x.rfind(min(x))
			(a[i], a[j]) = (a[j], a[i])
			break
	print(''.join(a) if a < b else '---')


t = int(input())
for i in range(t):
	(s, c) = input().split()
	s = list(s)
	curr = ''.join(s)
	swapped = False
	for j in range(len(s)):
		for k in range(j + 1, len(s)):
			if s[k] < s[j]:
				(s[j], s[k]) = (s[k], s[j])
				new = ''.join(s)
				if new < curr:
					curr = new
					swapped = True
				(s[j], s[k]) = (s[k], s[j])
		if swapped:
			break
	if curr < c:
		print(curr)
	else:
		print('---')


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations

def main():
	for _ in range(int(input())):
		(a, b) = input().split()
		if a < b:
			print(a)
		else:
			n = len(a)
			m = len(b)
			dp = [[0 for _ in range(26)] for i in range(n + 1)]
			for i in range(n):
				dp[i + 1][ord(a[i]) - 65] += 1
			for i in range(1, n + 1):
				for j in range(26):
					dp[i][j] += dp[i - 1][j]
			(ind, first) = (-1, -1)
			for i in range(min(n, m)):
				if a[i] >= b[i]:
					for j in range(ord(b[i]) - 65):
						if dp[-1][j] - dp[i + 1][j] >= 1:
							ind = i
							break
					if first == -1 and a[i] > b[i]:
						first = i
					if ind != -1 or a[i] > b[i]:
						break
			if ind == -1:
				if first == -1:
					print('---')
				else:
					mi = 'Z'
					for i in range(first + 1, n):
						if a[i] <= b[first]:
							if mi >= a[i]:
								ind = i
								mi = a[i]
					if ind == -1:
						print('---')
					else:
						a = list(a)
						(a[ind], a[first]) = (a[first], a[ind])
						a = ''.join(a)
						if a < b:
							print(a)
						else:
							print('---')
			else:
				f = 0
				for i in range(ind + 1, n):
					if a[i] < b[ind]:
						f = i
						break
				a = list(a)
				(a[ind], a[f]) = (a[f], a[ind])
				print(''.join(a))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(s1, s2) = input().split()
	arr = list(s1)
	ans = sorted(arr)
	dic = {}
	for i in range(len(s1)):
		try:
			dic[arr[i]].append(i)
		except:
			dic[arr[i]] = [i]
	if s1 < s2:
		print(s1)
		continue
	for i in range(len(s1)):
		if arr[i] != ans[i]:
			z = dic[ans[i]][-1]
			temp = arr[i]
			arr[i] = ans[i]
			arr[z] = temp
			break
	if ''.join(arr) < s2:
		print(''.join(arr))
	else:
		print('---')


from itertools import accumulate

def solve(s, t):
	IMP = '---'
	if s < t:
		return s
	n = len(s)
	next_smallest = [None for _ in range(n + 1)]
	for (i, c) in enumerate(s[::-1]):
		prev = next_smallest[n - i]
		if prev is None or c < prev[0]:
			best = (c, n - i - 1)
		else:
			best = prev
		next_smallest[n - i - 1] = best
	next_smallest.pop()
	for (i, a) in enumerate(s):
		if i == n - 1:
			continue
		s2 = list(s)
		(b, ind) = next_smallest[i + 1]
		(s2[i], s2[ind]) = (s2[ind], s2[i])
		s2 = ''.join(s2)
		if s2 < t:
			return s2
	return IMP
for _ in range(int(input())):
	(s, t) = input().split()
	print(solve(s, t))


def main():
	t = int(input())
	for _ in range(t):
		(s, c) = map(str, input().split())
		ss = list(s)
		cc = list(c)
		sss = ss[:]
		sss.sort()
		k = -1
		for i in range(len(s)):
			if sss[i] != ss[i]:
				k = i
				break
		if s < c:
			print(s)
		else:
			if k != -1:
				for i in range(len(s) - 1, k, -1):
					if ss[i] == sss[k]:
						(ss[k], ss[i]) = (ss[i], ss[k])
			p = ''.join(ss)
			if p < c:
				print(p)
			else:
				print('---')
	return
main()


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	(s, c) = inp().split()
	s = list(s)
	l = len(s)
	mnl = []
	mn = s[-1]
	for i in range(l - 1, -1, -1):
		mn = min(mn, s[i])
		mnl.append(mn)
	mnl.reverse()
	for i in range(l):
		if s[i] != mnl[i]:
			ind = s[::-1].index(mnl[i])
			(s[i], s[l - 1 - ind]) = (s[l - 1 - ind], s[i])
			break
	s = ''.join(s)
	out(s if s < c else '---')


for _ in range(int(input())):
	(a, b) = input().split()
	a = list(a)
	for i in range(len(a) - 1):
		j = min((i for i in range(i + 1, len(a))), key=lambda x: (a[x], -x))
		if a[i] > a[j]:
			(a[i], a[j]) = (a[j], a[i])
			break
	a = ''.join(a)
	if a < b:
		print(a)
	else:
		print('---')


def mai(m, b):
	a = list(m)
	l = a
	l.sort()
	str1 = ''
	str1 = str1.join(l)
	if m < b:
		return m
	elif str1 < b:
		for i in range(len(m)):
			str1 = ''
			a = list(m)
			if a[i] != l[i]:
				for j in range(len(a) - 1, -1, -1):
					if l[i] == a[j]:
						temp = a[j]
						a[j] = a[i]
						a[i] = temp
						str1 = ''
						str1 = str1.join(a)
						if str1 < b:
							return str1
						else:
							break
	return '---'
for i in range(int(input())):
	print(mai(*input().split()))


for _ in range(int(input())):
	(s, t) = input().split()
	n = len(s)
	m = len(t)
	cnt = [0] * 255
	for i in s:
		cnt[ord(i)] += 1
	for i in range(n):
		if i == m:
			s = '---'
			break
		elif s[i] < t[i]:
			break
		else:
			if s[i] > t[i]:
				cnt[ord(s[i])] -= 1
				for j in list(range(ord(t[i]) - 1, 50, -1)) + [ord(t[i])]:
					if cnt[j] >= 1:
						for k in range(n - 1, i, -1):
							if s[k] == chr(j):
								s = s[:i] + chr(j) + s[i + 1:k] + s[i] + s[k + 1:]
								break
						break
				else:
					s = '---'
				break
			else:
				cnt[ord(s[i])] -= 1
				for j in range(ord(t[i]) - 1, 50, -1):
					if cnt[j] >= 1:
						for k in range(n - 1, i, -1):
							if s[k] == chr(j):
								s = s[:i] + chr(j) + s[i + 1:k] + s[i] + s[k + 1:]
								break
						break
				else:
					continue
				break
			print(s)
	if s >= t:
		s = '---'
	print(s)


def find_lexicographically_smaller_swap(s, t):
	mins = sorted(s)
	for i in range(len(s)):
		if s[i] != mins[i]:
			j = max((j for (j, v) in enumerate(s[i:], i) if v == mins[i]))
			s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
			break
	return s if s < t else '---'
for cas in range(int(input())):
	print(find_lexicographically_smaller_swap(*input().split()))


for _ in range(int(input())):
	l = input().split(' ')
	s1 = l[0]
	s2 = l[-1]
	t = sorted(s1)
	i = 0
	while i < len(s1) and s1[i] == t[i]:
		i += 1
	if i < len(s1):
		j = s1.rindex(t[i])
		s1 = s1[:i] + s1[j] + s1[i + 1:j] + s1[i] + s1[j + 1:]
	if s1 < s2:
		print(s1)
	else:
		print('---')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

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

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

def getStr():
	return input().strip()
n = getInt()
for i in range(n):
	(s, c) = getStr().split()
	p = False
	for i in range(len(s) - 1):
		ch = i
		for j in range(i + 1, len(s)):
			if s[j] <= s[ch]:
				ch = j
		if s[ch] < s[i]:
			s = s[:i] + s[ch] + s[i + 1:ch] + s[i] + s[ch + 1:]
			break
	if s < c:
		print(s)
	else:
		print('---')


for i in range(int(input())):
	(s, c) = map(list, input().split())
	for i in range(len(s)):
		if s[i] != min(s[i:]):
			x = ''.join(s[i:])
			(s[i], s[i + x.rfind(min(x))]) = (s[i + x.rfind(min(x))], s[i])
			break
	print(''.join(s) if s < c else '---')


T = int(input())
for _ in range(T):
	(s, c) = input().split()
	n = s.__len__()
	flag = True if s < c else False
	s_ = s
	i = n - 2
	j = n - 1
	while i > -1 and (not flag):
		s_ = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
		j = min(i, j, key=lambda x: (s[x], -x))
		if s_ < c:
			flag = True
		i -= 1
	print(s_ if flag else '---')


def swapPositions(list, pos1, pos2):
	(list[pos1], list[pos2]) = (list[pos2], list[pos1])
	return list

def solve(s):
	ll = [0] * 26
	for i in range(len(s[0])):
		ll[ord(s[0][i]) - 65] += 1
	path = list()
	for i in range(26):
		if ll[i] != 0:
			path.append(i)
	arr1 = list()
	for i in range(len(s[0])):
		arr1.append(s[0][i])
	swap = -1
	ts = -1
	curr = 0
	ini = path[0]
	for i in range(len(arr1)):
		if ini == ord(arr1[i]) - ord('A'):
			ll[ini] -= 1
		elif ini < ord(arr1[i]) - ord('A'):
			swap = ini
			ts = i
			break
		if ll[ini] == 0:
			if curr != len(path) - 1:
				curr += 1
				ini = path[curr]
	if ts != -1:
		swap = swap + 65
		ch = chr(swap)
		for i in range(len(arr1)):
			if arr1[i] == ch:
				ind = i
		arr1 = swapPositions(arr1, ts, ind)
	for i in range(min(len(s[0]), len(s[1]))):
		if arr1[i] > s[1][i]:
			print('---')
			return
		if arr1[i] < s[1][i]:
			for i in range(len(arr1)):
				print(arr1[i], end='')
			print()
			return
	if len(s[0]) < len(s[1]):
		for i in range(len(arr1)):
			print(arr1[i], end='')
		print()
	else:
		print('---')
		return
t = int(input())
for i in range(t):
	s = input()
	s = s.split()
	solve(s)


def swap_char(arr, x, y):
	arr = list(arr)
	(arr[x], arr[y]) = (arr[y], arr[x])
	return ''.join(arr)
for _ in [0] * int(input()):
	(a, b) = [x for x in input().split()]
	lens = len(a)
	for i in range(lens - 1):
		curr = a[i + 1]
		poss = -1
		for j in range(i + 1, lens):
			if a[j] <= curr:
				poss = j
				curr = a[j]
		if curr < a[i]:
			a = swap_char(a, poss, i)
			break
	print(['---', a][a < b])


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

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

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for t in range(N()):
		(s, c) = input().split()
		(s, c) = (list(s), list(c))
		sa = sorted(s)
		for i in range(len(s)):
			if sa[i] != s[i]:
				for j in range(len(s) - 1, i, -1):
					if s[j] == sa[i]:
						(s[i], s[j]) = (s[j], s[i])
				break
		if s < c:
			print(''.join(s))
		else:
			print('---')
main()


t = int(input())
for _ in range(t):
	(s, c) = input().split()
	idx = {}
	for (i, cc) in enumerate(list(s)):
		idx[cc] = i
	si = sorted(s)
	for i in range(len(s)):
		if si[i] != s[i]:
			sl = list(s)
			(sl[i], sl[idx[si[i]]]) = (sl[idx[si[i]]], sl[i])
			s = ''.join(sl)
			break
	if s < c:
		print(s)
	else:
		print('---')


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

def array():
	return [int(i) for i in input().split()]
for _ in range(int(input())):
	(a, b) = input().split()
	a = list(a)
	a1 = sorted(a)
	for i in range(len(a)):
		if a[i] != a1[i]:
			ind = len(a) - 1 - a[::-1].index(a1[i])
			(a[ind], a[i]) = (a[i], a[ind])
			break
	a = ''.join(a)
	if a < b:
		print(a)
	else:
		print('---')


for t in range(int(input())):
	(s1, s2) = input().split()
	if s1 < s2:
		print(s1)
		continue
	found = False
	i = 0
	for i in range(min(len(s1), len(s2))):
		if s2[i] != s1[i]:
			found = True
			break
	flag = False
	(mex, index) = (s1[0], 0)
	for k in range(1, i + 1):
		if s1[k] < s1[k - 1]:
			print(s1[:k - 1] + s1[k] + s1[k - 1] + s1[k + 1:])
			flag = True
			break
		elif k < i:
			(mex, index) = (s1[k], k)
	if not flag:
		for (j, c) in enumerate(s1[i:], start=i):
			if c < mex and i != 0:
				print(s1[:index] + c + s1[index + 1:j] + mex + s1[j + 1:])
				flag = True
				break
			if c <= s2[i]:
				if s1[:i] + c + s1[i + 1:j] + s1[i] + s1[j + 1:] < s2:
					print(s1[:i] + c + s1[i + 1:j] + s1[i] + s1[j + 1:])
					flag = True
					break
	if not flag:
		print('---')


t = int(input())
for _ in range(t):
	(s1, s2) = input().split()
	if s1 < s2:
		print(s1)
		continue
	s3 = sorted(s1)
	s1 = list(s1)
	x = y = -1
	j = 0
	for i in range(len(s3)):
		if s1[i] == s3[j]:
			j += 1
			continue
		if s1[i] > s3[j]:
			x = i
			y = j
			r = ''.join(s1).rfind(s3[j])
			(s1[i], s1[r]) = (s1[r], s1[i])
			break
	s1 = ''.join(s1)
	if s1 < s2:
		print(s1)
	else:
		print('---')


def solve(s, t):
	(lst, n) = (list(s), len(s))
	for i in range(n - 2, -1, -1):
		lst[i] = min(lst[i], lst[i + 1])
	for i in range(n):
		if s[i] != lst[i]:
			j = max((j for (j, v) in enumerate(s[i:], i) if v == lst[i]))
			s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
			break
	return s if s < t else '---'
for _ in range(int(input())):
	print(solve(*input().split()))


from sys import stdin

def solve(a, b):
	(t, s) = (list(a), ''.join(sorted(a)))
	for i in range(len(a)):
		if t[i] != s[i]:
			y = a.rindex(s[i])
			(t[i], t[y]) = (t[y], t[i])
			break
	z = ''.join(t)
	return z if z < b else '---'
for _ in range(int(stdin.readline())):
	(s, c) = map(str, stdin.readline().split())
	print(solve(s, c))


t = int(input())
for _ in range(t):
	x = list(map(str, input().split()))
	arr = list(map(str, x[0].strip()))
	n = len(arr)
	brr = list(map(str, x[1].strip()))
	m = len(brr)
	post = []
	x = arr[n - 1]
	ind = n - 1
	for i in range(n - 1, -1, -1):
		if arr[i] < x:
			x = arr[i]
			ind = i
		post += [[x, ind]]
	post.reverse()
	for i in range(min(n, m)):
		if arr[i] < brr[i]:
			break
		elif i == n - 1:
			break
		elif arr[i] == brr[i] and i != n - 1:
			temp = arr[i]
			if temp > post[i + 1][0]:
				(arr[i], arr[post[i + 1][1]]) = (arr[post[i + 1][1]], arr[i])
				break
		else:
			temp = arr[i]
			if temp > post[i + 1][0]:
				(arr[i], arr[post[i + 1][1]]) = (arr[post[i + 1][1]], arr[i])
				break
			elif temp == post[i + 1][0]:
				(arr[i], arr[post[i + 1][1]]) = (arr[post[i + 1][1]], arr[i])
			else:
				break
	if arr < brr:
		print(''.join(arr))
	else:
		print('---')


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(s, s1) = input().split()
	ln = len(s)
	ln1 = len(s1)
	l = list(s)
	if s < s1:
		print(s)
		continue
	for i in range(ln):
		ch = s[i]
		p = -1
		mn = ch
		for j in range(i + 1, ln):
			if s[j] <= mn:
				p = j
				mn = s[j]
		if mn != ch:
			(l[i], l[p]) = (l[p], l[i])
			break
	s = ''.join(l)
	if s < s1:
		print(s)
	else:
		print('---')


def solve():
	(s, c) = input().split()
	mins = [char for char in s]
	for i in range(len(s) - 2, -1, -1):
		mins[i] = min(mins[i], mins[i + 1])
	(a, b, j) = ('', '', -1)
	for i in range(len(s)):
		if s[i] != mins[i]:
			a = s[i]
			b = mins[i]
			j = i
			break
	if j != -1:
		k = max((i for i in range(len(s)) if s[i] == b))
		string = list(s)
		(string[j], string[k]) = (string[k], string[j])
		s = ''.join(string)
	if s < c:
		print(s)
	else:
		print('---')
for _ in range(int(input())):
	solve()


def solve(s, c):
	s = [ord(i) for i in list(s)]
	c = [ord(i) for i in list(c)]
	m = min(len(s), len(c))
	for (i, x) in enumerate(s):
		sm = min(s[i:])
		if x != sm:
			ism = len(s[i:]) - s[i:][::-1].index(sm) + i - 1
			(s[i], s[ism]) = (s[ism], s[i])
			break
	for i in range(m):
		if s[i] == c[i]:
			continue
		if s[i] > c[i]:
			return '---'
		break
	if c[:m] == s[:m] and len(c) <= len(s):
		return '---'
	return ''.join([chr(i) for i in s])
for _ in range(int(input())):
	(s, c) = input().split()
	print(solve(s, c))


from sys import stdin, stdout
from math import gcd, sqrt, factorial
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(s, c) = S().split()
	s = list(s)
	c = list(c)
	ln = len(s)
	l = min(ln, len(c))
	for i in range(l):
		if s[i] == c[i] == 'A':
			continue
		elif s[i] < c[i]:
			break
		else:
			ind = 0
			ch = s[i]
			for j in range(i, ln):
				if s[j] <= ch:
					ch = s[j]
					ind = j
			if s[i] == c[i] and s[i] == ch:
				continue
			else:
				(s[i], s[ind]) = (s[ind], s[i])
				break
	if s[:l] < c[:l] or (s[:l] == c[:l] and ln < len(c)):
		print(*s, sep='')
	else:
		print('---')


def swap(c, i, j):
	c = list(c)
	(c[i], c[j]) = (c[j], c[i])
	return ''.join(c)
for i in range(int(input())):
	index1 = -1
	index2 = -1
	(m, n) = map(str, input().split())
	if m < n:
		print(m)
		continue
	original = m
	m = sorted(m)
	if m[0] > n[0]:
		print('---')
		continue
	z = 0
	for j in range(len(m)):
		if m[j] != original[j]:
			index1 = j
			for k in range(j + 1, len(m)):
				if original[k] == m[j]:
					index2 = k
			original = swap(original, index1, index2)
			break
	if original < n:
		print(original)
	else:
		print('---')


def gets(a):
	i = 0
	a = list(a)
	b = [0] * 100
	for j in a:
		b[ord(j) - ord('A')] += 1
	r = -1
	t = -1
	while b[i] == 0 and i < 26:
		i += 1
	for k in range(0, len(a)):
		if r == -1 and ord(a[k]) - ord('A') == i:
			b[i] -= 1
			while b[i] == 0 and i < 26:
				i += 1
		elif r == -1:
			t = k
			r = 0
		elif ord(a[k]) - ord('A') == i:
			r = k
	if r != -1 and t != -1:
		(a[t], a[r]) = (a[r], a[t])
	return ''.join(a)
for _ in range(int(input())):
	(a, b) = input().split()
	a = gets(a)
	if a < b:
		print(a)
	else:
		print('---')


T = int(input())
for cas in range(T):
	(s, c) = map(list, input().split())
	l = len(s)
	for i in range(l):
		tmp = 'Z'
		for j in range(i, l):
			if s[j] < s[i] and s[j] <= tmp:
				tmp = s[j]
				id = j
		if tmp < s[i]:
			s[id] = s[i]
			s[i] = tmp
			break
	if s < c:
		for i in range(l):
			print(s[i], end='')
		print()
	else:
		print('---')


n = int(input())

def h(a):
	z = 0
	go = True
	while z < len(a) and go:
		if a[z] > min(a[z:]):
			i = len(a) - 1
			m = 'ZZ'
			for x in range(i, z - 1, -1):
				if a[x] < m:
					m = a[x]
					i = x
			a = list(a)
			(a[z], a[i]) = (a[i], a[z])
			a = ''.join(a)
			go = False
		z += 1
	return a
for x in range(n):
	(b, c) = input().split()
	b = h(b)
	if b < c:
		print(b)
	else:
		print('---')


t = int(input())
while t:
	(x, l2) = input().split()
	n1 = len(x)
	l1 = []
	n2 = len(l2)
	n = min(n1, n2)
	l = list(x)
	for i in range(n1):
		l1.append(x[i])
	l.sort()
	ch = 0
	for i in range(n1):
		if l[i] != l1[i]:
			index1 = i
			ch = 1
			for j in range(n1 - 1, -1, -1):
				if l[index1] == l1[j]:
					index2 = j
					break
			break
	if ch == 1:
		k = l1[index2]
		l1[index2] = l1[index1]
		l1[index1] = k
	cnt = 0
	for i in range(n):
		if l1[i] == l2[i]:
			cnt += 1
	if cnt == n:
		if n1 < n2:
			for r in range(n1):
				print(l1[r], end='')
			print()
		else:
			print('---')
	else:
		ch = 0
		ans = 0
		for i in range(n):
			if l1[i] < l2[i]:
				cnt1 = 0
				for j in range(i):
					if l1[j] == l2[j]:
						cnt1 += 1
				if cnt1 == i:
					ans = 1
					for r in range(n1):
						print(l1[r], end='')
					print()
					break
		if ans == 0:
			print('---')
	t -= 1


T = int(input(''))

def prints(l):
	for c in l:
		print(c, end='')
	print('')
al = []
for i in range(65, 91, 1):
	al.append(chr(i))
for v in range(T):
	a = input('').split(' ')
	s = a[0]
	c = a[1]
	d = {}
	for i in range(len(s) - 1, -1, -1):
		ch = s[i]
		if ch not in d:
			d[ch] = []
		d[ch].append(i)
	res = False
	don = False
	f = 0
	s = list(s)
	c = list(c)
	ls = len(s)
	lc = len(c)
	for i in range(min(ls, lc)):
		d[s[i]].pop()
		if len(d[s[i]]) == 0:
			del d[s[i]]
		if s[i] < c[i]:
			res = True
			break
		if s[i] == c[i]:
			for a in al:
				if a < c[i] and a in d:
					si = d[a].pop()
					s[si] = s[i]
					s[i] = a
					res = True
					break
				if a >= c[i]:
					break
			if res:
				break
		else:
			for a in al:
				if a < c[i] and a in d:
					si = d[a].pop()
					s[si] = s[i]
					s[i] = a
					res = True
					break
				if a == c[i] and a in d:
					while len(d[a]) > 0:
						si = d[a].pop()
						if si >= lc or (si < lc and c[si] > s[i]) or len(d[a]) == 0:
							s[si] = s[i]
							s[i] = a
							res = True
							don = True
							break
					break
				if a > c[i]:
					f = 1
					print('---')
					break
			break
	if f == 1:
		continue
	if res:
		if don:
			res = 0
			for h in range(i + 1, min(ls, lc), 1):
				if s[h] < c[h]:
					res = 1
					break
				if s[h] == c[h]:
					continue
				else:
					res = -1
					break
			if res == 1:
				prints(s)
			elif res == 0:
				if ls < lc:
					prints(s)
				else:
					print('---')
			else:
				print('---')
		else:
			prints(s)
		continue
	if i == min(ls, lc) - 1:
		if ls < lc:
			prints(s)
		else:
			print('---')
	else:
		print('---')


t = int(input())
for i in range(t):
	(s, t) = map(str, input().split())
	s = list(s)
	t = list(t)
	s = list(reversed(s))
	n = len(s)
	while s[s.index(min(s[:n]))] == s[n - 1] and n > 0:
		n -= 1
		if n < 1:
			break
	if n >= 1:
		(s[n - 1], s[s.index(min(s[:n]))]) = (s[s.index(min(s[:n]))], s[n - 1])
	s = list(reversed(s))
	if s < t:
		print(''.join(s))
	else:
		print('---')


import sys
input = sys.stdin.readline
import heapq
import copy
import math

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

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

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

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		s = insr()
		for i in range(len(s)):
			if s[i] == ' ':
				a = s[0:i]
				b = s[i + 1:len(s)]
				break
		hashm = [0 for i in range(27)]
		for i in a:
			hashm[ord(i) - ord('A')] += 1

		def find_best(hashm):
			for i in range(len(hashm)):
				if hashm[i] != 0:
					top = i
					break
			return i
		top = find_best(hashm)
		for i in range(len(a)):
			if ord(a[i]) - ord('A') == top:
				hashm[top] -= 1
				if hashm[top] == 0:
					top = find_best(hashm)
			else:
				curr = a[i]
				for j in range(len(a) - 1, i, -1):
					if ord(a[j]) - ord('A') == top:
						break
				a[i] = a[j]
				a[j] = curr
				break
		flag = 0
		for i in range(len(b)):
			if i >= len(a):
				flag = 1
				break
			if a[i] < b[i]:
				flag = 1
				break
			if a[i] > b[i]:
				break
		if flag == 0:
			print('---')
		else:
			print(''.join(a))
main()


tc = int(input())
for _ in range(tc):
	(s, c) = input().split()
	sorteds = ''.join(sorted(s))
	i = 0
	while i < len(s) and s[i] == sorteds[i]:
		i += 1
	if i < len(s):
		j = s.rfind(sorteds[i])
		s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
	if s < c:
		print(s)
	else:
		print('---')


def solve(s, t):
	mns = list(s)
	for i in range(len(s) - 2, -1, -1):
		mns[i] = min(mns[i], mns[i + 1])
	for i in range(len(s)):
		if s[i] != mns[i]:
			j = max((j for (j, v) in enumerate(s[i:], i) if v == mns[i]))
			s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
			break
	return s if s < t else '---'
for cas in range(int(input())):
	print(solve(*input().split()))


def solve():
	I = input().split()
	S = I[0]
	C = I[1]
	ans = ''
	n = len(S)
	for i in range(0, n):
		mini = 'z'
		for j in range(i + 1, n):
			mini = min(mini, S[j])
		if S[i] > mini:
			pos = -1
			for j in range(i + 1, n):
				if S[j] == mini:
					pos = j
			for j in range(0, n):
				if j == i:
					ans += S[pos]
				elif j == pos:
					ans += S[i]
				else:
					ans += S[j]
			break
	if ans == '':
		ans = S
	if ans < C:
		print(ans)
	else:
		print('---')
t = int(input())
while t > 0:
	solve()
	t -= 1


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i1 in range(int(input())):
	(a, b) = map(list, input().split())
	r = min(a)
	for i in range(len(a)):
		if a[i] == min(a[i:]):
			continue
		else:
			s = len(a) - 1 - a[i:][::-1].index(min(a[i:]))
			(a[i], a[s]) = (a[s], a[i])
			break
	if a < b:
		print(*a, sep='')
	else:
		print('---')


for i in range(int(input())):
	(s, c) = map(str, input().split())
	if s < c:
		print(s)
	else:
		temp = True
		x = 0
		s = list(s)
		while x < len(s) and temp:
			if s[x] > min(s[x:]):
				mi = 'ZZ'
				for j in range(len(s) - 1, x - 1, -1):
					if s[j] < mi:
						ind = j
						mi = s[j]
				(s[x], s[ind]) = (s[ind], s[x])
				temp = False
			x += 1
		s = ''.join(s)
		if s < c:
			print(s)
		else:
			print('---')


t = int(input())
for _ in range(t):
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	last_occurrence = [-1 for i in range(26)]
	for i in range(len(s)):
		current_char_ord = ord(s[i]) - ord('A')
		last_occurrence[current_char_ord] = max(last_occurrence[current_char_ord], i)
	done = False
	for i in range(len(s)):
		for char_less_than_s_i in range(ord(s[i]) - ord('A')):
			if last_occurrence[char_less_than_s_i] > i:
				(s[i], s[last_occurrence[char_less_than_s_i]]) = (s[last_occurrence[char_less_than_s_i]], s[i])
				done = True
				break
		if done:
			break
	i = 0
	while i < len(s) and i < len(c):
		if s[i] == c[i]:
			i += 1
		elif s[i] < c[i]:
			print(''.join(s))
			break
		else:
			print('---')
			break
	if i == len(c):
		print('---')
	elif i == len(s):
		print(''.join(s))


t = int(input())
for k in range(t):
	names = input().split(' ')
	my = list(names[0])
	comp = list(names[1])
	impossible = False
	initial = my.copy()
	less_than = False
	swapped = False
	impossible = False
	for i in range(min(len(my), len(comp))):
		if my[i] < comp[i]:
			less_than = True
			break
		elif not swapped:
			for j in range(len(my) - 1, i, -1):
				if my[j] < comp[i]:
					temp = my[j]
					my[j] = my[i]
					my[i] = temp
					less_than = True
					break
			if less_than:
				break
			elif my[i] != comp[i]:
				for j in range(len(my) - 1, i, -1):
					if my[j] == comp[i]:
						temp = my[j]
						my[j] = my[i]
						my[i] = temp
						swapped = True
				if not swapped:
					impossible = True
					break
		elif my[i] > comp[i]:
			impossible = True
			break
	if less_than or (not impossible and len(my) < len(comp)):
		print(''.join(my))
	else:
		print('---')


n = int(input())
for k in range(n):
	(st, st1) = input().split()
	if st >= st1:
		a = ''.join(sorted(st))
		if a < st1:
			st = list(st)
			a = list(a)
			for i in range(len(st)):
				if st[i] != a[i]:
					b = st[i]
					st[i] = a[i]
					for j in range(len(st) - 1, -1, -1):
						if st[j] == a[i]:
							st[j] = b
							break
					st = ''.join(st)
					break
	if st < st1:
		print(st)
	else:
		print('---')


T = int(input())
for t in range(T):
	(s1, s2) = [list(x) for x in input().split()]
	ind = 0
	l = len(s1)
	while ind < l and s1[ind] == min(s1[ind:l]):
		ind += 1
	if ind < l:
		x = s1[ind:l]
		x.reverse()
		c = min(x)
		ii = x.index(c)
		cc = s1[ind]
		s1[ind] = c
		s1[ind + len(x) - 1 - ii] = cc
	if s1 < s2:
		print(''.join(s1))
	else:
		print('---')


def solve(s, c):
	smallest_after = []
	for i in range(len(s) - 1, -1, -1):
		if len(smallest_after) == 0:
			smallest_after.append((s[i], i))
		else:
			(current_smallest, smallest_index) = smallest_after[-1]
			if s[i] < current_smallest:
				(current_smallest, smallest_index) = (s[i], i)
			smallest_after.append((current_smallest, smallest_index))
	smallest_after = list(reversed(smallest_after))
	for i in range(len(s)):
		(smallest_value, smallest_index) = smallest_after[i]
		if smallest_value < s[i]:
			s = s[:i] + s[smallest_index] + s[i + 1:smallest_index] + s[i] + s[smallest_index + 1:]
			break
	return s if s < c else '---'
for T in range(int(input())):
	print(solve(*input().split()))


queries = int(input())
for i in range(queries):
	(S, C) = input().split()
	s = list(S)
	interim = 0
	if S < C:
		print(S)
	else:
		Ssize = len(S)
		pick = sorted(S)
		for i in range(Ssize):
			if S[i] != pick[i]:
				interim = pick[i]
				break
		for k in range(Ssize - 1, -1, -1):
			if S[k] == interim:
				s[k] = s[i]
				s[i] = interim
				break
		result = ''.join(s)
		print(result if result < C else '---')


n = int(input())
while n:
	n = n - 1
	s = input()
	s = s.split()
	a = s[0]
	b = s[1]
	flag = 0
	if a < b:
		print(a)
	else:
		a = list(a)
		for i in range(0, len(a) - 1):
			j = min((r for r in range(i + 1, len(a))), key=lambda x: (a[x], -x))
			if a[i] > a[j]:
				(a[i], a[j]) = (a[j], a[i])
				break
		a = ''.join(a)
		if a < b:
			print(a)
		else:
			print('---')


from collections import Counter
from string import ascii_uppercase
t = int(input())
for _ in range(t):
	(s, c) = input().split()
	s = list(s)
	count = Counter(s)
	swapMade = False
	for i in range(len(s)):
		count[s[i]] -= 1
		if i >= len(c):
			print('---')
			break
		if s[i] < c[i]:
			print(''.join(s))
			break
		if swapMade and s[i] < c[i]:
			print('---')
			break
		swapChar = None
		if not swapMade:
			for char in ascii_uppercase:
				if char < c[i]:
					if count[char] > 0:
						swapChar = char
						break
			if swapChar != None:
				curIndex = i + 1
				while curIndex < len(s) and s[curIndex] != swapChar:
					curIndex += 1
				(s[i], s[curIndex]) = (s[curIndex], s[i])
				print(''.join(s))
				break
			elif s[i] != c[i] and count[c[i]] > 0:
				indices = [idx for idx in range(i + 1, len(s)) if s[idx] == c[i]]
				for index in indices:
					(s[i], s[index]) = (s[index], s[i])
					if ''.join(s) < c:
						print(''.join(s))
						break
					else:
						(s[i], s[index]) = (s[index], s[i])
				else:
					print('---')
					break
				break
		if s[i] != c[i]:
			print('---')
			break
	else:
		print(''.join(s)) if len(c) > len(s) else print('---')


for t in range(int(input())):
	(s, c) = input().split(' ')
	(s, c) = (list(s), list(c))
	copy_s = s.copy()
	copy_s.sort()
	i = 0
	while i < len(s):
		if copy_s[i] != s[i]:
			break
		i += 1
	if i < len(s):
		j = len(s) - 1
		while j > i:
			if s[j] == copy_s[i]:
				(s[i], s[j]) = (s[j], s[i])
				break
			j -= 1
	same = True
	flag = False
	for i in range(min(len(s), len(c))):
		if s[i] < c[i]:
			flag = True
			same = False
			break
		elif not flag and s[i] > c[i]:
			same = False
			break
	if same and len(s) < len(c):
		print(''.join(s))
	elif flag:
		print(''.join(s))
	else:
		print('---')


def func():
	(s1, s2) = input().split()
	ss1 = list(s1)
	ss1_sort = sorted(ss1)
	ss2 = list(s2)
	lens = min(len(s1), len(s2))
	sortt = False
	for i in range(len(s1)):
		if ss1[i] != ss1_sort[i]:
			pos = i
			sortt = True
			break
	if sortt == False:
		if s1 < s2:
			print(s1)
		else:
			print('---')
		return
	swap = -1
	for i in range(pos + 1, len(ss1)):
		if ss1[i] == ss1_sort[pos]:
			swap = i
	ch = ss1[swap]
	ss1[swap] = ss1[pos]
	ss1[pos] = ch
	s1 = ''.join(ss1)
	if s1 < s2:
		print(s1)
	else:
		print('---')
t = int(input())
for i in range(t):
	func()


for i in range(int(input())):
	(a, b) = map(list, input().split())
	ind = -1
	for i in range(len(a)):
		if a[i] != 'A':
			m = a[i]
			flag = False
			ind = i
			for g in range(i + 1, len(a)):
				if flag:
					if a[g] <= m:
						m = a[g]
						ind = g
				elif a[g] < m:
					flag = True
					m = a[g]
					ind = g
			if flag:
				(a[i], a[ind]) = (a[ind], a[i])
				break
	a = ''.join(a)
	b = ''.join(b)
	if a < b:
		print(a)
	else:
		print('---')


for i in range(int(input())):
	(a, b) = map(list, input().split())
	minAtSuffix = [0] * (len(a) - 1)
	minAtSuffixPos = [0] * (len(a) - 1)
	minAtSuffix[-1] = a[-1]
	minAtSuffixPos[-1] = len(a) - 1
	for i in range(len(a) - 3, -1, -1):
		if a[i + 1] < minAtSuffix[i + 1]:
			minAtSuffix[i] = a[i + 1]
			minAtSuffixPos[i] = i + 1
		else:
			minAtSuffix[i] = minAtSuffix[i + 1]
			minAtSuffixPos[i] = minAtSuffixPos[i + 1]
	for i in range(len(a) - 1):
		if minAtSuffix[i] < a[i]:
			(a[i], a[minAtSuffixPos[i]]) = (a[minAtSuffixPos[i]], a[i])
			break
	a = ''.join(a)
	b = ''.join(b)
	if a >= b:
		print('---')
	else:
		print(a)


import sys
T = int(sys.stdin.readline().strip())
for _ in range(T):
	(a, b) = [list(s) for s in sys.stdin.readline().strip().split(' ')]
	sa = sorted(a)
	for i in range(len(a)):
		if a[i] != sa[i]:
			for j in range(len(a) - 1, i, -1):
				if a[j] == sa[i]:
					(a[i], a[j]) = (a[j], a[i])
			break
	if a < b:
		print(''.join(a))
	else:
		print('---')


for _ in range(int(input())):
	(a, b) = input().split()
	s = sorted(a)
	for i in range(len(a)):
		if a[i] != s[i]:
			idx = a.rfind(s[i])
			a = a[:i] + a[idx] + a[i + 1:idx] + a[i] + a[idx + 1:]
			break
	if a < b:
		print(a)
	else:
		print('---')


import sys, queue, copy
sys.setrecursionlimit(10 ** 8)

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

def inpl_str():
	return list(sys.stdin.readline().split())
INF = float('inf')

def solve():
	(S, c) = inpl_str()
	s_sort = sorted(S)
	for i in range(len(S)):
		if S[i] != s_sort[i]:
			j = max((j for (j, v) in enumerate(S[i:], i) if v == s_sort[i]))
			ans = S[:i] + S[j] + S[i + 1:j] + S[i] + S[j + 1:]
			break
	else:
		ans = S
	if ans >= c:
		ans = '---'
	print(ans)
q = int(input())
for _ in range(q):
	solve()


let = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ldic = {}
for i in range(26):
	ldic[let[i]] = i
t = int(input())
for loop in range(t):
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	ss = s.copy()
	ss.sort()
	use = False
	for i in range(len(s)):
		if s[i] != ss[i]:
			for j in range(len(s)):
				j = len(s) - 1 - j
				if s[j] == ss[i] and j > i:
					s[j] = s[i]
					s[i] = ss[i]
					use = True
					break
		if use:
			break
	flag = 0
	for i in range(min(len(s), len(c))):
		if ldic[s[i]] > ldic[c[i]]:
			flag = -1
			break
		elif ldic[s[i]] < ldic[c[i]]:
			flag = 1
			break
	if flag == 1 or (flag == 0 and len(s) < len(c)):
		print(''.join(s))
	else:
		print('---')


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def solve():
	(s, c) = input().split()
	n = len(s)
	for i in range(n - 1):
		prev = s[i]
		pos = i
		for j in range(i + 1, n):
			if s[j] < prev:
				prev = s[j]
				pos = j
			elif s[j] == prev:
				pos = j
		if prev == s[i]:
			continue
		t = list(s)
		(t[i], t[pos]) = (prev, s[i])
		s = ''.join(t)
		break
	if s < c:
		print(s)
	else:
		print('---')
t = int(input())
for i in range(t):
	solve()


t = int(input())
while t > 0:
	lst = input().split()
	s = lst[0]
	slst = list()
	nnn = 0
	for ppp in s:
		slst.append(ppp)
	c = lst[1]
	x = ''.join(sorted(s))
	newl = ''
	i = 0
	count = 0
	while i < len(s):
		if slst[i] != x[i]:
			letter = slst[i]
			kkk = 0
			while kkk < len(slst):
				if slst[kkk] == x[i]:
					indi = kkk
				kkk = kkk + 1
			slst[i] = x[i]
			slst[indi] = letter
			break
		i = i + 1
	newl = ''.join(slst)
	if newl < c:
		print(newl)
	else:
		print('---')
	t = t - 1


t = int(input())

def solve():
	(s, c) = input().split()
	a = sorted(s)
	b = ''
	j = 0
	for i in range(len(s)):
		if s[i] != a[i]:
			b = a[i]
			j = i
			break
	for i in range(len(s) - 1, -1, -1):
		if s[i] == b:
			s = s[:j] + s[i] + s[j + 1:i] + s[j] + s[i + 1:]
			break
	if s < c:
		print(s)
	else:
		print('---')
for i in range(t):
	solve()


n = int(input())
for test in range(n):
	(a, B) = map(str, input().split())
	a = list(a)
	b = sorted(a)
	t_rep = ''
	t_find = ''
	for i in range(len(a)):
		if a[i] != b[i]:
			index = 0
			for j in range(i + 1, len(a)):
				if a[j] == b[i]:
					index = j
			a[index] = a[i]
			a[i] = b[i]
			break
	a = ''.join(a)
	if a < B:
		print(a)
	else:
		print('---')


import math
a = int(input())
for i in range(a):
	(s, r) = map(str, input().split())
	ans = []
	from collections import defaultdict
	al = defaultdict(int)
	for i in range(len(s)):
		ans.append(ord(s[i]) - 65)
		al[ord(s[i]) - 65] = i
	mini = [math.inf for i in range(len(s))]
	mini[-1] = ord(s[-1]) - 65
	for i in range(len(s) - 2, -1, -1):
		mini[i] = min(ord(s[i]) - 65, mini[i + 1])
	flag = 0
	for i in range(min(len(s), len(r))):
		if s[i] > r[i]:
			flag = 1
			temp = ans[i]
			ans[i] = mini[i]
			ans[al[mini[i]]] = temp
			t = ''
			for i in range(len(ans)):
				t += chr(ans[i] + 65)
			if t < r:
				print(t)
			else:
				print('---')
			break
		if s[i] < r[i]:
			print(s)
			flag = 1
			break
		if s[i] == r[i]:
			if mini[i] == ans[i]:
				continue
			else:
				flag = 1
				temp = ans[i]
				ans[i] = mini[i]
				ans[al[mini[i]]] = temp
				t = ''
				for i in range(len(ans)):
					t += chr(ans[i] + 65)
				if t < r:
					print(t)
				break
	if flag == 0:
		if s >= r:
			print('---')
		else:
			print(s)


def solveOne(me, rival):
	me = list(me)
	sortedMe = sorted(me)
	for i in range(len(me)):
		if me[i] != sortedMe[i]:
			swapPos = i
			for j in range(i + 1, len(me)):
				if me[j] <= me[swapPos]:
					swapPos = j
			(me[i], me[swapPos]) = (me[swapPos], me[i])
			break
	me = ''.join(me)
	if me >= rival:
		print('---')
	else:
		print(me)
for _ in range(int(input())):
	solveOne(*input().split())


def swapPositions(S, pos1, pos2):
	(S[pos1], S[pos2]) = (S[pos2], S[pos1])
	return S
t = int(input())
for _ in range(t):
	(s, c) = input().split()
	if s < c:
		print(s)
	else:
		S = list(s)
		P = S[:]
		S.sort()
		i = 0
		while i < len(s) and s[i] == S[i]:
			i += 1
		loc = -1
		for j in range(i + 1, len(s)):
			if s[j] == S[i]:
				loc = j
		if loc != -1:
			P = swapPositions(P, i, loc)
		s = ''.join(P)
		if s < c:
			print(s)
		else:
			print('---')


def main():
	N = int(input())
	for _ in range(N):
		(S, C) = list(map(list, input().split()))
		(s, c, mi) = (len(S), len(C), 0)
		for i in range(s - 1):
			mi = i
			for j in range(i + 1, s):
				if S[j] <= S[mi]:
					mi = j
			if S[mi] != S[i]:
				(S[mi], S[i]) = (S[i], S[mi])
				break
		print(''.join(S) if S < C else '---')
main()


inp = input
for _ in range(int(inp().strip())):
	(a, b) = inp().strip().split(' ')
	if a < b:
		print(a)
		continue
	sa = list(sorted(a))
	aa = list(a)
	if aa == sa:
		a = ''.join(aa)
		if a < b:
			print(a)
		else:
			print('---')
		continue
	k = 0
	for (i, j) in zip(aa, sa):
		if i == j:
			k += 1
			continue
		else:
			break
	ch = -1
	trg = sa[k]
	for i in range(len(aa) - 1, -1, -1):
		if aa[i] == trg:
			ch = i
			break
	(aa[k], aa[ch]) = (aa[ch], aa[k])
	a = ''.join(aa)
	if a < b:
		print(a)
	else:
		print('---')


import sys, math, queue, collections
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	(s, t) = input().strip().split()
	ss = list(s)
	for i in reversed(range(len(s) - 1)):
		ss[i] = min(ss[i], ss[i + 1])
	for i in range(len(s)):
		if s[i] != ss[i]:
			j = max(filter(lambda x: s[x] == ss[i], list(range(len(s)))))
			s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
			break
	if s >= t:
		print('---')
	else:
		print(s)


def get_smallest(s, c):
	right_smallest = {}
	s = [i for i in s]
	right_smallest[len(s) - 1] = (s[-1], len(s) - 1)
	for i in range(len(s) - 2, -1, -1):
		if s[i] > right_smallest[i + 1][0]:
			right_smallest[i] = (right_smallest[i + 1][0], right_smallest[i + 1][1])
		else:
			right_smallest[i] = (s[i], i)
	if c.startswith(''.join(s)) and len(s) < len(c):
		return ''.join(s)
	for i in range(len(s)):
		if i >= len(c):
			return '---'
		if s[i] == c[i]:
			if right_smallest[i][0] < c[i]:
				better_index = right_smallest[i][1]
				(s[i], s[better_index]) = (s[better_index], s[i])
				return ''.join(s)
			else:
				continue
		elif s[i] < c[i]:
			return ''.join(s)
		elif s[i] > c[i]:
			if right_smallest[i][0] < c[i]:
				better_index = right_smallest[i][1]
				(s[i], s[better_index]) = (s[better_index], s[i])
				return ''.join(s)
			elif right_smallest[i][0] == c[i]:
				for better_index in range(i, len(s)):
					if s[better_index] > right_smallest[i][0]:
						continue
					(s[i], s[better_index]) = (s[better_index], s[i])
					if ''.join(s)[i:] < c[i:]:
						return ''.join(s)
					(s[i], s[better_index]) = (s[better_index], s[i])
				return '---'
			else:
				return '---'
	return '---'
T = int(input())
for _ in range(T):
	(s, c) = input().split()
	ans = get_smallest(s, c)
	print(ans)


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	l = inputstring()
	s = []
	c = []
	space = False
	for i in range(len(l)):
		if l[i] == ' ':
			space = True
			continue
		if not space:
			s.append(l[i])
		elif l[i] != '\n':
			c.append(l[i])
	s1 = ''
	c1 = ''
	for i in range(len(s)):
		s1 += s[i]
	for i in range(len(c)):
		c1 += c[i]
	if s1 < c1:
		print(s1)
		continue
	sort = sorted(s)
	ind1 = 0
	ind2 = 0
	for i in range(len(sort)):
		if s[i] != sort[i]:
			ind1 = i
			break
	for i in range(ind1 + 1, len(sort)):
		if s[i] == sort[ind1]:
			ind2 = i
	(s[ind1], s[ind2]) = (s[ind2], s[ind1])
	if s < c:
		for i in range(len(s)):
			print(s[i], end='')
		print()
	else:
		print('---')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	(s, t) = input().split()
	lis = [0] * len(s)
	for i in range(len(s) - 1, -1, -1):
		if i == len(s) - 1:
			lis[i] = i
		elif ord(s[i]) - ord('a') >= ord(s[lis[i + 1]]) - ord('a'):
			lis[i] = lis[i + 1]
		else:
			lis[i] = i
	s = [i for i in s]
	for i in range(len(s)):
		if lis[i] != i and s[lis[i]] != s[i]:
			(s[lis[i]], s[i]) = (s[i], s[lis[i]])
			break
	s = ''.join(s)
	if s < t:
		print(s)
	else:
		print('---')


q = int(input())

def func(l, smallest, s1, s2):
	i = 0
	while i < l:
		if s1[i] == s2[i]:
			if smallest[i] != -1:
				(s1[i], s1[smallest[i]]) = (s1[smallest[i]], s1[i])
				if s1 < s2:
					print(''.join(s1))
					return
				else:
					(s1[i], s1[smallest[i]]) = (s1[smallest[i]], s1[i])
		else:
			if smallest[i] != -1:
				(s1[i], s1[smallest[i]]) = (s1[smallest[i]], s1[i])
				if s1 < s2:
					print(''.join(s1))
				else:
					print('---')
			else:
				print('---')
			return
		i += 1
	print('---')
	return

def func2(smallest, s1, s2):
	for i in range(len(s1)):
		(s1[i], s1[smallest[i]]) = (s1[smallest[i]], s1[i])
		if s1 < s2:
			print(''.join(s1))
			return
		(s1[i], s1[smallest[i]]) = (s1[smallest[i]], s1[i])
	print('---')
for x in range(q):
	(s1, s2) = map(str, input().split())
	s1 = [x for x in s1]
	s2 = [x for x in s2]
	l = min(len(s1), len(s2))
	if s1 < s2:
		print(''.join(s1))
	else:
		smallest = [len(s1) - 1 for x in range(len(s1))]
		x = len(s1) - 2
		small = x + 1
		while x >= 0:
			if s1[x] < s1[small]:
				small = x
				smallest[x] = small
			else:
				smallest[x] = small
			x -= 1
		func2(smallest, s1, s2)


def best(s):
	new = ''
	for (idi, l) in enumerate(s):
		if idi and s[idi] == s[idi - 1]:
			continue
		besti = [l, 0]
		for (idj, m) in reversed(list(enumerate(s))):
			if idi == idj:
				break
			if m < besti[0]:
				besti = [m, idj]
		if besti != [l, 0]:
			new = s[0:idi] + s[besti[1]] + s[idi + 1:besti[1]] + s[idi] + s[besti[1] + 1:]
			break
	return new if new != '' else s
n = int(input())
for _ in range(n):
	(s, t) = input().split()
	s = best(s)
	if s < t:
		print(s)
	else:
		print('---')


x = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for _ in range(int(input())):
	(s, t) = input().split()
	d = {}
	for i in x:
		d[i] = 0
	for i in s:
		d[i] += 1
	j = 0
	while d[x[j]] == 0:
		j += 1
	ss = ''
	f = False
	for i in range(len(s)):
		if f:
			ss += s[i]
			continue
		if s[i] > x[j]:
			ss += x[j]
			f = True
			dd = s[i]
		else:
			ss += s[i]
		d[s[i]] -= 1
		while d[x[j]] == 0 and j != 25:
			j += 1
	if f:
		for i in range(len(s) - 1, 0, -1):
			if ss[i] == x[j]:
				if i != len(s) - 1:
					ss = ss[:i] + dd + ss[i + 1:]
				else:
					ss = ss[:i] + dd
				break
	if ss < t:
		print(ss)
	else:
		print('---')


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from collections import defaultdict as dd
(t,) = I()
while t:
	t -= 1
	(s, c) = [list(i) for i in input().split()]
	(i, j) = (0, 0)
	(n, m) = (len(s), len(c))
	l = ['Z'] * (n + 1)
	l[n] = ['Z', n]
	l[n - 1] = [s[-1], n - 1]
	for i in range(n - 2, -1, -1):
		l[i] = l[i + 1][:]
		if s[i] < l[i + 1][0]:
			l[i] = [s[i], i]
	for i in range(min(n, m)):
		if s[i] > l[i + 1][0]:
			(s[i], s[l[i + 1][1]]) = (l[i + 1][0], s[i])
			break
	if s < c:
		print(''.join(s))
	else:
		print('---')


def besty(s):
	s = list(s)
	for top in range(len(s)):
		best = s[top]
		index = top
		for i in range(len(s) - 1, top - 1, -1):
			if s[i] < best:
				best = s[i]
				index = i
		if index == top:
			continue
		else:
			(s[top], s[index]) = (s[index], s[top])
			break
	return s
q = int(input())
for _ in range(q):
	(s, c) = input().split(' ')
	s = besty(s)
	temp = ''
	s = temp.join(s)
	if s < c:
		print(s)
	else:
		print('---')


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	for case in range(int(input())):
		(player, rival) = [[y for y in x] for x in input().split()]
		temp = [x for x in player]
		temp.sort()
		swap_char = ''
		x = -1
		y = -1
		for i in range(len(player)):
			if player[i] != temp[i]:
				swap_char = temp[i]
				x = i
				break
		for i in range(len(player)):
			if player[i] == swap_char:
				y = i
		(player[x], player[y]) = (player[y], player[x])
		if player < rival:
			print(''.join(player))
		else:
			print('---')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(a, b) = input().split()
	la = len(a)
	lb = len(b)
	dd = []
	for i in range(la):
		dd.append([a[i], i])
	dd.sort()
	i = 0
	c = 0
	r = ''
	di = {i: i for i in range(la)}
	while i < la and c < 1:
		if dd[i][1] != i:
			for j in range(la - 1, -1, -1):
				if a[j] == dd[i][0]:
					gg = j
					break
			di[gg] = i
			di[i] = gg
			c += 1
		i += 1
	for i in range(la):
		r += a[di[i]]
	if la >= lb and r < b:
		print(r)
	elif la < lb and r <= b:
		print(r)
	else:
		print('---')


import math

def better(a, b):
	for i in range(min(len(a), len(b))):
		if ord(a[i]) < ord(b[i]):
			return True
		elif ord(a[i]) > ord(b[i]):
			return False
	return len(a) < len(b)

def optimize(a):
	occ = [0] * 26
	for i in range(len(a)):
		occ[ord(a[i]) - ord('A')] += 1
	p1 = -1
	p2 = -1
	t = 0
	for i in range(len(a)):
		if p1 < 0:
			occ[ord(a[i]) - ord('A')] -= 1
			for j in range(ord(a[i]) - ord('A')):
				if occ[j] > 0:
					p1 = i
					t = j
					break
		elif ord(a[i]) - ord('A') == t:
			p2 = i
	if p1 >= 0 and p2 >= 0:
		return a[:p1] + a[p2] + a[p1 + 1:p2] + a[p1] + a[p2 + 1:]
	return a

def main():
	t = int(input())
	for i in range(t):
		line = str(input())
		p = line.split()
		mine = p[0]
		yours = p[1]
		new = optimize(mine)
		if better(new, yours):
			print(new)
		else:
			print('---')

def __starting_point():
	main()
__starting_point()


import sys
r_input = sys.stdin.readline
t = int(r_input())
for _ in range(t):
	(s1, s2) = r_input().rstrip().split()
	l_s1 = list(s1)
	l_s2 = list(s2)
	if s1 < s2:
		print(s1)
		continue
	cons1 = {chr(ch): [] for ch in range(65, 91)}
	cons2 = {chr(ch): [] for ch in range(65, 91)}
	for (i, c) in enumerate(s1):
		cons1[c].append(i)
	for (i, c) in enumerate(s2):
		cons2[c].append(i)
	length1 = len(s1)
	length2 = len(s2)
	flag = 0
	for i in range(min(length1, length2)):
		if s1[i] >= s2[i]:
			for c in range(65, ord(s1[i])):
				for n in cons1[chr(c)]:
					if n > i:
						(l_s1[i], l_s1[n]) = (l_s1[n], l_s1[i])
						if l_s1 < l_s2:
							flag = 1
							break
						else:
							(l_s1[i], l_s1[n]) = (l_s1[n], l_s1[i])
				if flag:
					break
			if flag or s1[i] > s2[i]:
				break
	print(''.join(l_s1) if flag else '---')


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	for _ in range(ii()):
		(s, c) = msi()
		if s < c:
			print(s)
		else:
			s = list(s)
			for i in range(len(s)):
				pos = i
				ch = s[i]
				for j in range(i + 1, len(s)):
					if ch >= s[j]:
						ch = s[j]
						pos = j
				if ch != s[i]:
					(s[pos], s[i]) = (s[i], s[pos])
					break
			d = ''
			for i in s:
				d += i
			if d < c:
				print(d)
			else:
				print('---')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for tc in range(int(input())):
	(s, c) = list(map(str, input().split()))
	s = list(s)
	if ''.join(s) < c:
		print(''.join(s))
	else:
		for i in range(len(s)):
			temp = s[i]
			tem = 0
			for j in range(i + 1, len(s)):
				if temp >= s[j]:
					temp = s[j]
					tem = j
			if temp != s[i]:
				(s[tem], s[i]) = (s[i], s[tem])
				break
		s = ''.join(s)
		if s < c:
			print(''.join(s))
		else:
			print('---')


t = int(input())
answers = []
for ti in range(t):
	inp = input().split()
	s = inp[0]
	c = inp[1]
	if s < c:
		answers.append(s)
		continue
	s = list(inp[0])
	s_sorted = s.copy()
	s_sorted.sort()
	c = list(inp[1])
	for i in range(len(s)):
		if s[i] > s_sorted[i]:
			pos = 0
			for j in range(i + 1, len(s)):
				if s[j] == s_sorted[i]:
					pos = j
			if pos != 0:
				char = s[i]
				s[i] = s_sorted[i]
				s[pos] = char
				break
	if s < c:
		answers.append(''.join(s))
	else:
		answers.append('---')
for a in answers:
	print(a)


def smallest(a):
	l = -1
	p = -1
	f = [0] * 100
	a = list(a)
	for q in a:
		f[ord(q) - ord('A')] += 1
	i = 0
	while f[i] == 0 and i < 26:
		i += 1
	for j in range(0, len(a)):
		if l == -1 and ord(a[j]) - ord('A') == i:
			f[i] -= 1
			while f[i] == 0 and i < 26:
				i += 1
		elif ord(a[j]) - ord('A') == i:
			l = j
		elif l == -1:
			p = j
			l = 0
	if l != -1 and p != -1:
		(a[p], a[l]) = (a[l], a[p])
	return ''.join(a)
for _ in range(int(input())):
	(x, y) = input().split()
	x = smallest(x)
	if x >= y:
		print('---')
	else:
		print(x)


for _ in range(int(input())):
	(s, c) = map(list, input().split())
	cond = False
	for i in range(len(s)):
		m = ord(s[i])
		index = -1
		for j in range(i + 1, len(s)):
			if m > ord(s[j]):
				m = ord(s[j])
				index = j
			elif m == ord(s[j]) and ord(s[j]) < ord(s[i]):
				index = j
		if index != -1:
			temp = s[index]
			s[index] = s[i]
			s[i] = temp
			break
	result = False
	cnt = 0
	for i in range(min(len(s), len(c))):
		if s[i] == c[i]:
			cnt += 1
			continue
		if s[i] > c[i]:
			break
		result = True
		break
	if cnt == len(s) and len(s) < len(c):
		print(''.join(s))
	elif result:
		print(''.join(s))
	else:
		print('---')


from collections import Counter
for _ in range(int(input())):
	(s, c) = input().split()
	s = [i for i in s]
	c = [i for i in c]
	if s < c:
		print(''.join(s))
	else:
		characters_of_the_s = Counter(s)
		flag = 0
		for (ind, characters) in enumerate(zip(s, c)):
			(char_of_the_s, char_of_the_c) = characters
			characters_of_the_s[char_of_the_s] -= 1
			if characters_of_the_s[char_of_the_s] == 0:
				del characters_of_the_s[char_of_the_s]
			for sym in characters_of_the_s:
				if sym < char_of_the_c:
					index_to_replace = ind
					substitute = sym
					flag = 1
			if flag == 1:
				break
			if char_of_the_s > char_of_the_c:
				for sym in characters_of_the_s:
					if sym == char_of_the_c:
						index_to_replace = ind
						substitute = sym
						flag = 2
			if char_of_the_s != char_of_the_c:
				break
		if flag == 0:
			print('---')
		elif flag == 1:
			where_substitute_sym = s.index(substitute, index_to_replace + 1)
			(s[index_to_replace], s[where_substitute_sym]) = (s[where_substitute_sym], s[index_to_replace])
			print(''.join(s))
		elif flag == 2:
			s.reverse()
			where_substitute_sym = s.index(substitute)
			s.reverse()
			(s[index_to_replace], s[-where_substitute_sym - 1]) = (s[-where_substitute_sym - 1], s[index_to_replace])
			print(''.join(s) if s < c else '---')


import copy
t = int(input())

def main():
	equalities = set()
	(line, given) = map(str, input().split())
	line = list(line)
	given = list(given)
	possible = 0
	m = 0
	for i in range(min(len(line), len(given))):
		if line[i] > given[i]:
			for k in range(len(line) - 1, i, -1):
				if given[i] > line[k]:
					(line[i], line[k]) = (line[k], line[i])
					toret = ''
					for m in line:
						toret += m
					print(toret)
					return 0
				elif given[i] == line[k]:
					if possible != 0 and k < len(given) and (given[k] > line[i]):
						possible = k
					elif possible == 0:
						possible = k
			m = i
			break
		if line[i] < given[i]:
			toret = ''
			for m in line:
				toret += m
			print(toret)
			return 0
		if line[i] == given[i]:
			equalities.add(i)
	if equalities:
		best = -1
		for i in range(len(line) - 1, -1, -1):
			if line[i] < line[best]:
				best = i
			if i in equalities:
				if line[best] < given[i]:
					(line[best], line[i]) = (line[i], line[best])
					toret = ''
					for m in line:
						toret += m
					print(toret)
					return 0
	if len(equalities) == len(line) and len(given) > len(equalities):
		toret = ''
		for m in line:
			toret += m
		print(toret)
		return 0
	if possible == 0:
		print('---')
		return 0
	(line[possible], line[m]) = (line[m], line[possible])
	for i in range(m, min(len(line), len(given))):
		if line[i] > given[i]:
			print('---')
			return 0
		if line[i] < given[i]:
			toret = ''
			for m in line:
				toret += m
			print(toret)
			return 0
	if len(given) > len(line):
		toret = ''
		for m in line:
			toret += m
		print(toret)
		return 0
	print('---')
	return 0
for i in range(t):
	main()


def main():
	t = int(input())
	for n in range(t):
		(s, c) = (x for x in input().split())
		print(solver(s, c))

def solver(s, c):
	if s < c:
		return s
	else:
		smallestLetter = 'z'
		smallestLetterIndex = None
		swapLeft = None
		swapRight = None
		for i in range(len(s) - 1, 0, -1):
			if s[i] < smallestLetter:
				smallestLetter = s[i]
				smallestLetterIndex = i
			if s[i - 1] > smallestLetter:
				swapLeft = i - 1
				swapRight = smallestLetterIndex
		if swapLeft != None:
			newS = s[:swapLeft] + s[swapRight] + s[swapLeft + 1:swapRight] + s[swapLeft] + s[swapRight + 1:]
		else:
			newS = s
		if newS < c:
			return newS
		else:
			return '---'
main()


def main():
	(s, c) = input().split()
	n = len(s)
	if s < c:
		print(s)
		return
	i = n - 2
	j = n - 1
	while i > -1:
		sn = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
		j = min(i, j, key=lambda x: (s[x], -x))
		if sn < c:
			print(sn)
			return
		i -= 1
	print('---')
t = int(input())
for _ in range(t):
	main()


nn = int(input())
for _ in range(nn):
	(s1, s2) = input().split()
	l1 = list(s1)
	n = len(s1)
	s3 = ''
	if s1 < s2:
		print(s1)
	else:
		d = {'A': True}
		for i in range(n):
			if s1[i] in d:
				continue
			else:
				to_swap = i
				for j in range(n - 1, i, -1):
					if s1[j] < s1[to_swap]:
						to_swap = j
					if s1[j] == 'A':
						break
				if to_swap != i:
					(l1[to_swap], l1[i]) = (l1[i], l1[to_swap])
					s3 = ''.join(l1)
					break
				d[s1[i]] = True
		if s3 != '' and s3 < s2:
			print(s3)
		else:
			print('---')


for i in range(int(input())):
	(mine, other) = input().split()
	if mine < other:
		print(mine)
		continue
	perfect = ''.join(sorted(mine))
	if mine == perfect or perfect > other:
		print('---')
		continue
	(first_problem, second_problem) = (-1, -1)
	for j in range(len(mine)):
		if mine[j] != perfect[j]:
			first_problem = j
			second_problem = mine.rfind(perfect[j], j + 1)
			break
	mine = mine[:first_problem] + perfect[first_problem] + mine[first_problem + 1:second_problem] + mine[first_problem] + mine[second_problem + 1:]
	if mine < other:
		print(mine)
	else:
		print('---')


n = int(input())
while n > 0:
	n -= 1
	(s, c) = input().split()
	lst = list(s)
	now = s
	last = [-1] * 26
	for i in range(len(s)):
		last[ord(s[i]) - ord('A')] = i
	for i in range(len(s) - 1):
		x = min(s[i:])
		if x != s[i]:
			idx = last[ord(x) - ord('A')]
			(lst[idx], lst[i]) = (lst[i], lst[idx])
			now = ''.join(lst)
			break
	if now < c:
		print(now)
	else:
		print('---')


from __future__ import division, print_function
from collections import Counter
from string import ascii_uppercase
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	t = int(input())
	for _ in range(t):
		(s, c) = input().split()
		s = list(s)
		count = Counter(s)
		swapMade = False
		for i in range(len(s)):
			count[s[i]] -= 1
			if i >= len(c):
				print('---')
				break
			if s[i] < c[i]:
				print(''.join(s))
				break
			if swapMade and s[i] < c[i]:
				print('---')
				break
			swapChar = None
			if not swapMade:
				for char in ascii_uppercase:
					if char < c[i]:
						if count[char] > 0:
							swapChar = char
							break
				if swapChar != None:
					curIndex = i + 1
					while curIndex < len(s) and s[curIndex] != swapChar:
						curIndex += 1
					(s[i], s[curIndex]) = (s[curIndex], s[i])
					print(''.join(s))
					break
				elif s[i] != c[i] and count[c[i]] > 0:
					indices = [idx for idx in range(i + 1, len(s)) if s[idx] == c[i]]
					for index in indices:
						(s[i], s[index]) = (s[index], s[i])
						if ''.join(s) < c:
							print(''.join(s))
							break
						else:
							(s[i], s[index]) = (s[index], s[i])
					else:
						print('---')
						break
					break
			if s[i] != c[i]:
				print('---')
				break
		else:
			print(''.join(s)) if len(c) > len(s) else print('---')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(no, final) = map(str, input().split())
	no = list(no)
	final = list(final)
	new = sorted(no)
	for i in range(len(new)):
		if new[i] != no[i]:
			index = ''.join(no).rfind(new[i])
			(no[i], no[index]) = (no[index], no[i])
			break
	if ''.join(no) < ''.join(final):
		print(''.join(no))
	else:
		print('---')


n = int(input())
for i in range(n):
	(str1, str2) = map(str, input().split())
	lst1 = list(str1)
	lstord = []
	for i in range(len(lst1)):
		min1 = ord(lst1[i])
		pos = i
		for j in range(len(lst1) - 1, i, -1):
			if min1 > ord(lst1[j]):
				min1 = ord(lst1[j])
				pos = j
		if pos != i:
			temp = lst1[pos]
			lst1[pos] = lst1[i]
			lst1[i] = temp
			break
	str1 = ''.join(lst1)
	if str1 < str2:
		print(str1)
	else:
		print('---')


for _ in range(int(input())):
	a = list(map(str, input().split(' ')))
	sa = len(a[0])
	i = 0
	cd = sorted(a[0])
	s = ''
	flag = 0
	while i < sa and ord(a[0][i]) == ord(cd[i]):
		i += 1
	if i < sa:
		flag = 1
		c = a[0][i]
		index = i
		t = a[0].rfind(cd[i])
		s += a[0][:i] + a[0][t] + a[0][i + 1:t] + a[0][i] + a[0][t + 1:]
	if flag == 0 and a[0] < a[1]:
		print(a[0])
	elif flag == 1 and s < a[1]:
		print(s)
	else:
		print('---')


from string import ascii_uppercase
a = ascii_uppercase
N = int(input())
for i in range(N):
	(me, comp) = input().split(' ')
	best = ''.join(sorted(me))
	mismatch = -1
	for (index, pair) in enumerate(zip(best, me)):
		(i, j) = pair
		if i != j:
			mismatch = index
			break
	if mismatch != -1:
		swaploc = len(me) - me[mismatch + 1:][::-1].find(best[mismatch]) - 1
		swap1 = me[:mismatch] + me[swaploc] + me[mismatch + 1:swaploc] + me[mismatch] + me[swaploc + 1:]
	else:
		swap1 = me
	if swap1 < comp:
		print(swap1)
	else:
		print('---')


t = int(input())
for i in range(t):
	(s1, s2) = input().split()
	d = {}
	for symbol in s1:
		if symbol in d:
			d[symbol] += 1
		else:
			d[symbol] = 1
	d = {k: v for (k, v) in sorted(d.items(), key=lambda item: item[0])}
	new_s = ''
	found = False
	for (k, v) in d.items():
		for j in range(v):
			new_s += k
			if new_s != s1[:len(new_s)]:
				pos = s1[len(new_s) - 1:].rfind(k)
				old_sym = s1[len(new_s) - 1]
				pos += len(new_s) - 1
				ans = new_s + s1[len(new_s):pos] + old_sym + s1[pos + 1:]
				if ans < s2:
					print(ans)
				else:
					print('---')
				found = True
				break
		if found:
			break
	if not found:
		if s1 < s2:
			print(s1)
		else:
			print('---')


import re

def posi(A, pat):
	return [a.start() for a in list(re.finditer(pat, A))]
for _ in range(int(input())):
	(A, B) = input().split(' ')
	C = [*A]
	D = sorted(C)
	temp1 = 0
	temp2 = 0
	temp3 = 0
	for i in range(len(C)):
		if ord(C[i]) > ord(D[i]):
			temp1 = i
			temp2 = D[i]
			temp3 = C[i]
			break
	stri = A
	if temp2 != 0:
		X = posi(A, temp2)
		(C[X[-1]], C[temp1]) = (C[temp1], C[X[-1]])
		stri = ''.join(C)
	if stri < B:
		print(stri)
	else:
		print('---')


def isASmaller(a, b):
	n = len(a)
	m = len(b)
	for i in range(min(n, m)):
		if a[i] < b[i]:
			return True
		elif a[i] > b[i]:
			return False
	if n < m:
		return True
	else:
		return False

def smallestPossible(a):
	arr = list(a)
	n = len(arr)
	smallestChar = 'Z'
	smallestIdx = -1
	desirableSwaps = [-1 for _ in range(n)]
	for i in range(n - 1, -1, -1):
		if a[i] > smallestChar:
			desirableSwaps[i] = smallestIdx
		if a[i] < smallestChar:
			smallestChar = a[i]
			smallestIdx = i
	for i in range(n):
		if desirableSwaps[i] != -1:
			temp = arr[i]
			arr[i] = arr[desirableSwaps[i]]
			arr[desirableSwaps[i]] = temp
			break
	return ''.join(arr)

def main():
	t = int(input())
	allans = []
	for _ in range(t):
		(a, b) = input().split()
		a2 = smallestPossible(a)
		if isASmaller(a2, b):
			allans.append(a2)
		else:
			allans.append('---')
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultVal, dimensionArr):
	dv = defaultVal
	da = dimensionArr
	if len(da) == 1:
		return [dv for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


def lookForSwipe(string, value, index):
	res = -1
	for (i, v) in enumerate(string[index:]):
		if value >= v:
			value = v
			res = i + index
	return res

def swipe(str1, ind1, ind2):
	str1 = list(str1)
	(str1[ind1], str1[ind2]) = (str1[ind2], str1[ind1])
	return ''.join(str1)

def compare(str1, str2):
	if str1 < str2:
		return (str1, str2)
	for i in range(min(len(str1), len(str2))):
		if str1[i] < str2[i]:
			return (str1, str2)
		else:
			res = lookForSwipe(str1, str2[i], i + 1)
			if res != -1 and str1[res] != str1[i]:
				str1 = swipe(str1, i, res)
				if str1 >= str2:
					return (-1, -1)
				else:
					return (str1, str2)
			if str1[i] > str2[i]:
				return (-1, -1)
	return (-1, -1)
t = int(input())
for i in range(t):
	(site, opp) = input().split()
	(str1, str2) = compare(site, opp)
	if str1 == -1:
		print('---')
	else:
		print(str1)


def main():
	TT = int(input())
	for _ in range(TT):
		(w, t) = input().strip().split(' ')
		w = list(w)
		sf = [len(w) - 1 for _ in range(len(w))]
		for i in range(len(w) - 2, -1, -1):
			if w[i] < w[sf[i + 1]]:
				sf[i] = i
			else:
				sf[i] = sf[i + 1]
		for i in range(len(w)):
			if sf[i] != i and w[sf[i]] != w[i]:
				(w[i], w[sf[i]]) = (w[sf[i]], w[i])
				break
		w = ''.join(w)
		if w < t:
			print(w)
		else:
			print('---')
main()


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(s, c) = input().strip().split()
	s = list(s)
	n = len(s)
	f = 0
	for i in range(n - 1):
		if f:
			break
		x = s[i]
		ind = None
		mn = s[i]
		for j in range(n - 1, i, -1):
			if s[j] < mn:
				mn = s[j]
				ind = j
		if mn < x:
			swaparr(s, i, ind)
			f = 1
			break
	s = ''.join(s)
	if s < c:
		print(s)
	else:
		print('---')


t = int(input())
for _ in range(t):
	(s, c) = input().split()
	if s < c:
		print(s)
		continue
	saisho = []
	p = ord('Z')
	for i in range(len(s)):
		p = min(p, ord(s[-i - 1]))
		saisho.append(p)
	saisho.reverse()
	flag = 0
	for i in range(len(s) - 1):
		if flag == 1:
			break
		if ord(s[i]) > saisho[i + 1]:
			for j in range(len(s) - 1, i, -1):
				if ord(s[j]) == saisho[i + 1]:
					s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
					flag = 1
					break
	if s < c:
		print(s)
	else:
		print('---')


from bisect import bisect_left, bisect_right

def main():
	(s1, s2) = map(list, input().split())
	sort = list(sorted(list(s1)))
	for i in range(len(s1)):
		if s1[i] > sort[i]:
			res = s1[:]
			for j in range(i, len(s1)):
				new = s1[:]
				(new[i], new[j]) = (new[j], new[i])
				if new < res:
					res = new
			s1 = res
			break
	print(''.join(s1)) if s1 < s2 else print('---')
import sys
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
[main() for _ in range(int(input()))]


import string

def sort(s):
	counter = [0] * 26
	for char in s:
		counter[ord(char) - ord('A')] += 1
	res = list()
	for char in string.ascii_uppercase:
		res.extend([char] * counter[ord(char) - ord('A')])
	return ''.join(res)
n = int(input())
for _ in range(n):
	(s_str, c_str) = input().split()
	(s, c) = (list(s_str), list(c_str))
	arranged = sort(s)
	idx = -1
	while idx + 1 < len(s) and s[idx + 1] == arranged[idx + 1]:
		idx += 1
	idx += 1
	if idx < len(s):
		last_idx = s_str.rindex(arranged[idx])
		(s[idx], s[last_idx]) = (s[last_idx], s[idx])
	if ''.join(s) < ''.join(c):
		print(''.join(s))
	else:
		print('---')


import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def st():
	return input().rstrip('\n')

def val():
	return int(input())

def li2():
	return [i for i in input().rstrip('\n').split(' ')]
n = val()
for _ in range(n):
	(s1, s2) = li2()
	fin = sorted(s1)
	if fin[0] > s2[0]:
		print('---')
		continue
	for i in range(len(s1)):
		if s1[i] != fin[i]:
			for j in range(len(s1) - 1, -1, -1):
				if s1[j] == fin[i]:
					s1 = list(s1)
					s1[j] = s1[i]
					s1[i] = fin[i]
					s1 = ''.join(s1)
					break
			break
	print('---' if s1 >= s2 else s1)


INT_MAX = 10 ** 10

def get_nse(nums):
	best = (None, None)
	ans = []
	for j in range(len(nums) - 1, -1, -1):
		ans.append(best)
		if not best[0]:
			best = (a[j], j)
		elif a[j] < best[0]:
			best = (a[j], j)
	ans.reverse()
	return ans
n = int(input())
while n:
	a = input().split()
	b = list(a[0])
	a = list(a[1])
	(a, b) = (b, a)
	nse = get_nse(a)
	i = 0
	used = False
	while i < len(b):
		if i >= len(a):
			break
		if a[i] < b[i]:
			break
		if not used and nse[i][0] and (nse[i][0] < b[i]):
			(a[i], a[nse[i][1]]) = (a[nse[i][1]], a[i])
			break
		if not used and nse[i][0] and (nse[i][0] != a[i]) and (nse[i][0] == b[i]):
			(a[i], a[nse[i][1]]) = (a[nse[i][1]], a[i])
			used = True
		elif b[i] < a[i]:
			a = ['-' for each in range(3)]
		i += 1
	if i == len(b):
		print('---')
	else:
		print(''.join(a))
	n -= 1


from collections import Counter
import string

def sort(s):
	counter = Counter(s)
	res = list()
	for char in string.ascii_uppercase:
		res.extend([char] * counter[char])
	return ''.join(res)
n = int(input())
for _ in range(n):
	(s_str, c_str) = input().split()
	(s, c) = (list(s_str), list(c_str))
	arranged = sort(s)
	idx = -1
	while idx + 1 < len(s) and s[idx + 1] == arranged[idx + 1]:
		idx += 1
	idx += 1
	if idx < len(s):
		last_idx = s_str.rindex(arranged[idx])
		(s[idx], s[last_idx]) = (s[last_idx], s[idx])
	if ''.join(s) < ''.join(c):
		print(''.join(s))
	else:
		print('---')


for _ in range(int(input())):
	(a, b) = map(str, input().split())
	sa = sorted(a)
	index = 0
	while index < len(a) and a[index] == sa[index]:
		index += 1
	if index < len(a):
		j = a.rfind(sa[index])
		a = a[:index] + sa[index] + a[index + 1:j] + a[index] + a[j + 1:]
	if a < b:
		print(a)
	else:
		print('---')


t = int(input())
for _ in range(t):
	(s, t) = [list(i) for i in input().split(' ')]
	ss = sorted(s)
	for i in range(len(s)):
		if s[i] != ss[i]:
			break
	for j in range(len(s)):
		if s[j] == s[i]:
			break
	for k in range(len(ss) - 1, -1, -1):
		if ss[i] == s[k]:
			break
	(s[j], s[k]) = (s[k], s[j])
	if s < t:
		print(''.join(s))
	else:
		print('---')


import sys
import os
from io import BytesIO, IOBase

def cel(n, k):
	return n // k + (n % k != 0)

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

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

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
for _ in range(ii()):
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	if s < c:
		print(''.join(s))
		continue
	idx = 0
	s2 = sorted(s)
	for i in range(len(s)):
		if s[i] == s2[idx]:
			idx += 1
			continue
		if s[i] > s2[idx]:
			r = ''.join(s).rfind(s2[idx])
			(s[i], s[r]) = (s[r], s[i])
			break
	if s < c:
		print(''.join(s))
	else:
		print('---')


t = int(input())
for g in range(0, t):
	a = list(map(str, input().split()))
	inp = a[0]
	c = a[1]
	s = []
	for i in range(0, len(inp)):
		s.append(inp[i])
	n = len(s)
	swap = 0
	for i in range(0, n):
		if s[i] == 'A':
			continue
		else:
			temp = s[i]
			for j in range(i + 1, n):
				if temp > s[j]:
					temp = s[j]
			if temp != s[i]:
				for j in range(n - 1, i - 1, -1):
					if s[j] == temp:
						break
				(s[i], s[j]) = (s[j], s[i])
				swap = 1
		if swap == 1:
			break
	ans = ''
	for i in range(0, n):
		ans += s[i]
	if ans < c:
		print(ans)
	else:
		print('---')


from sys import stdin, stdout
for i in range(int(input())):
	(s, c) = stdin.readline().split()
	s = list(s)
	tmp = sorted(s)
	l = len(s)
	(s1, t1) = ('0', '0')
	for j in range(l):
		if tmp[j] != s[j]:
			(s1, t1) = (s[j], tmp[j])
			s[j] = t1
			break
	for j in range(l - 1, -1, -1):
		if s[j] == t1:
			s[j] = s1
			break
	s = ''.join(s)
	if s < c:
		stdout.write('%s\n' % s)
	else:
		stdout.write('---\n')


for _ in range(int(input())):
	(s, c) = map(str, input().split())
	ls = list(s)
	lc = list(c)
	for i in range(len(ls)):
		if ls[i] != min(ls[i:]):
			x = ''.join(ls[i:])
			(ls[i], ls[i + x.rfind(min(x))]) = (ls[i + x.rfind(min(x))], ls[i])
			break
	print(''.join(ls) if ls < lc else '---')


import math, collections, sys
for _ in range(int(input())):
	(s, t) = input().split()
	z = [i for i in s]
	z.sort()
	i = 0
	while i < len(s):
		if z[i] == s[i]:
			i += 1
		else:
			break
	if i != len(s):
		x = [c for c in s]
		j = len(s) - 1
		while j > -1:
			if s[j] == z[i]:
				(x[i], x[j]) = (x[j], x[i])
				break
			j -= 1
		s = ''.join(x)
	if s < t:
		print(s)
	else:
		print('---')


n = int(input())

def swap(s, i, j):
	lst = list(s)
	(lst[i], lst[j]) = (lst[j], lst[i])
	return ''.join(lst)

def indexr(ls, x):
	for i in range(len(ls)):
		if ls[len(ls) - 1 - i] == x:
			return len(ls) - 1 - i
			break
	else:
		return -1

def ttt(s, t):
	if len(s) < len(t) and t[0:len(s)] == s:
		return True
	else:
		for k in range(min(len(s), len(t))):
			if ord(s[k]) > ord(t[k]):
				return False
			elif ord(s[k]) < ord(t[k]):
				return True
	return False
for i in range(n):
	test = input().split()
	a = test[0]
	b = test[1]
	for k in range(min(len(a), len(b))):
		if ord(a[k]) < ord(b[k]):
			print(a)
			break
		else:
			l = min(a[k:], key=ord)
			ind = indexr(a[k:], l)
			ls = swap(a, ind + k, k)
			if ttt(ls, b):
				print(ls)
				break
			if a[k] != b[k]:
				print('---')
				break
	else:
		print('---')


def check(s1, s2):
	s = list(s1)
	s.sort()
	rtn = ''
	if s1 < s2:
		return s1
	if ''.join(s) > s2:
		return '---'
	for i in range(len(s)):
		if s1[i] != s[i]:
			rtn += s1[:i]
			rtn += s[i]
			for j in range(len(s) - 1, i, -1):
				if s1[j] == s[i]:
					rtn += s1[i + 1:j]
					rtn += s1[i]
					rtn += s1[j + 1:]
					return rtn if rtn < s2 else '---'
	return '---'
for rep in range(int(input())):
	print(check(*input().split()))


def amazon(s, c):
	if s < c:
		return s
	elif c == 'a' * len(c):
		return '---'
	else:
		for j in range(len(s) - 1):
			check = 0
			for k in range(j + 1, len(s)):
				word = s
				if s[j] > s[k]:
					word = word[:j] + word[k] + word[j + 1:k] + word[j] + word[k + 1:]
				if word < c:
					return word
		return '---'
for i in range(int(input())):
	(s, c) = input().strip().split()
	print(amazon(s, c))


from sys import stdin
from string import ascii_uppercase as y
for _ in range(int(stdin.readline())):
	(s, c) = stdin.readline().split()
	dct = {q: set() for q in y}
	fl = 0
	for i in range(len(s)):
		dct[s[i]].add(i)
	dct1 = {'A': set()}
	for r in range(1, 26):
		dct1[y[r]] = dct1[y[r - 1]].union(dct[y[r - 1]])
	for i in range(min(len(s), len(c))):
		if fl:
			break
		if s[i] < c[i]:
			print(s)
			fl = 1
			break
		elif s[i] == c[i]:
			for x in dct1[s[i]]:
				if x > i:
					print(s[:i] + s[x] + s[i + 1:x] + s[i] + s[x + 1:])
					fl = 1
					break
		else:
			for x in dct1[c[i]]:
				if x > i:
					print(s[:i] + s[x] + s[i + 1:x] + s[i] + s[x + 1:])
					fl = 1
					break
			if not fl:
				for x in dct[c[i]]:
					if x > i:
						u = s[:i] + s[x] + s[i + 1:x] + s[i] + s[x + 1:]
						if u < c:
							print(u)
							fl = 1
							break
				if not fl:
					print('---')
					fl = 1
					break
	if not fl:
		if len(s) < len(c):
			print(s)
		else:
			print('---')


mod = 10 ** 9 + 7
INF = float('inf')

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

def readTuples():
	return tuple(map(int, input().split()))

def I():
	return int(input())
q = I()
for _ in range(q):
	(S, T) = input().split()
	D = sorted(S)
	S = list(S)
	ln = len(S)
	for i in range(ln):
		if S[i] > D[i]:
			pos = ln - 1
			while i < pos:
				if S[pos] == D[i]:
					break
				pos -= 1
			(S[i], S[pos]) = (S[pos], S[i])
			break
	S = ''.join(S)
	if S < T:
		print(S)
	else:
		print('---')


import sys
from math import gcd
from heapq import heappop
from heapq import heappush
from heapq import heapify
from bisect import insort
from bisect import bisect_right
from bisect import bisect_left
from sys import stdin, stdout
from collections import defaultdict, deque
from math import log2, ceil, floor
inp = lambda : int(input())
sip = lambda : input()
mulip = lambda : map(int, input().split())
lst = lambda : list(map(int, input().split()))
slst = lambda : list(sip())
arr2d = lambda x: [[int(j) for j in input().split()] for i in range(x)]
odds = lambda l: len(list(filter(lambda x: x % 2 != 0, l)))
evens = lambda l: len(list(filter(lambda x: x % 2 == 0, l)))
mod = pow(10, 9) + 7
Judge = 0
if Judge:
	sys.stdin = open('input.in', 'r')
for _ in range(inp()):
	(s1, s2) = map(list, input().split())
	d = defaultdict(int)
	for i in range(len(s1)):
		d[s1[i]] = i
	x = sorted(s1)
	for i in range(len(s1)):
		if s1[i] != x[i]:
			tmp = s1[i]
			s1[i] = s1[d[x[i]]]
			s1[d[x[i]]] = tmp
			break
	if ''.join(s1) < ''.join(s2):
		print(''.join(s1))
	else:
		print('---')


import sys

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

def input():
	return sys.stdin.readline().rstrip('\n')

def oint():
	return int(input())
t = oint()
for _ in range(t):
	ss = input()
	(s, c) = ss.split()
	s = list(s)
	c = list(c)
	ls = len(s)
	lc = len(c)
	l = min(ls, lc)
	find = 0
	for i in range(l):
		if ord(s[i]) == ord(c[i]):
			for j in range(i + 1, ls):
				if ord(s[j]) < ord(c[i]):
					(s[i], s[j]) = (s[j], s[i])
					find = 1
					break
		elif ord(s[i]) > ord(c[i]):
			for j in range(i + 1, ls):
				if ord(s[j]) < ord(c[i]):
					(s[i], s[j]) = (s[j], s[i])
					find = 1
					break
			else:
				for j in range(ls - 1, i, -1):
					if ord(s[j]) == ord(c[i]):
						(s[i], s[j]) = (s[j], s[i])
						break
				else:
					find = -1
					break
				for j in range(i + 1, l):
					if ord(s[j]) < ord(c[j]):
						find = 1
						break
					elif ord(s[j]) > ord(c[j]):
						find = -1
						break
				if find:
					break
				if ls < lc:
					find = 1
					break
				else:
					find = -1
					break
		else:
			find = 1
			break
		if find:
			break
	if find == 0:
		if ls < lc:
			find = 1
		else:
			find = -1
	if find == 1:
		print(''.join(s))
	else:
		print('---')


for _ in range(int(input())):
	(s1, s2) = input().split()
	if s1 < s2:
		print(s1)
		continue
	di = [0] * 26 + [1]
	for i in s1:
		di[ord(i.upper()) - 65] += 1
	cur_i = 0
	for i in range(26):
		if di[i] > 0:
			cur_i = i
			break
	s11 = ''
	alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	ind = 0
	for j in range(len(s1)):
		i = s1[j]
		if i == alphabets[cur_i]:
			di[cur_i] -= 1
			if di[cur_i] == 0:
				for x in range(cur_i, 27):
					if di[x] > 0:
						cur_i = x
						break
		else:
			ind = j
			break
	s1 = list(s1)
	s11 = 'Z' * 6000
	for i in range(ind + 1, len(s1)):
		s1copy = list(s1)
		(s1copy[i], s1copy[ind]) = (s1copy[ind], s1copy[i])
		s11 = min(s11, ''.join(s1copy))
	if s11 >= s2:
		print('---')
	else:
		print(s11)


for _ in range(int(input())):
	t = input()
	(s, c) = t.split(' ')
	ln = 0
	ln = min(len(s), len(c))
	s1 = list()
	for i in s:
		s1.append(i)
	c1 = list()
	for i in c:
		c1.append(i)
	ch = -1
	for i in range(ln):
		ch = -1
		ind = -1
		od = ord(c1[i])
		for j in range(i, len(s1)):
			if ord(s1[j]) < od:
				ind = j
				ch = i
		if ind == -1:
			for j in range(i, len(s1)):
				if ord(s1[j]) == od:
					if j == i:
						ch = ln + 1
						break
					else:
						ch = i
						ind = j
		if ind >= 0:
			(s1[ch], s1[ind]) = (s1[ind], s1[ch])
			break
		elif ch == -1:
			break
	s = ''.join(s1)
	if s < c:
		print(s)
	else:
		print('---')


for _ in range(int(input())):
	(s, c) = input().split()
	l = len(s)
	if s < c:
		print(s)
		continue
	s = list(s)
	s1 = sorted(s)
	c = list(c)
	j = 0
	for i in range(l):
		if s[i] == s1[j]:
			j += 1
			continue
		if s[i] > s1[j]:
			r = ''.join(s).rfind(s1[j])
			(s[i], s[r]) = (s[r], s[i])
			break
	if ''.join(s) < ''.join(c):
		print(''.join(s))
	else:
		print('---')


for _ in [0] * int(input()):
	(s, c) = input().split()
	t = sorted(s)
	i = 0
	l = len(s)
	while i < l and s[i] == t[i]:
		i += 1
	if i < l:
		j = s.rfind(t[i])
		s = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
	print(('---', s)[s < c])


n = int(input())
for i in range(n):
	(s, t) = map(str, input().split())
	if len(s) == 1:
		if s < t:
			print(s)
		else:
			print('---')
		continue
	mas = [['ZZ', -1]]
	for j in range(len(s) - 1, -1, -1):
		if mas[-1][0] > s[j]:
			mas.append([s[j], j])
		else:
			mas.append(mas[-1])
	mas = mas[::-1]
	flag = True
	for j in range(len(s)):
		if s[j] > mas[j][0]:
			s = s[:j] + mas[j][0] + s[j + 1:mas[j][1]] + s[j] + s[mas[j][1] + 1:]
			if s >= t:
				print('---')
			else:
				print(s)
			flag = False
			break
	if flag:
		if s < t:
			print(s)
		else:
			print('---')


from collections import Counter
n = int(input())
for _ in range(n):
	(s_str, c_str) = input().split()
	(s, c) = (list(s_str), list(c_str))
	arranged = list(sorted(s))
	idx = -1
	while idx + 1 < len(s) and s[idx + 1] == arranged[idx + 1]:
		idx += 1
	idx += 1
	if idx < len(s):
		last_idx = s_str.rindex(arranged[idx])
		(s[idx], s[last_idx]) = (s[last_idx], s[idx])
	if ''.join(s) < ''.join(c):
		print(''.join(s))
	else:
		print('---')


t = int(input())
for _ in range(t):
	(a, b) = input().split()
	a = list(a)
	n = len(a)
	a_sorted = ''.join(list(sorted(a)))
	ind1 = -1
	ind2 = -1
	shortval = 99999
	for i in range(n):
		if a[i] == a_sorted[i]:
			continue
		else:
			ind1 = i
			short_val = a_sorted[i]
			break
	if ind1 != -1:
		for i in range(n - 1, -1, -1):
			if a[i] == short_val:
				ind2 = i
				break
		(a[ind1], a[ind2]) = (a[ind2], a[ind1])
	temp = ''.join(a)
	if temp < b:
		print(temp)
	else:
		print('---')


t = int(input())
for _ in range(t):
	(s, c) = input().split()
	n = len(s)
	f = False
	s2 = s
	if s2 < c:
		f = True
	i = n - 2
	j = n - 1
	while i > -1 and (not f):
		s2 = s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
		j = min(i, j, key=lambda x: (s[x], -x))
		if s2 < c:
			f = True
			break
		i -= 1
	if f:
		print(s2)
	else:
		print('---')


for i in range(int(input())):
	(a, b) = input().split()
	(t, s) = (list(a), ''.join(sorted(a)))
	for j in range(len(a)):
		if t[j] != s[j]:
			y = a.rindex(s[j])
			(t[j], t[y]) = (t[y], t[j])
			break
	a = ''.join(t)
	print(a if a < b else '---')


kl = int(input())
for kt in range(kl):
	(s, s1) = [str(i) for i in input().split()]
	mn = []
	for j in range(0, len(s)):
		mx = s[j]
		for i in range(j, len(s)):
			if s[i] < mx:
				mx = s[i]
				it = i
		if mx != s[j]:
			mn += [j]
			mn += [s.rfind(s[it])]
			break
	if mn != []:
		s2 = s[:mn[0]] + s[mn[1]] + s[mn[0] + 1:mn[1]] + s[mn[0]] + s[mn[1] + 1:]
	else:
		s2 = s
	s2 += '1'
	s1 += '1'
	pr = 1
	for i in range(len(s2)):
		if s2[i] < s1[i]:
			pr = 0
			print(s2[:len(s2) - 1])
			break
		if s2[i] > s1[i]:
			pr = 0
			print('---')
			break
	if pr:
		print('---')


from bisect import bisect

def minify(s: str) -> str:
	(s, n) = (list(s), len(s))
	for i in range(1, n):
		if s[i] < s[i - 1]:
			k = min(range(n - 1, i - 1, -1), key=lambda _: s[_])
			j = bisect(s, s[k], 0, i)
			(s[k], s[j]) = (s[j], s[k])
			break
	return ''.join(s)

def main():
	for _ in range(int(input())):
		(s, t) = input().split()
		s = minify(s)
		print(s if s < t else '---')
main()


import sys
from collections import deque
n = int(input())

def lastIndex(li, e):
	ind = -1
	for i in range(len(li)):
		if li[i] == e:
			ind = i
	return ind

def swap(i2, li):
	placehold = li[0]
	li[0] = li[i2]
	li[i2] = placehold
	return li

def optimize(string):
	x = sorted(string)
	newS = deque()
	for i in range(len(x)):
		if string[i] != x[i]:
			newS.extend(swap(lastIndex(string[i:], x[i]), string[i:]))
			break
		else:
			newS.append(x[i])
	return newS
for i in range(n):
	comps = [x for x in input().split(' ')]
	user = comps[0]
	opp = comps[1]
	if user < opp:
		print(user)
	else:
		string = ''.join(optimize(list(user)))
		if string < opp:
			print(string)
		else:
			print('---')


for _ in range(int(input())):
	(a, c) = input().split()
	a = list(a)
	b = sorted(a)
	if a != b:
		for (i, x) in enumerate(b):
			if a[i] != x:
				tmp = a[i]
				a[i] = x
				break
		for i in range(len(a) - 1, -1, -1):
			if a[i] == x:
				a[i] = tmp
				break
	a = ''.join(a)
	if a < c:
		print(a)
	else:
		print('---')


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

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

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

def ws(s):
	sys.stdout.write(s + '\n')

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

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		(s, c) = input().split()
		s = list(s)
		a = sorted(s)
		for i in range(len(a)):
			if s[i] != a[i]:
				for j in range(len(a) - 1, i, -1):
					if s[j] == a[i]:
						(s[i], s[j]) = (s[j], s[i])
						break
				break
		st = ''.join(s)
		if st >= c:
			print('---')
		else:
			print(st)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
p = int(sys.stdin.readline().strip())
ans = []

def findMinimum(lst):
	lo = 'a'
	for k in range(len(lst)):
		if lst[k] <= lo:
			lo = lst[k]
			idx = k
	return (lo, idx)
for i in range(p):
	(m, n) = sys.stdin.readline().strip().split(' ')
	m = list(m)
	n = list(n)
	objduplicate = m[:]
	if m < n:
		ans.append(''.join(m))
		continue
	for p in range(len(m) - 1):
		(lo, idx) = findMinimum(m[p + 1:])
		if lo < m[p]:
			(m[idx + p + 1], m[p]) = (m[p], m[idx + p + 1])
			break
	if m < n:
		ans.append(''.join(m))
	else:
		ans.append('---')
for i in ans:
	sys.stdout.write(i + '\n')


for _ in range(int(input())):
	(n, m) = map(str, input().split())
	n1 = sorted(list(n))
	for i in range(len(n1)):
		if n[i] != n1[i]:
			break
	else:
		if n < m:
			print(n)
		else:
			print('---')
		continue
	n = list(n)
	for j in range(len(n) - 1, -1, -1):
		if n[j] == n1[i]:
			(n[i], n[j]) = (n[j], n[i])
	n = ''.join(n)
	if n < m:
		print(n)
	else:
		print('---')


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	(s, t) = input().split()
	s = list(s)
	ss = sorted(s)
	t = list(t)
	for i in range(len(s)):
		if s[i] == ss[i]:
			continue
		for j in range(len(s) - 1, -1, -1):
			if s[j] == ss[i]:
				(s[i], s[j]) = (s[j], s[i])
				break
		break
	for (i, j) in zip(s, t):
		if i < j:
			print(''.join(s))
			break
		elif i > j:
			print('---')
			break
	else:
		if len(s) < len(t):
			print(''.join(s))
		else:
			print('---')


n_cases = int(input())
for _ in range(n_cases):
	(s, t) = input().split()
	if s < t:
		print(s)
		continue
	cnt = [0] * 26
	for ch in s:
		cnt[ord(ch) - 65] += 1
	s = list(s)
	for i in range(len(s)):
		ch = ord(s[i]) - 65
		flag = False
		for j in range(ch):
			if cnt[j] > 0:
				s[i] = chr(65 + j)
				cnt[j] -= 1
				flag = True
				break
		if flag:
			for j in range(i + 1, len(s)):
				temp = ord(s[j]) - 65
				if cnt[temp] > 0:
					cnt[temp] -= 1
				else:
					s[j] = chr(ch + 65)
			break
		else:
			cnt[ch] -= 1
	s = ''.join(s)
	if s < t:
		print(s)
	else:
		print('---')


for counter in range(int(input())):
	(s, t) = (x for x in input().split())
	s = list(s)
	n = len(s)
	for i in range(n - 1):
		mn = n - s[::-1].index(min(s[i:])) - 1
		if s[mn] < s[i]:
			(s[i], s[mn]) = (s[mn], s[i])
			break
	s = ''.join(s)
	if s < t:
		print(s)
	else:
		print('---')


def solve(s, c):
	(q, p) = (list(s), sorted(s))
	for i in range(len(s)):
		if s[i] != p[i]:
			j = s.rindex(p[i])
			(q[i], q[j]) = (q[j], q[i])
			break
	q = ''.join(q)
	return q if q < c else '---'
for T in range(int(input())):
	print(solve(*input().split()))


for i in range(int(input())):
	(a, b) = input().split()
	a = list(a)
	b = list(b)
	if a < b:
		print(''.join(a))
	else:
		A = list(sorted(a))
		for i in range(len(a)):
			if A[i] != a[i]:
				re = -1
				for k in range(i, len(a)):
					if a[k] == A[i]:
						re = max(k, re)
				(a[i], a[re]) = (a[re], a[i])
				break
		if a < b:
			print(''.join(a))
		else:
			print('---')


def solveOne(me, rival):
	(SMALLER, EQUAL, BIGGER) = range(3)
	if me < rival:
		return ''.join(me)
	n = min(len(me), len(rival))
	compare = [[EQUAL for _ in range(n)] for _ in range(n)]
	for i in range(n):
		if me[i] < rival[i]:
			compare[i][i] = SMALLER
		elif me[i] > rival[i]:
			compare[i][i] = BIGGER
		else:
			compare[i][i] = EQUAL
	for left in range(n):
		for right in range(left + 1, n):
			if compare[left][right - 1] != EQUAL:
				compare[left][right] = compare[left][right - 1]
			elif me[right] < rival[right]:
				compare[left][right] = SMALLER
			elif me[right] > rival[right]:
				compare[left][right] = BIGGER
			else:
				compare[left][right] = EQUAL

	def isLess():
		for seg in ((0, min(i - 1, n - 1)), (i,), (i + 1, min(j - 1, n - 1)), (j,), (j + 1, n - 1)):
			if len(seg) == 2:
				(left, right) = seg
				if right >= left:
					if compare[left][right] == SMALLER:
						return True
					if compare[left][right] == BIGGER:
						return False
			else:
				pos = seg[0]
				if pos < len(rival):
					if rival[pos] > me[pos]:
						return True
					if rival[pos] < me[pos]:
						return False
		return len(me) < len(rival)
	for i in range(len(me)):
		for j in range(i + 1, len(me)):
			if me[i] > me[j]:
				(me[i], me[j]) = (me[j], me[i])
				if isLess():
					return ''.join(me)
				else:
					(me[i], me[j]) = (me[j], me[i])
	return '---'
for _ in range(int(input())):
	print(solveOne(*map(list, input().split())))


for _ in range(int(input())):
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	n = len(s)
	x = s[:]
	x.sort()
	ind = 0
	for i in range(n):
		if s[i] > x[i]:
			ind = i
			break
	for j in range(n - 1, -1, -1):
		if s[j] == x[ind]:
			(s[j], s[ind]) = (s[ind], s[j])
			break
	if s < c:
		print(''.join(s))
	else:
		print('---')


for _ in range(int(input())):
	(s, c) = input().split()
	if s < c:
		print(s)
		continue
	a = sorted(s)
	for i in range(len(s)):
		if a[i] != s[i]:
			break
	if i == len(s) - 1:
		print('---')
		continue
	for j in range(len(s) - 1, i, -1):
		s = list(s)
		if s[j] == a[i]:
			(s[i], s[j]) = (s[j], s[i])
			break
	if s < list(c):
		print(*s, sep='')
	else:
		print('---')


t = int(input())
for _ in range(t):
	(s1, s2) = input().split()
	correct = ''.join(sorted(s1))
	val = change = -1
	for i in range(len(s1)):
		if s1[i] != correct[i]:
			val = correct[i]
			change = i
			break
	if val != -1 and change != -1:
		for i in range(change, len(s1)):
			if s1[i] == val:
				change1 = i
		s_fin = ''
		for i in range(len(s1)):
			if i == change:
				s_fin += s1[change1]
			elif i == change1:
				s_fin += s1[change]
			else:
				s_fin += s1[i]
		s_fin = ''.join(s_fin)
	else:
		s_fin = s1
	if s_fin >= s2:
		print('---')
	else:
		print(s_fin)


def better(a):
	if a == ''.join(sorted(a)):
		return a
	(a, n) = (list(a), len(a))
	for i in range(n):
		ans = -1
		for j in range(i + 1, n):
			if a[i] <= a[j]:
				continue
			if ans == -1 or a[ans] >= a[j]:
				ans = j
		if ans != -1:
			tmp = a[ans]
			a[ans] = a[i]
			a[i] = tmp
			break
	return ''.join(a)

def run(a, b):
	if a < b:
		return a
	a = better(a)
	if a < b:
		return a
	else:
		return '---'

def main():
	N = int(input())
	for i in range(N):
		print(run(*input().strip().split()))
main()


from sys import stdin, stdout

def main():
	T = int(stdin.readline())
	for i in range(T):
		a = list(map(str, stdin.readline().split()))
		newstr = str(a[0])
		already = False
		possible = False
		for (j, (x, y)) in enumerate(zip(a[0], a[1])):
			if x < y:
				possible = True
				break
			if x >= y and already is False:
				k = len(a[0]) - 1
				while k > j:
					if a[0][k] < a[1][j]:
						temp = list(newstr)
						(temp[j], temp[k]) = (temp[k], temp[j])
						newstr = ''.join(temp)
						already = True
						possible = True
						break
					k -= 1
				if already is False and x > y:
					k = len(a[0]) - 1
					while k > j:
						if a[0][k] == a[1][j] and a[0][k] != a[0][j]:
							temp = list(newstr)
							(temp[j], temp[k]) = (temp[k], temp[j])
							newstr = ''.join(temp)
							already = True
							possible = True
							break
						k -= 1
				if k == j and x > y:
					possible = False
					break
				elif k == j and x == y:
					continue
				else:
					break
			if possible is True or already is True:
				break
		if newstr < a[1]:
			stdout.write(newstr + '\n')
		else:
			stdout.write('---\n')
main()


t = int(input())
for _ in range(t):
	(a, b) = input().split()
	s = list(a)
	s.sort()
	(l, r) = (-1, -1)
	for i in range(len(a)):
		if l < 0:
			if a[i] != s[i]:
				l = i
		elif a[i] == s[l]:
			r = i
	s = list(a)
	(s[l], s[r]) = (s[r], s[l])
	a = ''.join(s)
	if a < b:
		print(a)
	else:
		print('---')


for _ in range(int(input())):
	(s1, s2) = map(str, input().split())
	l2 = list(s2)
	if s1 < s2:
		print(s1)
	else:
		l1 = list(s1)
		l1.sort()
		t = -1
		for i in range(len(l1)):
			if l1[i] < s1[i]:
				t = i
				break
		l1 = list(s1)
		if t == -1:
			print('---')
		else:
			f = 0
			for i in range(t + 1, len(s1)):
				p = l1[i]
				l1[i] = l1[t]
				l1[t] = p
				if l1 < l2:
					f = 1
					s = ''.join(map(str, l1))
					break
				else:
					p = l1[i]
					l1[i] = l1[t]
					l1[t] = p
			if f == 1:
				print(s)
			else:
				print('---')


def solve():
	(a, b) = input().split(' ')
	if a < b:
		return a
	a = list(a)
	mniejsze = [int()] * len(a)
	najmniejszy = len(a) - 1
	for i in range(len(a) - 1, -1, -1):
		mniejsze[i] = -1
		if a[najmniejszy] < a[i]:
			mniejsze[i] = najmniejszy
		if a[i] < a[najmniejszy]:
			najmniejszy = i
	for i in range(min(len(a), len(b))):
		if mniejsze[i] != -1 and a[mniejsze[i]] < b[i]:
			(a[i], a[mniejsze[i]]) = (a[mniejsze[i]], a[i])
			return ''.join(a)
		if mniejsze[i] != -1 and a[mniejsze[i]] == b[i]:
			(a[i], a[mniejsze[i]]) = (a[mniejsze[i]], a[i])
			if ''.join(a) < b:
				return ''.join(a)
			(a[i], a[mniejsze[i]]) = (a[mniejsze[i]], a[i])
		if a[i] > b[i]:
			break
	return '---'
n = int(input())
for i in range(n):
	print(solve())


q = int(input())
for _ in ' ' * q:
	(a, b) = map(list, input().split())
	l = [0] * len(a)
	minI = len(a) - 1
	minC = a[minI]
	for i in range(len(a) - 1, -1, -1):
		if a[i] < minC:
			minC = a[i]
			minI = i
		l[i] = [minC, minI]
	for i in range(len(a)):
		if a[i] > l[i][0]:
			(a[i], a[l[i][1]]) = (a[l[i][1]], a[i])
			break
	if a < b:
		print(*a, sep='')
	else:
		print('---')


def swap(lst, i, j):
	(lst[i], lst[j]) = (lst[j], lst[i])

def comp(l1, l2):
	for i in range(min(len(l1), len(l2))):
		if l1[i] < l2[i]:
			return True
		if l1[i] == l2[i]:
			continue
		return False
	return len(l1) < len(l2)

def logic(s, c):
	if comp(s, c):
		return s
	best = -1
	for i in reversed(range(len(s))):
		if best == -1 or s[i] < s[best]:
			best = i
		swap(s, i, best)
		if comp(s, c):
			return s
		swap(s, i, best)
	return list('---')
t = int(input())
for i in range(t):
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	a = logic(s, c)
	print(''.join(a))


for i in range(int(input())):
	(s, c) = [i for i in input().split()]
	arr = sorted(range(0, len(s)), key=s.__getitem__)
	s = [i for i in s]
	swap = None
	for i in range(len(s)):
		if i != arr[i]:
			swap = i
			break
	if swap != None:
		lt = s[arr[swap]]
		pos = arr[swap]
		for i in range(len(s)):
			if s[i] == lt:
				pos = i
		(s[swap], s[pos]) = (s[pos], s[swap])
	s = ''.join(s)
	if s < c:
		print(s)
	else:
		print('---')


import re
for _ in range(int(input())):
	(A, B) = input().split(' ')
	C = [*A]
	D = sorted(C)
	temp1 = 0
	for i in range(len(C)):
		if C[i] > D[i]:
			temp1 = i
			break
	stri = A
	if D[temp1] != 0:
		X = [a.start() for a in list(re.finditer(D[temp1], A))]
		(C[X[-1]], C[temp1]) = (C[temp1], C[X[-1]])
		stri = ''.join(C)
	print(('---', stri)[stri < B])


alphabet_large = {chr(i): i - 65 for i in range(65, 65 + 26)}
t = int(input())
for _ in range(t):
	(s, c) = input().split()
	l = len(s)
	t = False
	for i in range(26):
		for (j, x) in enumerate(s[::-1]):
			if alphabet_large[x] == i:
				for (k, y) in enumerate(s[:l - j - 1]):
					if alphabet_large[y] > i:
						s = s[:k] + x + s[k + 1:l - j - 1] + y + s[l - j:]
						t = True
						break
			if t:
				break
		if t:
			break
	now = 0
	while now < min(len(s), len(c)):
		if alphabet_large[s[now]] < alphabet_large[c[now]]:
			print(s)
			break
		elif alphabet_large[s[now]] > alphabet_large[c[now]]:
			print('---')
			break
		else:
			now += 1
	if now == min(len(s), len(c)):
		if len(s) < len(c):
			print(s)
		else:
			print('---')


n = int(input())
for i in range(0, n):
	ln = input().split(' ')
	s1 = ln[0]
	s2 = ln[1]
	inds = [-1] * 26
	for j in range(0, len(s1)):
		inds[ord(s1[j]) - 65] = j
	for j in range(0, len(s1)):
		o = ord(s1[j]) - 65
		repl = False
		for k in range(o - 1, -1, -1):
			if inds[k] > j:
				repl = [j, inds[k]]
		if repl:
			s1 = list(s1)
			os = s1[repl[0]]
			s1[repl[0]] = s1[repl[1]]
			s1[repl[1]] = os
			s1 = ''.join(s1)
			break
	f = True
	for j in range(0, len(s1)):
		if j >= len(s2):
			f = False
			break
		if s1[j] == s2[j]:
			continue
		if ord(s1[j]) > ord(s2[j]):
			f = False
			break
		if ord(s1[j]) < ord(s2[j]):
			break
	if f and s1 != s2:
		print(s1)
	else:
		print('---')


from collections import defaultdict
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(s, c) = input().split()
	N = len(s)
	s_list = [(ord(s[i]), i) for i in range(N)]
	s_list_set = list(set([ord(si) for si in s]))
	s_list_set.sort()
	val2idx = {s_list_set[i]: i for i in range(len(s_list_set))}
	s_list_cmpr = [0] * N
	for i in range(N):
		s_list_cmpr[s_list[i][1]] = val2idx[s_list[i][0]]
	idx = defaultdict(list)
	for i in range(N):
		idx[s_list_cmpr[i]].append(i)
	k = 0
	i1 = i2 = 0
	for i in range(N):
		if s_list_cmpr[i] == k:
			continue
		elif s_list_cmpr[i] == k + 1:
			if idx[k][-1] == i - 1:
				k += 1
			else:
				i1 = i
				i2 = idx[k][-1]
				break
		else:
			while idx[k][-1] <= i:
				k += 1
			i1 = i
			i2 = idx[k][-1]
			break
	s_list_raw = [ord(si) for si in s]
	(s_list_raw[i1], s_list_raw[i2]) = (s_list_raw[i2], s_list_raw[i1])
	ans = ''.join([chr(x) for x in s_list_raw])
	if ans < c:
		print(ans)
	else:
		print('---')


def swap(z, a, b):
	l = []
	for j in z:
		l.append(j)
	temp = l[a]
	l[a] = l[b]
	l[b] = temp
	zz = ''
	for tt in l:
		zz += tt
	return zz

def solve(s, k):
	for t in range(min(len(s), len(k))):
		if s[t] == k[t]:
			x = min(s[t:])
			if x < k[t]:
				for i in range(len(s) - 1, t - 1, -1):
					if s[i] == x:
						s = swap(s, t, i)
						break
			if s < k:
				return s
		if s[t] > k[t]:
			x = min(s[t:])
			if x <= k[t]:
				for i in range(len(s) - 1, t - 1, -1):
					if s[i] == x:
						s = swap(s, t, i)
						break
			if s < k:
				return s
			else:
				return '---'
	return '---'
for _ in range(int(input())):
	(s, k) = input().split()
	if s < k:
		print(s)
	else:
		print(solve(s, k))


import collections
T = int(input())
for _ in range(T):
	(s1, s2) = input().split()
	if len(set(s1)) > 1:
		arr = list(s1)
		cnt = collections.Counter(arr)
		i = 0
		while i < len(arr) and arr[i] == min(cnt.keys()):
			cnt[arr[i]] -= 1
			if cnt[arr[i]] == 0:
				del cnt[arr[i]]
			i += 1
		i2 = i
		if len(cnt.keys()) != 0:
			lo = min(cnt.keys())
			while i2 < len(arr):
				if arr[i2] == lo:
					i3 = i2
				i2 += 1
			(arr[i], arr[i3]) = (arr[i3], arr[i])
			ans = ''.join(arr)
		else:
			ans = s1
	else:
		ans = s1
	if ans < s2:
		print(ans)
	else:
		print('---')


import math
import cmath
from decimal import *
from fractions import *
q = int(input())
for _ in range(q):
	(a, b) = map(list, input().split())
	l = [0] * len(a)
	minI = len(a) - 1
	minC = a[minI]
	for i in range(len(a) - 1, -1, -1):
		if a[i] < minC:
			minC = a[i]
			minI = i
		l[i] = [minC, minI]
	for i in range(len(a)):
		if a[i] > l[i][0]:
			(a[i], a[l[i][1]]) = (a[l[i][1]], a[i])
			break
	if a < b:
		print(*a, sep='')
	else:
		print('---')


for nt in range(int(input())):
	(s, c) = input().split()
	if s < c:
		print(s)
		continue
	s = list(s)
	a = sorted(s)
	if a == s or c <= ''.join(map(str, a)):
		print('---')
		continue
	i = 0
	while s[i] == a[i]:
		i += 1
	for j in range(len(s) - 1, -1, -1):
		if a[i] == s[j]:
			ind = j
			break
	(s[i], s[ind]) = (s[ind], s[i])
	s = ''.join(map(str, s))
	if s >= c:
		print('---')
	else:
		print(s)


for _ in range(int(input())):
	(s, c) = input().split()
	X = [i for i in s]
	S = [i for i in s]
	S.sort()
	target = -1
	index = -1
	for i in range(len(S)):
		if s[i] != S[i]:
			target = S[i]
			index = i
			break
	if target == -1:
		pass
	else:
		for i in range(len(S) - 1, -1, -1):
			if s[i] == target:
				(X[index], X[i]) = (X[i], X[index])
				break
	x = ''.join(X)
	flag = 1
	for i in range(min(len(c), len(x))):
		if x[i] < c[i]:
			print(x)
			flag = 0
			break
		elif x[i] > c[i]:
			print('---')
			flag = 0
			break
	if flag:
		if c.find(x) == 0 and len(c) > len(x):
			print(x)
		else:
			print('---')


for _ in range(int(input())):
	(s, c) = input().split()
	if s < c:
		print(s)
	else:
		l = len(s)
		s = [i for i in s]
		for i in range(l - 1):
			p = min(s[i + 1:])
			if p < s[i]:
				u = s[::-1].index(p)
				u = l - 1 - u
				v = s[i]
				s[i] = s[u]
				s[u] = v
				break
		s = ''.join(s)
		if s < c:
			print(s)
		else:
			print('---')


t = int(input())
for __ in range(t):
	(a, b) = input().split()
	a = [c for c in a]
	c_idx = [-1] * 26
	for (i, c) in enumerate(a):
		c_ord = ord(c) - ord('A')
		c_idx[c_ord] = max(i, c_idx[c_ord])
	allowed = True
	for (i, c) in enumerate(a):
		c_ord = ord(c) - ord('A')
		if not allowed:
			break
		for j in range(26):
			if allowed and j < c_ord and (c_idx[j] > i):
				(a[c_idx[j]], a[i]) = (a[i], a[c_idx[j]])
				allowed = False
				break
	a = ''.join(a)
	if a < b:
		print(a)
	else:
		print('---')


def main():
	(string1, string2) = map(str, input().split())
	string1 = list(string1)
	length = len(string1)
	for i in range(length):
		index = i
		for j in range(length - 1, i, -1):
			if string1[j] < string1[index]:
				index = j
		if index != i:
			(string1[i], string1[index]) = (string1[index], string1[i])
			break
	if ''.join(string1) < string2:
		print(''.join(string1))
	else:
		print('---')
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7
t = int(input())
for y in range(t):
	(s, t) = input().split()
	s = list(s)
	n = len(s)
	m = len(t)
	key = 1
	pre = [0 for i in range(n)]
	pre[n - 1] = n - 1
	minn = n - 1
	for i in range(n - 2, -1, -1):
		if s[i] < s[minn]:
			minn = i
			pre[i] = i
		else:
			pre[i] = minn
	for i in range(n):
		j = pre[i]
		(s[j], s[i]) = (s[i], s[j])
		s1 = ''.join(s)
		if s1 < t:
			print(''.join(s))
			break
		(s[j], s[i]) = (s[i], s[j])
	else:
		s1 = ''.join(s)
		if s1 < t:
			print(''.join(s))
		else:
			print('---')


from collections import Counter
from sys import stdin
t = int(stdin.readline())
while t:
	(s, c) = input().split()
	s = list(s)
	b = sorted(s)
	if s != b:
		for i in range(len(b)):
			if s[i] != b[i]:
				temp = s[i]
				s[i] = b[i]
				x = s[i]
				break
		for i in range(len(s) - 1, -1, -1):
			if s[i] == x:
				s[i] = temp
				break
	s = ''.join(s)
	if s < c:
		print(s)
	else:
		print('---')
	t -= 1


def solve(a, b):
	n = len(a)
	for i in range(n):
		if a[i] != min(a[i:]):
			h = ''.join(a[i:])
			(a[i], a[i + h.rfind(min(h))]) = (a[i + h.rfind(min(h))], a[i])
			break
	return a
for _ in range(int(input())):
	(a, b) = list(input().split())
	p = solve(list(a), list(b))
	if p < list(b):
		print(*p, sep='')
	else:
		print('---')


import sys
reader = (line.rstrip() for line in sys.stdin)
t = int(next(reader))
for _ in range(t):
	(a, b) = next(reader).split()
	cs = [(c, i) for (i, c) in enumerate(a)]
	cs.sort()
	j = None
	for i in range(len(a)):
		if a[i] != cs[i][0]:
			c = cs[i][0]
			for j in range(len(a) - 1, i, -1):
				if a[j] == c:
					break
			break
	if j is None:
		newA = a
	else:
		newA = list(a)
		(newA[i], newA[j]) = (newA[j], newA[i])
		newA = ''.join(newA)
	if newA < b:
		print(newA)
	else:
		print('---')


def test(a, b):
	if a < b:
		print(a)
		return
	n = len(a)
	iiii = 0
	for i in range(n - 1):
		for j in range(i + 1, n):
			if a[i] <= a[j]:
				continue
			s = a[:i] + a[j] + a[i + 1:j] + a[i] + a[j + 1:]
			iiii = 1
			if s < b:
				print(s)
				return
		if iiii == 1:
			print('---')
			return
	print('---')
for i in range(int(input())):
	(a, b) = input().split()
	test(a, b)


from collections import defaultdict
t = int(input())
while t:
	dic = defaultdict(lambda : 0)
	t = t - 1
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	for i in range(len(s)):
		dic[s[i]] = max(dic[s[i]], i)
	s1 = s[:]
	s1.sort()
	ind = -1
	for i in range(len(s)):
		if s1[i] != s[i]:
			ind = i
			break
	if ind != -1:
		(s[ind], s[dic[s1[ind]]]) = (s[dic[s1[ind]]], s[ind])
	if s < c:
		a = ''
		for i in s:
			a += i
		print(a)
	else:
		print('---')


t = int(input())
for j in range(t):
	(s, c) = input().split()
	ar = [['a', -1]] * len(s)
	for i in range(len(s) - 2, -1, -1):
		if ar[i + 1][0] <= s[i + 1]:
			ar[i] = ar[i + 1]
		else:
			ar[i] = [s[i + 1], i + 1]
	fl = True
	for i in range(len(s) - 1):
		if fl and s[i] > ar[i][0]:
			fl = False
			if s[:i] + ar[i][0] + s[i + 1:ar[i][1]] + s[i] + s[ar[i][1] + 1:] < c:
				print(s[:i] + ar[i][0] + s[i + 1:ar[i][1]] + s[i] + s[ar[i][1] + 1:])
			else:
				print('---')
	if fl:
		if s < c:
			print(s)
		else:
			print('---')


for t in range(int(input())):
	(s, c) = input().split()
	s = list(s)
	lowest = sorted(s)
	pos = -1
	for i in range(len(s)):
		if s[i] != lowest[i]:
			pos = i
			break
	if pos == -1:
		string = ''.join(s)
		if string < c:
			print(string)
		else:
			print('---')
	else:
		for i in range(len(s) - 1, pos, -1):
			if s[i] == lowest[pos]:
				(s[pos], s[i]) = (s[i], s[pos])
				break
		string = ''.join(s)
		if string < c:
			print(string)
		else:
			print('---')


import sys
import bisect
from math import ceil, floor
RI = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
mod = 10 ** 9 + 7
n = int(ri())
for i in range(n):
	st = ri().split()
	temp = sorted(st[0])
	s = list(st[0])
	flag = True
	for i in range(len(s)):
		if temp[i] != s[i]:
			for j in range(len(s) - 1, i, -1):
				if s[j] == temp[i]:
					(s[i], s[j]) = (s[j], s[i])
					flag = not flag
					break
		if not flag:
			break
	st1 = ''.join(s)
	st2 = st[1]
	if st1 < st2:
		print(st1)
	else:
		print('---')


def swap(s: str) -> str:
	for i in range(len(s) - 1):
		j = i
		for k in range(len(s) - 1, i, -1):
			if s[k] < s[j]:
				j = k
		if s[i] > s[j]:
			return s[:i] + s[j] + s[i + 1:j] + s[i] + s[j + 1:]
	return s
t = int(input())
for _ in range(t):
	(s, c) = input().split()
	s = swap(s)
	if s < c:
		print(s)
	else:
		print('---')


n = int(input())
for q in range(n):
	(s, c) = input().split()
	s = list(s)
	c = list(c)
	a = sorted(s)
	t = [(i, a[i]) for i in range(len(a))]
	if s != a:
		for (i, j) in t:
			if s[i] != j:
				x = s[i]
				s[i] = j
				break
		for k in range(len(s) - 1, -1, -1):
			if s[k] == j:
				s[k] = x
				break
	if s < c:
		print(''.join(s))
	else:
		print('---')


n = int(input())
while n != 0:
	n -= 1
	(a, b) = map(list, input().split())
	la = len(a)
	lb = len(b)
	for i in range(la):
		p = i
		for j in range(la - 1, i, -1):
			if a[j] < a[p]:
				p = j
		if p != i:
			(a[i], a[p]) = (a[p], a[i])
			break
	if a < b:
		la = len(a)
		for i in range(la):
			print(a[i], end='')
		print()
	else:
		print('---')


class OrderedDict:

	class Node:

		def __init__(self, val, pos):
			self.val = val
			self.pos = pos
			self.prev = None
			self.next = None

	def __init__(self, l):
		lst = [(l[c], c) for c in range(len(l))]
		lst.sort(key=lambda tup: tup[1], reverse=True)
		lst.sort(key=lambda tup: tup[0])
		self.hashmap = {}
		self.start = self.Node('Start', None)
		self.end = self.Node('end', None)
		cur = self.start
		for tup in lst:
			char = tup[0]
			pos = tup[1]
			node = self.Node(char, pos)
			cur.next = node
			node.prev = cur
			cur = node
			if char not in self.hashmap:
				self.hashmap[char] = {}
			self.hashmap[char][pos] = node
		cur.next = self.end
		self.end.prev = cur

	def get_min(self):
		node = self.start.next
		if node == self.end:
			return (None, None)
		else:
			return (node.val, node.pos)

	def delete(self, char, pos):
		if char not in self.hashmap:
			return (None, None)
		elif pos not in self.hashmap[char]:
			return (None, None)
		else:
			node = self.hashmap[char][pos]
			self.hashmap[char].pop(pos)
			if len(self.hashmap[char]) == 0:
				self.hashmap.pop(char)
			prev_node = node.prev
			next_node = node.next
			prev_node.next = next_node
			next_node.prev = prev_node
			return (node.val, node.pos)

	def is_present(self, e):
		return e in self.hashmap

def swap(arr, i, j):
	temp = arr[i]
	arr[i] = arr[j]
	arr[j] = temp
T = int(input())
for t in range(T):
	[word1, word2] = input().rstrip().split()
	lst1 = list(word1)
	lst2 = list(word2)
	od = OrderedDict(lst1)
	num_swaps = 0
	Impossible = False
	i = 0
	while i < min(len(lst1), len(lst2)):
		if lst1[i] > lst2[i]:
			if num_swaps == 1:
				Impossible = True
				break
			else:
				(min_char, min_pos) = od.get_min()
				if min_char > lst2[i]:
					Impossible = True
					break
				else:
					swap(lst1, i, min_pos)
					num_swaps += 1
					od.delete(min_char, min_pos)
					if min_char < lst2[i]:
						break
		elif lst1[i] == lst2[i]:
			if num_swaps == 1:
				od.delete(lst1[i], i)
			else:
				(min_char, min_pos) = od.get_min()
				if min_char >= lst2[i]:
					od.delete(lst1[i], i)
				else:
					swap(lst1, i, min_pos)
					num_swaps += 1
					od.delete(min_char, min_pos)
					if min_char < lst2[i]:
						break
		else:
			break
		i += 1
	if i == min(len(lst1), len(lst2)) and lst1[i - 1] == lst2[i - 1]:
		Impossible = True if len(lst1) >= len(lst2) else False
	if Impossible == True:
		print('---')
	else:
		print(''.join(lst1))


exec(int(input()) * "s,c=input().split();t=sorted(s);i=0;l=len(s)\nwhile i<l and s[i]==t[i]:i+=1\nif i<l:j=s.rfind(t[i]);s=s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:]\nprint(('---',s)[s<c]);")


for _ in range(int(input())):
	(a, b) = map(str, input().rstrip().split())
	if a < b:
		print(a)
	else:
		a = list(a)
		mina = sorted(a)
		for i in range(len(mina)):
			if a[i] != mina[i]:
				t = a[i]
				ti = -1
				for j in range(len(a) - 1, i, -1):
					if a[j] == mina[i]:
						ti = j
						break
				a[i] = mina[i]
				a[ti] = t
				break
		a = ''.join(a)
		if a < b:
			print(''.join(a))
		else:
			print('---')


from string import ascii_uppercase
for _ in range(int(input())):
	(s, c) = input().split()
	if s < c:
		print(s)
		continue
	cnt = dict()
	for i in s:
		try:
			cnt[i] += 1
		except:
			cnt[i] = 1
	sorted_s = ''
	for char in ascii_uppercase:
		try:
			sorted_s += char * cnt[char]
		except:
			pass
	obtained_new_s = False
	for i in range(len(s)):
		if s[i] > sorted_s[i]:
			pos = -1
			for j in range(len(s)):
				if sorted_s[i] == s[j]:
					pos = j
			new_string = s[:i] + s[pos] + s[min(i + 1, len(s)):min(pos, len(s))] + s[i] + s[min(pos + 1, len(s)):]
			obtained_new_s = True
			break
	if not obtained_new_s:
		new_string = s
	if new_string < c:
		print(new_string)
	else:
		print('---')


from math import *
from copy import *
from string import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
from collections import defaultdict

def solve(s1, s2):
	if s1 < s2:
		return s1
	l1 = list(s1)
	sl1 = sorted(list(s1))
	f = 0
	for i in range(len(s1)):
		if s1[i] != sl1[i]:
			for j in range(len(s1) - 1, i, -1):
				if s1[j] == sl1[i]:
					(l1[j], l1[i]) = (l1[i], l1[j])
					f = 1
					break
		if f == 1:
			break
	news1 = ''.join(l1)
	if news1 < s2:
		return news1
	else:
		return '---'
T = int(input())
for _ in range(T):
	(s1, s2) = map(str, stdin.readline().rstrip().split())
	print(solve(s1, s2))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for _ in range(t):
	(s1, s2) = map(str, input().split())
	s1 = list(s1)
	s2 = list(s2)
	c = s1.copy()
	if s1 < s2:
		print(''.join(s1))
		continue
	c.sort()
	n = len(s1)
	for i in range(n):
		if s1[i] == c[i]:
			continue
		else:
			ca = -1
			for j in range(i + 1, n):
				if s1[j] == c[i]:
					ca = j
			(s1[i], s1[ca]) = (s1[ca], s1[i])
			break
	if s1 < s2:
		print(''.join(s1))
	else:
		print('---')


t = int(input())
while t:
	t -= 1
	(s, c) = input().split()
	s = list(s)
	n = len(s)
	c = list(c)
	s1 = s[:]
	s1.sort()
	if s < c:
		print(''.join(s))
		continue
	for i in range(0, n):
		if s[i] != s1[i]:
			char = s1[i]
			for j in range(n - 1, -1, -1):
				if s[j] == char:
					(s[i], s[j]) = (s[j], s[i])
					break
			break
	if s < c:
		print(''.join(s))
	else:
		print('---')


for i in ' ' * int(input()):
	(s, c) = input().split()
	if s < c:
		print(s)
		continue
	L = list(s)
	sL = sorted(L)
	if L == sL:
		if s < c:
			print(s)
		else:
			print('---')
	else:
		k = 0
		for i in range(len(s) - 1):
			if L[i] != sL[i]:
				k = i
				break
		m = 'Z'
		count = 0
		for j in range(i, len(s)):
			if L[j] <= m:
				m = L[j]
				count = j
		(L[count], L[i]) = (L[i], L[count])
		ss = ''
		for i in L:
			ss += i
		if ss < c:
			print(ss)
		else:
			print('---')


import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(map(str, input().rstrip().split())) for _ in range(Q)]
for (S, T) in Query:
	L = len(S)
	update = False
	A = list(S)
	for i in range(L - 1):
		tmp = S[i]
		for j in range(i + 1, L):
			if update and tmp == S[j]:
				ind = j
			if tmp > S[j]:
				tmp = S[j]
				update = True
				ind = j
		if update:
			A[ind] = S[i]
			A[i] = S[ind]
			break
	A_str = ''.join(A)
	if A_str < T:
		print(A_str)
	else:
		print('---')


def work(s, n):
	s_copy = s[:]
	s_copy.sort()
	for i in range(n):
		if s_copy[i] == s[i]:
			continue
		for j in range(n - 1, i, -1):
			if s[j] == s_copy[i]:
				(s[i], s[j]) = (s[j], s[i])
				return
for _ in range(int(input())):
	(s, c) = map(list, input().split(' '))
	work(s, len(s))
	broke = 0
	for i in range(min(len(s), len(c))):
		if s[i] > c[i]:
			print('---')
			broke = 1
			break
		if s[i] < c[i]:
			print(*s, sep='')
			broke = 2
			break
	if broke == 0:
		if len(c) <= len(s):
			print('---')
		else:
			print(*s, sep='')


from sys import stdin

def iin():
	return int(stdin.readline())

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

def main():
	t = iin()
	while t:
		t -= 1
		(s, c) = input().split()
		(s, c) = (list(s), list(c))
		(n1, n2) = (len(s), len(c))
		i = 0
		ch1 = 0
		while i < n1 and i < n2:
			if ch1:
				break
			if s[i] < c[i]:
				break
			elif s[i] == c[i]:
				for j in range(i + 1, n1):
					if s[j] == c[i]:
						pass
					elif s[j] < c[i]:
						(s[i], s[j]) = (s[j], s[i])
						ch1 = 1
						break
				if s[i] < c[i]:
					break
			else:
				ch = 0
				for j in range(i + 1, n1):
					if s[j] == c[i]:
						ch = j
					elif s[j] < c[i]:
						(s[i], s[j]) = (s[j], s[i])
						ch1 = 1
						break
				else:
					if ch:
						(s[i], s[ch]) = (s[ch], s[i])
						ch1 = 1
				if s[i] < c[i]:
					break
			i += 1
		if s < c:
			print(''.join(s))
		else:
			print('---')
main()


def swap(s):
	l = len(s)
	index = list(range(l))
	index.sort(key=lambda i: s[i])
	low = None
	for i in range(l):
		ind = index[i]
		if s[i] != s[ind]:
			left = i
			low = s[ind]
			break
	if low is None:
		return
	right = 0
	for i in range(l):
		if s[i] == low:
			right = i
	(s[left], s[right]) = (s[right], s[left])

def f():
	(s, c) = [list(str) for str in input().split()]
	if s < c:
		print(''.join(s))
		return
	swap(s)
	if s < c:
		print(''.join(s))
	else:
		print('---')
t = int(input())
for i in range(t):
	f()


import collections
for t in range(int(input())):
	(s, c) = input().split()
	s = list((ch for ch in s))
	sor = sorted(s)
	for i in range(len(s)):
		if s[i] != sor[i]:
			j = max((j for (j, v) in enumerate(s[i:], i) if v == sor[i]))
			s = s[:i] + [s[j]] + s[i + 1:j] + [s[i]] + s[j + 1:]
			break
	s = ''.join(s)
	print(s if s < c else '---')


import sys
import math
from collections import defaultdict, Counter, deque
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for i in range(t):
	(s, c) = input().split()
	s = list(s)
	l = len(c)
	cnt = 0
	for j in range(len(s)):
		if j == l:
			break
		if s[j] < c[j]:
			break
		elif s[j] == c[j]:
			for k in range(j + 1, len(s)):
				if s[k] < c[j]:
					(s[j], s[k]) = (s[k], s[j])
					cnt = 1
					break
			if cnt:
				break
		else:
			for k in range(j + 1, len(s)):
				if s[k] < c[j]:
					(s[j], s[k]) = (s[k], s[j])
					cnt = 1
					break
			if cnt:
				break
			for k in range(j + 1, len(s)):
				if s[k] == c[j]:
					(s[j], s[k]) = (s[k], s[j])
					s1 = ''.join(s)
					if s1 < c:
						break
					(s[j], s[k]) = (s[k], s[j])
			break
	s = ''.join(s)
	if s < c:
		print(s)
	else:
		print('---')


for _ in range(int(input())):
	(a, b) = input().split()
	good = 0
	if min(a, b) != b:
		print(a)
		continue
	for i in range(len(a)):
		idx = i
		for j in range(i + 1, len(a)):
			if a[j] <= a[idx]:
				idx = j
		if idx == i:
			continue
		tmp = a[:i] + a[idx] + a[i + 1:idx] + a[i] + a[idx + 1:]
		if min(tmp, b) != b:
			good = 1
			print(tmp)
			break
	if not good:
		print('---')


def change(a, b):
	a = list(a)
	b = list(b)
	if a < b:
		return ''.join(a)
	else:
		x = sorted(a)
		for i in range(len(a)):
			if a[i] != x[i]:
				for j in range(1, len(a) + 1):
					if a[-j] == x[i]:
						a[-j] = a[i]
						a[i] = x[i]
						break
				break
		if a < b:
			return ''.join(a)
		if a >= b:
			return '---'
n = int(input())
ans = []
for i in range(n):
	(a, b) = input().split()
	ans.append(change(a, b))
[print(j) for j in ans]


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

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

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
import collections

def CountFrequency(arr):
	return collections.Counter(arr)

def main():
	for i in range(ii()):
		(s, t) = mi()
		if s < t:
			print(s)
			continue
		s = list(s)
		t = list(t)
		x = sorted(s)
		if s == x:
			if s < t:
				print(s)
			else:
				print('---')
			continue
		for j in range(len(s)):
			if s[j] != x[j]:
				pos = j
				c = x[j]
				break
		for j in range(len(s) - 1, pos, -1):
			if s[j] == c:
				(s[pos], s[j]) = (s[j], s[pos])
				break
		if s < t:
			print(''.join(s))
		else:
			print('---')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()