Zak Galou

Find the cheapest path from room 1 to room N in an undirected graph, where entering or leaving a room costs the minimum mana to kill all monsters living there.

Medium6GraphShortest pathGreedyMathNo attempts yetTime limit2sMemory limit512 MB

Problem

Zak Galou is a famous wizard who hunts monsters. Legend says a cave hidden deep in the jungle holds a treasure that is thousands of years old. No adventurer has recovered it, because terrible monsters guard it well. Zak Galou is not an ordinary adventurer, and he has started preparing to take the treasure.

Zak Galou has a certain amount of mana, a kind of magical energy, and a list of MM spells. Every monster has a fixed number of hit points. Each time Zak casts a spell at a monster, he spends the mana cost of that spell and deals the damage of that spell to the monster. A monster that takes damage loses that many hit points. A monster is dead once its hit points are 00 or less. Zak always fights one monster at a time. He is a powerful wizard, so he can cast the same spell as many times as he wants while he still has the mana it needs.

His research got Zak Galou the treasure map. The cave is given as a set of rooms connected by galleries. The rooms are numbered from 11 to NN. Zak always starts in room 11 and the treasure is always in room NN. There are KK monsters, numbered from 11 to KK. Each monster lives in one room and never leaves it, and several monsters can live in the same room. Zak can leave a room, or pick up the treasure of a room, only while the room is empty, that is, while no monster is in it. In other words, before he leaves a room or takes the treasure of a room, he must kill every monster living there.

Given the spells, the monsters and the cave, work out the smallest amount of mana Zak Galou needs at the start to recover the treasure.

Input

The input holds several test cases. The first line of each test case has four integers MM, NN, GG and KK, in this order the number of spells (1M10001 \le M \le 1000), of rooms (1N10001 \le N \le 1000), of galleries (0G10000000 \le G \le 1000000) and of monsters (0K10000 \le K \le 1000).

Each of the next MM lines describes one spell with two integers, the amount of mana it consumes (from 11 to 10001000) and the damage it deals (also from 11 to 10001000).

The next GG lines describe one gallery each with two integers AA and BB (ABA \ne B), the rooms that the gallery connects. Zak can use a gallery in both directions, so he can go from AA to BB and from BB to AA.

The last KK lines of a test case describe one monster each with two integers, the room it lives in (from 11 to NN) and its initial number of hit points (from 11 to 10001000).

The end of the input is marked by a line with M=N=G=K=0M = N = G = K = 0, which is not processed.

Output

For each test case print one line with one integer, the minimum initial amount of mana. If the treasure cannot be recovered, print -1.