#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 8, M = 1e2 + 9;
const long long int LIMIT = 1 << 31;
vector<long long int> pos[26];
vector<long long int> possibility[5009];
void update_pos(string s) {
  for (long long int i = 0; i <= 25; i++) {
    pos[i].clear();
  }
  for (long long int i = 0; i <= 5002; i++) {
    possibility[i].clear();
  }
  long long int l = s.length();
  for (long long int i = l - 1; i >= 0; --i) {
    long long int val = s[i] - 'A';
    pos[val].push_back(i);
    for (int j = 0; j < val; ++j) {
      if (pos[j].size() != 0) {
        possibility[i] = pos[j];
        break;
      }
    }
  }
}
bool check(string s1, string s2) {
  long long int i = 0;
  while (i < s1.length() && i < s2.length()) {
    if (s1[i] == s2[i]) {
      i++;
      continue;
    } else if (s1[i] < s2[i]) {
      return true;
    } else {
      return false;
    }
  }
  return s1.size() < s2.size();
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long int t;
  string s1, s2;
  cin >> t;
  while (t--) {
    cin >> s1 >> s2;
    update_pos(s1);
    long long int l = s1.length();
    bool done = false;
    for (long long int i = 0; i < l; ++i) {
      if (i < s2.length()) {
        if (s1[i] < s2[i]) {
          cout << s1 << endl;
          done = true;
          break;
        } else if (s1[i] == s2[i]) {
          if (!possibility[i].empty()) {
            long long int x = possibility[i][0];
            swap(s1[i], s1[x]);
            cout << s1 << endl;
            done = true;
            break;
          }
        } else {
          if (possibility[i].empty()) {
            cout << "---" << endl;
            done = true;
            break;
          }
          long long int val = possibility[i][0];
          if (s1[val] < s2[i]) {
            swap(s1[i], s1[val]);
            cout << s1 << endl;
            done = true;
            break;
          } else {
            for (auto x : possibility[i]) {
              string s = s1;
              swap(s[i], s[x]);
              if (check(s, s2)) {
                cout << s << endl;
                done = true;
                break;
              }
            }
            if (!done) {
              cout << "---" << endl;
              done = true;
              break;
            }
          }
        }
        if (done) {
          break;
        }
      } else {
        break;
      }
    }
    if (!done) {
      if (s1.size() < s2.size()) {
        cout << s1 << endl;
      } else {
        cout << "---" << endl;
      }
    }
  }
  return 0;
}
