#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    string s, c;
    cin >> s >> c;
    long long int len_s = s.length();
    long long int len_c = c.length();
    vector<vector<long long int>> occur(26);
    for (long long int i = 0; i < len_s; ++i) {
      occur[s[i] - 'A'].push_back(i);
    }
    for (long long int i = 0; i < min(len_s, len_c); ++i) {
      if (s[i] - 'A' < c[i] - 'A') {
        cout << s << endl;
        goto next;
      } else if (s[i] - 'A' > c[i] - 'A') {
        long long int alpha = c[i] - 'A';
        for (long long int j = 0; j <= alpha; ++j) {
          if (occur[j].size() == 0) continue;
          long long int last = occur[j][occur[j].size() - 1];
          if (last > i) {
            swap(s[i], s[last]);
            goto check;
          }
        }
        cout << "---" << endl;
        goto next;
      } else {
        long long int alpha = c[i] - 'A';
        for (long long int j = 0; j < alpha; ++j) {
          if (occur[j].size() == 0) continue;
          long long int last = occur[j][occur[j].size() - 1];
          if (last > i) {
            swap(s[i], s[last]);
            goto check;
          }
        }
      }
      if (i == min(len_s, len_c) - 1) goto check;
    }
  check : {
    for (long long int i = 0; i < min(len_s, len_c); ++i) {
      if (s[i] < c[i]) {
        cout << s << endl;
        goto next;
      } else if (s[i] > c[i]) {
        cout << "---" << endl;
        goto next;
      }
    }
    if (len_s < len_c)
      cout << s << endl;
    else
      cout << "---" << endl;
    goto next;
  }
  next:;
  }
}
