Crop Triangles (Small)

Count triples of generated tree points whose coordinate sums are divisible by 3 in both axes.

Medium4CombinatoricsNumber theoryBrute forceNo attempts yetTime limit5sMemory limit512 MB

Problem

A few pranksters want to flatten a large triangle into a wheat field overnight. Seen from above the field is an evenly spaced grid, and trees grow at some of the grid points where two grid lines cross. The pranksters want all three vertices of the triangle to sit on trees. They also want the center of the triangle to land on a grid point.

The center of a triangle with vertices (x1,y1)(x_1, y_1), (x2,y2)(x_2, y_2) and (x3,y3)(x_3, y_3) is (x1+x2+x33,y1+y2+y33)\left(\dfrac{x_1 + x_2 + x_3}{3}, \dfrac{y_1 + y_2 + y_3}{3}\right).

You are given the integer coordinates of every tree on the field. Count the triangles whose three vertices are three distinct trees and whose center has integer coordinates in both axes. A triangle of area 0 counts as a valid triangle.

Input

The first line contains the number of test cases NN. Each of the next NN lines contains the integers nn, AA, BB, CC, DD, x0x_0, y0y_0 and MM separated by exactly one space. nn is the number of trees.

The coordinates of the trees are the values printed by the pseudocode below, driven by those eight numbers. mod is the remainder operation. The first tree uses x0x_0 and y0y_0 as given, without the remainder, so it can exceed MM.

X = x0, Y = y0
print X, Y
for i = 1 to n-1
  X = (A * X + B) mod M
  Y = (C * Y + D) mod M
  print X, Y

The parameters are chosen so that no two trees share a position.

Limits:

  • 1N101 \le N \le 10
  • 0A,B,C,D,x0,y01090 \le A, B, C, D, x_0, y_0 \le 10^9
  • 1M1091 \le M \le 10^9
  • 3n1003 \le n \le 100

Output

For each test case, print one line of the form Case #X: Y, where XX is the test case number starting from 1 and YY is the number of triangles whose vertices are three distinct trees and whose center is a grid point.

Hint

In the first test case of the first sample, the eight numbers generate the four trees (0,1)(0, 1), (7,3)(7, 3), (17,5)(17, 5) and (17,7)(17, 7).