#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){
        int n;
        cin >> n;
        string dummy;
        getline(cin, dummy);
        while(n--){
                string text;
                getline(cin, text);

                int m, c = 0;
                cin >> m;
                while(m--){
                        string cmd;
                        cin >> cmd;
                        if(cmd == "forward"){
                                cin >> cmd;
                                if(cmd == "char"){
                                        if(c < text.size()){ ++c; }
                                }else if(cmd == "word"){
                                        for(; c < text.size(); ++c){
                                                if(text[c] != ' '){ break; }
                                        }
                                        for(; c < text.size(); ++c){
                                                if(text[c] == ' '){ break; }
                                        }
                                }
                                getline(cin, dummy);
                        }else if(cmd == "backward"){
                                cin >> cmd;
                                if(cmd == "char"){
                                        if(c > 0){ --c; }
                                }else if(cmd == "word"){
                                        for(; c > 0; --c){
                                                if(text[c - 1] != ' '){ break; }
                                        }
                                        for(; c > 0; --c){
                                                if(text[c - 1] == ' '){ break; }
                                        }
                                }
                                getline(cin, dummy);
                        }else if(cmd == "insert"){
                                getline(cin, cmd);
                                int front = cmd.find_first_of('"');
                                int tail = cmd.find_last_of('"');
                                cmd = cmd.substr(front + 1, tail - front - 1);
                                text.insert(c, cmd);
                                c += cmd.size();
                        }else if(cmd == "delete"){
                                cin >> cmd;
                                if(cmd == "char"){
                                        if(c < text.size()){ text.erase(text.begin() + c); }
                                }else if(cmd == "word"){
                                        int end = c;
                                        bool erase = false;
                                        for(; end < text.size(); ++end){
                                                if(text[end] != ' '){
                                                        erase = true;
                                                        break;
                                                }
                                        }
                                        for(; end < text.size(); ++end){
                                                if(text[end] == ' '){ break; }
                                        }
                                        if(erase){
                                                text.erase(text.begin() + c, text.begin() + end);
                                        }
                                }
                                getline(cin, dummy);
                        }
                }
                text.insert(c, "^");
                cout << text << endl;
        }
        return 0;
}

Measured memory_bytes: 0