#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    char s[5001] = {'\0'}, t[5001] = {'\0'}, c[5001] = {'\0'};
    cin >> s >> c;
    int n1 = strlen(s), n2 = strlen(c);
    strcpy(t, s);
    sort(t, t + n1);
    int p = -1;
    for (int i = 0; i < n1; i++) {
      if (s[i] != t[i]) {
        p = i;
        break;
      }
    }
    if (p != -1) {
      for (int i = n1 - 1; i >= p + 1; i--) {
        if (s[i] == t[p]) {
          swap(s[i], s[p]);
          break;
        }
      }
    }
    if (strcmp(s, c) < 0)
      cout << s;
    else
      cout << "---";
    cout << endl;
  }
  return 0;
}
