Fairland (Small)

Marie keeps the largest manager-closed team containing herself whose salaries span at most D.

Medium5TreeDFSIntervalsSortingNo attempts yetTime limit5sMemory limit512 MB

Problem

Fairland has strict laws about how a company organizes and pays its employees.

  1. Every 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 in 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 of Fairland 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 Fairland dollars.

Marie is the CEO of Fairland General Stuff Corporation, and she has to make the company comply with the new law. That may mean laying off some employees. She knows the list of employees, the manager of each employee, and every salary. Find the largest number of employees she can keep, counting 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 largest 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 00, and the other employees have ids from 11 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. This means M0M_0 does not decide Marie's manager. Marie has none.

Limits

  • 1T1001 \le T \le 100
  • 1N10001 \le N \le 1000
  • 1D10001 \le D \le 1000
  • 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
  • 1Rs,Rm10001 \le R_s, R_m \le 1000

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 11 and yy is the largest number of employees Marie can keep while laws 1 to 7 all hold. Marie herself counts toward that number.

Hint

In the first test case the company has only the CEO and no other employee. No law is broken, so nobody is laid off.

In the second test case the sequences for employees 11 through 55 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 best choice is to keep employees 00, 11, and 55, whose salaries are 10, 13, and 8. Employee 22 cannot be kept, for example: her salary of 16 is more than 5 away from employee 00's salary of 10, and employee 00 cannot be laid off. When employee 22 leaves, every employee reporting to her leaves too.