#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct Menu {
  int limit, time;
  int id;
  Menu(int limit, int time, int id) : limit(limit), time(time), id(id) {}
  Menu() {}
};
bool operator<(const Menu &a, const Menu &b) {
  return a.time != b.time ? a.time > b.time : a.id < b.id;
};

struct Order {
  int T;
  Menu menu;
  int id;
  Order(int T, Menu menu, int id) : T(T), menu(menu), id(id) {}
  Order() {}
};
bool operator<(const Order &a, const Order &b) {
  return a.T != b.T ? a.T < b.T : a.menu < b.menu;
}

Menu menu[20];
Order order[1000];
int ok[1000];                   // »Ì¶ðoµIíÁ½Ô
int K[100];
int endTime[100][10];

int main() {
  int n, m;
  int hoge = 0;
  while(cin >> n >> m, n || m) {
    if (hoge++) cout << endl;
    map<string, int> mp;
    REP(i, n) {
      string name;
      cin >> name >> menu[i].limit >> menu[i].time;
      menu[i].id = i;
      mp[name] = i;
    }
    int endnum[m];
    int num = 0;
    REP(i, m) {
      int T;
      cin >> T;
      cin >> K[i];
      REP(j, K[i]) {
        string dish;
        cin >> dish;
        order[num] = Order(T, menu[mp[dish]], i);
        num++;
      }
    }
    sort(order, order+num);
    ll nowtime = 0;
    memset(ok, -1, sizeof(ok));
    REP(i, num) {
      if (ok[i] >= 0) continue;
      nowtime = max(nowtime, (ll)order[i].T);
      //cout << i << " " << nowtime << " " << order[i].id << " " << order[i].menu.time << " " << order[i].menu.id << endl;
      ok[i] = nowtime + order[i].menu.time;
      int cnt = 1;
      for (int j=i+1; j<num && order[j].T<=nowtime && cnt<order[i].menu.limit; ++j) {
        if (order[j].menu.id == order[i].menu.id) {
          //cout << j << endl;
          ok[j] = nowtime + order[i].menu.time;
          cnt++;
        }
      }
      nowtime += order[i].menu.time;
    }
    int ans[m];
    memset(ans, 0, sizeof(ans));
    REP(i, num) {
      ans[order[i].id] = max(ans[order[i].id], ok[i]);
    }
    REP(i, m)
      cout << ans[i] << endl;
  }
}