#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 20;
const int MAX_M = 100;

/* typedef */

typedef vector<int> vi;
typedef set<int> si;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

struct Menu {
  int id, limit, time;
  string name;
  bool operator<(const Menu& m0) const {
    return time > m0.time || (time == m0.time && id < m0.id);
  }
  void print() {
    printf("Menu: id=%d, limit=%d, time=%d, name=%s\n",
	   id, limit, time, name.c_str());
  }
};

struct Order {
  int id, t, k, mnums[MAX_N];
  Order() { memset(mnums, 0, sizeof(mnums)); }
  void print() {
    printf("Order: id=%d, t=%d, k=%d: ", id, t, k);
    for (int i = 0; i < MAX_N; i++) {
      if (i > 0) putchar(',');
      printf("%d", mnums[i]);
    }
    putchar('\n');
  }
};

enum { C_IDLE, C_COOK, C_WAIT };
enum { E_ORDER, E_CFIN, E_CST};

struct Event {
  int t, ev, oid;
  Event() {}
  Event(int _t, int _ev, int _oid) : t(_t), ev(_ev), oid(_oid) {}
  bool operator>(const Event& e0) const {
    return t > e0.t || (t == e0.t && ev > e0.ev);
  }
  void print() { printf("Event: t=%d, ev=%d, oid=%d\n", t, ev, oid); }
};

/* global variables */

int n, m;
Menu menu[MAX_N];
Order orders[MAX_M];
int fts[MAX_M], omsums[MAX_N];

/* subroutines */

/* main */

int main() {
  for (bool first = true;; first = false) {
    cin >> n >> m;
    if (n == 0) break;

    for (int i = 0; i < n; i++) {
      cin >> menu[i].name >> menu[i].limit >> menu[i].time;
      menu[i].id = i;
    }
    sort(menu, menu + n);
    //for (int i = 0; i < n; i++) menu[i].print();
    
    for (int i = 0; i < m; i++) {
      cin >> orders[i].t >> orders[i].k;
      orders[i].id = i;
      for (int j = 0; j < orders[i].k; j++) {
	string mname;
	cin >> mname;
	for (int mi = 0; mi < n; mi++)
	  if (menu[mi].name == mname) {
	    orders[i].mnums[mi]++;
	    break;
	  }
      }
    }
    //for (int i = 0; i < m; i++) orders[i].print();

    priority_queue<Event, vector<Event>, greater<Event> > q;
    
    for (int i = 0; i < m; i++)
      q.push(Event(orders[i].t, E_ORDER, i));

    int cstat = C_IDLE, cmenu, cnum;
    vpii clist;
    si olist;

    memset(omsums, 0, sizeof(omsums));

    while (! q.empty()) {
      Event e = q.top();
      q.pop();
      //e.print();

      // order arrived
      if (e.ev == E_ORDER) {
	olist.insert(e.oid);

	for (int i = 0; i < n; i++)
	  omsums[i] += orders[e.oid].mnums[i];
	
	if (cstat == C_IDLE) {
	  cstat = C_WAIT;
	  q.push(Event(e.t, E_CST, -1));
	}
      }

      // finish cooking
      else if (e.ev == E_CFIN) {
	for (int i = 0; i < clist.size(); i++) {
	  int oid = clist[i].first;
	  int num = clist[i].second;
	  orders[oid].mnums[cmenu] -= num;
	  orders[oid].k -= num;
	  if (orders[oid].k == 0) {
	    olist.erase(oid);
	    fts[oid] = e.t;
	  }
	}

	omsums[cmenu] -= cnum;
	
	cstat = C_IDLE;
	if (! olist.empty()) {
	  cstat = C_WAIT;
	  q.push(Event(e.t, E_CST, -1));
	}
      }

      // start cooking
      else if (e.ev == E_CST) {
	if (! olist.empty()) {
	  int oid = *(olist.begin());
	  for (cmenu = 0; cmenu < n; cmenu++)
	    if (orders[oid].mnums[cmenu] > 0) break;

	  cnum = omsums[cmenu];
	  if (cnum > menu[cmenu].limit) cnum = menu[cmenu].limit;

	  clist.clear();
	  int num = cnum;

	  for (si::iterator oit = olist.begin();
	       num > 0 && oit != olist.end(); oit++) {
	    int oid = *oit;
	    int onum = orders[oid].mnums[cmenu];
	    if (onum > 0) {
	      if (num > onum) {
		clist.push_back(pii(oid, onum));
		num -= onum;
	      }
	      else {
		clist.push_back(pii(oid, num));
		num = 0;
	      }
	    }
	  }

	  cstat = C_COOK;
	  q.push(Event(e.t + menu[cmenu].time, E_CFIN, -1));
	}
	else {
	  cstat = C_IDLE;
	}
      }
    }

    if (! first) putchar('\n');
    
    for (int i = 0; i < m; i++) cout << fts[i] << endl;
  }

  return 0;
}

Measured memory_bytes: 1240