output_description: For each input text, print the final text with a character '^' representing
  the cursor position. Each output line shall contain exactly a single text with
  a character '^'.
input_description: The first input line contains a positive integer, which represents the number
  of texts the editor will edit. For each text, the input contains the following
  descriptions:
  
      * The first line is an initial text whose length is at most 100.
      * The second line contains an integer _M_ representing the number of editing commands.
      * Each of the third through the _M_ +2nd lines contains an editing command.
  
  You can assume that every input line is in a proper format or has no syntax
  errors. You can also assume that every input line has no leading or trailing
  spaces and that just a single blank character occurs between a command name
  (e.g., forward) and its argument (e.g., char).
attempt: n = int(input())
  for _ in range(n):
      tx = list(input())
      m = int(input())
      cursorpoint = 0
      for i in range(m):
          qur = input().split()
          if qur[0] == "forward":
              if qur[1] == "char":
                  cursorpoint = min(len(tx), cursorpoint + 1)
              else:
                  flag = cursorpoint == 0
                  while cursorpoint < len(tx):
                      if tx[cursorpoint] == " ":
                          if flag:
                              break
                          else:
                              cursorpoint += 1
                      else:
                          cursorpoint += 1
                          flag = True
          if qur[0] == "backward":
              if qur[1] == "char":
                  cursorpoint = max(0, cursorpoint - 1)
              else:
                  flag = False
                  while cursorpoint > 0:
                      if tx[cursorpoint - 1] == " ":
                          cursorpoint -= 1
                          flag = True
                      elif flag:
                          break
                      else:
                          cursorpoint -= 1
          if qur[0] == "insert":
              txtt = list("".join(qur[1:]))
              txtt.remove('"')
              txtt.remove('"')
              if cursorpoint < len(tx):
                  tx = tx[:cursorpoint] + txtt + tx[cursorpoint:]
              else:
                  tx = tx[:cursorpoint] + txtt
              cursorpoint += len(txtt)
          if qur[0] == "delete":
              if qur[1] == "char":
                  if cursorpoint < len(tx):
                      tx.pop(cursorpoint)
              else:
                  memo = cursorpoint
                  flag = cursorpoint == len(tx)
                  dodelete = True
                  while cursorpoint < len(tx):
                      if tx[cursorpoint] == " ":
                          if flag:
                              break
                          else:
                              cursorpoint += 1
                      else:
                          cursorpoint += 1
                          flag = True
                  else:
                      dodelete = False
                  if dodelete:
                      tx = tx[:memo] + tx[cursorpoint:]
                  cursorpoint = memo
  
      print("".join(tx[:cursorpoint]) + "^" + "".join(tx[cursorpoint:]))
  
problem_description: A Simple Offline Text Editor
  
  A text editor is a useful software tool that can help people in various
  situations including writing and programming. Your job in this problem is to
  construct an offline text editor, i.e., to write a program that first reads a
  given text and a sequence of editing commands and finally reports the text
  obtained by performing successively the commands in the given sequence.
  
  The editor has a text buffer and a cursor. The target text is stored in the
  text buffer and most editing commands are performed around the cursor. The
  cursor has its position that is either the beginning of the text, the end of
  the text, or between two consecutive characters in the text. The initial
  cursor position (i.e., the cursor position just after reading the initial
  text) is the beginning of the text.
  
  A text manipulated by the editor is a single line consisting of a sequence of
  characters, each of which must be one of the following: 'a' through 'z', 'A'
  through 'Z', '0' through '9', '.' (period), ',' (comma), and ' ' (blank). You
  can assume that any other characters never occur in the text buffer. You can
  also assume that the target text consists of at most 1,000 characters at any
  time. The definition of words in this problem is a little strange: a word is a
  non-empty character sequence delimited by not only blank characters but also
  the cursor. For instance, in the following text with a cursor represented as
  '^',
  
  He^llo, World.
  
  the words are the following.
  
  He
  
  llo,
  
  World.
  
  Notice that punctuation characters may appear in words as shown in this
  example.
  
  The editor accepts the following set of commands. In the command list, "_any-
  text_ " represents any text surrounded by a pair of double quotation marks
  such as "abc" and "Co., Ltd.".
  
  **Command** |  **Descriptions**  
  ---|---  
  forward char |  Move the cursor by one character to the right, unless the cursor is already at the end of the text.  
  forward word |  Move the cursor to the end of the leftmost word in the right. If no words occur in the right, move it to the end of the text.  
  backward char |  Move the cursor by one character to the left, unless the cursor is already at the beginning of the text.  
  backward word |  Move the cursor to the beginning of the rightmost word in the left. If no words occur in the left, move it to the beginning of the text.  
  insert "_any-text_ " |  Insert _any-text_ (excluding double quotation marks) at the position specified by the cursor. After performing this command, the new cursor position is at the end of the inserted text. The length of _any-text_ is less than or equal to 100.  
  delete char |  Delete the character that is right next to the cursor, if it exists.  
  delete word |  Delete the leftmost word in the right of the cursor. If one or more blank characters occur between the cursor and the word before performing this command, delete these blanks, too. If no words occur in the right, delete no characters in the text buffer.
samples: [{"input": "A sample input\n    9\n    forward word\n    delete char\n    forward word\n    delete char\n    forward word\n    delete char\n    backward word\n    backward word\n    forward word\n    Hallow, Word.\n    7\n    forward char\n    delete word\n    insert \"ello, \"\n    forward word\n    backward char\n    backward char\n    insert \"l\"\n    \n    3\n    forward word\n    backward word\n    delete word", "output": "Asampleinput^\n    Hello,  Worl^d.\n    ^"}]