#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int GetForwardLast( int cs, string t )
{
    if ( cs == (int)t.size() ) return cs;

    bool flag = false;
    for ( int i = cs; i < (int)t.size(); i++ )
    {
        if ( t[i] == ' ' )
        {
            if ( flag )
            {
                return i;
            }
        }
        else
        {
            flag = true;
        }
    }
    return (int)t.size();
}

int GetBackwardFirst( int cs, string t )
{
    if ( cs == 0 ) return 0;

    bool flag = false;
    for ( int i = cs-1; i >= 0; i-- )
    {
        if ( t[i] == ' ' )
        {
            if ( flag )
            {
                return i + 1;
            }
        }
        else
        {
            flag = true;
        }
    }
    return 0;
}

int main( void )
{
    string line;

    getline( cin, line );
    istringstream is( line );
    int n;
    is >> n;

    for ( int i = 0; i < n; i++ )
    {
        int cs = 0;

        getline( cin, line );
        string text = line;

        getline( cin, line );
        istringstream is( line );
        int m;
        is >> m;
        is.clear();

        for ( int j = 0; j < m; j++ )
        {
            getline( cin, line );
            string c = line;
            istringstream is( c );
            string c1, c2;
            is >> c1;
            getline( is, c2 );
            c2 = c2.substr( 1 );
            is.clear();

            int cs_f = GetForwardLast( cs, text );
            int cs_b = GetBackwardFirst( cs, text );

            if ( c1 == "forward" )
            {
                if ( c2 == "char" )
                {
                    if ( cs + 1 <= (int)text.size() ) cs++;
                }
                else
                {
                    cs = cs_f;
                }
            }
            else if ( c1 == "backward" )
            {
                if ( c2 == "char" )
                {
                    if ( cs - 1 >= 0 ) cs--;
                }
                else
                {
                    cs = cs_b;
                }
            }
            else if ( c1 == "insert" )
            {
                string next;
                string trim;
                for ( int k = 1; k + 1 < (int)c2.size(); k++ ) trim += c2[k];
                for ( int k = 0; k < cs; k++ ) next += text[k];
                for ( int k = 0; k < (int)trim.size(); k++ ) next += trim[k];
                for ( int k = cs; k < (int)text.size(); k++ ) next += text[k];
                text = next;
                cs += (int)trim.size();
            }
            else if ( c1 == "delete" )
            {
                if ( c2 == "char" )
                {
                    if ( cs < (int)text.size() )
                    {
                        string next = text.substr( 0, cs ) + text.substr( cs+1 );
                        text = next;
                    }
                }
                else
                {
                    bool blank = true;
                    for ( int k = cs; k < (int)text.size(); k++ ) if ( text[k] != ' ' ) blank = false;
                    if ( blank == false && cs < (int)text.size() )
                    {
                        string next = text.substr( 0, cs ) + text.substr( cs_f );
                        text = next;
                    }
                }
            }
        }

        for ( int i = 0; i <= (int)text.size(); i++ )
        {
            if ( i == cs ) cout << "^";
            if ( i < (int)text.size() ) cout << text[i];
        }
        cout << endl;
    }

    return 0;
}