#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long mod = 1e9 + 7;
const long double eps = 1e-13;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 2e5;
void solve() {
  string s, c;
  cin >> s >> c;
  if (s < c) {
    cout << s << endl;
    return;
  }
  for (int i = (0); i < (min((int)s.size(), (int)c.size())); i++) {
    if (s[i] == c[i]) {
      for (int j = (i + 1); j < ((int)s.size()); j++) {
        if (s[j] < c[i]) {
          swap(s[i], s[j]);
          cout << s << endl;
          return;
        }
      }
    } else {
      for (int j = (i + 1); j < ((int)s.size()); j++) {
        if (s[j] < c[i]) {
          swap(s[i], s[j]);
          cout << s << endl;
          return;
        }
      }
      for (int j = (i + 1); j < ((int)s.size()); j++) {
        if (s[j] == c[i]) {
          swap(s[i], s[j]);
          if (s < c) {
            cout << s << endl;
            return;
          }
          swap(s[i], s[j]);
        }
      }
      cout << "---" << endl;
      return;
    }
  }
  cout << "---" << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
