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

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
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 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('---')


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


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('---')


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


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


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())
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('---')


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


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('---')


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


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)


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('---')


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


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('---')


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('---')


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 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('---')


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)


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('---')


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('---')


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('---')


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('---')


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


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


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('---')


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 _ 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('---')


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)


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('---')


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('---')


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('---')


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 _ 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('---')


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 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('---')


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('---')


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


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='')


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