//David Pham

// Timothy Christian School

// Senior Division 3

// Contest 2

 

import java.util.*;

import java.io.*;

import java.lang.*;

 

public class ACSL2

{

    public static String alphabetize(String word) 

    {

      int pos = 0;

      String result = "";

      int counter = 0;

      String temp = "";

      char[] letters = new char[word.length()];

      char[] results = new char[word.length()];

      String alphabet = "ABCDEFGH";

      while(counter < word.length())

      {

            letters[counter] = word.charAt(counter);

            counter = counter + 1;

      }

     

      Arrays.sort(letters);

     

      while(pos < word.length())

      {

            result = result + letters[pos];

            pos = pos + 1;

      }

   

      return(result);

    }

    public static String divide(String word) 

    {

      String result1 = alphabetize(word.substring(0, 4)) + alphabetize(word.substring(0, 4));

      String result2 = alphabetize(word.substring(4, 8)) + alphabetize(word.substring(4, 8));

      String result = result1 + " and " + result2;

      return(result);

    }

    public static String add(String word, int num) 

    {

      String result = word.substring(0, num);

      result = result + alphabetize(word.substring(0, num));

      result = result + word.substring(num, word.length());

      result = result.substring(0, 8);

      return(result);

    }

    public static String subtract(String word, int num) 

    {

      String copy = "";

      String result = word.substring(num, 8);

      copy = word.substring(8 - num, 8);

      result = result + alphabetize(copy) + alphabetize(copy);

      return(result.substring(0, 8));

    }

    public static String union(String word, String word2) 

    {

      String result = alphabetize(word.substring(4, 8));

      result = result + alphabetize(word2.substring(0, 4));

      return(result);

    }

    public static String intersect(String word, String word2) 

    {

      String result1 = word.substring(0, 2) + word.substring(6, 8);

      String result2 = word2.substring(0, 2) + word2.substring(6, 8);

      String result = alphabetize(result1) + alphabetize(result2);

      return(result);

    }

    public static void main(String[] args)

    {

      int counter = 0;

      int pos = 0;

      String word = "";

      String words = "";

      String words2 = "";

      int num = 0;

      String result = "";

      Scanner in = new Scanner(System.in);

      String chars = "DASUI";

     

      while(counter < 5)

      {

            word = in.next();

            pos = chars.indexOf(word.charAt(0));

            words = in.next();

            if(pos == 0)

            {

                  System.out.println(result = divide(words));

            }

            else if(pos == 1)

            {

                  num = word.charAt(word.length() - 1) - 48;

                  result = add(words, num);

                  System.out.println(result);

            }

            else if(pos == 2)

            {

                  num = word.charAt(word.length() - 1) - 48;

                  result = subtract(words, num);

                  System.out.println(result);

            }

            else if(pos == 3)

            {

                  words2 = in.next();

                  result = union(words, words2);

                  System.out.println(result);

            }

            else

            {

                  words2 = in.next();

                  result = intersect(words, words2);

                  System.out.println(result);

            }

            counter = counter + 1;

      }

    }

}