#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
using namespace std;
long long power(long long x, unsigned long long y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return (power(x, y / 2) * power(x, y / 2)) % 1000000007;
  else
    return (x * power(x, y / 2) * power(x, y / 2)) % 1000000007;
}
bool comp(long long a, long long b) { return a > b; }
int32_t main() {
  iostream::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long testcases;
  cin >> testcases;
  for (long long x = 0; x < testcases; x++) {
    string a, b;
    cin >> a >> b;
    long long arr[26] = {0};
    for (long long i = 0; i < a.size(); i++) {
      arr[a[i] - 'A']++;
    }
    for (long long i = 0; i < a.size(); i++) {
      arr[a[i] - 'A']--;
      for (long long l = 0; l < 26; l++) {
        if ((l + 'A') < a[i]) {
          if (arr[l]) {
            for (long long x = a.size() - 1; x >= 0; x--) {
              if (a[x] == (char)(l + 'A')) {
                swap(a[x], a[i]);
                goto a;
              }
            }
          }
        } else
          break;
      }
    }
  a:;
    if (a < b)
      cout << a << '\n';
    else
      cout << "---" << '\n';
  }
  return 0;
}
