// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1281/B

package main

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

func compare(x string, y string) string {
	sortedxb := []byte(x)
	sort.Slice(sortedxb, func(i, j int) bool { return sortedxb[i] < sortedxb[j] })
	xb := []byte(x)
	diffIndex := 0
	for i := 0; i < len(xb); i++ {
		if xb[i] != sortedxb[i] {
			diffIndex = i
			break
		}
	}
	lastIndex := 0
	for i := len(xb) - 1; i >= 0; i-- {
		if xb[i] == sortedxb[diffIndex] {
			lastIndex = i
			break
		}
	}
	xb[diffIndex], xb[lastIndex] = xb[lastIndex], xb[diffIndex]
	s := string(xb)
	if s < y {
		return s
	}
	return "---"
}

func Solve(t int, s []string, c []string) []string {
	ans := make([]string, t)
	for i := 0; i < t; i++ {
		ans[i] = compare(s[i], c[i])
	}
	return ans
}

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

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

	for _, elem := range Solve(t, s, c) {
		fmt.Fprintf(writer, "%s\n", elem)
	}
}

package main

import (
	"fmt"
	"sort"
)

func _1281B() {

	var n int
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		var a, b string
		var solved bool

		fmt.Scan(&a, &b)

		solved = false
		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		sc := make([]byte, len(s))
		copy(sc, s)
		sort.Slice(sc, func(i, j int) bool { return sc[i] < sc[j] })

		if string(sc) > b {
			fmt.Println("---")
			continue
		}

		for i, chr := range s {
			if chr != sc[i] {
				for k := len(s) - 1; k >= 0; k-- {
					if sc[i] == s[k] {
						aux := chr
						s[i] = s[k]
						s[k] = aux
						if string(s) < b {
							solved = true
						}
						break
					}
				}
				break
			}
		}

		if solved == true {
			fmt.Println(string(s))
		} else {
			fmt.Println("---")
		}

	}
}

func main() {

	_1281B()

}

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var S, C []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		S = ReadRuneSlice()
		C = ReadRuneSlice()

		solve()
	}
}

const impossible = "---"

func solve() {
	n := len(S)
	lidx, ridx := -1, -1
	for i := 0; i < n; i++ {
		midx, minc := 5005, rune(100)
		// できるだけ後方から最小の文字を見つける
		// for j := i; j < n; j++ {
		// 	if minc > S[j] {
		// 		midx, minc = j, S[j]
		// 	}
		// }
		for j := n - 1; j >= i+1; j-- {
			if minc > S[j] {
				midx, minc = j, S[j]
			}
		}

		// PrintDebug("char: %c, idx: %d\n", minc, midx)
		if minc < S[i] {
			lidx, ridx = i, midx
			break
		}
	}

	if lidx == -1 && ridx == -1 {
		if compare(S, C) {
			fmt.Println(string(S))
		} else {
			fmt.Println(impossible)
		}
		return
	}

	S[lidx], S[ridx] = S[ridx], S[lidx]
	// PrintDebug("%s\n", string(S))

	if compare(S, C) {
		fmt.Println(string(S))
	} else {
		fmt.Println(impossible)
	}
}

// AのほうがBよりも小さかったらtrue
func compare(A, B []rune) bool {
	m := Min(len(A), len(B))
	for i := 0; i < m; i++ {
		if A[i] == B[i] {
			continue
		} else if A[i] < B[i] {
			return true
		} else {
			return false
		}
	}

	if len(A) == len(B) {
		return false
	} else if len(A) > len(B) {
		return false
	} else {
		return true
	}
}

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

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var S, C []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		S = ReadRuneSlice()
		C = ReadRuneSlice()

		solve()
	}
}

const impossible = "---"

func solve() {
	n := len(S)
	lidx, ridx := -1, -1
	for i := 0; i < n; i++ {
		midx, minc := 5005, rune(100)
		// できるだけ後方から最小の文字を見つける
		for j := n - 1; j >= i+1; j-- {
			if minc > S[j] {
				midx, minc = j, S[j]
			}
		}

		if minc < S[i] {
			lidx, ridx = i, midx
			break
		}
	}

	if lidx == -1 && ridx == -1 {
		if string(S) < string(C) {
			fmt.Println(string(S))
		} else {
			fmt.Println(impossible)
		}
		return
	}

	S[lidx], S[ridx] = S[ridx], S[lidx]

	if string(S) < string(C) {
		fmt.Println(string(S))
	} else {
		fmt.Println(impossible)
	}
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

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

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]

	if err := swapOneByte(rightSByte, c[matchIdx+1]); err != nil {

	}
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin <= c {
		return sMin
	}

	return "---"
}

func swapOneByte(arr []byte, x byte) error {
	for i := range arr {
		for j := i + 1; j < len(arr); j++ {
			if arr[j] < x {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
				return nil
			}
		}
	}
	return fmt.Errorf("ee")
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j >= 0; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) < c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]

	if err := swapOneByte(rightSByte, c[matchIdx+1]); err != nil {

	}
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin <= c {
		return sMin
	}

	return "---"
}

func swapOneByte(arr []byte, x byte) error {
	for i := range arr {
		for j := i + 1; j < len(arr); j++ {
			if arr[j] < x {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
				return nil
			}
		}
	}
	return fmt.Errorf("ee")
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) < c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var S, C []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		S = ReadRuneSlice()
		C = ReadRuneSlice()

		solve()
	}
}

const impossible = "---"

func solve() {
	n := len(S)
	lidx, ridx := -1, -1
	for i := 0; i < n; i++ {
		midx, minc := 5005, rune(100)
		// 最小の文字を見つける
		for j := i; j < n; j++ {
			if minc > S[j] {
				midx, minc = j, S[j]
			}
		}

		// PrintDebug("char: %c, idx: %d\n", minc, midx)
		if minc < S[i] {
			lidx, ridx = i, midx
			break
		}
	}

	if lidx == -1 && ridx == -1 {
		if string(S) < string(C) {
			fmt.Println(string(S))
		} else {
			fmt.Println(impossible)
		}
		return
	}

	S[lidx], S[ridx] = S[ridx], S[lidx]
	// PrintDebug("%s\n", string(S))

	if string(S) < string(C) {
		fmt.Println(string(S))
	} else {
		fmt.Println(impossible)
	}
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var S, C []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		S = ReadRuneSlice()
		C = ReadRuneSlice()

		solve()
	}
}

const impossible = "---"

func solve() {
	n := len(S)
	lidx, ridx := -1, -1
	for i := 0; i < n; i++ {
		midx, minc := 5005, rune(100)
		// 最小の文字を見つける
		for j := i; j < n; j++ {
			if minc > S[j] {
				midx, minc = j, S[j]
			}
		}

		// PrintDebug("char: %c, idx: %d\n", minc, midx)
		if minc < S[i] {
			lidx, ridx = i, midx
			break
		}
	}

	if lidx == -1 && ridx == -1 {
		// if string(S) < string(C) {
		// 	fmt.Println(string(S))
		// } else {
		// 	fmt.Println(impossible)
		// }
		if compare(S, C) {
			fmt.Println(string(S))
		} else {
			fmt.Println(impossible)
		}
		return
	}

	S[lidx], S[ridx] = S[ridx], S[lidx]
	// PrintDebug("%s\n", string(S))

	// if string(S) < string(C) {
	// 	fmt.Println(string(S))
	// } else {
	// 	fmt.Println(impossible)
	// }
	if compare(S, C) {
		fmt.Println(string(S))
	} else {
		fmt.Println(impossible)
	}

	// a := "AAA"
	// b := "AAAA"
	// if a < b {
	// 	fmt.Println(a)
	// } else {
	// 	fmt.Println(b)
	// }
}

// AのほうがBよりも小さかったらtrue
func compare(A, B []rune) bool {
	m := Min(len(A), len(B))
	for i := 0; i < m; i++ {
		if A[i] == B[i] {
			continue
		} else if A[i] < B[i] {
			return true
		} else {
			return false
		}
	}

	if len(A) == len(B) {
		return false
	} else if len(A) > len(B) {
		return false
	} else {
		return true
	}
}

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

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

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

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)
	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		} else {
			continue
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j >= 0; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	if matchIdx == len(s)-1 {
		return "---"
	}

	rightSByte := sByte[matchIdx+1:]

	if matchIdx == len(c)-1 {
		return "---"
	}

	if err := swapOneByte(rightSByte, c[matchIdx+1]); err != nil {
		return "---"
	}

	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin <= c {
		return sMin
	}

	return "---"
}

func swapOneByte(arr []byte, x byte) error {
	for i := range arr {
		for j := i + 1; j < len(arr); j++ {
			if arr[j] < x {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
				return nil
			}
		}
	}
	return fmt.Errorf("ee")
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) < c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s < c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]

	if matchIdx == len(c)-1 {
		return "---"
	}

	if err := swapOneByte(rightSByte, c[matchIdx+1]); err != nil {
		return "---"
	}

	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func swapOneByte(arr []byte, x byte) error {
	for i := range arr {
		for j := i + 1; j < len(arr); j++ {
			if arr[j] < x {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
				return nil
			}
		}
	}
	return fmt.Errorf("ee")
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) < c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	if matchIdx == len(s)-1 {
		return "---"
	}

	rightSByte := sByte[matchIdx+1:]

	if matchIdx == len(c)-1 {
		return "---"
	}

	if err := swapOneByte(rightSByte, c[matchIdx+1]); err != nil {
		return "---"
	}

	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin <= c {
		return sMin
	}

	return "---"
}

func swapOneByte(arr []byte, x byte) error {
	for i := range arr {
		for j := i + 1; j < len(arr); j++ {
			if arr[j] <= x {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
				return nil
			}
		}
	}
	return fmt.Errorf("ee")
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) < c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {

	if s == c {
		return s
	}

	if len(s) < len(c) && s == c[:len(s)] {
		return s
	}

	for i := range s {
		if i >= len(c) || s[i] < c[i] {
			return s
		} else if s[i] == c[i] {
			continue
		} else if s[i] > c[i] {
			for j := i + 1; j < len(s); j++ {
				if j >= len(c) {
					return s
				}
				if s[j] < c[j] {
					sOrg := s
					s = sOrg[0:i] + string(sOrg[j]) + sOrg[i+1:j] + string(sOrg[i]) + sOrg[j+1:]
					continue
				}
			}
		}
	}

	return "---"
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)
	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		} else {
			continue
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {

	if s == c {
		return s
	}

	if len(s) < len(c) && s == c[:len(s)] {
		return s
	}

	for i := range s {
		if i >= len(c) || s[i] < c[i] {
			return s
		} else if s[i] == c[i] {
			continue
		} else if s[i] > c[i] {
			for j := i + 1; j < len(s); j++ {
				if j >= len(c) {
					return s
				}
				if s[j] < c[j] {
					sOrg := s
					s = sOrg[0:i] + string(sOrg[j]) + sOrg[i+1:j] + string(sOrg[i]) + sOrg[j+1:]
					continue
				}
			}
		}
	}

	return "---"
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)
	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	for i := range sArr {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
				break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin <= c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j >= 0; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {

	for i := range s {
		if i >= len(c) || s[i] < c[i] {
			return s
		} else if s[i] == c[i] {
			continue
		} else if s[i] > c[i] {
			for j := i + 1; j < len(s); j++ {
				if j >= len(c) {
					return s
				}
				if s[j] < c[j] {
					sOrg := s
					s = sOrg[0:i] + string(sOrg[j]) + sOrg[i+1:j] + string(sOrg[i]) + sOrg[j+1:]
					continue
				}
			}
		}
	}
	return "---"
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	if s < c {
		return s
	} else {
		//fmt.Println("sortByte(sByte) ", string(sByte))
		//isfound := false

		//for i := range sArr {
		for i := 0; i < len(sArr); i++ {
			if sArr[i] != sByte[i] {
				for j := len(sArr) - 1; j >= 0; j-- {
					if sArr[j] == sByte[i] {
						tmp := sArr[i]
						sArr[i] = sArr[j]
						sArr[j] = tmp
						//fmt.Println("sortArr: ", string(sArr))
						//isfound = true
						break
					}
				}
				//if isfound {
				break
				//}
			} else {
				continue
			}
		}

		//fmt.Println("Last: ", string(sArr))
		if string(sArr) <= c {
			return string(sArr)
		} else {
			return "---"
		}
	}
	return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	if s < c {
		return s
	} else {
		//fmt.Println("sortByte(sByte) ", string(sByte))
		//isfound := false

		//for i := range sArr {
		for i := 0; i < len(sArr); i++ {
			if sArr[i] != sByte[i] {
				for j := len(sArr) - 1; j > i; j-- {
					if sArr[j] == sByte[i] {
						tmp := sArr[i]
						sArr[i] = sArr[j]
						sArr[j] = tmp
						//fmt.Println("sortArr: ", string(sArr))
						//isfound = true
						break
					}
				}
				//if isfound {
				break
				//}
			} else {
				continue
			}
		}

		//fmt.Println("Last: ", string(sArr))
		if string(sArr) <= c {
			return string(sArr)
		} else {
			return "---"
		}
	}
	return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {

	if s == c {
		return s
	}

	if len(s) < len(c) && s == c[:len(s)] {
		return s
	}

	for i := range s {
		if i >= len(c) || s[i] < c[i] {
			return s
		} else if s[i] == c[i] {
			continue
		} else if s[i] > c[i] {
			for j := i + 1; j < len(s); j++ {
				if j >= len(c) {
					return s
				}
				if s[j] < c[j] {
					sOrg := s
					s = sOrg[0:i] + string(sOrg[j]) + sOrg[i+1:j] + string(sOrg[i]) + sOrg[j+1:]
					continue
				}
			}
		}
	}

	return "---"
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)
	//fmt.Println("sortByte(sByte) ", string(sByte))
	isfound := false

	for i := range sArr {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					isfound = true
					break
				}
			}
			if isfound {
				break
			}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {

	if s == c {
		return s
	}

	if len(s) < len(c) && s == c[:len(s)] {
		return s
	}

	for i := range s {
		if i >= len(c) || s[i] < c[i] {
			return s
		} else if s[i] == c[i] {
			continue
		} else if s[i] > c[i] {
			for j := i + 1; j < len(s); j++ {
				if j >= len(c) {
					return s
				}
				if s[j] < c[j] {
					sOrg := s
					s = sOrg[0:i] + string(sOrg[j]) + sOrg[i+1:j] + string(sOrg[i]) + sOrg[j+1:]
					continue
				}
			}
		}
	}

	return "---"
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)
	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s < c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	if matchIdx == len(s)-1 {
		return "---"
	}
	rightSByte := sByte[matchIdx+1:]

	if matchIdx == len(c)-1 {
		return "---"
	}

	if err := swapOneByte(rightSByte, c[matchIdx+1]); err != nil {
		return "---"
	}

	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin <= c {
		return sMin
	}

	return "---"
}

func swapOneByte(arr []byte, x byte) error {
	for i := range arr {
		for j := i + 1; j < len(arr); j++ {
			if arr[j] < x {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
				return nil
			}
		}
	}
	return fmt.Errorf("ee")
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		} else {
			break
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) < c {
		return string(sArr)
	} else {
		return "---"
	}
	//return ""
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	if s < c {
		return s
	}
	//fmt.Println("sortByte(sByte) ", string(sByte))
	//isfound := false

	//for i := range sArr {
	for i := 0; i < len(sArr); i++ {
		if sArr[i] != sByte[i] {
			for j := len(sArr) - 1; j > i; j-- {
				if sArr[j] == sByte[i] {
					tmp := sArr[i]
					sArr[i] = sArr[j]
					sArr[j] = tmp
					//fmt.Println("sortArr: ", string(sArr))
					//isfound = true
					break
				}
			}
			//if isfound {
			break
			//}
		} else {
			continue
		}
	}

	//fmt.Println("Last: ", string(sArr))
	if string(sArr) <= c {
		return string(sArr)
	} else {
		return "---"
	}
}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, searchRankOptimisation(s, c))
	}
}

func searchRankOptimisation(s, c string) string {

	if s == c {
		return s
	}

	for i := range s {
		if i >= len(c) || s[i] < c[i] {
			return s
		} else if s[i] == c[i] {
			continue
		} else if s[i] > c[i] {
			for j := i + 1; j < len(s); j++ {
				if j >= len(c) {
					return s
				}
				if s[j] < c[j] {
					sOrg := s
					s = sOrg[0:i] + string(sOrg[j]) + sOrg[i+1:j] + string(sOrg[i]) + sOrg[j+1:]
					continue
				}
			}
		}
	}

	if len(s) < len(c) && s == c[:len(s)] {
		return s
	} else {
		return "---"
	}
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		s, c := "", ""
		fmt.Fscanf(in, "%s %s\n", &s, &c)

		// output
		fmt.Fprintln(out, aws(s, c))
	}
}

func searchRankOptimisation(s, c string) string {
	sByte := []byte(s)
	cByte := []byte(c)

	if s <= c {
		return s
	}

	matchIdx := getMaxMatchingIdx(sByte, cByte)
	rightSByte := sByte[matchIdx+1:]
	sortByte(rightSByte)
	sMin := string(s[0:matchIdx+1]) + string(rightSByte)
	if sMin < c {
		return sMin
	}

	return "---"
}

func getMaxMatchingIdx(s, c []byte) int {

	minLen := len(s)
	if len(c) < minLen {
		minLen = len(c)
	}
	idx := -1
	for i := 0; i < minLen; i++ {
		if s[i] == c[i] {
			idx++
		}
	}
	return idx
}

func aws(s, c string) string {
	sArr := []byte(s)
	sByte := []byte(s)
	sortByte(sByte)

	if s < c {
		return s
	} else {
		//fmt.Println("sortByte(sByte) ", string(sByte))
		//isfound := false

		//for i := range sArr {
		for i := 0; i < len(sArr); i++ {
			if sArr[i] != sByte[i] {
				for j := len(sArr) - 1; j > i; j-- {
					if sArr[j] == sByte[i] {
						tmp := sArr[i]
						sArr[i] = sArr[j]
						sArr[j] = tmp
						//fmt.Println("sortArr: ", string(sArr))
						//isfound = true
						break
					}
				}
				//if isfound {
				break
				//}
			} else {
				continue
			}
		}

		//fmt.Println("Last: ", string(sArr))
		if string(sArr) <= c {
			return string(sArr)
		} else {
			return "---"
		}
	}

}

func sortByte(arr []byte) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

package main

import (
	"fmt"
	"sort"
)

func _1281B() {

	var n int
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		var a, b string
		var solved bool

		fmt.Scan(&a, &b)
		solved = false
		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		sc := make([]byte, len(s))
		copy(sc, s)

		sort.Slice(sc, func(i, j int) bool { return sc[i] < sc[j] })

		if string(sc) > b {
			fmt.Println("---")
			continue
		}

		for i, chr := range s {
			if chr != sc[i] {
				for k := len(s) - 1; k >= 0; k-- {
					if sc[i] == s[k] {
						aux := chr
						s[i] = s[k]
						s[k] = aux
						if string(s) < b {
							solved = true
						}
						break
					}
				}
			}
			break
		}

		if solved == true {
			fmt.Println(string(s))
		} else {
			fmt.Println("---")
		}

	}
}

func main() {

	_1281B()

}

package main

import (
	"fmt"
)

func _1281B() {

	var n int
	var a, b string
	var solved bool
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		solved = false
		fmt.Scan(&a, &b)

		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		c := []byte(b)
		clen := len(c)

		for i, chr := range s {
			if i < clen && chr > c[i] && !solved {
				for j, swap := range s[i+1:] {
					if swap <= c[i] {
						aux_s := s
						aux_s[i] = swap
						aux_s[i+j+1] = chr
						if string(aux_s) < b {
							solved = true
							break
						}
					}
				}
			}
		}

		if solved {
			fmt.Println(string(s))
		} else {
			fmt.Println("---")
		}

	}

}

func main() {

	_1281B()
}

package main

import (
	"fmt"
)

func _1281B() {

	var n int
	var a, b string
	var solved bool
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		solved = false
		fmt.Scan(&a, &b)

		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		c := []byte(b)
		slen := len(s)
		clen := len(c)

		for i, chr := range s {
			if i < clen && chr > c[i] && !solved {
				for j, swap := range s[i+1:] {
					if swap <= c[i] {
						aux_s := make([]byte, slen)
						copy(aux_s, s)
						aux_s[i] = swap
						aux_s[i+j+1] = chr
						if string(aux_s) < b {
							fmt.Println(string(aux_s))
							solved = true
							break
						}
					}
				}
			}
			if solved {
				break
			}
		}

		if !solved {
			fmt.Println("---")
		}

	}

}

func main() {

	_1281B()
}

package main

import (
	"fmt"
)

func _1281B() {

	var n int
	var a, b string
	var solved bool
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		solved = false
		fmt.Scan(&a, &b)

		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		c := []byte(b)
		clen := len(c)

		for i, chr := range s {

			if i < clen && chr >= c[i] && !solved {
				for j, swap := range s[i+1:] {
					if swap <= c[i] {
						aux_s := s
						aux_s[i] = swap
						aux_s[i+j+1] = chr
						if string(aux_s) < b {
							fmt.Println(string(aux_s))
							solved = true
							break
						}
					}
				}
			}
		}

		if !solved {
			fmt.Println("---")
		}

	}

}

func main() {

	_1281B()
}

package main

import (
	"fmt"
)

func _1281B() {

	var n int
	var a, b string
	var solved bool
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		solved = false
		fmt.Scan(&a, &b)

		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		c := []byte(b)
		clen := len(c)

		for i, chr := range s {

			if i < clen && chr >= c[i] && !solved {
				for j, swap := range s[i+1:] {
					if swap <= c[i] {
						aux_s := s
						aux_s[i] = swap
						aux_s[i+j+1] = chr
						if string(aux_s) < b {
							solved = true
							break
						}
					}
				}
			}
		}

		if solved {
			fmt.Println(string(s))
		} else {
			fmt.Println("---")
		}

	}

}

func main() {

	_1281B()
}

package main

import (
	"fmt"
)

func _1281B() {

	var n int
	var a, b string
	var solved bool
	fmt.Scan(&n)

	for i := 0; i < n; i++ {

		fmt.Scan(&a, &b)

		if a < b {
			fmt.Println(a)
			continue
		}

		s := []byte(a)
		c := []byte(b)
		clen := len(c)

		solved = false

		for i, chr := range s {
			if i < clen && chr > c[i] && !solved {
				for j, swap := range s[i+1:] {
					if swap < c[i] {
						s[i] = swap
						s[j+i+1] = chr
						solved = true
						break
					}
				}
			}
		}

		if solved {
			fmt.Println(string(s))
		} else {
			fmt.Println("---")
		}

	}

}

func main() {

	_1281B()
}


package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for iter := 0; iter < ntests; iter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slice := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		res := 0
		last := 0
		for i := 0; i < n; i++ {
			if last > slice[i] {
				res++
				last = 0
			} else {
				last = slice[i]
			}
		}
		fmt.Fprintln(writer, res)
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1686B
Date: 9/1/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1686/B
Problem: CF1686B
**/
func (in *CF1686B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}

		min := 0
		countMin := -1
		count := 0
		for i := n - 1; i >= 0; i-- {
			if countMin == -1 {
				countMin = 1
				min = arr[i]
				continue
			} else if arr[i] < min {
				min = arr[i]
				countMin++
			} else {
				if countMin%2 != 0 || countMin > 1 {
					count++
				}
				countMin = -1

			}
		}
		fmt.Println(count)
	}
}

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

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); P := gis(N); for i:=0;i<N;i++ { P[i]-- }
		if N == 1 { fmt.Fprintln(wrtr,0); continue }
		dp := ia(N); dp[0] = 0; if P[0] > P[1] { dp[1] = 1 } else { dp[1] = 0 }
		for i:=2;i<N;i++ {
			if P[i-1] > P[i] {
				dp[i] = max(dp[i-2]+1,dp[i-1])
			} else {
				dp[i] = dp[i-1]
			}
		}
		ans := maxarr(dp)
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func solve(n int, a []int) int {
	var dp = make([]int, n)
	for i := 0; i < n; i++ {
		var p, q = 0, 0
		if i > 0 {
			p = dp[i-1]
			if a[i] < a[i-1] {
				q++
			}
		}
		if i > 1 {
			q += dp[i-2]

		}
		dp[i] = max(p, q)
	}
	return dp[n-1]
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		var a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

var bufferedStdin = bufio.NewReader(os.Stdin)
var bufferedStdout = bufio.NewWriter(os.Stdout)

func solve() {
	var n int
	_, _ = fmt.Fscan(bufferedStdin, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscan(bufferedStdin, &a[i])
	}
	ans := 0
	for i := 1; i < n; i++ {
		if a[i-1] > a[i] {
			ans++
			i++
		}
	}
	_, _ = fmt.Fprintf(bufferedStdout, "%d\n", ans)
}

func main() {
	var t int
	_, _ = fmt.Fscan(bufferedStdin, &t)
	for i := 0; i < t; i++ {
		solve()
	}
	_ = bufferedStdout.Flush()
}

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

	var t int
	fmt.Fscan(in,&t)
	for t>0 {
		var n int
		fmt.Fscan(in,&n)
		a := make([]int, n)
		for i:=0; i<n; i++{
			fmt.Fscan(in,&a[i])
		}
		var c int
		for i:=1; i<n; i++{
			if a[i]<a[i-1] {
				c++;
				i++;
			}
		}
		fmt.Fprintln(out,c)
	t--;
	}

}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for c := 0; c < testCases; c++ {
		var n int
		Fscan(in, &n)

		nums := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &nums[i])
		}

		Fprintln(out, solveB(nums))
	}

}

func solveB(nums []int) int {
	ans := 0

	max := -1
	for _, n := range nums {
		if max != -1 && max > n {
			ans++
			max = -1
			continue
		}

		if max < n {
			max = n
		}

	}

	return ans
}

package main

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

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

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

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

		var tot int
		for i := 1; i < n; i++ {
			if arr[i] < arr[i-1] {
				tot++
				i++
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1686B
Date: 9/1/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1686/B
Problem: CF1686B
**/
func (in *CF1686B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}

		min := 0
		countMin := -1
		count := 0
		for i := n - 1; i >= 0; i-- {
			if countMin == -1 {
				countMin = 1
				min = arr[i]
			} else if arr[i] < min {
				min = arr[i]
				countMin++
			} else {
				if countMin%2 != 0 {
					count++
				}
				countMin = -1

			}
		}
		fmt.Println(count)
	}
}

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

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