#include <bits/stdc++.h>
using namespace std;
bool cmp(string a, string b) {
  for (int i = 0; i < min(a.size(), b.size()); ++i) {
    if (a[i] < b[i])
      return 1;
    else if (a[i] > b[i])
      return 0;
  }
  return a.size() < b.size();
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s, p;
    cin >> s >> p;
    int fd = 0;
    if (cmp(s, p)) {
      cout << s << endl;
      continue;
    }
    for (int i = 0; i < s.size(); ++i) {
      for (int j = s.size() - 1; j > i; --j) {
        if (s[j] < s[i]) {
          swap(s[j], s[i]);
          if (cmp(s, p))
            fd = 1, cout << s << endl;
          else
            swap(s[j], s[i]);
          if (fd) break;
        }
      }
      if (fd == 1) break;
    }
    if (fd == 0) cout << "---" << endl;
  }
  return 0;
}
