#include <bits/stdc++.h>
char s[5003], c[5003];
int last[500];
int main(void) {
  int t;
  scanf("%d\n", &t);
  while (t--) {
    scanf("%s %s\n", s, c);
    for (int i = 'A'; i <= 'Z'; ++i) last[i] = -1;
    for (int i = 0; s[i]; ++i) last[s[i]] = i;
    bool found = false;
    for (int i = 0; s[i] && !found; ++i) {
      for (int j = 'A'; j < s[i]; ++j) {
        if (last[j] > i) {
          char ch = s[i];
          s[i] = s[last[j]];
          s[last[j]] = ch;
          found = true;
          break;
        }
      }
    }
    if (strcmp(s, c) < 0)
      printf("%s\n", s);
    else
      printf("---\n");
  }
  return 0;
}
