import scala.io.StdIn._
object Main extends App {
  val d = readInt
  val singleMark = readInt match {
    case 0 ⇒ Nil
    case x ⇒
      Array.tabulate(x) { _ ⇒
        val Array(a, p) = readLine.trim.split(' ')
        a → p.toInt
      }.filter(_._1 == "D").map(_._2).sorted(Ordering.Int.reverse).toList
  }
  val doubleMark = readInt match {
    case 0 ⇒ Nil
    case y ⇒
      Array.tabulate(y) { _ ⇒
        val Array(bc, p) = readLine.trim.split(' ')
        bc → p.toInt
      }.filter {_._1 == "DD"}.map(_._2).sorted(Ordering.Int.reverse).toList
  }
  println(
    solve(singleMark, doubleMark, d)
  )

  def solve(single: List[Int], double: List[Int], count: Int): Int = {
    (single, double) match {
      case (a::as, b::bs) if count > 1 ⇒
        math.max(solve(as, double, count - 1) + a, solve(single, bs, count - 2) + b)
      case (a::as, _) if count > 0 ⇒
        solve(as, double, count - 1) + a
      case (Nil, b::bs) if count > 1 ⇒
        solve(single, bs, count - 2) + b
      case _ ⇒ 0
    }
  }
}

