0.SI. Marble Cut


Submit solution

Points: 3 (partial)
Time limit: 1.0s
Memory limit: 98M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, C, C++, C11, CLANG, CLANGX, Classical, COBOL, Coffee, CSC, D lang, DART, F95, FORTH, Fortrn, GAS32, GO, Haskell, Itercal, Java, kotlin, LEAN, LISP, LUA, MONOVB, Nasm, OCAML, Pascal, Perl, php, PIKE, prolog, Pypy, Python, Ruby 2, RUST, Scala, SCM, SED, SWIFT, TCL, TUR, V8JS, VB, ZIG

Famous sculptor Phong is making preparations to build a marvelous monument. For this purpose he needs rectangular marble plates of sizes W1 x H1;W2 x H2;.... ;WN x HN.

Recently, Phong has received a large rectangular marble slab. He wants to cut the slab to obtain plates of the desired sizes. Any piece of marble (the slab or the plates cut from it) can be cut either horizontally or vertically into two rectangular plates with integral widths and heights, cutting completely through that piece. This is the only way to cut pieces and pieces cannot be joined together. Since the marble has a pattern on it, the plates cannot be rotated: if Phong cuts a plate of size A ? B then it cannot be used as a plate of size B , A unless A = B. He can make zero or more plates of each desired size. A marble plate is wasted if it is not of any of the desired sizes after all cuts are completed. Phidias wonders how to cut the initial slab so that as little of it as possible will be wasted.

As an example, assume that in the ?gure below the width of the original slab is 21 and the height of the original slab is 11, and the desired plate sizes are 10?4; 6?2; 7?5, and 15?10. The minimum possible area wasted is 10, and the ?gure shows one sequence of cuts with total waste area of size 10.

10 x 4 10 x 4

6 x 2 6 x 2 6 x 2

7 x 5 7 x 5 7 x 5

Your task is to write a program that, given the size of the original slab and the desired plate sizes, calculates the minimum total area of the original slab that must be wasted.

Input

The ?rst line of the input ?le contains the number of datasets which is a positive integer and is not greater than 20. The following lines describe the datasets.

? The ?rst line of input contains two integers: ?rst W, the width of the original slab, and then H, the height of the original slab;

? The second line contains one integer N: the number of desired plate sizes. The following N lines contain the desired plate sizes. Each of these lines contains two integers: ?rst the width Wi and then the height Hi of that desired plate size (1 = < i = < N).

Output

For each dataset, write in one line a single integer: the minimum total area of the original slab that must be wasted.

Examples

Input

1

21 11

4

10 4

6 2

7 5

15 10

Output

10

Scoring

In all datasets, 1 = < W = < 600; 1 = < H = < 600; 0 < N = < 200; 1 = < Wi = < W, and 1 = < Hi = < H.

Additionally, in 50% of the inputs, W = < 20;H = < 20 and N = < 5.


Comments

There are no comments at the moment.