This page is still under construction.

Parts of this page are still being built. What you see may change.

Advertising Revenue

Time limit2sMemory limit512 MB

Summary
For each data set, sum the payments for every display ad shown and every clickthrough ad clicked, then print the total.
Level

Easy2 of 10

Topics
Implementation, Simulation
Solved
No attempts yet

Problem

One motivation for producing fake news stories is money. The site earns from ads that are shown to its visitors or clicked on by them. Mysterious, surprising, or scandalous stories attract a lot of readers, which is why fake news headlines are often outrageous and vague ("You won't believe ...").

Online ads usually come in two forms. For a display ad, the advertiser pays the site just for showing the ad. The idea is to build brand recognition. For a clickthrough ad, the advertiser pays only when a visitor clicks the ad. The idea is that some fraction of clicks leads to purchases, now or later. Prices for online ads run from a few cents per display or click up to $100 per click. (The advertisers who pay $100 per click are class-action lawyers.)

You are given a list of ads, both clickthrough and display, together with a history of visitors that records what each visitor saw and what each visitor clicked. Calculate how much money the fake news site made.

Input

The first line contains the number KK of data sets in the file, where K≥1K \ge 1. Then come KK data sets of the following form.

The first line of each data set contains two integers nn, vv. Here 2≤n≤10002 \le n \le 1000 is the number of ads the site has as customers, and 0≤v≤10000 \le v \le 1000 is the number of visitors to the site.

The next nn lines each describe one ad ii with two integers did_i, pip_i. The value did_i is either 0 or 1. A value of 1 means the ad is a display ad, so the site gets paid just for showing it. A value of 0 means the ad is a clickthrough ad, so the site gets paid only when a visitor clicks it. 1≤pi≤10001 \le p_i \le 1000 is the payment for the ad, paid for each display of a display ad and for each click on a clickthrough ad.

The next vv lines each describe one visitor jj with three integers aj,1a_{j,1}, aj,2a_{j,2}, cjc_j. The first two numbers are the two ads the visitor is shown. They are distinct and lie between 1 and nn, inclusive. The number cj∈{0,1,2}c_j \in \{0, 1, 2\} is the ad the visitor clicked on. A value of 0 means the visitor clicked nothing, 1 means the visitor clicked aj,1a_{j,1}, and 2 means the visitor clicked aj,2a_{j,2}. A visitor may click a display ad, and the site gets no extra payment for that.

Output

For each data set, first output Data Set x: on a line by itself, where xx is its number. On the next line, output the total payment that the site receives. Follow each data set with a blank line.

Examples6

  1. Example 1

    Input
    2
    3 3
    1 10
    1 15
    0 100
    1 2 0
    2 3 2
    1 3 0
    2 3
    1 9
    0 13
    1 2 0
    1 2 1
    1 2 2
    
    Expected output
    Data Set 1:
    150
    
    Data Set 2:
    40
    
  2. Example 2

    Input
    1
    2 0
    0 1
    1 1
    
    Expected output
    Data Set 1:
    0
    
    
  3. Example 3

    Input
    1
    4 3
    0 1000
    0 999
    0 500
    0 1
    1 2 0
    3 4 0
    2 4 0
    
    Expected output
    Data Set 1:
    0
    
    
  4. Example 4

    Input
    1
    3 4
    1 7
    1 11
    1 13
    1 2 1
    1 2 2
    2 3 1
    1 3 2
    
    Expected output
    Data Set 1:
    80
    
    
  5. Example 5

    Input
    1
    2 4
    0 250
    0 375
    1 2 1
    1 2 2
    2 1 1
    2 1 2
    
    Expected output
    Data Set 1:
    1250
    
    
  6. Example 6

    Input
    4
    2 1
    1 1000
    0 1000
    1 2 2
    2 0
    1 5
    0 5
    3 2
    0 10
    1 20
    0 30
    1 3 1
    2 3 2
    2 1
    1 1
    1 1
    2 1 0
    
    Expected output
    Data Set 1:
    2000
    
    Data Set 2:
    0
    
    Data Set 3:
    60
    
    Data Set 4:
    2