#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char buffer[1024];
int cursor = 0;

typedef struct{
    char cmd[1024];
    char any_text[1024];
}Command;

void exec_cmd(Command command);

int main(void)
{
    int n, i;

    scanf("%d", &n);
    getchar();
    for(i = 0; i < n; i++){
        cursor = 0;
        int num_cmd;
        Command command;

        fgets(buffer, sizeof(buffer), stdin);
        buffer[strlen(buffer) - 1] = '\0';
        scanf("%d", &num_cmd);
        getchar();

        int j;
        for(j = 0; j < num_cmd; j++){
            char temp[1024];
            fgets(temp, sizeof(temp), stdin);
            int k;
            for(k = 0; k < strlen(temp); k++){
                if(temp[k] == '\n')
                    temp[k] = '\0';
            }

            if(temp[0] != 'i'){
                strcpy(command.cmd, temp);
            }else{
                char *p = temp;
                while(*p != ' ') p++;
                *p = '\0';
                p += 2;
                char *start = p;
                while(*p != '"') p++;
                *p = '\0';
                strcpy(command.cmd, temp);
                strcpy(command.any_text, start);
            }

            exec_cmd(command);
            if(cursor > strlen(buffer) + 1){
                fprintf(stderr, "command: %s\n", command.cmd);
                fprintf(stderr, "cursor overflow\n");
                exit(1);
            }
            //int len = strlen(buffer);
            //for(k = 0; k <= len; k++){
            //    if(cursor == k)
            //        putchar('^');
            //    putchar(buffer[k]);
            //}
            //printf("\n");
            //printf("\n");
        }
        int len = strlen(buffer);
        for(j = 0; j <= len; j++){
            if(cursor == j)
                putchar('^');
            putchar(buffer[j]);
        }
        printf("\n");
    }

    return 0;
}

void exec_cmd(Command command)
{
    if(strcmp(command.cmd, "forward char") == 0){
        if(buffer[cursor] != '\0')
            cursor++;
    }

    else if(strcmp(command.cmd, "forward word") == 0){
        while(buffer[cursor] == ' ' && buffer[cursor] != '\0'){
            cursor++;
        }
        while(buffer[cursor] != ' ' && buffer[cursor] != '\0'){
            cursor++;
        }
    }

    else if(strcmp(command.cmd, "backward char") == 0){
        if(cursor != 0)
            cursor--;
    }

    else if(strcmp(command.cmd, "backward word") == 0){
        while(buffer[cursor - 1] == ' ' && cursor != 0){
            cursor--;
        }
        while(buffer[cursor - 1] != ' ' && cursor != 0){
            cursor--;
        }
    }

    else if(strcmp(command.cmd, "insert") == 0){
        char temp[1024];
        strcpy(temp, buffer);
        temp[cursor] = '\0';
        strcat(temp, command.any_text);
        strcat(temp, buffer + cursor);
        strcpy(buffer, temp);
        cursor += strlen(command.any_text);
    }

    else if(strcmp(command.cmd, "delete char") == 0){
        if(buffer[cursor] != '\0'){
            char *p = buffer + cursor + 1;
            buffer[cursor] = '\0';
            strcat(buffer, p);
        }
    }

    else if(strcmp(command.cmd, "delete word") == 0){
        char temp[1024];
        int flag = 0;
        strcpy(temp, buffer);
        char *p = temp + cursor;
        if(*p == ' '){
            flag = 1;
            *p = '\0';
            p++;
            while(*p == ' '){
                p++;
            }
        }

        if(*p != '\0'){
            flag = 1;
            *p = '\0';
            p++;
            while(*p != ' ' && *p != '\0'){
                p++;
            }
        }

        if(flag){
            char t[1024];
            strcpy(t, p);
            strcat(temp, t);
            strcpy(buffer, temp);
        }
    }

    else{
        fprintf(stderr, "command %s not found\n", command.cmd);
        exit(1);
    }
}