#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  string s1, s2, s3;
  cin >> t;
  while (t--) {
    cin >> s1 >> s2;
    long long int n = min(s1.length(), s2.length()), temp = -1, flag = 0;
    if (s1 < s2) {
      cout << s1 << endl;
      continue;
    }
    s3 = s1;
    sort(s3.begin(), s3.end());
    for (long long int i = 0; i <= s1.length() - 1; i++) {
      if (s1[i] != s3[i]) {
        temp = i;
        break;
      }
    }
    if (temp == -1) {
      cout << "---\n";
      continue;
    }
    for (long long int i = temp + 1; i <= s1.length() - 1; i++) {
      if (s1[i] == s3[temp]) {
        swap(s1[i], s1[temp]);
        if (s1 < s2) {
          flag = 1;
          break;
        }
        swap(s1[i], s1[temp]);
      }
    }
    if (flag)
      cout << s1 << endl;
    else
      cout << "---" << endl;
  }
  return 0;
}
