#ARC142E. [ARC142E] Pairing Wizards

[ARC142E] Pairing Wizards

Score : 900900 points

Problem Statement

There are NN wizards, numbered 1,,N1, \ldots, N. Wizard ii has a strength of aia_i and plans to defeat a monster with a strength of bib_i.

You can perform the following operation any number of times.

  • Increase the strength of any wizard of your choice by 11.

A pair of Wizard ii and Wizard jj (let us call this pair (i,j)(i, j)) is said to be good when at least one of the following conditions is satisfied.

  • Wizard ii has a strength of at least bib_i and Wizard jj has a strength of at least bjb_j.
  • Wizard ii has a strength of at least bjb_j and Wizard jj has a strength of at least bib_i.

Your objective is to make the pair (xi,yi)(x_i, y_i) good for every i=1,,Mi=1, \ldots, M. Find the minimum number of operations needed to achieve it.

Constraints

  • 2N1002 \leq N \leq 100
  • 1ai,bi1001 \leq a_i,b_i \leq 100
  • 1MN(N1)21 \leq M \leq \frac{N(N-1)}{2}
  • 1xi<yiN1 \leq x_i \lt y_i \leq N
  • (xi,yi)(xj,yj)(x_i,y_i) \neq (x_j,y_j), if iji\neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 b1b_1

\vdots

aNa_N bNb_N

MM

x1x_1 y1y_1

\vdots

xMx_M yMy_M

Output

Print the answer.

5
1 5
2 4
3 3
4 2
5 1
3
1 4
2 5
3 5
2

You can perform the operation once for Wizard 11 and once for Wizard 44 to achieve the objective with the fewest operations.

4
1 1
1 1
1 1
1 1
3
1 2
2 3
3 4
0

No operation is needed.

9
1 1
2 4
5 5
7 10
9 3
9 13
10 9
3 9
2 9
7
1 5
2 5
1 6
2 4
3 4
4 9
8 9
22