#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>

using namespace std;

struct Dish{
    string name;
    int time;
    int limit;
    int orderTime;
    int dishIdx;
    int whois;
    int ordNum;
    Dish(){}
    Dish(string _name,int _time,int _limit,int _orderTime,int _dishIdx,int _whois,int _ordNum){
        name=_name;
        time=_time;
        limit=_limit;
        orderTime=_orderTime;
        dishIdx=_dishIdx;
        whois=_whois;
        ordNum=_ordNum;
    }
    bool operator<(const Dish&dh)const{
        if(dh.orderTime!=orderTime)return dh.orderTime>this->orderTime;
        else if(dh.time!=this->time)return dh.time<this->time;
        else if(dh.dishIdx!=this->dishIdx)return dh.dishIdx>this->dishIdx;
        else if(dh.whois!=this->whois)return dh.whois>this->whois;
        return dh.ordNum<this->ordNum;
    }
};
struct Event{
    int time;
    int idx;
    int dishIdx;
    int kind;
    vector<int> who;
    Event(){}
    Event(int _time,int _idx,int _dishIdx,int _kind){
        time=_time;
        idx=_idx;
        dishIdx=_dishIdx;
        kind=_kind;
    }
    bool operator<(const Event&e)const{
        if(e.time!=this->time)return e.time<this->time;
        return (e.kind<this->kind);
    }
};

set<Dish> waitDish;
int N,M;
Dish ds[101];
map<string,int> ms;
int T[101];
int K[101];
int leftDish[101];
int finTimes[101];
bool need[100001];
vector<string> order[101];

int main(){
    bool fst=true;
    while(cin>>N>>M&&(N|M)){
        if(fst)fst=false;
        else cout<<endl;
        waitDish.clear();
        ms.clear();
        for(int i=0;i<N;i++){
            cin>>ds[i].name>>ds[i].limit>>ds[i].time;
            ms[ds[i].name]=i;
            ds[i].dishIdx=i;
        }
        for(int i=0;i<M;i++){
            order[i].clear();
            cin>>T[i];
            cin>>K[i];
            leftDish[i]=K[i];
            for(int j=0;j<K[i];j++){
                string str;
                cin>>str;
                order[i].push_back(str);
            }
        }
        priority_queue<Event> pq;
        bool isCook=false;
        for(int i=0;i<M;i++){
            pq.push(Event(T[i],i,0,0));
        }
        while(pq.size()){
            Event e=pq.top();pq.pop();
            if(e.kind==0){
                for(int j=0;j<K[e.idx];j++){
                    int dk=ms[order[e.idx][j]];
                    Dish tmp=ds[dk];
                    tmp.whois=e.idx;
                    tmp.orderTime=e.time;
                    tmp.ordNum=j;
                    waitDish.insert(tmp);
                }
                Event ne=Event(e.time,0,0,2);
                pq.push(ne);
            }
            else if(e.kind==1){
                int dkind=e.idx;
                for(int i=0;i<e.who.size();i++){
                    int user=e.who[i];
                    leftDish[user]--;
                    if(leftDish[user]==0)
                        finTimes[user]=e.time;
                }
                isCook=false;
                Event ne=Event(e.time,0,0,2);
                pq.push(ne);
            }
            else{
                if(isCook||waitDish.size()==0)continue;
                isCook=true;
                Dish d=*(waitDish.begin());
                int lim=(waitDish.begin())->limit;
                Event ne=Event(e.time+(waitDish.begin())->time,0,d.dishIdx,1);
                vector<Dish> tmp;
                for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++)
                    tmp.push_back(*it);
                vector<int> idxs;
                for(int i=0;i<tmp.size();i++){
                    if(lim>0&&tmp[i].dishIdx==d.dishIdx){
                        ne.who.push_back(tmp[i].whois);
                        lim--;
                    }
                    else idxs.push_back(i);
                }
                waitDish.clear();
                for(int i=0;i<idxs.size();i++){
                    int cur=idxs[i];
                    waitDish.insert(tmp[cur]);
                }
                pq.push(ne);
            }
        }
         for(int i=0;i<M;i++)
            cout<<finTimes[i]<<endl;
    }
    return 0;
}

Measured memory_bytes: 0