#include <bits/stdc++.h>
using namespace std;
int main() {
  int tt;
  cin >> tt;
  while (tt--) {
    string s, c;
    cin >> s >> c;
    bool possible = false;
    if (s < c) {
      possible = true;
    }
    if (!possible) {
      for (int i = 0; i < (int)min(s.size(), c.size()); i++) {
        for (int j = i; j < (int)s.size(); j++) {
          if (s[j] < c[i]) {
            swap(s[i], s[j]);
            possible = true;
            break;
          }
        }
        if (!possible) {
          if (s[i] == c[i]) {
            continue;
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
    if (!possible) {
      for (int i = 0; i < (int)min(s.size(), c.size()); i++) {
        if (s[i] > c[i] && (!possible)) {
          for (int j = i + 1; j < (int)s.size(); j++) {
            if (s[j] == c[i]) {
              swap(s[i], s[j]);
              if (s >= c) {
                swap(s[i], s[j]);
              } else {
                possible = true;
                break;
              }
            }
          }
        }
      }
    }
    if (possible) {
      cout << s << "\n";
    } else {
      cout << "---\n";
    }
  }
  return 0;
}
