#include <bits/stdc++.h>
using namespace std;
void run_case() {
  string s, c;
  cin >> s >> c;
  if (s < c) {
    cout << s << "\n";
    return;
  }
  vector<vector<int>> a(26, vector<int>(1));
  for (int i = 0; i < s.size(); ++i) {
    char ch = s[i];
    a[ch - 'A'][0]++;
    a[ch - 'A'].push_back(i);
  }
  bool flag = true;
  for (int i = 0; i < s.size() && flag; ++i) {
    char ch = s[i];
    for (int j = 0; j < ch - 'A' && flag; ++j) {
      if (a[j][0] > 0) {
        int change = a[j][a[j][0]];
        if (change <= i) continue;
        swap(s[i], s[a[j][a[j][0]]]);
        flag = false;
      }
    }
  }
  if (s < c)
    cout << s << "\n";
  else
    cout << "---\n";
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cout.flags(ios::fixed);
  cout.precision(2);
  int t;
  cin >> t;
  while (t--) run_case();
  cout.flush();
  return 0;
}
