#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s, t;
    cin >> s >> t;
    vector<pair<char, int>> mn(int(s.size()) + 1);
    mn[int(s.size())] = {'Z' + 1, 1};
    for (int i = int(s.size()) - 1; i >= 0; i--)
      mn[i] = min(mn[i + 1], {s[i], -i});
    for (int i = 0, ThxDem = int(s.size()); i < ThxDem; ++i)
      if (mn[i + 1].first < s[i]) {
        swap(s[i], s[-mn[i + 1].second]);
        break;
      }
    if (s < t)
      cout << s << "\n";
    else
      cout << "---\n";
  }
}
