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$$$.

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$$$.

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.

3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA

NoteIn 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".

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

}

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


For an array $$$[b_1, b_2, \ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \le i < j \le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$.You are given a permutation $$$[p_1, p_2, \ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$2\cdot 10^5$$$.

For each test case output a single integer  — the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays.

NoteIn the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays.In the second test case, we can split our permutation into subarrays $$$[4, 3], [2, 1]$$$, both of which are odd since their numbers of inversions are $$$1$$$.In the fourth test case, we can split our permutation into a single subarray $$$[2, 1]$$$, which is odd.In the fifth test case, we can split our permutation into subarrays $$$[4, 5], [6, 1, 2, 3]$$$. The first subarray has $$$0$$$ inversions, and the second has $$$3$$$, so it is odd.

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

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

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


Eikooc and Sushi play a game.The game is played on a tree having $$$n$$$ nodes numbered $$$1$$$ to $$$n$$$. Recall that a tree having $$$n$$$ nodes is an undirected, connected graph with $$$n-1$$$ edges.They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node $$$u$$$ such that   $$$u$$$ is adjacent to the node $$$v$$$ the token is currently on  $$$u$$$ has not been visited before  $$$u \oplus v \leq min(u, v)$$$ Here $$$x \oplus y$$$ denotes the bitwise XOR operation on integers $$$x$$$ and $$$y$$$.Both the players play optimally. The player who is unable to make a move loses.The following are examples which demonstrate the rules of the game.     Suppose Eikooc starts the game by placing the token at node $$$4$$$. Sushi then moves the token to node $$$6$$$, which is unvisited and adjacent to $$$4$$$. It also holds that $$$6 \oplus 4 = 2 \leq min(6, 4)$$$. In the next turn, Eikooc moves the token to node $$$5$$$, which is unvisited and adjacent to $$$6$$$. It holds that $$$5 \oplus 6 = 3 \leq min(5, 6)$$$. Sushi has no more moves to play, so she loses.   Suppose Eikooc starts the game by placing the token at node $$$3$$$. Sushi moves the token to node $$$2$$$, which is unvisited and adjacent to $$$3$$$. It also holds that $$$3 \oplus 2 = 1 \leq min(3, 2)$$$. Eikooc cannot move the token to node $$$6$$$ since $$$6 \oplus 2 = 4 \nleq min(6, 2)$$$. Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation $$$p$$$ of length $$$n$$$ (sequence of $$$n$$$ integers wherein each integer from $$$1$$$ to $$$n$$$ occurs exactly once) where $$$p_i$$$ denotes the new numbering of node $$$i$$$.She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so.

The first line contains a single integer $$$t~(1 \le t \le 10^5)$$$  — the number of test cases. The description of each test case is as follows. The first line of each test case contains an integer $$$n~(1 \le n \le 2 \cdot 10^5)$$$  — the number of nodes in the tree. The next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ $$$(1 \le u, v \le n; u \neq v)$$$  — denoting an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print any suitable relabeling  — a permutation of length $$$n$$$ which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them.

NoteIn the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win.In the second test case, $$$1 \oplus 2 = 3 \nleq min(1, 2)$$$. Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both $$$\{1, 2\}$$$ and $$$\{2, 1\}$$$ are optimal relabelings.

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
)

type node struct {
	dist  int
	value int
	edges []int
}

var nodes []node

func dfs(u int, d int) {
	nodes[u].dist = d
	for _, v := range nodes[u].edges {
		if nodes[v].dist == 0 {
			dfs(v, d+1)
		}
	}
}

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

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		fmt.Fscan(in, &n)
		nodes = make([]node, n+1)
		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			nodes[u].edges = append(nodes[u].edges, v)
			nodes[v].edges = append(nodes[v].edges, u)
		}
		dfs(1, 1)

		nodegrps := [2][]int{}
		for u := 1; u <= n; u++ {
			g := nodes[u].dist % 2
			nodegrps[g] = append(nodegrps[g], u)
		}

		k := n
		for k > 0 {
			if len(nodegrps[0]) < len(nodegrps[1]) {
				nodegrps[0], nodegrps[1] = nodegrps[1], nodegrps[0]
			}
			msb := 32 - bits.LeadingZeros32(uint32(k))
			for ; k >= 1<<(msb-1); k-- {
				nodes[nodegrps[0][0]].value = k
				nodegrps[0] = nodegrps[0][1:]
			}
		}

		for u := 1; u <= n; u++ {
			fmt.Fprintf(out, "%d ", nodes[u].value)
		}
		out.WriteByte('\n')
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
)

type node struct {
	dist  int
	value int
	edges []int
}

var nodes []node

func dfs(u int, d int) {
	nodes[u].dist = d
	for _, v := range nodes[u].edges {
		if nodes[v].dist == 0 {
			dfs(v, d+1)
		}
	}
}

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

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		fmt.Fscan(in, &n)
		nodes = make([]node, n+1)
		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(in, &u, &v)
			nodes[u].edges = append(nodes[u].edges, v)
			nodes[v].edges = append(nodes[v].edges, u)
		}
		dfs(1, 1)

		nodegrps := [2][]int{}
		for u := 1; u <= n; u++ {
			g := nodes[u].dist % 2
			nodegrps[g] = append(nodegrps[g], u)
		}
		if len(nodegrps[0]) >= len(nodegrps[1]) {
			nodegrps[0], nodegrps[1] = nodegrps[1], nodegrps[0]
		}

		msbgrps := [2][]int{}
		for i := 1; i <= n; i++ {
			g := bits.LeadingZeros(uint(i)) % 2
			msbgrps[g] = append(msbgrps[g], i)
		}
		if len(msbgrps[0]) > len(msbgrps[1]) {
			msbgrps[0], msbgrps[1] = msbgrps[1], msbgrps[0]
		}

		for _, u := range nodegrps[0] {
			g := 0
			if len(msbgrps[g]) == 0 {
				g = 1
			}
			nodes[u].value = msbgrps[g][0]
			msbgrps[g] = msbgrps[g][1:]
		}
		for _, u := range nodegrps[1] {
			g := 1
			if len(msbgrps[g]) == 0 {
				g ^= 1
			}
			nodes[u].value = msbgrps[g][0]
			msbgrps[g] = msbgrps[g][1:]
		}

		for u := 1; u <= n; u++ {
			fmt.Fprintf(out, "%d ", nodes[u].value)
		}
		out.WriteByte('\n')
	}
}