package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func min(a, b int) int {
if a < b {
return a
}
return b
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
var t int
fmt.Sscan(scanner.Text(), &t)
for test := 0; test < t; test++ {
scanner.Scan()
parts := strings.Fields(scanner.Text())
s := parts[0]
c := parts[1]
if s < c {
fmt.Println(s)
continue
}
n := len(s)
l := len(c)
pp := 0
mm := min(n, l)
for pp < mm && s[pp] == c[pp] {
pp++
}
found := false
var news string
for q := 0; q < pp; q++ {
target := s[q]
for kk := q + 1; kk < n; kk++ {
if s[kk] < target {
bs := []byte(s)
bs[q], bs[kk] = bs[kk], bs[q]
news = string(bs)
found = true
break
}
}
if found {
break
}
}
if !found && pp < mm {
target := c[pp]
for kk := pp + 1; kk < n; kk++ {
if s[kk] < target {
bs := []byte(s)
bs[pp], bs[kk] = bs[kk], bs[pp]
news = string(bs)
found = true
break
}
}
if !found {
for kk := pp + 1; kk < n; kk++ {
if s[kk] == target {
isSmaller := false
pos := 0
stopped := false
for pos < n && pos < l && !stopped {
var ch byte
if pos == pp {
ch = s[kk]
} else if pos == kk {
ch = s[pp]
} else {
ch = s[pos]
}
if ch != c[pos] {
if ch < c[pos] {
isSmaller = true
}
stopped = true
}
pos++
}
if !stopped {
if n < l {
isSmaller = true
}
}
if isSmaller {
bs := []byte(s)
bs[pp], bs[kk] = bs[kk], bs[pp]
news = string(bs)
found = true
break
}
}
}
}
}
if found {
fmt.Println(news)
} else {
fmt.Println("---")
}
}
}