Under construction

Still being built. Things may change

Cloud Retainer's Game

Time limit2sMemory limit1024 MB

Summary
Remove some wooden boards so a pinball bouncing between y=0 and y=H collects as many coins as possible, and report the maximum count.
Level

Hard9 of 10

Topics
Dynamic programming, Segment tree, Sorting
Solved
No attempts yet

Problem

Cloud Retainer, the builder of the Dwelling in the clouds above Qingyun Peak, is very interested in mechanics. The Lantern Rite Festival in Liyue is more than a month away, but she has already started designing a gaming event for it.

Cloud Retainer and the Traveler

The game is mainly about releasing pinballs to score as high as possible. It is played on a two-dimensional plane with two horizontal lines y=0y = 0 and y=Hy = H. Between the two lines are nn tiny wooden boards and mm coins. Both are single points. The ii-th wooden board is at (xi,yi)(x_i, y_i), and the ii-th coin is at (xi′,yi′)(x'_i, y'_i).

The player releases a pinball from (10−9,10−9)(10^{-9}, 10^{-9}). Let v→=(vx,vy)\overrightarrow{v} = (v_x, v_y) be the velocity of the ball. If the ball is at (x,y)(x, y), it moves to (x+vxϵ,y+vyϵ)(x + v_x\epsilon, y + v_y\epsilon) after ϵ\epsilon seconds. Initially v→=(1,1)\overrightarrow{v} = (1, 1).

When the ball hits a wooden board or one of the two horizontal lines, vyv_y is negated and vxv_x stays the same. When the ball hits a coin, the score goes up by 1 and the velocity stays the same.

Before the release, the player may remove any number of wooden boards, including none. Cloud Retainer wants to estimate the difficulty of the game. Find the maximum score the player can get after 101010101010^{10^{10^{10^{10}}}} seconds under the best strategy.

Input

The first line contains an integer TT, the number of test cases. Each test case has the following format.

The first line contains HH (2≤H≤1092 \le H \le 10^9).

The second line contains nn (1≤n≤1051 \le n \le 10^5), the number of wooden boards. The next nn lines each contain xix_i and yiy_i (1≤xi≤1091 \le x_i \le 10^9, 1≤yi<H1 \le y_i < H), the location of the ii-th wooden board.

The next line contains mm (1≤m≤1051 \le m \le 10^5), the number of coins. The next mm lines each contain xi′x'_i and yi′y'_i (1≤xi′≤1091 \le x'_i \le 10^9, 1≤yi′<H1 \le y'_i < H), the location of the ii-th coin.

All n+mn + m points in a test case are distinct. The sums of nn and of mm over all test cases do not exceed 5×1055 \times 10^5.

Output

For each test case, print one line with the maximum score the player can get after removing some wooden boards, or none.

Hint

The figure below shows two test cases. Solid diamonds are remaining wooden boards, hollow diamonds are removed wooden boards, and round dots are coins.

Test case 1Test case 2

Examples1

  1. Example 1

    Input
    2
    4
    3
    1 1
    2 2
    6 2
    4
    3 1
    3 3
    5 1
    7 3
    3
    1
    4 2
    3
    1 1
    6 2
    9 1
    
    Expected output
    3
    3