Baeknam's Preparation for Travel Preparation
Time limit3sMemory limit512 MB
Given moduli A, B, C and remainders a, b, c, find the smallest nonnegative integer satisfying all three congruences, or -1 if none exists.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Brute force, Implementation
- Solved
- No attempts yet
Problem
Cute Baeknam is on vacation and getting ready to set off on a trip.
Before preparing for the trip, Baeknam plans to smash open his piggy bank to fund the purchase of necessities. He ends up with a lot of gold coins, and he becomes curious about how many there are.
With a T (thinking) MBTI type, Baeknam wants to work out the number of gold coins in an unusual way, to sharpen his thinking.
Baeknam's method for finding the number of gold coins is as follows.
He prepares three special boxes. Each box has a number engraved on it, and those numbers are , , . When an object is placed in a box, the box reveals the remainder when the number of objects is divided by the engraved number. Let the numbers produced by putting the gold coins into the boxes be , , . Find how many gold coins Baeknam has.

Input
The first line gives the number of test cases . ()
Each test case takes one line. Each line gives six integers , , , , , .
() () () ()
Output
For each test case, print the number of gold coins. If several numbers of gold coins are possible, print the smallest one.
If no solution exists for the given integers, print -1.