#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
using namespace std;
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    string s, t;
    cin >> s >> t;
    string x = s;
    long long n = x.size();
    sort(x.begin(), x.end());
    char c = 'Z' + 1;
    long long pos = n;
    for (long long i = 0; i < s.size(); i++) {
      if (x[i] < s[i]) {
        c = x[i];
        pos = i;
        break;
      }
    }
    for (long long i = s.size() - 1; i > pos; i--) {
      if (c == s[i]) {
        swap(s[i], s[pos]);
        break;
      }
    }
    if (s < t)
      cout << s << '\n';
    else
      cout << "---\n";
  }
  return 0;
}
