#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int Q;
  cin >> Q;
  while (Q--) {
    int n, i;
    string second, t;
    cin >> second >> t;
    map<char, int> cnt;
    for (auto& x : second) cnt[x]++;
    for (i = 0; i < (int)(second).size(); i++) {
      if (cnt.begin()->first != second[i]) {
        auto j = second.rfind(cnt.begin()->first);
        assert(j != string::npos);
        second[j] = second[i];
        second[i] = cnt.begin()->first;
        break;
      } else {
        cnt.begin()->second--;
        if (!cnt.begin()->second) cnt.erase(cnt.begin());
      }
    }
    if (second < t)
      cout << second << "\n";
    else
      cout << "---"
           << "\n";
  }
  return 0;
}
