import scala.annotation.tailrec
import scala.io.StdIn._
object Main extends App {

  val n = readInt
  for (i ← 0 until n){
    if (i != 0) println()
    val expr = Array.tabulate(readInt){_ ⇒
      readLine.trim.split(' ').toList
    }.foldRight(Variable: Term){
      case ("R"::_::operator, highPriority) ⇒ new RightJoin(operator.mkString.toList, highPriority)
      case ("L"::_::operator, highPriority) ⇒ new LeftJoin(operator.mkString.toList, highPriority)
      case _ ⇒ ???
    }
    for (_ ← 0 until readInt){
      println(expr.parse(readLine.trim.toList))
    }
  }
  case class ParseTarget(str: List[Char], term: Term)
  sealed trait Term {
    def parse(str: List[Char]): Expression = {
      val t = this
      ParseTarget(str, this) match {
        case t(e, ParseTarget(Nil, _)) ⇒ e
        case _ ⇒ ???
      }
    }
    def unapply(str: ParseTarget): Option[(Expression, ParseTarget)]
  }
  sealed trait Expression
  class Variable(char: Char) extends  Expression {
    override def toString: String = char.toString
  }
  class LeftJoinOperator(left: Expression, operator: Char, right: Expression) extends Expression {
    override def toString: String = s"($left$operator$right)"
  }
  class RightJoinOperator(left: Expression, operator: Char, right: Expression) extends Expression {
    override def toString: String = s"($left$operator$right)"
  }
  object Variable extends Term {
    override def unapply(str: ParseTarget): Option[(Expression, ParseTarget)] = {
      str match {
        case ParseTarget(h::t, e) if h.isLetter ⇒ Some(new Variable(h), ParseTarget(t, e))
        case ParseTarget('('::s, e) ⇒ ParseTarget(s, e) match {
          case e(exp, ParseTarget(')'::rest, _)) ⇒ Some(exp, ParseTarget(rest, e))
          case _ ⇒ ???
        }
        case _ ⇒ None
      }
    }
  }
  class LeftJoin(private val operator: List[Char], private val higherOperator: Term) extends Term {
    override def unapply(str: ParseTarget): Option[(Expression, ParseTarget)] = {
      str match {
        case higherOperator(term, rest) ⇒ Some(takeAll(rest, term))
        case _ ⇒ None
      }
    }
    @tailrec private[this] def takeAll(str: ParseTarget, left: Expression): (Expression, ParseTarget) = {
      str match {
        case ParseTarget(h::t, e) if operator.contains(h) ⇒ ParseTarget(t, e) match {
          case higherOperator(right, rest) ⇒ takeAll(rest, new LeftJoinOperator(left, h, right))
          case _ ⇒ ???
        }
        case _ ⇒ (left, str)
      }
    }
  }
  class RightJoin(private val operator: List[Char], private val higherOperator: Term) extends Term {
    override def unapply(str: ParseTarget): Option[(Expression, ParseTarget)] = {
      str match {
        case higherOperator(term, rest) ⇒ Some(takeAll(rest, term))
        case _ ⇒ None
      }
    }
    @tailrec private[this] def takeAll(str: ParseTarget, left: Expression, result: List[(Expression, Char)] = Nil): (Expression, ParseTarget) = {
      str match {
        case ParseTarget(h::t, e) if operator.contains(h) ⇒ ParseTarget(t, e) match {
          case higherOperator(right, rest) ⇒ takeAll(rest, right, (left, h)::result)
          case _ ⇒ ???
        }
        case _ ⇒ (result.foldLeft(left){case (r, (l, o)) ⇒ new RightJoinOperator(l, o, r)}, str)
      }
    }
  }
}
