Under construction

Still being built. Things may change

Knight Polygon

Time limit1sMemory limit1024 MB

Summary
Given integers p and q, find a simple lattice polygon whose edges are all knight moves and whose area is p/q, or print -1 if none exists.
Level

Hard8 of 10

Topics
Geometry, Math, Implementation
Solved
No attempts yet

Problem

Two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are a knight-move apart if exactly one of the following conditions holds:

  • ∣x1−x2∣=2|x_1 - x_2| = 2 and ∣y1−y2∣=1|y_1 - y_2| = 1
  • ∣x1−x2∣=1|x_1 - x_2| = 1 and ∣y1−y2∣=2|y_1 - y_2| = 2

This definition closely matches how a knight moves in chess. For example, here are three pairs of points that are a knight-move apart:

You are given integers pp and qq. Find a simple lattice polygon whose area is p/qp/q, where each pair of adjacent vertices is a knight-move apart, or state that no such polygon exists.

A polygon is simple if there are exactly two edges touching each vertex, and no two edges of the polygon intersect except at its vertices. A polygon is a lattice polygon if the coordinates of each of its vertices are integers.

Input

The first line contains a single integer tt (1≤t≤101 \le t \le 10), the number of test cases. Each test case consists of a single line with two integers pp and qq (1≤p,q≤1041 \le p, q \le 10^4), the numerator and denominator of the desired area.

Output

For each test case, if no solution exists, output a single integer −1-1.

Otherwise, the first line should contain a single integer nn (3≤n≤1053 \le n \le 10^5), the number of vertices in your polygon. The next nn lines should each contain two integers xx and yy (−109≤x,y≤109-10^9 \le x, y \le 10^9), the vertices of your polygon in either clockwise or counterclockwise order.

Your polygon must be simple, have an area of pq\frac{p}{q}, and have every pair of adjacent vertices a knight-move apart. If there are multiple solutions, print any of them.

Hint

The polygon described by the output of the first test case, with an area of 183=6\frac{18}{3} = 6, is shown below.

The polygon described by the output of the third test case, with an area of 81=8\frac{8}{1} = 8, is shown below.

For the second and fourth test cases, we can show that no valid polygon exists.

Examples1

  1. Example 1

    Input
    4
    18 3
    1 2
    8 1
    20 3
    
    Expected output
    6
    0 0
    2 1
    1 3
    0 1
    -1 3
    -2 1
    -1
    14
    -1 -2
    -3 -1
    -2 1
    -1 3
    0 1
    1 3
    2 1
    3 -1
    1 -2
    2 0
    1 2
    0 0
    -1 2
    -2 0
    -1