Buggy python submission (verdict: Runtime Error):
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        temp=raw_input()
        cmd,obj=temp.replace('"',"").split()
        if cmd=="forward":
            if obj=="char":
                cur+=1
                cur=min(cur,len(s))
            else:
                while cur<len(s):
                    if s[cur]==" ":
                        break
                    cur+=1
        elif cmd=="backward":
            if obj=="char":
                cur-=1
                cur=max(cur,0)
            else:
                while cur>0:
                    cur-=1
                    if s[cur]==" ":
                        break
        elif cmd=="delete":
            if obj=="char":
                if cur!=len(s):
                    s=s[:cur]+s[cur+1:]
            else:
                end=s.find(" ",cur)
                if end==-1:
                    end=len(s)
                s=s[:cur]+s[end+1:]
        elif cmd=="insert":
            idx=temp.find('"')+1
            s=s[:cur]+temp[idx:-1]+s[cur:]
            cur=len(s[:cur]+temp[idx:-1])
    print s[:cur]+"^"+s[cur:]

Fixed submission:
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        cmd=raw_input()
        if cmd=="forward char":
            cur=min(cur+1,len(s))
        elif cmd=="forward word":
            while cur<len(s) and s[cur]==" ":
                cur+=1
            while cur<len(s) and s[cur]!=" ":
                cur+=1
        elif cmd=="backward char":
            cur=max(cur-1,0)
        elif cmd=="backward word":
            while cur>0 and s[cur-1]==" ":
                cur-=1
            while cur>0 and s[cur-1]!=" ":
                cur-=1
        elif cmd=="delete char":
            if cur!=len(s):
                s=s[:cur]+s[cur+1:]
        elif cmd=="delete word":
            temp=cur
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]==" ":
                temp+=1
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]!=" ":
                temp+=1
            s=s[:cur]+s[temp:]
        elif "insert" in cmd:
            idx1=cmd.find('"')
            idx2=cmd.find('"',idx1+1)
            s=s[:cur]+cmd[idx1+1:-1]+s[cur:]
            cur+=idx2-idx1-1
    print s[:cur]+"^"+s[cur:]

Buggy python submission (verdict: Runtime Error):
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        cmd=raw_input()
        if cmd=="forward char":
            cur=min(cur+1,len(s))
        elif cmd=="forward word":
            while cur<len(s) and s[cur]==" ":
                cur+=1
            while cur<len(s) and s[cur]!=" ":
                cur+=1
        elif cmd=="backward char":
            cur=max(cur-1,0)
        elif cmd=="backward word":
            while cur>0 and s[cur-1]==" ":
                cur-=1
            while cur>0 and s[cur-1]!=" ":
                cur-=1
        elif cmd=="delete char":
            if cur!=len(s):
                s=s[:cur]+s[cur+1:]
        elif cmd=="delete word":
            temp=cur
            if temp>=len(s):
                continue
            while s[temp]==" ":
                temp+=1
            if temp>=len(s):
                continue
            while s[temp]!=" " and temp<len(s):
                temp+=1
            s=s[:cur]+s[temp:]
        elif "insert" in cmd:
            idx1=cmd.find('"')
            idx2=cmd.find('"',idx1+1)
            s=s[:cur]+cmd[idx1+1:-1]+s[cur:]
            cur+=idx2-idx1-1
    print s[:cur]+"^"+s[cur:]

Fixed submission:
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        cmd=raw_input()
        if cmd=="forward char":
            cur=min(cur+1,len(s))
        elif cmd=="forward word":
            while cur<len(s) and s[cur]==" ":
                cur+=1
            while cur<len(s) and s[cur]!=" ":
                cur+=1
        elif cmd=="backward char":
            cur=max(cur-1,0)
        elif cmd=="backward word":
            while cur>0 and s[cur-1]==" ":
                cur-=1
            while cur>0 and s[cur-1]!=" ":
                cur-=1
        elif cmd=="delete char":
            if cur!=len(s):
                s=s[:cur]+s[cur+1:]
        elif cmd=="delete word":
            temp=cur
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]==" ":
                temp+=1
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]!=" ":
                temp+=1
            s=s[:cur]+s[temp:]
        elif "insert" in cmd:
            idx1=cmd.find('"')
            idx2=cmd.find('"',idx1+1)
            s=s[:cur]+cmd[idx1+1:-1]+s[cur:]
            cur+=idx2-idx1-1
    print s[:cur]+"^"+s[cur:]

Buggy python submission (verdict: Runtime Error):
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        cmd=raw_input()
        if cmd=="forward char":
            cur=min(cur+1,len(s))
        elif cmd=="forward word":
            while cur<len(s) and s[cur]==" ":
                cur+=1
            while cur<len(s) and s[cur]!=" ":
                cur+=1
        elif cmd=="backward char":
            cur=max(cur-1,0)
        elif cmd=="backward word":
            while cur>0 and s[cur-1]==" ":
                cur-=1
            while cur>0 and s[cur-1]!=" ":
                cur-=1
        elif cmd=="delete char":
            if cur!=len(s):
                s=s[:cur]+s[cur+1:]
        elif cmd=="delete word":
            temp=cur
            if temp>=len(s):
                continue
            while s[temp]==" ":
                temp+=1
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]!=" ":
                temp+=1
            s=s[:cur]+s[temp:]
        elif "insert" in cmd:
            idx1=cmd.find('"')
            idx2=cmd.find('"',idx1+1)
            s=s[:cur]+cmd[idx1+1:-1]+s[cur:]
            cur+=idx2-idx1-1
    print s[:cur]+"^"+s[cur:]

Fixed submission:
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        cmd=raw_input()
        if cmd=="forward char":
            cur=min(cur+1,len(s))
        elif cmd=="forward word":
            while cur<len(s) and s[cur]==" ":
                cur+=1
            while cur<len(s) and s[cur]!=" ":
                cur+=1
        elif cmd=="backward char":
            cur=max(cur-1,0)
        elif cmd=="backward word":
            while cur>0 and s[cur-1]==" ":
                cur-=1
            while cur>0 and s[cur-1]!=" ":
                cur-=1
        elif cmd=="delete char":
            if cur!=len(s):
                s=s[:cur]+s[cur+1:]
        elif cmd=="delete word":
            temp=cur
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]==" ":
                temp+=1
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]!=" ":
                temp+=1
            s=s[:cur]+s[temp:]
        elif "insert" in cmd:
            idx1=cmd.find('"')
            idx2=cmd.find('"',idx1+1)
            s=s[:cur]+cmd[idx1+1:-1]+s[cur:]
            cur+=idx2-idx1-1
    print s[:cur]+"^"+s[cur:]