Contaminated Milk

From drink and sickness logs, keep each milk every sick person drank before falling sick and report the largest drinker count.

Easy3SimulationBrute forceInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Farmer John is known for the quality of the milk his farm produces. Today he is hosting a milk tasting party for NN of his best friends (1N501 \le N \le 50). The party features MM types of milk (1M501 \le M \le 50), and exactly one of them has gone bad. Farmer John does not know which one. Anyone who drinks the bad milk gets sick, either during the rest of the party or after it ends.

You are given a transcript of the party. It records who drank which milk at which time, and who got sick at which time. From this record you can work out which milk types could be the bad one. Find the minimum number of doses of medicine Farmer John needs so that he can cure everyone who gets sick, whichever milk turns out to be the bad one. People who get sick after the party ends also need a dose.

Input

The first line contains the integers NN, MM, DD, and SS.

Each of the next DD lines (1D10001 \le D \le 1000) contains three integers pp, mm, tt, meaning that person pp drank milk mm at time tt. Here pp is between 11 and NN, mm is between 11 and MM, and tt is between 11 and 100100. A person may drink the same milk several times, and may drink several types of milk at the same time.

Each of the next SS lines (1SN1 \le S \le N) contains two integers pp, tt, meaning that person pp got sick at time tt. Here pp is between 11 and NN, and tt is between 11 and 100100. Each person gets sick at most once, and a person gets sick only because they drank the bad milk at a time strictly earlier than tt.

Output

Print one integer, the minimum number of doses of medicine Farmer John needs in order to treat everyone who gets sick, both during the party and after it.

Sample explanation

There are 3 people and 4 milk types. Person 1 gets sick at time 3 and person 2 gets sick at time 8. Person 3 does not get sick during the party, but may still get sick after it ends. Consider the milk types one at a time. A milk type can be the bad one when every person who got sick drank it before getting sick.

Milk 1: both sick people, 1 and 2, drank it before getting sick, so it can be the bad milk. Person 3 also drank it, so 3 people in total would get sick. Person 3 would get sick after the party.

Milk 2: both sick people drank it before getting sick, so it can be the bad milk as well. Nobody else drank it, so at worst 2 people would get sick.

Milk 3: person 1 did not drink it before getting sick, so it is not the bad milk. Person 1 drank it at time 4 and got sick at time 3. For milk 3 to be the cause, person 1 would have had to drink it by time 2.

Milk 4: person 2 never drank it and still got sick, so it is not the bad milk.

If milk 1 is the bad one, 3 people need treatment, so the answer is 3.