#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
  string s, x;
  cin >> t;
  while (t--) {
    bool f1 = false, e = false, ff = false;
    cin >> s;
    cin >> x;
    int len = s.size();
    for (int i = 0; i < len; i++) {
      if (s[i] == x[i]) {
        for (int j = len - 1; j > i; j--) {
          if (s[j] < x[i]) {
            swap(s[j], s[i]);
            f1 = true;
            cout << s << endl;
            e = true;
            break;
          }
        }
        if (f1) break;
      } else if (s[i] < x[i]) {
        cout << s << endl;
        e = true;
        f1 = true;
        break;
      } else {
        for (int j = len - 1; j > i; j--) {
          if (s[j] < x[i]) {
            swap(s[j], s[i]);
            f1 = true;
            break;
          }
        }
        if (!f1) {
          for (int j = len - 1; j > i; j--) {
            if (s[j] <= x[i]) {
              swap(s[j], s[i]);
              f1 = true;
              break;
            }
          }
          if (!f1) {
            cout << "---" << endl;
            e = true;
            break;
          } else {
            for (int i = 0; i < len; i++) {
              if (s[i] > x[i]) {
                cout << "---" << endl;
                e = true;
                ff = true;
                break;
              } else if (s[i] < x[i]) {
                cout << s << endl;
                e = true;
                ff = true;
                break;
              } else if (i + 1 == len)
                e = false, ff = true;
            }
          }
        } else {
          cout << s << endl;
          e = true;
          break;
        }
        if (ff) break;
      }
    }
    if (!e) {
      if (s.size() < x.size())
        cout << s << endl;
      else
        cout << "---" << endl;
    }
  }
  return 0;
}
