#include <bits/stdc++.h>
using namespace std;
void solve() {
  string a, b;
  cin >> a >> b;
  long long int la = a.size();
  long long int lb = b.size();
  if (a < b) {
    cout << a << endl;
    return;
  } else {
    string s = a;
    sort(s.begin(), s.end());
    long long int rec = -1;
    bool fnd = 0;
    for (long long int i = 0; i < la; i++) {
      if (a[i] != s[i]) {
        rec = i;
        break;
      }
    }
    if (rec == -1) {
      cout << "---" << endl;
      return;
    }
    for (long long int i = rec + 1; i < la; i++) {
      swap(a[i], a[rec]);
      if (a < b) {
        fnd = 1;
        break;
      }
      swap(a[i], a[rec]);
    }
    if (fnd == 1) {
      cout << a << endl;
    } else {
      cout << "---" << endl;
    }
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
