Hashing

No attempts yetTime limit2sMemory limit128 MB

Problem

Sanggeun built a hashing function that maps an integer to a value in 00 through m1m-1:

h(y)=(ay+b)modmh(y) = (a \cdot y + b) \bmod m

Given integers xx, nn, cc, and dd, write a program that counts how many of the hash values

h(x), h(x+1), , h(x+n)h(x),\ h(x+1),\ \dots,\ h(x+n)

fall within the interval [c,d][c, d].

Input

The first line contains the number of test cases tt (1t1051 \le t \le 10^{5}).

Each of the next tt lines contains the integers aa, bb, xx, nn, cc, dd, mm separated by spaces.

  • 1m10151 \le m \le 10^{15}
  • 0cd<m0 \le c \le d < m
  • 0a,b<m0 \le a, b < m
  • 0x+n10150 \le x + n \le 10^{15}
  • a(x+n)+b1015a \cdot (x + n) + b \le 10^{15}

Every number in the input is a non-negative integer.

Output

For each test case, output on its own line the number of indices ii (0in0 \le i \le n) satisfying c(a(x+i)+b)modmdc \le (a \cdot (x + i) + b) \bmod m \le d.