#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;

int main(){
    int n,m;
    string s,q;
    cin>>n;cin.ignore();
    while(n--){
        int cur=0;
        string t("^");
        getline(cin,s);
        s=t+s;
        cin>>m;cin.ignore();
        rep(i,m){
            getline(cin,q);
            if(q=="forward char"){
                if(cur<s.size()-1) {
                    swap(s[cur],s[cur+1]);
                    ++cur;
                } 
            }
            else if(q=="forward word"){
                int spcnt=(s[cur+1]!=' ')?1:0;
                while(cur<s.size()-1){
                    if(s[cur+1]==' '){
                        ++spcnt;
                        if(spcnt==2) break;
                        while(s[cur+1]==' '){
                            swap(s[cur],s[cur+1]);
                            ++cur;
                        } 
                    }
                    if(cur<s.size()-1){
                    swap(s[cur],s[cur+1]);
                    ++cur;
                    }
                }
            }
            else if(q=="backward char"){
                if(cur>0) {
                    swap(s[cur],s[cur-1]);
                    --cur;
                } 
            }
            else if(q=="backward word"){
                int spcnt=(s[cur-1]!=' ')?1:0;;
                while(cur>0){
                    if(s[cur-1]==' '){
                        ++spcnt;
                        if(spcnt==2) break;
                        while(s[cur-1]==' '){
                            swap(s[cur],s[cur-1]);
                            --cur;
                        }
                    }
                    if(cur>0){
                    swap(s[cur],s[cur-1]);
                    --cur;
                    }
                }
            }
            else if(q[0]=='i'){
                s.insert(cur,q,8,q.size()-9);
                cur +=q.size()-9;
            }
            else if(q=="delete char"){
                if(cur<s.size()-1){
                s.erase(cur+1,1);
                }
            }
            else if(q=="delete word"){
                int spcnt=(s[cur+1]!=' ')?1:0,tm=cur;
                while(tm<s.size()-1){
                    if(s[tm+1]==' '){
                        ++spcnt;
                        if(spcnt==2) break;
                        while(tm<s.size()-1&&s[tm+1]==' '){
                            swap(s[tm],s[tm+1]);
                            ++tm;
                        } 
                    }
                    if(tm==s.size()-1&&s[tm-1]==' '){
                        while(cur!=tm){
                            swap(s[tm],s[tm-1]);
                            --tm;
                        }
                        break;
                    }
                    if(tm<s.size()-1){
                        swap(s[tm],s[tm+1]);++tm;
                    }     
                }
                while(tm!=cur){
                    s.erase(tm-1,1);
                    --tm;
                }
            }
        }
        cout<<s<<endl;
    }
    return 0;
}

Measured memory_bytes: 3208