#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;
    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;
    bool operator<(const Event&e)const{
        if(e.time<this->time)return true;
        else if(e.time>this->time)return false;
        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++){
            Event e;
            e.kind=0;
            e.idx=i;
            e.time=T[i];
            pq.push(e);
        }
        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;
                ne.time=e.time;
                ne.kind=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;
                ne.time=e.time;
                ne.kind=2;
                pq.push(ne);
            }
            else{
                if(isCook||waitDish.size()==0)continue;
                isCook=true;
                Dish d=*(waitDish.begin());
                int lim=(waitDish.begin())->limit;
                Event ne;ne.kind=1;ne.dishIdx=d.dishIdx;
                ne.time=e.time+(waitDish.begin())->time;
                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: 1048