#!/usr/bin/env python

# Vi
# Phil Bordelon

import os
import sys

DEBUG = os.getenv("DEBUG", False)

MAX_SIZE = 100

# As usual, we use classes as structures.
class Struct(object):
   pass

####
# deleteLeft deletes one character to the left of the cursor.
def deleteLeft(buffer):

   # If we're on the left side, do nothing.  Otherwise ...
   if buffer.cursor > 0:

      new_text = []
      new_text.extend(buffer.text[0:buffer.cursor - 1])
      new_text.extend(buffer.text[buffer.cursor:])
      buffer.text = new_text

      # Move the cursor one space left.
      buffer.cursor -= 1

####
# deleteWordLeft deletes a single word to the left of the cursor.
def deleteWordLeft(buffer):

   # As always, we only care if we're not on the left edge ...
   if buffer.cursor > 0:

      # All right.  Let's find the first space before the cursor.
      curr_loc = buffer.cursor
      while curr_loc > 0 and buffer.text[curr_loc - 1] != ' ':
         curr_loc -= 1

      # Got it.  Build the new text.
      new_text = []
      new_text.extend(buffer.text[0:curr_loc])
      new_text.extend(buffer.text[buffer.cursor:])
      buffer.text = new_text

      # Move the cursor the appropriate number of spaces.
      buffer.cursor -= buffer.cursor - curr_loc

####
# dupLeft duplicates the letter to the left of the cursor.
def dupLeft(buffer):

   # You know the drill about left commands.
   if buffer.cursor > 0:

      new_text = []
      new_text.extend(buffer.text[0:buffer.cursor])
      new_text.extend(buffer.text[buffer.cursor - 1])
      new_text.extend(buffer.text[buffer.cursor:])
      buffer.text = new_text

      buffer.cursor += 1

####
# dupWordLeft duplicates the word to the left of the cursor.
def dupWordLeft(buffer):

   if buffer.cursor > 0:

      # Find the first space before the cursor.
      curr_loc = buffer.cursor
      while curr_loc > 0 and buffer.text[curr_loc - 1] != ' ':
         curr_loc -= 1

      # Got it.  Build the new text.
      new_text = []
      new_text.extend(buffer.text[0:buffer.cursor])
      new_text.extend(buffer.text[curr_loc:buffer.cursor])
      new_text.extend(buffer.text[buffer.cursor:])
      buffer.text = new_text

      # Move ahead the length of the duplicated word.
      buffer.cursor += buffer.cursor - curr_loc

####
# reverseWordLeft reverses the word to the left of the cursor.
def reverseWordLeft(buffer):

   if buffer.cursor > 0:

      # Find the first space before the cursor.
      curr_loc = buffer.cursor
      while curr_loc > 0 and buffer.text[curr_loc - 1] != ' ':
         curr_loc -= 1

      # Got it.  Build the new text.
      new_text = []
      new_text.extend(buffer.text[0:curr_loc])
      reverse_text = buffer.text[curr_loc:buffer.cursor]

      reverse_text.reverse()
      new_text.extend(reverse_text)

      new_text.extend(buffer.text[buffer.cursor:])

      buffer.text = new_text

      # The cursor doesn't move with reverses.

####
# killLeft kills everything to the left of the cursor.
def killLeft(buffer):

   buffer.text = buffer.text[buffer.cursor:]
   buffer.cursor = 0

####
# killRight kills everything to the right of the cursor.
def killRight(buffer):

   buffer.text = buffer.text[0:buffer.cursor]

####
# move moves the cursor.
def move(buffer, delta):

   buffer.cursor += delta
   if buffer.cursor < 0:
      buffer.cursor = 0
   elif buffer.cursor > len(buffer.text):
      buffer.cursor = len(buffer.text)

####
# insert handles standard character inserts.
def insert(buffer, character):

   new_text = []
   new_text.extend(buffer.text[0:buffer.cursor])
   new_text.append(character)
   new_text.extend(buffer.text[buffer.cursor:])
   buffer.text = new_text

   buffer.cursor += 1

####
# handleKeystroke handles a single keystroke.
def handleKeystroke(buffer, key):

   # Commands first.
   if key == "x":
      deleteLeft(buffer)
   elif key == "K":
      deleteWordLeft(buffer)
   elif key == "c":
      dupLeft(buffer)
   elif key == "D":
      dupWordLeft(buffer)
   elif key == "R":
      reverseWordLeft(buffer)
   elif key == "p":
      killLeft(buffer)
   elif key == "W":
      killRight(buffer)
   elif key == "h":
      move(buffer, -1)
   elif key == "L":
      move(buffer, 1)
   elif key == "f":
      move(buffer, -MAX_SIZE)
   elif key == "G":
      move(buffer, MAX_SIZE)
   else:
      insert(buffer, key)

####
# printBuffer prints out the buffer.
def printBuffer(buffer):

   to_print = ""
   for i in range(len(buffer.text) + 1):
      if buffer.cursor == i:
         to_print += "^"
      if i < len(buffer.text):
         to_print += buffer.text[i]

   print(to_print)
         
if "__main__" == __name__:

   dataset_count = int(sys.stdin.readline())

   for dataset_loop in range(dataset_count):

      # Each dataset is a fresh buffer.
      buffer = Struct()
      buffer.text = []
      buffer.cursor = 0

      # Read in the series of keystrokes.
      keystrokes = sys.stdin.readline().strip()
      for keystroke in keystrokes:
         handleKeystroke(buffer, keystroke)
         if DEBUG:
            printBuffer(buffer)

      # Print out the result.
      printBuffer(buffer)