import java.io.*; import java.util.*; public class restaurant_yuizumi { private static boolean doCase(Scanner cin) throws Exception { int n = cin.nextInt(); int m = cin.nextInt(); if(n == 0 && m == 0) return false; Map menu = new HashMap(); for(int i = 0; i < n; i++) { String s = cin.next(); menu.put(s, new MenuItem(i, cin.nextInt(), cin.nextInt())); } List orders = new ArrayList(); for(int i = 0; i < m; i++) { int t = cin.nextInt(); int k = cin.nextInt(); for(int j = 0; j < k; j++) { orders.add(new Order(t, menu.get(cin.next()))); } Collections.sort(orders); } SortedMap delivTime = new TreeMap(); int t = 0; for(int i = 0; i < orders.size(); i++) { Order oi = orders.get(i); // removing elements during iteration is problematic // so we use a flag instead if(oi.hasCooked()) { continue; } t = Math.max(oi.time(), t); n = oi.item().n; for(int j = i; j < orders.size(); j++) { Order oj = orders.get(j); if(t < oj.time()) { break; } if(n < 1) { break; } if(oi.item() != oj.item()) { continue; } oj.cook(); delivTime.put(oj.time(), t + oi.item().c); --n; } t += oi.item().c; } for(Map.Entry e : delivTime.entrySet()) { System.out.println(e.getValue()); } return true; } public static void main(String[] args) throws Exception { Scanner cin = new Scanner(System.in); while(doCase(cin)) ; } private static class MenuItem { public final int i; // index public final int n; // limit public final int c; // time public MenuItem(int i, int n, int c) { this.i = i; this.n = n; this.c = c; } } private static class Order implements Comparable { private final int t; private final MenuItem m; private boolean h; public Order(int t, MenuItem m) { this.t = t; this.m = m; this.h = false; } public int compareTo(Object o) { Order that = (Order)(o); if(this.t < that.t ) { return -1; } if(this.t > that.t ) { return +1; } if(this.m.c > that.m.c) { return -1; } if(this.m.c < that.m.c) { return +1; } if(this.m.i < that.m.i) { return -1; } if(this.m.i > that.m.i) { return +1; } return 0; } public int time() { return t; } public MenuItem item() { return m; } public void cook() { h = true; } public boolean hasCooked() { return h; } } }