import scala.annotation.tailrec
import scala.collection.mutable

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

  solve()
  @tailrec
  def solve(second: Boolean = false): Unit = {
    val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
    if (n == 0 && m == 0) return
    if (second) println()
    val menu = Array.tabulate(n) { rank ⇒
      val Array(name, limit, time) = readLine.trim.split(' ')
      (name, Menu(name, limit.toInt, time.toInt, rank))
    }.toMap

    val customers = Array.tabulate(m) { _ ⇒
      val order = readLine.trim.split(' ')
      new Customer(order.head.toInt, order.drop(2).map(menu))
    }

    val hall = new Hall(customers, menu.toArray.map(_._2))

    println(
      f(hall).mkString("\n")
    )
    solve(true)
  }
  @tailrec
  def f(hall: Hall, time: Int = 0): Array[Int] = {
    hall.nextCustomer() match {
      case None ⇒ hall.allCustomers.map(_.lastServeTime)
      case Some(c) if c.time <= time ⇒
        val menu = c.unServedMenu.head
        val waiting = hall.currentWaiting(menu, time)
        hall.serve(menu, math.min(waiting, menu.limit), time + menu.time)
        f(hall, time + menu.time)
      case Some(c) ⇒ f(hall, c.time)
    }
  }

  class Hall(private[this] val customers: Array[Customer], menu: Array[Menu]) {
    private[this] val waiting = mutable.Map[String, List[Customer]]().withDefaultValue(Nil).also{map ⇒
      customers.reverseIterator.foreach{c ⇒
        c.unServedMenu.foreach(m ⇒ map(m.name) ::= c)
      }
    }
    private[this] var firstIn: Int = 0
    def nextCustomer(): Option[Customer] = Some(firstIn).filter(customers.indices.contains).map(customers)
    def serve(menu: Menu, dish: Int, time: Int): Unit = {
      val (s, r) = waiting(menu.name).splitAt(dish)
      s.foreach(_.serve(menu, time))
      waiting(menu.name) = r
      while(firstIn < customers.length && customers(firstIn).isServeAll){
        firstIn += 1
      }
    }
    def currentWaiting(menu: Menu, time: Int): Int = {
      waiting(menu.name).takeWhile(_.time <= time).length
    }
    def allCustomers: Array[Customer] = customers
  }


  class Customer(val time: Int, order: Seq[Menu]) {
    private[this] var orders: List[Menu] = order.sorted.toList
    private[this] var lastServed = -1
    def serve(menu: Menu, servedTime: Int): Unit = {
      orders = orders.span(_ != menu) match {
        case (a, Nil) ⇒ a
        case (a, _::b) ⇒ a ::: b
      }
      lastServed = servedTime
    }
    def unServedMenu: Iterable[Menu] = orders
    def lastServeTime = lastServed
    def isNotServeAll: Boolean = !isServeAll
    def isServeAll: Boolean = orders.isEmpty
  }
  case class Menu(name: String, limit: Int, time: Int, rank: Int) extends Ordered[Menu] {
    override def compare(that: Menu): Int = if (time == that.time) rank.compare(that.rank) else that.time.compare(time)
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
    @inline
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}

