/*
    HARSHAD VAGHASIYA
 
        @DAIICT
        
          H_M_Vaghasiya
 
            */
#include<bits/stdc++.h>
using namespace std;
#define FS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define MAX_INT 100000
#define ld long double
#define pb push_back
#define mp make_pair
#define pu push
#define vsz(v) cout<<v.size()<<endl; 
#define printv(v) for(ll i=0;i<v.size();i++)cout<<v[i]<<" "; cout<<endl;
#define printa(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" ";cout<<endl;
#define printrv(v) for(ll i=v.size()-1;i>=0;i--)cout<<v[i]<<" "; cout<<endl;
#define printvpair(v) for(ll i=0;i<v.size();i++)cout<<v[i].first<<" "<<v[i].second<<endl;
#define sz size()
#define ff first
#define ss second
#define vll vector<ll>
#define vbool vector<bool>
#define vpll vector<pair<ll,ll>>
#define vvpll vector<vector<pair<ll,ll>>>
#define qll queue<ll>
#define dqll deque<ll>
#define pll pair<ll,ll>
#define vllv vector<vector<ll>> 
#define vstr vector<string>
#define setpri(x) cout<<setprecision(x);
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define scana(a,n) for(ll i=0;i<n;i++)cin>>a[i];
#define inf 1000000000000
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define yesr {cout<<"YES"<<endl;return;}
#define nor {cout<<"NO"<<endl;return;}
ll MOD=1e9+7;
ll ceil1(ll n,ll x){if(n%x==0)return n/x; else return (n/x)+1;}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll lcm(ll a,ll b){return (max(a,b)/gcd(a,b))*min(a,b);}
ll fact(ll n){ll s=1; for(ll i=1;i<=n;i++)s*=i; return s;}
vll fact1tonmod(ll n,ll mod){vll  ans(n+1);ans[0]=ans[1]=1;
	for(ll i=2;i<=n;i++)ans[i]=(ans[i-1]*i)%mod;
	return ans;
}
ll ncrmod(ll n,ll r,ll mod){if(n==1 || r==0 || n==r)return 1;if(r>n/2)r=n-r;
	ll ans=1;for(ll i=1;i<=r;i++){ans=(((ans * n )/i)%mod);n--;}return ans;
}
ll ncr(ll n,ll r){if(n==1 || r==0)return 1;if(r>n/2)r=n-r;
	ll ans=1;for(ll i=1;i<=r;i++){ans=((ans * n )/i);n--;}return ans;
}
ll ncrsum(ll n,ll r, ll mod){
	if(n==1 || r==0)return 1;
	ll ans=1,sum=1;for(ll i=1;i<=r;i++){ans=(((ans * n )/i)%mod);n--;sum+=ans;sum%=mod;}return sum;
}
bool isprime(ll n){if(n==1 || (n!=2 && n%2==0))return false; if(n==2)return true; 
		for(ll i=3;i<=sqrt(n);i+=2)if(n%i==0)return false; return true; }
vll createprime(ll n){
	vector<bool> prime(n+1,true);vll ans;;prime[0]=prime[1]=false;
	for(ll i=2;i*i<=n;i++)if(prime[i])for(ll j=i*i;j<=n;j+=i)prime[j]=false;
	for(ll i=2;i<=n;i++)if(prime[i])ans.pb(i);
	return ans;
}
vbool createprimebool(ll n){
	vector<bool> prime(n+1,true);prime[0]=prime[1]=false;
	for(ll i=2;i*i<=n;i++)if(prime[i])for(ll j=i*i;j<=n;j+=i)prime[j]=false;
	return prime;
}
vll factor(ll n){
	vll ans;
	if(n>0)ans.pb(1);if(n==2)ans.pb(2);
	for(ll i=2;i*i<=n;i++){if(n%i==0){ans.pb(i);if(n/i!=i)ans.pb(n/i);}}if(n>2)ans.pb(n);
	return ans;
}
ll numoffactor(ll n){ return factor(n).sz;}
ll pow1(ll n,ll k,ll mm){
    if(k==0)return 1;
    if(k%2==0)return (pow1((n%mm)*(n%mm),k/2,mm))%mm;
    return ((n%mm)*(pow1((n%mm)*(n%mm),k/2,mm)%mm))%mm;
}
ll pow2(ll n,ll k){ll ans=1;
	while(k>0){
        if(k%2==1)ans=ans*n;n=n*n;k/=2;
    }return ans;
}
ll modinv(ll A,ll M){return pow1(A,M-2,M);}
bool func(pair<ll,ll> &a,pair<ll,ll> &b ){
	if(a.ff != b.ff)return a.ff < b.ff;
	return a.ss > b.ss;
}
void solve(){
    ll i,j,k,n,l,m,x,y,z,d;
    string s,t;
    cin>>s>>t;
    i=0;
    ll flf=0;
	for(i=0;i<s.sz && i<t.sz;i++){
		if(s[i]<t[i]){
			cout<<s<<endl;
			// cout<<"YY";
			return;
		}
		if(s[i]>t[i]){
			flf=1;
			break;
		}
	}
	if(flf==0 &&s.sz<t.sz){
		cout<<s<<endl;
		// cout<<"LL";
		return;
	}
    vll a(26);
    for(i=0;i<s.sz;i++){
    	a[s[i]-'A']++;
    }
    vll b(26);
    for(i=0;i<s.sz && i<t.sz;i++){
    	b[s[i]-'A']++;
    	char c=s[i];
    	ll fl=0;
    	for(j=0 ;j< c-'A' ;j++){
    		if(a[j]>b[j] && t[i]>'A'+j ){
    			s[i]='A'+j;
    			for(k=i+1;k<s.sz;k++){
    				if(s[k]=='A'+j){s[k]=c;
    				// cout<<"ii";
    				break;
    				}
    			}
    			fl=1;
    			break;
    		}
    	}
    	if(fl==1)break;
    }
	 flf=0;
	for(i=0;i<s.sz && i<t.sz;i++){
		if(s[i]<t[i]){
			cout<<s<<endl;
			// cout<<"YY";
			return;
		}
		if(s[i]>t[i]){
			flf=1;
			break;
		}
	}
	if(flf==0 &&s.sz<t.sz){
		cout<<s<<endl;
		return;
	}
	cout<<"---\n";
}	
int main(){
	FS;
	ll i,j,k,n,m,x,y,z,q;
	q=1;
	 cin>>q;
	while(q--){
		solve();
	}
	return 0;
}	