#include <bits/stdc++.h>
using namespace std;
bool check(string s, string t) {
  int n = min((int)s.length(), (int)t.length());
  int i;
  for (i = 0; i < n; i++) {
    if (s[i] < t[i]) return true;
    if (s[i] > t[i]) return false;
  }
  if (s.length() < t.length()) return true;
  return false;
}
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  int tc;
  cin >> tc;
  while (tc--) {
    string s, t;
    cin >> s >> t;
    int len = s.length();
    vector<string> vi;
    for (i = 0; i < len; i++) {
      char now = s[i];
      char mini = now;
      for (j = i + 1; j < len; j++) {
        char a = s[j];
        mini = min(mini, a);
      }
      if (now == mini) continue;
      for (j = i + 1; j < len; j++) {
        char a = s[j];
        if (a == mini) {
          swap(s[i], s[j]);
          vi.push_back(s);
          swap(s[i], s[j]);
        }
      }
      break;
    }
    string chaeng = "---";
    if (check(s, t)) chaeng = s;
    for (auto x : vi) {
      if (check(x, t)) chaeng = x;
    }
    cout << chaeng << endl;
  }
  return 0;
}
