ENG  RUSTimus Online Judge
Online Judge
Online contests
About Online Judge
Frequently asked questions
Site news
Problem set
Submit solution
Judge status
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests

1085. Meeting

Time limit: 2.0 second
Memory limit: 64 MB
K friends has decided to meet in order to celebrate their victory at the programming contest. Unfortunately, because of the tickets rise in price there is a problem: all of them live in different parts of the city, and they are to choose a place of meeting so that they wouldn't pay too much for the tickets. You are to help them make the best choice.
All stops are enumerated with integers 1, …, N inclusive. There are M tram routes in the city (the friends take only trams and do not go on foot from stop to stop). For each route numbers of its stops are known. For each friend we know an amount of money he has and whether he has a month tram ticket. A ticket price equals 4 rubles.
You are to find out a stop number, such that all of the friends might come there and the sum of money they spend for their tramps would be minimal. Naturally, they may change routes (it means that each one may make changes on his way to the required stop). Note, that changing the route one has to pay for a new ticket: the friends are honest people — they do always pay for tickets. Everyone pays for a ticket from his own money. No one is to leave money for the return tickets.


The first line contains two integers N and M; 1 ≤ N, M ≤ 100 (N is a number of stops, M is a number of routes). The next M lines define the routes in the following sort: there is an integer L in the beginning of a line — that is an amount of stops of the corresponding route (2 ≤ L ≤ 100). Then L integers defining stops numbers of the route follow. The numbers are separated with a space. A route is defined by its stops along the line in one direction. The next line contains an integer K (1 ≤ K ≤ 100), that is an amount of friends. The next K lines contain information about each of them (one line for one person): there is a positive integer in the beginning of a line that is an amount of money (in rubles) the person has, then a number of a stop that he goes there from his home on foot, then 0 (if this person has no month ticket) or 1 (if he has). The numbers in a line are separated with a space. No one of the friends has more than 1000 rubles.


Output a number of a stop that is a meeting point (if there are several numbers choose the minimal one) and a total sum of money (in rubles) that the friends has paid for their trips to the appointed place. The numbers should be separated with a space. If the friends won't be able to meet at one stop, output the only number 0.


4 3
2 1 2
2 2 3
2 3 4
27 1 0
15 4 0
45 4 0
4 12
Problem Author: Alexander Somov
Problem Source: The 3rd high school children programming contest, USU, Yekaterinburg, Russia, March 4, 2001