import java.io.*;
import java.util.*;
/**
* We read t test‐cases, each with two integers p, s. We must build (if possible)
* a connected shape of N unit‐cells (N <= 50,000) so that
*
* (perimeter of shape) / (area of shape) = p / s.
*
* Equivalently, if gcd(p,s)=g, put p0 = p/g, s0 = s/g, then we look for k>0
* such that
* N = k * s0,
* M = k * p0
* and N <= 50,000, and there is a connected arrangement of N cells of
* perimeter exactly M. We know any connected arrangement has
* minimal‐possible‐perimeter(N) = min_{a*b=N} [ 2*(a+b) ], (a,b in ℕ)
* and maximal‐possible‐perimeter(N) = 2*N + 2 (a “tree” chain of N cells).
* Hence we just search over k until
* k*s0 <= 50000,
* k*p0 <= 2*(k*s0)+2,
* minimal‐possible‐perimeter(k*s0) <= k*p0,
* and (k*p0) is even
* (since any grid‐perimeter must be even). If none is found, print “-1”.
*
* Once (N,M) = (k*s0, k*p0) is chosen, define
* T = (2*N + 2 - M)/2 (an integer >=0).
* Then factor T = x*y (with x <= y) so that the rectangle of size (x+1) x (y+1)
* has area A_rect = (x+1)(y+1) <= N. The remainder d = N - A_rect cells are hung
* off a corner in a one‐cell‐thick chain. That guarantees the final perimeter is
* Perimeter(rect) + 2*d = 2*( (x+1)+(y+1) ) + 2*(N - (x+1)(y+1) ) = M.
*
* We then output N lines of coordinates. Any valid shape is acceptable.
*/
public class Main {
FastReader fr = new FastReader();
StringBuilder sb = new StringBuilder();
int t = fr.nextInt();
for(int _case=0; _case<t; _case++) {
int p = fr.nextInt();
int s = fr.nextInt();
// 1) Reduce p,s by their gcd
int g = gcd(p, s);
int p0 = p / g;
int s0 = s / g;
// 2) If p0 > 2*s0 + 2, impossible.
if (p0 > 2*s0 + 2) {
sb.append("-1\n");
continue;
}
// We will choose some integer k > 0 with N = k*s0, M = k*p0.
// There are special small‐cases if p0 = 2*s0+2 or p0=2*s0+1; otherwise
// we do a small search in k.
int bestK = -1, bestN=0, bestM=0;
if (p0 == 2*s0 + 2) {
// Must have k=1
bestK = 1;
bestN = s0;
bestM = p0;
}
else if (p0 == 2*s0 + 1) {
// Must have k=2
bestK = 2;
bestN = 2*s0;
bestM = 2*p0;
}
else {
// p0 <= 2*s0. We try k = 1 (if p0 even) or k=2 (if p0 odd),
// and keep raising k until N=k*s0 <= 50000 and
// M=k*p0 <= 2*N+2 AND minPerimeter(N) <= M
boolean found = false;
int startK = (p0 % 2 == 0 ? 1 : 2);
for(int k = startK; k * s0 <= 50000; ){
int N = k * s0;
int M = k * p0;
// Check M <= 2N+2
if (M <= 2*N + 2) {
int minP = minimalPerimeter(N);
if (minP <= M) {
bestK = k;
bestN = N;
bestM = M;
found = true;
break;
}
}
// Increase k (by 1 if p0 even, else by 2 if p0 odd)
if (p0 % 2 == 0) {
k++;
} else {
k += 2;
}
}
if (!found) {
sb.append("-1\n");
continue;
}
}
// Now we have N = bestK*s0, M = bestK*p0, with N <= 50000, minPer(N) <= M <= 2N+2
int N = bestN;
int M = bestM;
// 3) Build an explicit connected shape whose area = N, perimeter = M.
// Let T = (2N + 2 - M)/2 >= 0. We factor T = x*y with x<=y, then
// rectangle is size (x+1)x(y+1), area = (x+1)(y+1). The leftover
// d = N - (x+1)(y+1) cells are hung off one corner as a straight chain.
// That construction has exactly perimeter = M.
int T = (2*N + 2 - M)/2;
int a,b;
if (T == 0) {
// Then (x,y)=(0,0) so a=1,b=1
a = 1;
b = 1;
} else {
while (x > 0 && T % x != 0) {
x--;
}
int y = T / x;
a = x + 1;
b = y + 1;
}
int A_rect = a * b;
int d = N - A_rect; // number of “chain” cells
// 4) Emit the coordinates:
// - All rectangle cells: 0 <= i < a, 0 <= j < b.
// - Then d cells: (a,0), (a,-1), …, (a,-(d-1)).
ArrayList<int[]> cells = new ArrayList<>();
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
cells.add(new int[]{i,j});
}
}
for(int t2=0;t2<d;t2++){
// chain runs “downward” from (a,0)
cells.add(new int[]{a, -t2});
}
// 5) Print them
sb.append(cells.size()).append("\n");
for(int[] C: cells) {
sb.append(C[0]).append(" ").append(C[1]).append("\n");
}
}
}
// Fast GCD
static int gcd(int a, int b) {
return (b==0 ? a : gcd(b, a%b));
}
/**
* minimalPerimeter(N) = \min_{(r,c): r*c = N} 2*(r + c).
* We simply try all divisors up to sqrt(N).
*/
static int minimalPerimeter(int N) {
int lim
= (int)Math.
floor(Math.
sqrt(N
)); for(int r=1; r<=lim; r++) {
if (N % r == 0) {
int c = N / r;
int per = 2*(r + c);
if (per < best) best = per;
}
}
return best;
}
/** Very fast reader **/
static class FastReader {
public FastReader() {
}
while(st==null || !st.hasMoreTokens()) {
if(line==null) return null;
}
return st.nextToken();
}
return Integer.
parseInt(nextToken
()); }
}
}