#include <ctype.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

/*
 * DEBUG ONLY: If compiled -DSTEP, the contents of the edit buffer are displayed
 * to stderr after every single command/character in the input. Usefull for
 * tracing the behavior of individual commands.
 */

/* SANITY CHECK: assertion macro for verifying input data and internal state */
#define ASSERT(e) { if(!(e)) { cerr << #e << endl; throw; } }

/* Print the edit buffer and cursor position to specified stream */
void output(ostream &out, string buffer, int cursor)
{
    out << buffer.substr(0, cursor) << '^' << buffer.substr(cursor) << endl;
}

/* Main body of program */
void process(void)
{
    int data_num, data_idx;

    /* Read how many data sets to process */
    cin >> data_num;
    
    /* Skip over the trailing newline so getline() doesn't see it */
    cin >> ws;
    
    /* Process each data set separately */
    for(data_idx = 0; data_idx < data_num; data_idx++) {
        string buffer, input;
        int cursor = 0;

        /* Read line of input and process one character at a time */        
        getline(cin, input);
        for(int i = 0; i < input.size(); i++) {
            char key = input[i];
            int pos;
            
            switch(key) {

                /* Backspace (delete one char to the left); one gap left */
                case 'x':
                    if(cursor > 0) {
                        buffer.erase(cursor - 1, 1);
                        cursor--;
                    }
                    break;
                    
                /* Erase non-space to the left; gap right of the stop char */
                case 'K':
                    pos = buffer.rfind(" ", cursor);
                    if(pos == string::npos)
                        pos == -1;
                    if(pos != cursor - 1) {
                        buffer.erase(pos + 1, cursor - pos - 1);
                        cursor -= cursor - pos - 1;
                    }
                    break;
                    
                /* Duplicate one char from left to right; one gap right */
                case 'c':
                    if(cursor > 0) {
                        buffer.insert(cursor, 1, buffer[cursor - 1]);
                        cursor++;
                    }
                    break;
                                    
                /* Duplicate non-space to the left; gap right of duplication */
                case 'D':
                    pos = buffer.rfind(" ", cursor);
                    if(pos == string::npos)
                        pos == -1;
                    if(pos != cursor - 1) {
                        buffer.insert(cursor, buffer.substr(pos + 1,
                            cursor - pos - 1));
                        cursor += cursor - pos - 1;
                    }
                    break;
                                    
                /* Reverse non-space to the left; no cursor change */
                case 'R':
                    pos = buffer.rfind(" ", cursor);
                    if(pos == string::npos)
                        pos == -1;
                    if(pos != cursor - 1)                
                        reverse(&buffer[pos + 1], &buffer[cursor]);
                    break;
                    
                /* Delete all chars to the left; cursor to start */
                case 'p':
                    buffer.erase(0, cursor);
                    cursor = 0;
                    break;
                
                /* Delete all chars to the right; no cursor change */
                case 'W':
                    buffer.erase(cursor);
                    break;
                    
                /* Move cursor one gap to the left */                
                case 'h':
                    if(cursor > 0)
                        cursor--;
                    break;
                    
                /* Move cursor one gap to the right */
                case 'L':
                    if(cursor < buffer.size())
                        cursor++;
                    break;                    
                
                /* Move cursor to the first gap on the line */                  
                case 'f':
                    cursor = 0;
                    break;
                            
                /* Move cursor to the last gap one the line */
                case 'G':
                    cursor = buffer.size();
                    break;
                    
                /* Insert char at cursor position; one gap right */
                default:
                    ASSERT(isalnum(key) || key == ' ');
                    buffer.insert(cursor, 1, key);
                    cursor++; 
                    break;
            }
#ifdef STEP
            cerr << input.substr(i) << endl;
            output(cerr, buffer, cursor);
#endif            
        }        
        output(cout, buffer, cursor);
#ifdef STEP
        cerr << endl; 
#endif           
    }
}

/* Run program and print out any exceptions that occur */
int main(void)
{
    /* Throw exceptions on EOF or failed data extraction in >> operator */
    cin.exceptions(ios::eofbit | ios::failbit);

    /* Run main body of code */
    try {
        process();
    }
    
    /* Catch unexpected EOF or bad input data */
    catch(ios::failure const &e) {
        cerr << "Unexpected EOF or data type mismatch on input" << endl;
    }

    return 0;
}