TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Do you remember the problem "Exam is coming"? Here is another similar problem.

Give you many arithmetic number sequences (sequence where the adjacent numbers have same difference). Then there are many queries. In each query, give you three digits x and ask you print the number of numbers such that last three digits of these number is x in these number sequences.

Input Format

The first line of input contains T, denoting the number of test cases.

The first line of each test contains a number n, denoting the number of number sequences. Each line of next n lines contains three number a, d, m indicating a number sequence, in which a indicates the first item of the sequence, d indicates the difference of the sequence, and m indicates the number of number in the sequence. Next line contain a number q, denoting the number of queries. Each line of next q lines contains three digits x.

1<=n<=106

103<=a<=109

1<=d,m<=109

1<=q<=1000

000<=x<=999 (x has 3 digits)

Output Format

For each query,please print a line contains the answer for this query.

Sample Input 1

1
2
2001 2 67
1001 3 73
3
001
002
003

Sample Output 1

2
0
1

Hints

Problem Source

Migrated from old NTUJ.

dreamoon

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 4000 65536 200