Feast Coins

Count ways to reach total S with owned coins so that every chosen coin value appears the same number of times.

Medium5Dynamic programmingCombinatoricsInterviewNo attempts yetTime limit3sMemory limit256 MB

Problem

At the last feast the young princess received far too many coins. She is very young and does not know what each coin is worth. Hand her a coin of value 5 or a coin of value 1 and she counts each one as a single coin, whatever its value.

She does notice that a coin of value 5 does not look like a coin of value 1. She is happy when she holds the same number of coins of every value she has, and unhappy otherwise.

She owns many coins of different values. She wants to pick some of them so that their values add up to exactly SS, and so that every value present among the picked coins appears the same number of times. Count how many different ways she has to do this.

Input

The first line contains one integer TT, the number of test cases (1T1001 \le T \le 100). TT test cases follow.

Each test case starts with a line holding two integers separated by a single space, SS and NN (1S50001 \le S \le 5000, 1N501 \le N \le 50), the required total value and the number of different coin values.

Each of the next NN lines holds two integers separated by a single space, ViV_i and CiC_i (1Vi,Ci50001 \le V_i, C_i \le 5000), the value of a coin and the number of coins of that value the princess owns. Within one test case all ViV_i are distinct.

Output

For each test case print one line in the form Case n: X, where nn is the test case number starting from 1 and XX is the number of different ways to reach the total SS described above.

Two ways count as different when some coin value appears a different number of times in them.

The answer always fits in a 64-bit signed integer.

Notes

With S=10S = 10, two coins of value 2 and one coin of value 6, the only combination adding up to 10 is (2, 2, 6). It does not count, because value 2 appears twice while value 6 appears once.

With S=10S = 10 and ten coins of each of the values 1, 2, 3 and 4, there are 5 ways: (1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (2, 2, 2, 2, 2), (2, 2, 3, 3), (1, 1, 4, 4), (1, 2, 3, 4).