#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  for (long long _ = 0; _ < t; _++) {
    string s, c;
    cin >> s >> c;
    vector<pair<long long, long long>> a;
    for (long long i = 0; i < s.size(); i++) {
      a.push_back({s[i], i});
    }
    sort(a.begin(), a.end());
    for (long long i = 0; i < s.size(); i++) {
      if ((long long)s[i] != a[i].first) {
        long long x = a[i].first;
        long long j = i;
        while (a[j].first == x && j < s.size()) j++;
        j--;
        swap(s[i], s[a[j].second]);
        break;
      }
    }
    bool f = 0;
    for (long long i = 0; i < min(c.size(), s.size()); i++) {
      if (c[i] == s[i])
        continue;
      else
        f = 1;
    }
    if (f == 0) {
      if (s.size() < c.size()) {
        cout << s << '\n';
        continue;
      } else {
        cout << "---\n";
        continue;
      }
    }
    bool fl = 0;
    for (long long i = 0; i < min(s.size(), c.size()); i++) {
      if (c[i] > s[i]) {
        break;
      }
      if (c[i] < s[i]) {
        fl = 1;
        cout << "---\n";
        break;
      }
    }
    if (fl == 0) {
      cout << s << '\n';
    }
  }
  return 0;
}
