The mobile network market in Byteland is dominated by two large corporations, Byteland Telecom and Byteland Mobile. The central government has recently realized that radio-frequency spectrum is a scarce resource and wants to regulate its usage. The spectrum currently in use is divided into 1000000 channels. Any wireless service provider who wishes to use part of the spectrum must apply for licenses on these channels. While some services may require multiple channels, a single channel cannot be shared by different services.
The government wants to maximize its revenue from the spectrum by auctioning the channels. The only two bidders are Byteland Telecom and Byteland Mobile. Each may place bids on combinations of channels through which their services communicate with customers. A company may place at most one bid on any specific channel.
The government can accept only a subset of the bids, chosen so that no two accepted bids conflict (share a channel). Determining which bids to accept in order to maximize revenue turned out to be difficult, so the officials are asking for your help.
Write a program that reads the bids of Byteland Telecom and Byteland Mobile and computes the maximum revenue the government can achieve.
The input consists of two bid sections, the first for Byteland Telecom and the second for Byteland Mobile.
Each section starts with an integer n (1≤n≤500), the number of bids that follow. Each of the next n lines describes one bid: the first integer p (1≤p≤1000) is the price of the bid, the second integer m (1≤m≤1000000) is the number of channels in the bid, followed by the m channel numbers in increasing order. Every channel number is an integer in the range 1…1000000. No two bids of the same company contain the same channel.
Print a single integer — the maximum revenue the government can collect by issuing licenses on the channels.
For the given input, the maximum revenue is obtained by accepting the first, second, and fourth bids of Byteland Telecom together with the third bid of Byteland Mobile.