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: 
  import java.io.DataInputStream;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Scanner;
  
   
  public class forbiddensubsequence
  {	
  	
      private static void run() throws IOException {
  		Scanner s = new Scanner(System.in);
  		Reader sc=new Reader();
  	    
  		int t = sc.nextInt();
  		
  		while(t-- > 0)
  		{
  			String a  = sc.next();
  			String b = sc.next();
  			
  			char arr[] = a.toCharArray();
  			
  			Arrays.sort(arr);
  			
  			HashMap<Character,Integer> map = new HashMap<>();
  			int i =0;
  			for(char c = 'a';c<='z';c++)
  			{
  				map.put(c,i++);
  			}
  			boolean af=false , bf = false, cf = false;
  			for(i=0;i<arr.length;i++)
  			{
  				if(arr[i]=='a')
  					af = true;
  				else if(arr[i] == 'b')
  					bf = true;
  				else if (arr[i]=='c')
  					cf = true;
  			}
  			
  			if(b.equals("abc") && af && bf && cf)
  			{
  				
  				map.put('b',2);
  				map.put('c',1);
  				
  			}
  			for(int x =0;x<arr.length;x++)
  			{
  				for(int y =0;y<arr.length;y++)
  				{
  					if(map.get(arr[x]) < map.get(arr[y]))
  					{
  						char temp = arr[x];
  						arr[x] = arr[y];
  						arr[y] = temp;
  					}
  				}
  			}
  			String x = new String(arr);
  			
  			System.out.println(x);
  		}
      
      
      }
      public static void main(String[] args) throws IOException {
          in = new Reader();
          out = new PrintWriter(new OutputStreamWriter(System.out));
   
      
          run();
          	 
          out.flush();
          in.close();
          out.close();
      }
   
      private static int gcd(int a, int b) {
          if (a == 0 || b == 0)
              return 0;
          while (b != 0) {
              int tmp;
              tmp = a % b;
              a = b;
              b = tmp;
          }
          return a;
      }
   
      static final long mod = 1000000007;
   
      static long pow_mod(long a, long b) {
          long result = 1;
          while (b != 0) {
              if ((b & 1) != 0) result = (result * a) % mod;
              a = (a * a) % mod;
              b >>= 1;
          }
          return result;
      }
   
      private static long multiplied_mod(long... longs) {
          long ans = 1;
          for (long now : longs) {
              ans = (ans * now) % mod;
          }
          return ans;
      }
   
      @SuppressWarnings("FieldCanBeLocal")
      private static Reader in;
      private static PrintWriter out;
   
      private static int[] read_int_array(int len) throws IOException {
          int[] a = new int[len];
          for (int i = 0; i < len; i++) {
              a[i] = in.nextInt();
          }
          return a;
      }
   
      private static long[] read_long_array(int len) throws IOException {
          long[] a = new long[len];
          for (int i = 0; i < len; i++) {
              a[i] = in.nextLong();
          }
          return a;
      }
   
      private static void print_array(int[] array) {
          for (int now : array) {
              out.print(now);
              out.print(' ');
          }
          out.println();
      }
   
      private static void print_array(long[] array) {
          for (long now : array) {
              out.print(now);
              out.print(' ');
          }
          out.println();
      }
   
      static class Reader {
          private static final int BUFFER_SIZE = 1 << 16;
          private final DataInputStream din;
          private final byte[] buffer;
          private int bufferPointer, bytesRead;
   
          Reader() {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
   
          public String readLine() throws IOException {
              final byte[] buf = new byte[1024]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n') {
                      break;
                  }
                  buf[cnt++] = (byte) c;
              }
              return new String(buf, 0, cnt);
          }
   
          public int nextSign() throws IOException {
              byte c = read();
              while ('+' != c && '-' != c) {
                  c = read();
              }
              return '+' == c ? 0 : 1;
          }
   
          private static boolean isSpaceChar(int c) {
              return !(c >= 33 && c <= 126);
          }
   
          public int skip() throws IOException {
              int b;
              // noinspection ALL
              while ((b = read()) != -1 && isSpaceChar(b)) {
                  ;
              }
              return b;
          }
   
          public char nc() throws IOException {
              return (char) skip();
          }
   
          public String next() throws IOException {
              int b = skip();
              final StringBuilder sb = new StringBuilder();
              while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
                  sb.appendCodePoint(b);
                  b = read();
              }
              return sb.toString();
          }
   
          public int nextInt() throws IOException {
              int ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              final boolean neg = c == '-';
              if (neg) {
                  c = read();
              }
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
   
              if (neg) {
                  return -ret;
              }
              return ret;
          }
   
          public long nextLong() throws IOException {
              long ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              final boolean neg = c == '-';
              if (neg) {
                  c = read();
              }
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
              if (neg) {
                  return -ret;
              }
              return ret;
          }
   
          public double nextDouble() throws IOException {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              final boolean neg = c == '-';
              if (neg) {
                  c = read();
              }
   
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
   
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
   
              if (neg) {
                  return -ret;
              }
              return ret;
          }
   
          private void fillBuffer() throws IOException {
              bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
              if (bytesRead == -1) {
                  buffer[0] = -1;
              }
          }
   
          private byte read() throws IOException {
              if (bufferPointer == bytesRead) {
                  fillBuffer();
              }
              return buffer[bufferPointer++];
          }
   
          public void close() throws IOException {
              din.close();
          }
   
      }
  
  }