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

void process_commands(char *buf, int *cursor);
void print_result(char *buf, int cursor);
void forward_char(char *buf, int *cursor);
void forward_word(char *buf, int *cursor);
void backward_char(char *buf, int *cursor);
void backward_word(char *buf, int *cursor);
void insert_text(char *buf, int *cursor, char *text);
void delete_char(char *buf, int cursor);
void delete_word(char *buf, int cursor);

int main()
{
    char buf[1024];
    char numbuf[8];
    int cursor;
    int n, m;
    int i, j;

    fgets(numbuf, sizeof(numbuf), stdin);
    sscanf(numbuf, "%d", &n);

    for(i = 0; i < n; i++){
        cursor = 0;
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';
        fgets(numbuf, sizeof(numbuf), stdin);
        sscanf(numbuf, "%d", &m);
        for(j = 0; j < m; j++) {
            process_commands(buf, &cursor);
        }
        print_result(buf, cursor);
    }

    return 0;
}

void process_commands(char *buf, int *cursor)
{
    char command[128];
    fgets(command, sizeof(command), stdin);
    command[strlen(command)-1] = '\0';

    if(strcmp(command, "forward char") == 0) forward_char(buf, cursor);
    else if(strcmp(command, "forward word") == 0) forward_word(buf, cursor);
    else if(strcmp(command, "backward word") == 0) backward_word(buf, cursor);
    else if(strcmp(command, "backward char") == 0) backward_char(buf, cursor);
    else if(strncmp(command, "insert", strlen("insert")) == 0)
        insert_text(buf, cursor, command+strlen("insert "));
    else if(strcmp(command, "delete char") == 0) delete_char(buf, *cursor);
    else if(strcmp(command, "delete word") == 0) delete_word(buf, *cursor);
}

void forward_char(char *buf, int *cursor)
{
    if(*cursor == strlen(buf)) return;
    (*cursor)++;
}

void forward_word(char *buf, int *cursor)
{
    int tmp;
    for(tmp = *cursor; buf[tmp] == ' '; tmp++);
    for(; tmp < strlen(buf); tmp++)
        if(buf[tmp] == ' ') break;
    *cursor = tmp;
}

void backward_char(char *buf, int *cursor)
{
    if(*cursor == 0) return;
    (*cursor)--;
}

void backward_word(char *buf, int *cursor)
{
    int tmp;
    if(*cursor == 0) return;
    for(tmp = *cursor-1; buf[tmp] == ' '; tmp--);
    for(; tmp >= 0; tmp--)
        if(buf[tmp] == ' ') break;
    if(tmp == 0) *cursor = tmp;
    else *cursor = tmp+1;
}

void insert_text(char *buf, int *cursor, char *text_with_quote)
{
    char tmp[1024];
    char text[128];
    int len = strlen(text_with_quote) - 2;
    int i;

    for(i = 0; i < len; i++)
        text[i] = text_with_quote[i+1];
    text[len] = '\0';
    
    len = strlen(text);
    strcpy(tmp, buf+(*cursor));
    strcpy(buf+(*cursor), text);
    *cursor += len;
    strcpy(buf+(*cursor), tmp);
}

void delete_char(char *buf, int cursor)
{
    if(cursor == strlen(buf)) return;
    char tmp[1024];
    strcpy(tmp, buf+cursor);
    strcpy(buf+cursor, tmp+1);
}

void delete_word(char *buf, int cursor)
{
    int head = 0;
    char tmp[1024];

    for(head = 0; buf[cursor+head] == ' '; head++);
    if(cursor+head == strlen(buf)) return;
    for(; cursor+head < strlen(buf); head++)
        if(buf[cursor+head] == ' ') break;

    strcpy(tmp, buf+cursor+head);
    strcpy(buf+cursor, tmp);
}

void print_result(char *buf, int cursor)
{
    int i;
    for(i = 0; i < cursor; i++) putchar(buf[i]);
    putchar('^');
    for(; i < strlen(buf); i++) putchar(buf[i]);
    putchar('\n');
}



Measured memory_bytes: 2028