#P1231. 教辅的组成

教辅的组成

Description

In a Chinese textbook, HansBug found an answer key, but he clearly remembered that the book should also come with a workbook. However, there are too many items in front of him to count, including books, answer keys, and workbooks. A complete set should contain and only contain one book, one workbook, and one answer key, but everything is in a mess now. Although many labels have faded, HansBug can still roughly tell whether an item is a book, a workbook, or an answer key. He also roughly knows the possible correspondences between a book and an answer key, and between a book and a workbook (that is, he only knows which book and which answer key, and which book and which workbook could possibly correspond; all other pairs are impossible). Given this information, HansBug wants to know the maximum number of complete sets that can be formed simultaneously.

Input Format

The first line contains three positive integers N1,N2,N3N_1,N_2,N_3, representing the numbers of books, workbooks, and answer keys respectively.

The second line contains a positive integer M1M_1, representing the number of possible correspondences between books and workbooks.

Each of the next M1M_1 lines contains two positive integers x,yx,y, indicating that book xx and workbook yy could correspond. (1xN11\leq x \leq N_1, 1yN21 \leq y \leq N_2)

The next line contains a positive integer M2M_2, representing the number of possible correspondences between books and answer keys.

Each of the next M2M_2 lines contains two positive integers x,yx,y, indicating that book xx and answer key yy could correspond. (1xN11 \leq x \leq N_1, 1yN31 \leq y \leq N_3)

Output Format

Output a single positive integer, the maximum possible number of complete sets.

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

2

Hint

Sample explanation:

As described, N1=5N_1=5, N2=3N_2=3, N3=4N_3=4, meaning there are 55 books, 33 workbooks, and 44 answer keys.

M1=5M_1=5, meaning there are 55 possible correspondences between books and workbooks, namely: book 44 with workbook 33, book 22 with workbook 22, book 55 with workbook 22, book 55 with workbook 11, and book 55 with workbook 33.

M2=5M_2=5, meaning there are 55 possible correspondences between books and answer keys, namely: book 11 with answer key 33, book 33 with answer key 11, book 22 with answer key 22, book 33 with answer key 33, and book 44 with answer key 33.

Therefore, at most two complete sets can be formed simultaneously in this case, namely: book 22 + workbook 22 + answer key 22, and book 44 + workbook 33 + answer key 33.

Constraints:

  • For testdata points 1,2,31,2,3, 1M1,M2201\le M_1,M_2\leq 20.
  • For testdata points 4104\sim 10, 1M1,M2200001\le M_1,M_2 \leq 20000.

Translated by ChatGPT 5