#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s, c;
    cin >> s >> c;
    int n = s.size();
    int m = c.size();
    int i = 0, j = 0;
    bool truth = true;
    bool swapped = false;
    while (i < n && j < m) {
      int x = s[i] - 'A';
      int y = c[j] - 'A';
      if (x < y) {
        truth = true;
        break;
      } else if (x == y) {
        if (swapped) {
          i++;
          j++;
          continue;
        }
        bool done = false;
        for (int k = i + 1; k < n; k++) {
          int u = s[k] - 'A';
          if (u < x) {
            char temp = s[i];
            s[i] = s[k];
            s[k] = temp;
            done = true;
            break;
          }
        }
        if (done) {
          truth = true;
          break;
        }
      } else {
        if (swapped) {
          truth = false;
          break;
        }
        bool done = false;
        bool ok = false;
        for (int k = i + 1; k < n; k++) {
          int u = s[k] - 'A';
          if (u < y) {
            char temp = s[i];
            s[i] = s[k];
            s[k] = temp;
            done = true;
            break;
          }
        }
        if (done) {
          truth = true;
          break;
        } else {
          int pp = -1;
          for (int k = i + 1; k < n; k++) {
            int u = s[k] - 'A';
            if (u == y) {
              pp = k;
            }
          }
          if (pp != -1) {
            char temp = s[i];
            s[i] = s[pp];
            s[pp] = temp;
            ok = true;
            swapped = true;
          }
        }
        if (!ok) {
          truth = false;
          break;
        }
      }
      i++;
      j++;
    }
    if (truth) {
      if (i == n || j == m) {
        if (n < m) {
          if (s[i - 1] <= s[i - 1]) {
            cout << s << endl;
          } else {
            cout << "---" << endl;
          }
        } else {
          if (s[j - 1] < s[j - 1]) {
            cout << s << endl;
          } else {
            cout << "---" << endl;
          }
        }
      } else {
        cout << s << endl;
      }
    } else {
      cout << "---" << endl;
    }
  }
}
