Clock arithmetic

Modular arithmetic works with remainders, so the answer always lies between 0 and m − 1. Your program adds, subtracts and multiplies numbers modulo m.

What to build

A program that prints the sum, difference and product of a and b modulo m.

Requirements

  • Read the number of cases t, then t lines with a, b and m.
  • For each case print three numbers separated by spaces: (a + b) mod m, (a − b) mod m and (a × b) mod m. Each is between 0 and m − 1.

Input

The first line has t. Each of the next t lines has a, b and m.

Output

One line of three numbers for each case.

Constraints

1 ≤ t ≤ 20; |a|, |b| ≤ 10 to the power 9; 1 ≤ m ≤ 10 to the power 9 + 7.

Example

Input:

2
17 9 12
5 7 10

Output:

2 8 9
2 8 5

Why it works

Two numbers are congruent mod m when they leave the same remainder, and sums, differences and products respect that. Python's % returns a value from 0 to m − 1 even for a negative difference, which is exactly the standard representative.

Examples

Example 1 · Input
2
17 9 12
5 7 10
Expected output
2 8 9
2 8 5
Example 2 · Input
1
100 3 7
Expected output
5 6 6

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Python's % gives a non-negative remainder when the modulus is positive, even for negative numbers.

Hint 2

Compute (a + b) % m, (a - b) % m and (a * b) % m.

Hint 3

Python integers have no limit, so a * b is safe.

Solution

Show one correct solution (try it yourself first)
t = int(input())
for _ in range(t):
    a, b, m = map(int, input().split())
    print((a + b) % m, (a - b) % m, (a * b) % m)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Modular arithmetic and RSA →