#P5289. [十二省联考 2019] 皮配

[十二省联考 2019] 皮配

Description

This season, “China’s Best Coder” invites student elites from all over the country to compete. They come from nn different schools in cc cities nationwide (cities are numbered from 11 to cc, and schools are numbered from 11 to nn). The city index of school ii is bib_i, and there are sis_i contestants from this school.

Besides the total size limits mentioned in Background, the mentor selection stage this season has additional rules:

  • All contestants from the same city must join the same camp.
  • All contestants from the same school must choose the same mentor.

For mentors, students from most schools have no preference. However, there are kk schools where, for each such school, the students have exactly one mentor they dislike. Students in the same school dislike the same mentor, and they will not join the team of the mentor they dislike.

With so many rules and preferences, as a loyal viewer you want to compute: after all contestants have chosen teams, how many different final outcomes are possible?

  • Two outcomes are considered different if and only if there exists a school such that, in the two outcomes, contestants from this school join teams led by different mentors.
  • Since the answer may be large, output it modulo 998244353998244353.

Input Format

Each test file contains multiple test cases. The first line contains a non-negative integer TT indicating the number of test cases. Then each test case is described as follows:

  • Line 11: two positive integers nn, cc, representing the number of schools and the number of cities.
  • Line 22: four positive integers C0C_0, C1C_1, D0D_0, D1D_1, representing the four limits described in the statement.
  • Next nn lines, each with two positive integers:
    • On line ii of this part, the two numbers are bib_i, sis_i, representing the city of school ii and its number of contestants.
    • It is guaranteed that bicb_i \leqslant c, simin{M,10}s_i \leqslant\min\left\{M, 10\right\}. Here M=max{C0,C1,D0,D1}M = \max \left\{C_0, C_1, D_0, D_1\right\}.
  • Next line: a non-negative integer kk, the number of schools with preferences.
  • Next kk lines, each with two integers ii, pp, describing that contestants from school ii have a preference:
    • Here pp is an integer from 00 to 33, describing the mentor disliked by this school: 00 means Yazid, 11 means Little R, 22 means Zayid, 33 means Big R.
    • It is guaranteed that 1in1 \leqslant i \leqslant n, and all ii are distinct.

For each input line, if it contains multiple numbers, they are separated by a single space.

Output Format

Output the answer for each test case in order:

  • For each test case, output one line with one integer, the number of possible outcomes modulo 998244353998244353.
2
2 1
3 2 2 2
1 1
1 2
1
1 0
4 2
10 30 20 30
1 6
2 4
1 7
2 4
2
2 3
3 1
1
22

Hint

Sample 1 Explanation

For the 11st test case:

  • The only city 11 contains a total of 33 contestants, but the total size limit of the Red camp is 22, so it cannot hold these contestants. Therefore, they are forced to choose the Blue camp.
  • Based on this, since contestants from school 11 dislike mentor Yazid, they must join mentor Little R in the R faction.
  • Because the total size limit of the R faction is 22, mentor Little R’s team cannot hold the contestants from school 22, so they are forced to join mentor Yazid’s team.
  • Therefore, there is only one possible outcome.

For the 22nd test case:

  • An obvious fact is that all contestants in city 11 cannot join the Blue camp, because the total number of contestants in city 11 exceeds the total size limit of the Blue camp, so they are forced to all join the Red camp.
  • If contestants from city 22 join the Blue camp, a small calculation shows there are 1515 possible outcomes.
  • If contestants from city 22 join the Red camp, a small calculation shows there are 77 possible outcomes.
  • Therefore, the number of possible outcomes is 15+7=2215 + 7 = 22.

Constraints and Notes

::cute-table{tuack}

Test point nn cc kk mm
11 =1=1 =n=n 1\le1 =1=1
22 =10=10 ^ 10\le10 100\le100
33 =20=20 =0=0 ^
44 =30=30 ^
55 ^ 30\le30 500\le500
66 =500=500 =0=0 1000\le1000
77 ^ =30=30 ^
88 =n=n ^
99 =1000=1000 ^ =0=0 2500\le2500
1010 ^ =30= 30 ^

Here, M=max{C0,C1,D0,D1}M = \max\left\{C_0, C_1, D_0, D_1\right\}.

For all test points, it is guaranteed that T5T \leqslant5.

For each test case in all test points, it is guaranteed that cn1000c \leqslant n \leqslant 1000, k30k \leqslant 30, M2500M \leqslant 2500, 1simin{M,10}1 \leqslant s_i \leqslant \min\left\{M, 10\right\}.

Also, note that the testdata does not guarantee that all cc cities have participating schools.

Additional Hint

There are also two additional sample files. Please download them from the attachments.

A warm reminder from the Twelve Provinces Joint Exam problem committee:

There are thousands of testdata, clearing arrays is the first rule.
If you forget to clear in multiple tests, you will cry with a zero score.

Translated by ChatGPT 5