Messed Up Pictures

For each given image, find the smallest listed-ratio size that covers it directly or after a 90-degree rotation, and report the operation count.

Medium5MathBrute forceNo attempts yetTime limit3sMemory limit256 MB

Problem

Someone messed up the pictures on your friend's computer. Most of the image files were cropped, rotated by 90 degrees, or both, and the rest were left alone. Your friend still remembers the list of aspect ratios of all the original images.

The aspect ratio of an image of size (A,B)(A, B) is (A/G,B/G)(A / G, B / G), where GG is the greatest common divisor of AA and BB.

The contents of an image do not matter here, so a rotation swaps the width and the height. A crop selects a rectangle with integer side lengths inside the image, with its sides parallel to the sides of the image, and the selected rectangle is smaller than the image it is taken from.

Every messed up image was produced from its original by applying at most one crop and at most one rotation, in either order.

You are given the size of every messed up image and the list of possible aspect ratios. For each image, print one possible original size and the number of operations used to mess it up.

Input

The first line contains one integer TT (1T1001 \le T \le 100), the number of test cases.

Each test case starts with a line containing one integer MM (1M1001 \le M \le 100), the number of possible aspect ratios. Each of the next MM lines contains two integers RxR_x and RyR_y (1RxRy1001 \le R_x \le R_y \le 100) describing one aspect ratio. The greatest common divisor of RxR_x and RyR_y is always 1, and all aspect ratios inside one test case are distinct.

The next line contains one integer NN (1N1001 \le N \le 100), the number of messed up images. Each of the next NN lines contains two integers XX and YY (1X,Y1001 \le X, Y \le 100), the size of one messed up image.

Output

For each test case, print a line Case n:, where nn is the number of the test case counting from 1. Then print NN lines, one per image, each containing three integers WW, HH and KK separated by single spaces.

WW and HH are a possible original size of that image. With GG the greatest common divisor of WW and HH, the pair (W/G,H/G)(W / G, H / G) must equal one of the given aspect ratios. KK is the number of operations used to mess the image up, so KK is 0 when nothing changed, 1 when the image was only cropped or only rotated, and 2 when it was both cropped and rotated.

If several answers are possible, print the one with the smallest W×HW \times H. If several remain, print the one with the smallest WW. If several still remain, print the one with the smallest KK.