fun main(args:Array<String>):Unit{
    repeat(readLine()!!.toInt()){
        val buffer = TextBuffer(readLine()!!.trim())
        repeat(readLine()!!.toInt()){
            val order = readLine()!!.trim()
            when(order) {
                "forward char" ->
                    buffer.forwardChar()
                "forward word" ->
                    buffer.forwardWord()
                "backward char" ->
                    buffer.backwardChar()
                "backward word" ->
                    buffer.backwardWord()
                "delete char" ->
                    buffer.deleteChar()
                "delete word" ->
                    buffer.deleteWord()
                else ->
                    buffer.insertText("\".*\"".toRegex().find(order)!!.value)
            }
        }
        println(buffer)
    }
}
class TextBuffer(text:String){
    private var mCursor:LinkedText = Text(text)
    fun forwardChar():Unit {
        mCursor = mCursor.forwardChar()
    }
    fun forwardWord():Unit {
        mCursor = mCursor.forwardWord()
    }
    fun backwardChar():Unit {
        mCursor = mCursor.backwardChar()
    }
    fun backwardWord():Unit {
        mCursor = mCursor.backwardWord()
    }
    fun insertText(text:String){
        val target = text.removePrefix("\"").removeSuffix("\"")
        if (!target.isEmpty())
            mCursor = mCursor.insertText(target)
    }
    fun deleteChar():Unit {
        mCursor = mCursor.deleteChar()
    }
    fun deleteWord():Unit {
        mCursor = mCursor.deleteWord()
    }
    override fun toString(): String = mCursor.toString()
}
interface LinkedText {
    fun forwardChar(): LinkedText
    fun forwardWord(): LinkedText
    fun backwardChar(): LinkedText
    fun backwardWord(): LinkedText
    fun insertText(text: String): LinkedText
    fun deleteChar(): LinkedText
    fun deleteWord(): LinkedText
}
object Text {
    operator fun invoke(text:String):LinkedText {
        return if (text.isEmpty()){
            val start = TextStart(Dummy)
            val end = TextEnd(Dummy)
            start.next = end
            end.prev = start
            start
        }else {
            val (first, last) = text.toLinkedText()
            val start = TextStart(first)
            val end = TextEnd(last)
            first.prev = start
            last.next = end
            start
        }
    }
    private interface StrictLinkedText:LinkedText{
        override fun forwardChar(): ForwardText
        override fun forwardWord(): ForwardText
        override fun deleteChar(): ForwardText
        override fun deleteWord(): ForwardText
        override fun backwardChar(): BackwardText
        override fun backwardWord(): BackwardText
        override fun insertText(text: String): ForwardText
    }
    private interface BackwardText : StrictLinkedText {
        var next: ForwardText
        val isWordSeparator:Boolean
        fun toTextPrev():String
        fun backwardSub(wordExplored:Boolean = false):BackwardText
    }
    private interface ForwardText : StrictLinkedText {
        var prev: BackwardText
        fun forwardSub(wordExplored:Boolean = false):ForwardText
        fun toTextNext():String
        override fun insertText(text: String): ForwardText {
            val (first, last) = text.toLinkedText()
            prev.next = first
            first.prev = prev
            last.next = this
            prev = last
            return this
        }
    }
    private class InTextCursor(private val mRightChar:Char, override var next: ForwardText, override var prev: BackwardText):BackwardText, ForwardText {
        override fun backwardSub(wordExplored: Boolean): BackwardText {
            return when{
                wordExplored && prev.isWordSeparator -> this
                else -> prev.backwardSub(!prev.isWordSeparator)
            }
        }
        override fun forwardSub(wordExplored: Boolean): ForwardText {
            return when{
                wordExplored && isWordSeparator -> this
                else -> next.forwardSub(!isWordSeparator)
            }
        }
        override val isWordSeparator: Boolean = mRightChar == ' '
        override fun toTextPrev(): String = "${prev.toTextPrev()}$mRightChar"
        override fun toTextNext():String = "$mRightChar${next.toTextNext()}"
        override fun forwardChar(): ForwardText = next
        override fun forwardWord(): ForwardText = forwardSub()
        override fun backwardChar(): BackwardText = prev
        override fun backwardWord(): BackwardText = backwardSub()
        override fun deleteChar(): ForwardText {
            prev.next = next
            next.prev = prev
            return next
        }
        override fun deleteWord(): ForwardText {
            val wordEnd = forwardWord()
            return when {
                wordEnd.backwardChar().isWordSeparator -> this
                else -> {
                    prev.next = wordEnd
                    wordEnd.prev = prev
                    wordEnd
                }
            }
        }
        override fun toString(): String = "${prev.toTextPrev()}^$mRightChar${next.toTextNext()}"
    }
    private class TextStart(override var next:ForwardText) : BackwardText {
        override fun backwardSub(wordExplored: Boolean): BackwardText = this
        override val isWordSeparator: Boolean
            get() = true
        override fun forwardChar(): ForwardText = next.forwardChar()
        override fun forwardWord(): ForwardText = next.forwardWord()
        override fun deleteChar(): ForwardText {
            next = next.deleteChar()
            return next
        }
        override fun deleteWord(): ForwardText {
            next = next.deleteWord()
            return next
        }
        override fun toTextPrev(): String = ""
        override fun backwardChar(): BackwardText = this
        override fun backwardWord(): BackwardText = this
        override fun insertText(text: String): ForwardText = next.insertText(text)
        override fun toString(): String = "^${next.toTextNext()}"
    }
    private class TextEnd(override var prev:BackwardText) : ForwardText {
        override fun forwardSub(wordExplored: Boolean): ForwardText = this
        override fun forwardChar(): ForwardText = this
        override fun forwardWord(): ForwardText = this
        override fun deleteChar(): ForwardText = this
        override fun deleteWord(): ForwardText = this
        override fun backwardChar(): BackwardText = prev
        override fun backwardWord(): BackwardText = prev.backwardSub(!prev.isWordSeparator)
        override fun toTextNext(): String = ""
        override fun toString(): String = "${prev.toTextPrev()}^"
    }
    private object Dummy:ForwardText, BackwardText{
        override fun backwardSub(wordExplored: Boolean): BackwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun forwardSub(wordExplored: Boolean): ForwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override val isWordSeparator: Boolean
            get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.

        override fun forwardChar(): ForwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun forwardWord(): ForwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun deleteChar(): ForwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun deleteWord(): ForwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun backwardChar(): BackwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun backwardWord(): BackwardText {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override var next: ForwardText
            get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
            set(value) {}

        override fun toTextPrev(): String {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override var prev: BackwardText
            get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
            set(value) {}

        override fun toTextNext(): String {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    }
    private val wordSeparator = listOf(' ')
    private fun Char.isWordSeparator(): Boolean = wordSeparator.contains(this)
    private fun String.toLinkedText(): Pair<InTextCursor, InTextCursor> {
        val result = this.map { InTextCursor(it, Dummy, Dummy) }.toTypedArray()
        return if (result.size != 1){
            for (i in 1 until result.size){
                result[i - 1].next = result[i]
                result[i].prev = result[i - 1]
            }
            Pair(result.first(), result.last())
        }else{
            Pair(result.first(), result.first())
        }
    }
}
