Svada – Kattis, Kattis (original) (raw)

Hide

The local zoo has acquired a large open garden in which animals may freely move as in their natural habitats and entertain visitors with their usual shenanigans.

The most popular animals are monkeys. With their climbing and jumping and other skills, they delight old and young visitors alike.

One species of monkey has specialized in climbing tall trees and picking off coconuts. Another species has specialized in breaking them open.

There are NNN monkeys of the first type (numbered 1 through NNN) and MMM monkeys of the second type (numbered 1 through MMM).

Monkey kkk of the first type takes AkA_ kAk seconds to find a good spot on the tree, after which it picks off its first coconut. After that the monkey produces a new coconut every BkB_ kBk seconds.

Monkey kkk of the second type takes CkC_ kCk seconds to find a good tool for opening the coconuts, after which it opens its first coconut. After that the monkey opens another coconut every DkD_ kDk seconds.

Unfortunately, the second type of monkey is extremely aggressive so the two types may not be in the garden at the same time. Therefore, zoo keepers will chase away the first type of monkeys as soon as they have picked off all the coconuts. Similarly, if monkeys of the same type stay too long after opening all the coconuts, fights will ensue. Because of that, zoo keepers will send them away as soon as they have opened all the coconuts.

The zoo keepers first arrive immediately after all coconuts have been picked, and again immediately after the monkeys open them all. The time needed for monkeys to enter or leave the garden is also negligibly small.

Tomislav especially likes the second type of monkey, but can never guess when to arrive in order to see them. Help him calculate the time when the second type arrives if he knows the total time that monkeys spent in the garden, but does not know the number of coconuts in the garden.

Input

The first line contains the integer TTT (1leqTleq1,000,000,000)(1 \leq T \leq 1\, 000\, 000\, 000)(1leqTleq1,000,000,000), the total time that monkeys spent in the garden, in seconds.

The next line contains the integer NNN (1leqNleq100)(1 \leq N \leq 100)(1leqNleq100), the number of monkeys of the first type. Each of the following NNN lines contains two integers$A_ k$ and BkB_ kBk (1leqAk,Bkleq1,000,000,000)(1 \leq A_ k, B_ k \leq 1\, 000\, 000\, 000)(1leqAk,Bkleq1,000,000,000), how fast monkey kkk of the first type is.

The next line contains the integer MMM (1leqMleq100)(1 \leq M \leq 100)(1leqMleq100), the number of monkeys of the second type. Each of the following MMM lines contains two integers$C_ k$ and DkD_ kDk (1leqCk,Dkleq1,000,000,000)(1 \leq C_ k, D_ k \leq 1\, 000\, 000\, 000)(1leqCk,Dkleq1,000,000,000), how fast monkey kkk of the second type is.

Output

Output the number of seconds between the arrival of the first type of monkeys and the arrival of the second type.

Sample Input 1 Sample Output 1
12 1 3 1 1 5 1 5
Sample Input 2 Sample Output 2
20 2 3 2 1 3 3 3 1 4 1 5 1 13

Please log in to submit a solution to this problem

Log in

You haven't made any submissions on this problem yet.