#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cassert>
#include<sstream>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)

using namespace std;
int cursor;

void ForwardChar(string &message)
{
  assert(message[cursor] == '^');
  string tmp = "";
  bool found = false;
  rep(i,message.size())
    {
      if(message[i] == '^')
	{
	  found = true;
	  continue;
	}

      tmp += message[i];
      if(found)
	{
	  tmp += "^";
	  found = false;
	}	
    }
  if(found)tmp += "^",cursor--;
  cursor++;
  message = tmp;
}

void ForwardWord(string &message)
{
  assert(message[cursor] == '^');
  string tmp = "";
  bool cursor_found = false;
  bool word_found = false;
  rep(i,message.size())
    {
      if(message[i] == '^')
	{
	  cursor_found = true;
	  continue;
	}
      if(cursor_found && word_found && message[i] == ' ')
	{
	  cursor_found = word_found = false;
	  tmp += "^";
	  cursor = tmp.size()-1;
	}
      tmp += message[i];
      if(cursor_found && message[i] != ' ')
	{
	  word_found = true;
	}
    }
  if(cursor_found)
    {
      tmp += "^";
      cursor = tmp.size()-1;
    }
  message = tmp;

}

void BackwardChar(string &message)
{
  assert(message[cursor] == '^');
  int last = message.size();
  string tmp = "";
  bool found = false;
  for(int i=last-1;i>=0;i--)
    {
      if(message[i] == '^')
	{
	  found = true;
	  continue;
	}

      tmp += message[i];

      if(found)
	{
	  found = false;
	  tmp += "^";
	  cursor--;
	}
    }
  if(found)tmp += "^";
  reverse(tmp.begin(),tmp.end());
  message = tmp;
}

void BackwardWord(string &message)
{
  assert(message[cursor] == '^');
  string tmp = "";
  bool cursor_found = false;
  bool word_found = false;
  for(int i=message.size()-1;i>=0;i--)
    {
      if(message[i] == '^')
	{
	  cursor_found = true;
	  continue;
	}
      if(cursor_found && word_found && message[i] == ' ')
	{
	  cursor_found = word_found = false;
	  tmp += "^";
	}
      tmp += message[i];
      if(cursor_found && message[i] != ' ')
	{
	  word_found = true;
	}
    }
  if(cursor_found)
    {
      tmp += "^";
    }
  reverse(tmp.begin(),tmp.end());
  message = tmp;
  rep(i,message.size())
    if(message[i] == '^')
      {
	cursor = i;
	break;
      }
}

void Insert(string &message,string text)
{
  assert(message[cursor] == '^');
  string tmp = "";
  rep(i,message.size())
    {
      if(message[i] == '^')
	{
	  tmp += text + "^";
	  cursor += text.size();
	  continue;
	}
      tmp += message[i];
    }
  message = tmp;

}

void DeleteChar(string &message)
{
  assert(message[cursor] == '^');
  bool found = false;
  string tmp = "";
  rep(i,message.size())
    {
      if(message[i] == '^')
	{
	  found = true;
	  tmp += "^";
	  continue;
	}
      if(!found)
	tmp += message[i];
      else
	found = false;
    }
  message = tmp;
}

void DeleteWord(string &message)
{
  assert(message[cursor] == '^');

  bool ok = false;
  bool ok2 = false;
  for(int i=message.size()-1;i>=0;i--)
    {
      if(message[i] != ' ' && message[i] != '^')ok = true;
      if(ok && message[i] == '^')
	{
	  ok2 = true;
	  break;
	}
    }
  if(!ok2)return;
  string tmp = "";
  bool cursor_found = false;
  bool word_found = false;
  rep(i,message.size())
    {
      if(message[i] == '^')
	{
	  tmp += "^";
	  cursor_found = true;
	  continue;
	}

      if(!cursor_found)
	{
	  tmp += message[i];
	}
      else if(cursor_found && message[i] != ' ')
	{
	  word_found = true;
	}      
      else if(cursor_found && word_found && message[i] == ' ')
	{
	  cursor_found = word_found = false;
	  tmp += message[i];
	}
    }

  message = tmp;
}



int main()
{
  int T,n;
  cin >>T;
  cin.ignore();

  while(T--)
    {
      cursor = 0;

      string line;
      getline(cin,line);

      line = "^" + line;
      cin >> n;
      cin.ignore();
      string o1,o2;
      rep(_,n)
	{

	  cin >> o1;
	  cin.ignore();
	  getline(cin,o2);
	  if(o1 == "forward")
	    {
	      if(o2 == "char")
		{
		  ForwardChar(line);
		}
	      else if(o2 == "word")
		{
		  ForwardWord(line);
		}
	      else assert(false);
	    }
	  else if(o1 == "backward")
	    {
	      if(o2 == "char")
		{
		  BackwardChar(line);
		}
	      else if(o2 == "word")
		{
		  BackwardWord(line);
		}
	      else assert(false);
	    }
	  else if(o1 == "insert")
	    {
	      o2 = o2.substr(1,o2.size()-2);
	      Insert(line,o2);
	    }
	  else if(o1 == "delete")
	    {
	      if(o2 == "char")
		{
		  DeleteChar(line);
		}
	      else if(o2 == "word")
		{
		  DeleteWord(line);
		}
	    }
	  else 
	    {
	      assert(false);
	    }
	}
      cout << line << endl;

    }
  return 0;
}