description: You are given strings S and T, consisting of lowercase English letters. It is guaranteed that T is a permutation of the string abc. 
  
  Find string S', the lexicographically smallest permutation of S such that T is not a subsequence of S'.
  
  String a is a permutation of string b if the number of occurrences of each distinct character is the same in both strings.
  
  A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
  
  A string a is lexicographically smaller than a string b if and only if one of the following holds:
  
    * a is a prefix of b, but a ≠ b;
    * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
  
  Input
  
  Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Description of the test cases follows.
  
  The first line of each test case contains a string S (1 ≤ |S| ≤ 100), consisting of lowercase English letters.
  
  The second line of each test case contains a string T that is a permutation of the string abc. (Hence, |T| = 3).
  
  Note that there is no limit on the sum of |S| across all test cases.
  
  Output
  
  For each test case, output a single string S', the lexicographically smallest permutation of S such that T is not a subsequence of S'.
  
  Example
  
  Input
  
  
  7
  abacaba
  abc
  cccba
  acb
  dbsic
  bac
  abracadabra
  abc
  dddddddddddd
  cba
  bbc
  abc
  ac
  abc
  
  
  Output
  
  
  aaaacbb
  abccc
  bcdis
  aaaaacbbdrr
  dddddddddddd
  bbc
  ac
  
  Note
  
  In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.
  
  In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.
  
  In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence.
solution: //
  // Source code recreated from a .class file by IntelliJ IDEA
  // (powered by Fernflower decompiler)
  //
  
  import java.io.*;
  import java.util.*;
  
  public class CFA {
      static CFA.Reader sc = new CFA.Reader();
      static BufferedWriter bw;
  
      static long count = 0;
  
      public CFA() {
      }
  
      static boolean flag = false;
  
      public static void main(String[] args) throws IOException {
          Scanner sc = new Scanner(System.in);
          int t = sc.nextInt();
          process:
          for (int p = 0; p < t; p++) {
              String s = sc.next();
              String t1=sc.next();
              char[] ar=s.toCharArray();
              int count[] =new int[26];
             for(Character character:ar){
                 count[character-'a']++;
             }
             StringBuilder ans=new StringBuilder();
             if(t1.equals("abc")){
                 if(count[0]>0&&count[1]>0&&count[2]>0){
                     for(int i=1;i<=count[0];i++){
                         ans.append('a');
                     }
                     for(int i=1;i<=count[2];i++){
                         ans.append('c');
                     }
                     for(int i=1;i<=count[1];i++){
                         ans.append('b');
                     }
                     for(int i=3;i<=25;i++){
                         for(int j=1;j<=count[i];j++){
                             ans.append((char)('a'+i));
                         }
                     }
                 }else {
                     for(int i=0;i<=25;i++){
                         for(int j=1;j<=count[i];j++){
                             ans.append((char)('a'+i));
                         }
                     }
                 }
  
             }else if(t1.equals("acb")){
                /* for(int i=1;i<=count[0];i++){
                     ans.append('a');
                 }
                 for(int i=1;i<=count[2];i++){
                     ans.append('c');
                 }
                 for(int i=1;i<=count[1];i++){
                     ans.append('b');
                 }*/
                 for(int i=0;i<=25;i++){
                     for(int j=1;j<=count[i];j++){
                         ans.append((char)('a'+i));
                     }
                 }
             }else if(t1.equals("bac")){
                /* for(int i=1;i<=count[0];i++){
                     ans.append('a');
                 }
                 for(int i=1;i<=count[2];i++){
                     ans.append('c');
                 }
                 for(int i=1;i<=count[1];i++){
                     ans.append('b');
                 }*/
                 for(int i=0;i<=25;i++){
                     for(int j=1;j<=count[i];j++){
                         ans.append((char)('a'+i));
                     }
                 }
             }else if(t1.equals("bca")){
                  /* for(int i=1;i<=count[0];i++){
                     ans.append('a');
                 }
                 for(int i=1;i<=count[2];i++){
                     ans.append('c');
                 }
                 for(int i=1;i<=count[1];i++){
                     ans.append('b');
                 }*/
                 for(int i=0;i<=25;i++){
                     for(int j=1;j<=count[i];j++){
                         ans.append((char)('a'+i));
                     }
                 }
             }else if(t1.equals("cab")){
                 /* for(int i=1;i<=count[0];i++){
                     ans.append('a');
                 }
                 for(int i=1;i<=count[2];i++){
                     ans.append('c');
                 }
                 for(int i=1;i<=count[1];i++){
                     ans.append('b');
                 }*/
                 for(int i=0;i<=25;i++){
                     for(int j=1;j<=count[i];j++){
                         ans.append((char)('a'+i));
                     }
                 }
             }else if(t1.equals("cba")){
                  /* for(int i=1;i<=count[0];i++){
                     ans.append('a');
                 }
                 for(int i=1;i<=count[2];i++){
                     ans.append('c');
                 }
                 for(int i=1;i<=count[1];i++){
                     ans.append('b');
                 }*/
                 for(int i=0;i<=25;i++){
                     for(int j=1;j<=count[i];j++){
                         ans.append((char)('a'+i));
                     }
                 }
             }
              System.out.println(ans.toString());
  
  
  
          }
  
          bw.flush();
          bw.close();
      }
  
      static void dfs(int i, int j, int n, char[][] ar, boolean[][] reached) {
          // reached[i][j] = true;
          //System.out.println(i +" "+j);
          if (i >= 2 || j >= n) {
              return;
          }
          if (i == 1 && j == (n - 1)) {
              flag = true;
              return;
          }
          if (i + 1 < 2) {
              if (ar[i + 1][j] == '0' && !reached[i + 1][j]) {
                  reached[i][j] = true;
                  dfs(i + 1, j, n, ar, reached);
                  reached[i][j] = false;
              }
          }
          if (j + 1 < n) {
              if (ar[i][j + 1] == '0' && !reached[i][j + 1]) {
                  reached[i][j] = true;
                  dfs(i, j + 1, n, ar, reached);
                  reached[i][j] = false;
              }
          }
          if (i + 1 < 2 && j + 1 < n) {
              if (ar[i + 1][j + 1] == '0' && !reached[i + 1][j + 1]) {
                  reached[i][j] = true;
                  dfs(i + 1, j + 1, n, ar, reached);
                  reached[i][j] = false;
              }
          }
          if (i - 1 >= 0) {
              if (ar[i - 1][j] == '0' && !reached[i - 1][j]) {
                  reached[i][j] = true;
                  dfs(i - 1, j, n, ar, reached);
                  reached[i][j] = false;
              }
          }
         /* if (j + 1 < n) {
              if (ar[i][j + 1] == '0' && !reached[i][j + 1]) {
                  reached[i][j] = true;
                  dfs(i, j + 1, n, ar, reached);
                  reached[i][j] = false;
              }
          }*/
          if (i - 1 >= 0 && j + 1 < n) {
              if (ar[i - 1][j + 1] == '0' && !reached[i - 1][j + 1]) {
                  reached[i][j] = true;
                  dfs(i - 1, j + 1, n, ar, reached);
                  reached[i][j] = false;
              }
          }
  
      }
  
      public static class Timings {
          int arr, dept;
  
          public Timings(int arr, int dept) {
              this.arr = arr;
              this.dept = dept;
          }
      }
  
      static void merge(long arr[], int l, int m, int r) {
          int s1 = m - l + 1;
          int s2 = r - (m + 1) + 1;
          long[] ar1 = new long[s1];
          long[] ar2 = new long[s2];
          for (int i = 0; i < s1; i++) {
              ar1[i] = arr[l + i];
          }
          for (int i = 0; i < s2; i++) {
              ar2[i] = arr[m + 1 + i];
          }
          int ind = l;
          int i = 0, j = 0;
          while (i < s1 && j < s2) {
              if (ar1[i] < ar2[j]) {
                  arr[ind] = ar1[i];
                  i++;
  
              } else if (ar1[i] == ar2[j]) {
                  arr[ind] = ar1[i];
                  i++;
              } else {
                  arr[ind] = ar2[j];
                  j++;
                  count += (m + 1) - (l + i);
              }
              ind++;
          }
          while (i < s1) {
              arr[ind] = ar1[i];
              i++;
              ind++;
              //count++;
          }
          while (j < s2) {
              arr[ind] = ar2[j];
              j++;
              ind++;
          }
          //System.out.println(count);
  
      }
  
      static void mergeSort(long arr[], int l, int r) {
          //code here
          if (r > l) {
              int mid = l + (r - l) / 2;
              mergeSort(arr, l, mid);
              mergeSort(arr, mid + 1, r);
              merge(arr, l, mid, r);
  
          }
      }
  
      public static int checker(int a) {
          if (a % 2 == 0) {
              return 1;
          } else {
              byte b;
              do {
                  b = 2;
              } while (a + b != (a ^ b));
  
              return b;
          }
      }
  
      public static boolean checkPrime(int n) {
          if (n != 0 && n != 1) {
              for (int j = 2; j * j <= n; ++j) {
                  if (n % j == 0) {
                      return false;
                  }
              }
  
              return true;
          } else {
              return false;
          }
      }
  
      public static void dfs1(List<List<Integer>> g, boolean[] visited, Stack<Integer> stack, int num) {
          visited[num] = true;
          Iterator var4 = ((List) g.get(num)).iterator();
  
          while (var4.hasNext()) {
              Integer integer = (Integer) var4.next();
              if (!visited[integer]) {
                  dfs1(g, visited, stack, integer);
              }
          }
  
          stack.push(num);
      }
  
      public static void dfs2(List<List<Integer>> g, boolean[] visited, List<Integer> list, int num, int c, int[] raj) {
          visited[num] = true;
          Iterator var6 = ((List) g.get(num)).iterator();
  
          while (var6.hasNext()) {
              Integer integer = (Integer) var6.next();
              if (!visited[integer]) {
                  dfs2(g, visited, list, integer, c, raj);
              }
          }
  
          raj[num] = c;
          list.add(num);
      }
  
      public static int inputInt() throws IOException {
          return sc.nextInt();
      }
  
      public static long inputLong() throws IOException {
          return sc.nextLong();
      }
  
      public static double inputDouble() throws IOException {
          return sc.nextDouble();
      }
  
      public static String inputString() throws IOException {
          return sc.readLine();
      }
  
      public static void print(String a) throws IOException {
          bw.write(a);
      }
  
      public static void printSp(String a) throws IOException {
          bw.write(a + " ");
      }
  
      public static void println(String a) throws IOException {
          bw.write(a + "\n");
      }
  
      public static long getAns(int[] ar, int c, long[][] dp, int i, int sign) {
          if (i < 0) {
              return 1L;
          } else if (c <= 0) {
              return 1L;
          } else {
              dp[i][c] = Math.max(dp[i][c], Math.max((long) ar[i] * getAns(ar, c - 1, dp, i - 1, sign), getAns(ar, c, dp, i - 1, 1)));
              return dp[i][c];
          }
      }
  
      public static long power(long a, long b, long c) {
          long ans;
          for (ans = 1L; b != 0L; b /= 2L) {
              if (b % 2L == 1L) {
                  ans *= a;
                  ans %= c;
              }
  
              a *= a;
              a %= c;
          }
  
          return ans;
      }
  
      public static long power1(long a, long b, long c) {
          long ans;
          for (ans = 1L; b != 0L; b /= 2L) {
              if (b % 2L == 1L) {
                  ans = multiply(ans, a, c);
              }
  
              a = multiply(a, a, c);
          }
  
          return ans;
      }
  
      public static long multiply(long a, long b, long c) {
          long res = 0L;
  
          for (a %= c; b > 0L; b /= 2L) {
              if (b % 2L == 1L) {
                  res = (res + a) % c;
              }
  
              a = (a + a) % c;
          }
  
          return res % c;
      }
  
      public static long totient(long n) {
          long result = n;
  
          for (long i = 2L; i * i <= n; ++i) {
              if (n % i == 0L) {
                  while (n % i == 0L) {
                      n /= i;
                  }
  
                  result -= result / i;
              }
          }
  
          if (n > 1L) {
              result -= result / n;
          }
  
          return result;
      }
  
      public static long gcd(long a, long b) {
          return b == 0L ? a : gcd(b, a % b);
      }
  
      public static boolean[] primes(int n) {
          boolean[] p = new boolean[n + 1];
          p[0] = false;
          p[1] = false;
  
          int i;
          for (i = 2; i <= n; ++i) {
              p[i] = true;
          }
  
          for (i = 2; i * i <= n; ++i) {
              if (p[i]) {
                  for (int j = i * i; j <= n; j += i) {
                      p[j] = false;
                  }
              }
          }
  
          return p;
      }
  
      public String LargestEven(String S) {
          int[] count = new int[10];
  
          int num;
          for (num = 0; num < S.length(); ++num) {
              ++count[S.charAt(num) - 48];
          }
  
          num = -1;
  
          for (int i = 0; i <= 8; i += 2) {
              if (count[i] > 0) {
                  num = i;
                  break;
              }
          }
  
          StringBuilder ans = new StringBuilder();
  
          for (int i = 9; i >= 0; --i) {
              int j;
              if (i != num) {
                  for (j = 1; j <= count[i]; ++j) {
                      ans.append(i);
                  }
              } else {
                  for (j = 1; j <= count[num] - 1; ++j) {
                      ans.append(num);
                  }
              }
          }
  
          if (num != -1 && count[num] > 0) {
              ans.append(num);
          }
  
          return ans.toString();
      }
  
      static {
          bw = new BufferedWriter(new OutputStreamWriter(System.out));
  
      }
  
      public static class Score {
          int l;
          String s;
  
          public Score(int l, String s) {
              this.l = l;
              this.s = s;
          }
      }
  
      public static class Ath {
          int r1;
          int r2;
          int r3;
          int r4;
          int r5;
          int index;
  
          public Ath(int r1, int r2, int r3, int r4, int r5, int index) {
              this.r1 = r1;
              this.r2 = r2;
              this.r3 = r3;
              this.r4 = r4;
              this.r5 = r5;
              this.index = index;
          }
      }
  
      static class Reader {
          private final int BUFFER_SIZE = 65536;
          private DataInputStream din;
          private byte[] buffer;
          private int bufferPointer;
          private int bytesRead;
  
          public Reader() {
              this.din = new DataInputStream(System.in);
              this.buffer = new byte[65536];
              this.bufferPointer = this.bytesRead = 0;
          }
  
          public Reader(String file_name) throws IOException {
              this.din = new DataInputStream(new FileInputStream(file_name));
              this.buffer = new byte[65536];
              this.bufferPointer = this.bytesRead = 0;
          }
  
          public String readLine() throws IOException {
              byte[] buf = new byte[64];
  
              int cnt;
              byte c;
              for (cnt = 0; (c = this.read()) != -1 && c != 10; buf[cnt++] = (byte) c) {
              }
  
              return new String(buf, 0, cnt);
          }
  
          public int nextInt() throws IOException {
              int ret = 0;
  
              byte c;
              for (c = this.read(); c <= 32; c = this.read()) {
              }
  
              boolean neg = c == 45;
              if (neg) {
                  c = this.read();
              }
  
              do {
                  ret = ret * 10 + c - 48;
              } while ((c = this.read()) >= 48 && c <= 57);
  
              return neg ? -ret : ret;
          }
  
          public long nextLong() throws IOException {
              long ret = 0L;
  
              byte c;
              for (c = this.read(); c <= 32; c = this.read()) {
              }
  
              boolean neg = c == 45;
              if (neg) {
                  c = this.read();
              }
  
              do {
                  ret = ret * 10L + (long) c - 48L;
              } while ((c = this.read()) >= 48 && c <= 57);
  
              return neg ? -ret : ret;
          }
  
          public double nextDouble() throws IOException {
              double ret = 0.0D;
              double div = 1.0D;
  
              byte c;
              for (c = this.read(); c <= 32; c = this.read()) {
              }
  
              boolean neg = c == 45;
              if (neg) {
                  c = this.read();
              }
  
              do {
                  ret = ret * 10.0D + (double) c - 48.0D;
              } while ((c = this.read()) >= 48 && c <= 57);
  
              if (c == 46) {
                  while ((c = this.read()) >= 48 && c <= 57) {
                      ret += (double) (c - 48) / (div *= 10.0D);
                  }
              }
  
              return neg ? -ret : ret;
          }
  
          private void fillBuffer() throws IOException {
              this.bytesRead = this.din.read(this.buffer, this.bufferPointer = 0, 65536);
              if (this.bytesRead == -1) {
                  this.buffer[0] = -1;
              }
  
          }
  
          private byte read() throws IOException {
              if (this.bufferPointer == this.bytesRead) {
                  this.fillBuffer();
              }
  
              return this.buffer[this.bufferPointer++];
          }
  
          public void close() throws IOException {
              if (this.din != null) {
                  this.din.close();
              }
          }
      }
  }
  