Problem: Lets think of an infinite series of numbers such that the difference between any two consecutive numbers is d. Let the first number in the sequence be a.
Now your task is to find the multiple that occurs first in the sequence of a given number n and print its index in the output.
Input
1st line  T  denoting test cases.
T test cases follow :
1st line consists of two space separated integers a and d. 0 ≤ a,d ≤ pow(10,18);
2nd line contains a single integer n. 1 ≤ n ≤ pow(10,9);
Output
print as said above  and if there is no such number then print -1.
Update: the number n is strictly a prime number and index of the sequence starts with 0.

SAMPLE INPUT
2
5 6
7
4 9
11

SAMPLE OUTPUT
5
2

Solution (in PYTHON):

Solution:
from sys import stdin
import math
t = int(stdin.readline())
for _ in xrange(t):
	a,b = map(int,stdin.readline().split())
	p = int(stdin.readline())
	if b%p==0:
		if a%p==0:
			print 0
		else:
			print -1
	else:
		rem = (p - a%p)%p
		b = b%p
		inv = pow(b, p-2, p)
		rem = (rem * inv)%p
		print rem