#include <bits/stdc++.h>
using namespace std;
int t;
string s, c;
int main() {
  cin >> t;
  while (t--) {
    cin >> s >> c;
    int p = 0, pp = 0;
    string ss = s;
    sort(ss.begin(), ss.end());
    for (; p < s.size(); p++) {
      if (s[p] != ss[p]) {
        break;
      }
    }
    char mn = s[p];
    pp = p;
    if (p < s.size() - 1) {
      for (int i = p + 1; i < s.size(); i++) {
        if (s[i] <= mn) {
          mn = s[i];
          pp = i;
        }
      }
    }
    swap(s[p], s[pp]);
    if (s < c) {
      cout << s << endl;
    } else {
      cout << "---" << endl;
    }
  }
  return 0;
}
