Ticket Swapping (Small)

Given rider groups traveling between stations on one line, compute the largest fare loss from riders swapping entry cards, modulo 1000002013.

Medium7GreedyStackMathNo attempts yetTime limit5sMemory limit512 MB

Problem

The city opened its first subway line. It has NN stations, and fares are charged with entry cards.

A passenger takes an entry card when boarding. The card records the station where the passenger boarded. To leave the subway the passenger hands the card back and pays according to the distance, counted in stations traveled, between the station written on the card and the station where the card is handed back.

  • If the two stations are the same, the fare is 0 pounds.
  • If they are adjacent, the fare is NN pounds.
  • If the distance is two stations, the fare is 2N12N - 1 pounds: NN for the first station and N1N - 1 for the second.
  • The third station costs N2N - 2, so a three station ride costs 3N33N - 3. The fourth station costs N3N - 3, and the iith station costs N+1iN + 1 - i.
  • Riding from one end of the line to the other covers N1N - 1 stations. The last of them costs 2 pounds, and the whole ride costs N2+N22\frac{N^2 + N - 2}{2} pounds.

After the system started, the city found its revenue smaller than expected. Passengers were swapping entry cards. Suppose one passenger boards at station AA, rides two stations and leaves at BB, while another boards at BB, rides three stations and leaves at CC. Together they would pay 2N1+3N3=5N42N - 1 + 3N - 3 = 5N - 4. If the two swap cards at station BB, the first one hands back a card that reads BB while leaving station BB, so the recorded distance is 0 and the ride is free. The second one hands back a card that reads AA while leaving station CC, a distance of 5 stations, and pays 5N105N - 10. The city loses 6 pounds.

The city wants to know how much it can lose if this becomes common. Consider only one direction of the line, from station 1 to station NN through every station in order, and only one train. A passenger who rides from station oo to station ee receives an entry card at oo, and can swap that card any number of times with any other passenger anywhere between oo and ee, including passengers who leave at oo and passengers who board at ee. The passenger then leaves the train at ee and hands back some card. Handing back a card is required in order to leave. A passenger never gets off the train in between, so nobody hands back a card and takes a new one during a ride.

You are given a traffic table that says how many passengers ride from which station to which. Compute the money the city loses when passengers swap cards to make that loss as large as possible.

Input

The first line has the number of test cases TT. Each test case begins with a line holding the number of stations NN and the number of records MM. Stations are numbered 1 through NN. Each of the next MM lines has three integers oio_i, eie_i and pip_i, meaning that pip_i passengers ride from station oio_i to station eie_i.

Limits

  • 1T201 \le T \le 20
  • 2N1002 \le N \le 100
  • 1M1001 \le M \le 100
  • 1oi<eiN1 \le o_i < e_i \le N
  • 1pi1001 \le p_i \le 100

The same pair (oi,ei)(o_i, e_i) can appear on more than one line.

Output

For each test case print one line in the form "Case #x: y", where xx is the test case number starting from 1 and yy is the largest loss the city can suffer from card swapping, modulo 1000002013.

Sample explanation

In the first test case of the example, the two passengers meet at station 3 and swap cards, which is the situation described in the statement. In the second test case the two passengers never meet, so they cannot swap and the city loses nothing. In the third test case only one of the two earlier passengers can swap with the later passenger.