Fairland (Large)

Keep the largest rooted connected subtree containing the CEO so all kept salaries fit within a range of width D.

Hard8TreeSliding windowSortingSegment treeNo attempts yetTime limit10sMemory limit512 MB

Problem

Fairland regulates how companies organize and pay their employees with these laws.

  1. Each company has exactly one CEO, and the CEO has no manager.
  2. Every employee other than the CEO has exactly one manager. The org chart of a company is therefore a tree with no cycles.
  3. While an employee works for the company, that employee's manager never changes. If a manager leaves, every employee reporting to that manager must leave as well.
  4. The CEO never leaves the company.
  5. Every employee receives a salary, an amount of Fairland dollars per year. An employee's salary never changes.
  6. Different employees may have different salaries, and a salary has no relation to the employee's position in the org chart.

The government has just passed one more law.

  1. The difference between the largest salary and the smallest salary in the whole company must be at most DD.

Marie is the CEO of Fairland General Stuff Corporation, and she has to make the company comply with the new law. That may require laying off some employees. She knows the list of employees, each employee's manager, and each employee's salary. Find the largest number of employees she can keep, including herself.

Input

The first line contains the number of test cases TT. TT test cases follow.

The first line of each test case contains two space separated integers, the number of employees NN and the maximum allowed salary difference DD. The second line contains four space separated integers S0S_0, AsA_s, CsC_s, RsR_s, and the third line contains four space separated integers M0M_0, AmA_m, CmC_m, RmR_m. These eight integers define two sequences.

Si+1=(Si×As+Cs)modRsS_{i+1} = (S_i \times A_s + C_s) \bmod R_s

Mi+1=(Mi×Am+Cm)modRmM_{i+1} = (M_i \times A_m + C_m) \bmod R_m

Marie's employee ID is 0, and the other employees have IDs from 1 to N1N-1. The salary of employee ii is SiS_i. For every employee ii other than Marie, the manager is MimodiM_i \bmod i. So M0M_0 has nothing to do with Marie's manager. Marie has none.

Limits

  • 1T1001 \le T \le 100
  • 1N1061 \le N \le 10^6, and the sum of NN over all test cases is at most 10610^6
  • 1D1061 \le D \le 10^6
  • 1Rs,Rm1061 \le R_s, R_m \le 10^6
  • 0S0<Rs0 \le S_0 < R_s
  • 0M0<Rm0 \le M_0 < R_m
  • 0As,Am10000 \le A_s, A_m \le 1000
  • 0Cs,Cm1090 \le C_s, C_m \le 10^9

Output

For each test case, print one line in the format Case #x: y, where xx is the test case number starting from 1 and yy is the largest number of employees Marie can keep, including herself, so that laws 1 through 7 all hold.

Explanation

In the first test case of the sample input the company has only the CEO and no other employees. It breaks none of the laws, so nobody is laid off.

In the second test case the sequences for employees 1 through 5 are these.

  • SS: 13, 16, 2, 5, 8
  • MM: 17, 3, 13, 14, 16
  • Manager numbers: 17mod1=017 \bmod 1 = 0, 3mod2=13 \bmod 2 = 1, 13mod3=113 \bmod 3 = 1, 14mod4=214 \bmod 4 = 2, 16mod5=116 \bmod 5 = 1

The org chart is therefore this. Marie (employee 0) manages employee 1, employee 1 manages employees 2, 3 and 5, and employee 2 manages employee 4. The salaries of employees 0 through 5 are 10, 13, 16, 2, 5, 8, and D=5D = 5.

The best choice keeps employees 0, 1 and 5, whose salaries are 10, 13 and 8. Employee 2, for instance, cannot be kept. Her salary is more than 5 away from employee 0's salary of 10, and employee 0 cannot be laid off, so employee 2 and everyone reporting to her must go.