#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll t1,t2,t3;

int main(){
    ll TESTS=1;
    cin>>TESTS;
    // ios;
    while(TESTS--){
        string s,c;
        string tot;
        // char toto[5002];
        // cin>>tot;
        cin>>s>>c;
        ll idx;
        // cout<<s<<endl;
        // getline(cin, tot);
        // cin.getline(toto,5002);
        // for(int i=0;i<strlen(toto);i++){
        //     tot.push_back(toto[i]);
        // }
        // cout<<tot<<endl;
        // return 0;
        // cout<<tot.size()<<endl;
        // return 0;
        // // cout<<tot<<endl;
        // for(int i=0;i<tot.length();i++){
        //     if(isspace(tot[i])){
        //         idx=i;
        //         break;
        //     }
        // }
        // s=tot.substr(0,idx);
        // // cout<<tot<<endl;
        // // cout<<s;
        // // return 0;
        // c=tot.substr(idx+1,tot.length()-idx-1);
        // cout<<s<<endl<<c<<endl;
        // cout<<s<<"tt"<<c<<"tt"<<endl;
        // cout<<s<<endl<<c;
        // cin>>s>>c;
        ll ls=s.length(),lc=c.length();
        if(ls<lc){
            if(s==c.substr(0,ls)){
                cout<<s<<endl;
                continue;
            }
        }
        // cout<<"a";
        bool eq=true,conf=false;
        int i=0,j=0;
        while(eq){
            // if(s[i]==c[j]){
            //     i++;j++;
            // }
            if(s[i]<c[j]){
                // i++;
                // cout<<"4";
                // cout<<s[i]<<" "<<c[j]<<endl;
                conf=true;
                eq=false;
            }
            else if(s[i]>=c[j]){
                // cout<<"YES"<<endl;
                for(int k=i+1;k<ls;k++){
                    // cout<<s[k];
                    if(c[j]>=s[k] and s[k]<s[i]){
                        // cout<<"YES"<<endl;
                        swap(s[i],s[k]);
                        eq=false;
                        conf=true;
                        break;
                    }
                }
                // if(eq){
                //     for(int k=i+1;k<ls;k++){
                //         if(c[j]==s[k]and s[k]<s[i]){
                //             swap(s[i],s[k]);
                //             eq=false;
                //             break;
                //         }
                //     }
                // }
            }
            if(eq){
                if(s[i]>c[j])
                eq=false;
            }
            i++;j++;
            if(i==ls or j==lc){
                eq=false;
            }
        }
        // cout<<conf;
        if(conf){
            cout<<s<<endl;
            continue;
        }
        if(ls<lc){
            if(s==c.substr(0,ls)){
                // cout<<"4";
                cout<<s<<endl;
                continue;
            }
        }
        for(int i=0;i<min(ls,lc);i++){
            if(s[i]<c[i]){
                conf=true;
                break;
            }
            else if(s[i]>c[i]){
                break;
            }
        }
        // cout<<"4";
        if(conf){
            cout<<s<<endl;
        }
        else{
            cout<<"---"<<endl;
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll t1,t2,t3;

int main(){
    ll TESTS=1;
    cin>>TESTS;
    ios;
    while(TESTS--){
        string s,c;
        string tot;
        cin>>tot;
        ll idx;
        for(int i=0;i<tot.length();i++){
            if(tot[i]==' '){
                idx=i;
                break;
            }
        }
        s=tot.substr(0,idx+1);
        // cout<<tot<<endl;
        // cout<<s;
        // return 0;
        c=tot.substr(idx+2,tot.length()-idx-2);
        // cout<<s<<endl<<c;
        // cin>>s>>c;
        ll ls=s.length(),lc=c.length();
        if(ls<lc){
            if(s==c.substr(0,ls)){
                cout<<s<<endl;
                continue;
            }
        }
        // cout<<"a";
        bool eq=true,conf=false;
        int i=0,j=0;
        while(eq){
            // if(s[i]==c[j]){
            //     i++;j++;
            // }
            if(s[i]<c[j]){
                // i++;
                conf=true;
                eq=false;
            }
            else if(s[i]>=c[j]){
                // cout<<"YES"<<endl;
                for(int k=i+1;k<ls;k++){
                    // cout<<s[k];
                    if(c[j]>s[k]){
                        // cout<<"YES"<<endl;
                        swap(s[i],s[k]);
                        eq=false;
                        conf=true;
                        break;
                    }
                }
                if(eq){
                    for(int k=i+1;k<ls;k++){
                        if(c[j]==s[k]and s[k]<s[i]){
                            swap(s[i],s[k]);
                            eq=false;
                            break;
                        }
                    }
                }
            }
            i++;j++;
            if(i==ls or j==lc){
                eq=false;
            }
        }
        if(conf){
            cout<<s<<endl;
            continue;
        }
        if(ls<lc){
            if(s==c.substr(0,ls)){
                cout<<s<<endl;
                continue;
            }
        }
        for(int i=0;i<min(ls,lc);i++){
            if(s[i]<c[i]){
                conf=true;
                break;
            }
            else if(s[i]>c[i]){
                break;
            }
        }
        if(conf){
            cout<<s<<endl;
        }
        else{
            cout<<"---"<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){
            if(s[i] > c[i]){
                int j = i + 1;
                int in = 0;
                char cc = 'z';
                while(j < m){
                    if(cc >= s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) break;
                else{
                    swap(s[i], s[in]);
                    break;
                }
            }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){
            if(s[i] > c[i]){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) break;
                else{
                    swap(s[i], s[in]);
                    break;
                }
            }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        bool possible = false, swapped = false;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] > c[i] && !swapped){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) {
                    possible = false;
                    break;
                }
                swapped = true;
                swap(s[i], s[in]);
                possible = true;
                if(s[i] != c[i])
                    break;
            }
           // else if(swapped && s[i] > c[i]) { possible = false; break; }
        }
        if(s >= c) possible = false;
        if(possible) cout << s << "\n";
        if(!possible) cout << "---\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){
            if(s[i] > c[i]){
                int j = i + 1;
                int in = 0;
                char cc = 'z';
                while(j < m){
                    if(cc >= s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(s[in] < s[i]){
                    swap(s[i], s[in]);
                    break;
                }
            }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        bool possible = false, swapped = false;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] > c[i] && !swapped){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) {
                    possible = false;
                    break;
                }
                swapped = true;
                swap(s[i], s[in]);
                possible = true;
                if(s[i] != c[i])
                    break;
            }
            else if(swapped && s[i] > c[i]) { possible = false; break; }
        }
        if(s => c) possible = false;
        if(possible) cout << s << "\n";
        if(!possible) cout << "---\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        bool possible = false, swapped = false;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] >= c[i] && !swapped){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) {
                    possible = false;
                    break;
                }
                swapped = true;
                swap(s[i], s[in]);
                possible = true;
                if(s[i] != c[i])
                    break;
            }
            else if(swapped && s[i] >= c[i]) { possible = false; break; }
        }
        if(s >= c) possible = false;
        if(possible) cout << s << "\n";
        if(!possible) cout << "---\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) break;
                else{
                    swap(s[i], s[in]);
                    break;
                }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        bool possible = false, swapped = false, flag = false;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] > c[i] && !swapped){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) {
                    possible = false;
                    break;
                }
                swapped = true;
                swap(s[i], s[in]);
                flag = true;
                possible = true;
                if(s[i] != c[i])
                    break;
            }
            else if(swapped && s[i] > c[i]) { possible = false; break; }
        }
        //if(m > n) cout << "---\n";
        if(possible) cout << s << "\n";
        if(!possible) cout << "---\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){
            //if(s[i] > c[i]){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) break;
                else{
                    swap(s[i], s[in]);
                    break;
                }
       //     }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){
            if(s[i] >= c[i]){
                int j = i + 1;
                int in = 0;
                char cc = 'z';
                while(j < m){
                    if(cc >= s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(s[in] < s[i]){
                    swap(s[i], s[in]);
                    break;
                }
            }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        bool possible = false, swapped = false, flag = false;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] > c[i] && !swapped){
                int j = i + 1;
                int in;
                char cc = 'Z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) {
                    possible = false;
                    break;
                }
                swapped = true;
                swap(s[i], s[in]);
                flag = true;
                possible = true;
                if(s[i] != c[i])
                    break;
            }
            else if(swapped && s[i] > c[i]) { possible = false; break; }
        }
        //if(m > n) cout << "---\n";
        if(possible) cout << s << "\n";
        if(!possible) cout << "---\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        bool possible = false, swapped = false;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] > c[i] && !swapped){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) {
                    possible = false;
                    break;
                }
                swapped = true;
                swap(s[i], s[in]);
                possible = true;
                if(s[i] != c[i])
                    break;
            }
            else if(swapped && s[i] > c[i]) { possible = false; break; }
        }
        if(s >= c) possible = false;
        if(possible) cout << s << "\n";
        if(!possible) cout << "---\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < min(m, n); i++){
            if(s[i] > c[i]){
                int j = i + 1;
                int in;
                char cc = 'z';
                while(j < m){
                    if(cc > s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(cc > c[i]) break;
                else{
                    swap(s[i], s[in]);
                    break;
                }
            }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string s, c;
        cin >> s >> c;
        if(s < c) { cout << s << "\n"; continue; }
        int m = s.length(), n = c.length();
        for(int i = 0; i < m; i++){

                int j = i + 1;
                int in = 0;
                char cc = 'z';
                while(j < m){
                    if(cc >= s[j]){
                        cc = s[j];
                        in = j;
                    }
                    j++;
                }
                if(s[in] < s[i]){
                    swap(s[i], s[in]);
                    break;
                }
        }
        if(s >= c) cout << "---\n";
        else cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int q;
	cin>>q;
	while(q--){
		string s1,s2;
		cin>>s1;
		cin>>s2;
		int k=0;
		for(int i=0;i<s1.length();i++){
			char c='Z'+6;
			for(int j=i+1;j<s1.length();j++){
				if(s1[j]<=c){
					c=s1[j];
					k=j;
				}
			}
			if(s1[i]>s1[k]){
				swap(s1[k],s1[i]);
				break;
			}
		}
		if(s1<s2) cout<<s1<<endl;
		else cout<<"---"<<endl;
	}
	return 0;
}


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    //cout<<"here"
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if(sz1>sz)
    {
        int flag=0;
        f(i,0,sz)
        {
            if(s[i]!=c[i])
            {
                flag=1;
                break;
            }
        }
        if(flag==0){ cout<<s<<nl;
        return;}
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                if(s[i]!=c[i])
                {cout << "---" << nl;
                return;}
                else
                {
                    i++;
                    continue;
                }

            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else cout<<s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                cout << "---" << nl;
                break;
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else cout<<s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            break;
        }
    }
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    //cout<<"here"
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if(sz1>sz)
    {
        int flag=0;
        f(i,0,sz)
        {
            if(s[i]!=c[i])
            {
                flag=1;
                break;
            }
        }
        if(flag==0){ cout<<s<<nl;
        return;}
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                cout << "---" << nl;
                return;
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else cout<<s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    //cout<<"here"
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if (sz1 > sz)
    {
        vi inds;
        int flag = 0;
        f(i, 0, sz)
        {
            if (s[i] != c[i])
            {
                flag += 1;
                inds.pb(i);
                break;
            }
        }
        if (flag == 0)
        {
            cout << s << nl;
            return;
        }
        if(flag==2)
        {
            if(s[inds[0]]==c[inds[1]] && s[inds[1]]==c[inds[0]])
            {
                swap(s[inds[0]],s[inds[1]]);
                cout<<s<<nl;
                return;
            }
        }
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                if (s[i] != c[i])
                {
                    cout << "---" << nl;
                    return;
                }
                else
                {
                    i++;
                    continue;
                }
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else
                        cout << s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }
    cout<<"---"<<nl;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s >> c;
    int sz1, sz2;
    sz1 = s.size();
    sz2 = c.size();
    int cnt[26] = {0};
    f(i, 0, sz1) cnt[s[i] - 'A']++;
    int i = 0;
    int nums = 0;
    while (i < sz1 && i < sz2)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[0] -= 1;
            i++;
        }
        else
        {
            if (nums == 1)
            {
                cout << "---" << nl;
                return;
            }
            cnt[s[i]-'A'] -= 1;
            int flag = -1;
            f(j, 0, c[i] - 'A' + 1)
            {
                if (cnt[j])
                {
                    flag = j;
                }
            }
            if (flag == -1)
            {
                if (s[i] == c[i])
                {
                    i++;
                    continue;
                }
                cout << "---" << nl;
                return;
            }
            char ch = 'A' + flag;
            f(j, i + 1, sz1)
            {
                if (s[j] == ch)
                {
                    swap(s[i], s[j]);
                    nums++;
                    break;
                }
            }

            if (ch != c[i])
            {cout << s;
                return;
            }
            i++;
        }
    }
    if(sz2>sz1) cout<<s;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s,c;
     cin>>s>>c;
    int sz=s.size();
    if(s<c)
    {
        cout<<s<<nl;
        return;
    }
    int flag=0;
    f(i,0,sz-1)
    f(j,i+1,sz)
    {
       swap(s[i],s[j]);
       if(s<c)
       {
           cout<<s<<nl;
           return;
       }
       swap(s[i],s[j]);
    }
    cout<<"---\n";
    return;

}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if(sz1>sz)
    {
        int flag=0;
        f(i,0,sz)
        {
            if(s[i]!=c[i])
            {
                flag=1;
                break;
            }
        }
        if(flag==0) cout<<s;
        return;
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                cout << "---" << nl;
                return;
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else cout<<s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }

}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s >> c;
    int sz1, sz2;
    sz1 = s.size();
    sz2 = c.size();
    int cnt[26] = {0};
    f(i, 0, sz1) cnt[s[i] - 'A']++;
    int i = 0;
    int nums = 0;
    while (i < sz1 && i < sz2)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[0] -= 1;
            i++;
        }
        else
        {
            if (nums == 1)
            {
                cout << "---" << nl;
                return;
            }
            cnt[s[i]] -= 1;
            int flag = -1;
            f(j, 0, c[i] - 'A' + 1)
            {
                if (cnt[j])
                {
                    flag = j;
                }
            }
            if (flag == -1)
            {
                if (s[i] == c[i])
                {
                    i++;
                    continue;
                }
                cout << "---" << nl;
                return;
            }
            char ch = 'A' + flag;
            f(j, i + 1, sz1)
            {
                if (s[j] == ch)
                {
                    swap(s[i], s[j]);
                    nums++;
                    break;
                }
            }

            if (ch != c[i])
            {cout << s;
                return;
            }
            i++;
        }
    }
    if(sz2>sz1) cout<<s;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                cout << "___" << nl;
                break;
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else cout<<s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            break;
        }
    }
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s >> c;
    int sz1, sz2;
    sz1 = s.size();
    sz2 = c.size();
    int cnt[26] = {0};
    f(i, 0, sz1) cnt[s[i] - 'A']++;
    int i = 0;
    int nums = 0;
    while (i < sz1 && i < sz2)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[0] -= 1;
            i++;
        }
        else
        {
            if (nums == 1)
            {
                cout << "---" << nl;
                return;
            }
            cnt[s[i]-'A'] -= 1;
            int flag = -1;
            f(j, 0, c[i] - 'A' + 1)
            {
                if (cnt[j])
                {
                    flag = j;
                    break;
                }
            }
            if (flag == -1)
            {
                if (s[i] == c[i])
                {
                    i++;
                    continue;
                }
                cout << "---" << nl;
                return;
            }
            char ch = 'A' + flag;
            f(j, i + 1, sz1)
            {
                if (s[j] == ch)
                {
                    swap(s[i], s[j]);
                    nums++;
                    break;
                }
            }

            if (ch != c[i])
            {cout << s<<nl;
                return;
            }
            i++;
        }
    }
    if(sz2>sz1) cout<<s;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s >> c;
    int sz1, sz2;
    sz1 = s.size();
    sz2 = c.size();
    int cnt[26] = {0};
    f(i, 0, sz1) cnt[s[i] - 'A']++;
    int i = 0;
    int nums = 0;
    while (i < sz1 && i < sz2)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[0] -= 1;
            i++;
        }
        else
        {
            if (nums == 1)
            {
               if(s[i]==c[i])
{i++;
continue;
}

                cout << "---" << nl;
                return;
            }
            cnt[s[i]-'A'] -= 1;
            int flag = -1;
            f(j, 0, c[i] - 'A' + 1)
            {
                if (cnt[j])
                {
                    flag = j;
                    break;
                }
            }
            if (flag == -1)
            {
                if (s[i] == c[i])
                {
                    i++;
                    continue;
                }
                cout << "---" << nl;
                return;
            }
            char ch = 'A' + flag;
            f(j, i + 1, sz1)
            {
                if (s[j] == ch)
                {
//cout<<s[i]<<sp<<s[j]<<nl;
                    swap(s[i], s[j]);
                    nums++;
                    break;
                }
            }

            if (ch != c[i])
            {cout << s<<nl;
                return;
            }
            i++;
        }
    }
    if(sz2>sz1) cout<<s;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'a'] += 1;
    }
    int i = 0;
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'a')
        {
            cnt[s[i] - 'a'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'a'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'a'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                cout << "___" << nl;
                break;
            }
            f(j, 0, i) cout << s[i];
            char c = 'a' + flag;
            cout << c;
            c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                }
                else
                    cout << s[j];
            }
            cout << nl;
        }
    }
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            break;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                cout << "___" << nl;
                break;
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else cout<<s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            break;
        }
    }
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    //cout<<"here"
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if (sz1 > sz)
    {
        int flag = 0;
        f(i, 0, sz)
        {
            if (s[i] != c[i])
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            cout << s << nl;
            return;
        }
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                if (s[i] != c[i])
                {
                    cout << "---" << nl;
                    return;
                }
                else
                {
                    i++;
                    continue;
                }
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else
                        cout << s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }
    cout<<"---"<<nl;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    //cout<<"here"
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if (sz1 > sz)
    {
        vi inds;
        int flag = 0;
        f(i, 0, sz)
        {
            if (s[i] != c[i])
            {
                flag += 1;
                inds.pb(i);
                //break;
            }
        }
        if (flag == 0)
        {
            cout << s << nl;
            return;
        }
        if(flag==2)
        {
            if(s[inds[0]]==c[inds[1]] && s[inds[1]]==c[inds[0]])
            {
                swap(s[inds[0]],s[inds[1]]);
                cout<<s;
                return;
            }
        }
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                if (s[i] != c[i])
                {
                    cout << "---" << nl;
                    return;
                }
                else
                {
                    i++;
                    continue;
                }
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else
                        cout << s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }
    cout<<"---"<<nl;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    //cout<<"here"
    string s, c;
    cin >> s;
    cin >> c;
    int cnt[26];
    int sz = s.size();
    int sz1 = c.size();
    f(i, 0, 26) cnt[i] = 0;
    f(i, 0, sz)
    {
        cnt[s[i] - 'A'] += 1;
    }
    int i = 0;
    if (sz1 > sz)
    {
        vi inds;
        int flag = 0;
        f(i, 0, sz)
        {
            if (s[i] != c[i])
            {
                flag += 1;
                inds.pb(i);
                //break;
            }
        }
        if (flag == 0)
        {
            cout << s << nl;
            return;
        }
        if(flag==2)
        {
            if(s[inds[0]]==c[inds[1]] && s[inds[1]]==c[inds[0]])
            {
                swap(s[inds[0]],s[inds[1]]);
                cout<<s<<nl;
                return;
            }
        }
    }
    while (i < sz && i < sz1)
    {
        if (s[i] < c[i])
        {
            cout << s << nl;
            return;
        }
        else if (s[i] == c[i] && s[i] == 'A')
        {
            cnt[s[i] - 'A'] -= 1;
            i++;
        }
        else
        {
            cnt[s[i] - 'A'] -= 1;
            int flag = -1;
            f(j, 0, 26)
            {
                if (cnt[j])
                {
                    if (j < (c[i] - 'A'))
                    {
                        flag = j;
                        break;
                    }
                }
            }
            if (flag == -1)
            {
                if (s[i] != c[i])
                {
                    cout << "---" << nl;
                    return;
                }
                else
                {
                    i++;
                    continue;
                }
            }
            f(j, 0, i) cout << s[j];
            char c = 'A' + flag;
            cout << c;
            //c = s[i];
            f(j, i + 1, sz)
            {
                if (flag != -1)
                {
                    if (s[j] == c)
                    {
                        cout << s[i];
                        flag = -1;
                    }
                    else
                        cout << s[j];
                }
                else
                    cout << s[j];
            }
            cout << nl;
            return;
        }
    }
    cout<<"---"<<nl;
}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};


//#define SET

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define MX 100005
#define MOD 1000000007
/*ll fac[MX], modInv[MX];

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}

ll nCrModPFermat(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    if (n < r)
        return 1;

    return ((((fac[n] * modInv[r]) % p) *
             modInv[n - r]) %
            p);
}

void pre()
{
    fac[0] = 1;
    for (ll i = 1; i < MX ; i++)
        fac[i] = (fac[i - 1] * i) % MOD;
    f(i, 0, MX )
        modInv[i] = modInverse(fac[i], MOD);
}*/
/*ll md = 1000000007;
ll fact[100005], mi[100005], expt[100005];
ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pw(n, md - 2);
}

inline ll add(ll a, ll b) { return (md + a + b) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * a * b) % md; }

ll ncr(int n, int r)
{
    if (!n)
        return 1;
    return mult(fact[n], mult(mi[r], mi[n - r]));
}
void pre()
{
    fact[0] = 1;
    f(i, 1, 100003) fact[i] = mult(i, fact[i - 1]);
    f(i, 0, 100003) mi[i] = modinv(fact[i]);
}

int myceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
};*/

void solvetestcase(int tcn)
{
    string s,c;
     cin>>s>>c;
    int sz=s.size();
    if(s<c)
    {
        cout<<s<<nl;
        return;
    }
    int flag=0;
    string t=s;
    sort(all(t));
    f(i,0,sz-1)
    { if(s[i]>t[i])
    f(j,i+1,sz)
    {
       swap(s[i],s[j]);
       if(s<c)
       {
           cout<<s<<nl;
           return;
       }
       swap(s[i],s[j]);
    }
    }
    cout<<"---\n";
    return;

}
int main()
{
    fio;
    int t = 1;
    //pre();
    // UNCOMMENT FOR TESTCASES
    cin >> t;

    f(T, 1, t + 1)
    {
        // UNCOMMENT FOR CODEJAM,HACKERCUP
        // cout << "Case #" << T << ": ";
        solvetestcase(T);
        //cout << nl;
    }
    //return 1;
};