#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s, c;
    cin >> s >> c;
    bool done = false;
    if (s >= c) {
      int loop = min(s.length(), c.length());
      int i = 0;
      while (s[i] == 'A' && c[i] == 'A') i++;
      while (i < loop) {
        for (int j = s.length() - 1; j > i; j--) {
          if (s[j] <= c[i] && s[i] != s[j]) {
            char temp = s[j];
            s[j] = s[i];
            s[i] = temp;
            if (s >= c) {
              char temp2 = s[j];
              s[j] = s[i];
              s[i] = temp2;
            } else {
              done = true;
              break;
            }
          }
        }
        if (done) break;
        if (s[i] == c[i])
          i++;
        else
          break;
      }
    } else {
      done = true;
    }
    done ? cout << s : cout << "---";
    if (t != 0) cout << "\n";
  }
  return 0;
}
