#include<stdio.h>

int gcd(int a, int b);  /* Return gcd(a,b) */

int compare(int a, int b, int x, int y);  /* Compare fractions a/b and x/y.  
                                             Return -1 if a/b < x/y,
 					             0 if a/b == x/y,
					             1 if a/b > x/y
					  */
           
void qsort(int *a, int *b, 
           int lower, int upper);  /* Interpret a as an array of
                                      numerators, b as an array of 
				      denominators.  Use quicksort to order 
				      the resulting fractions in range 
				      lower <= index < upper in ascending order
				   */

int bsearch(int *a, int *b, 
	    int lower, int upper, 
	    int n, int d);         /* Binary search for n/d in the sorted
				      arrays a and b of numerators 
				      and denominators  */

int swap(int *a, int *b, 
	 int i, int j);            /* Swap a[i],b[i] with a[j],b[j] */

int main(){

  int i, j, k;
  int num[10000],denom[10000];
  int len, n, d;
  int pos;

  k = 0;

  /* Generate all proper fractions 0 < n/d < 1 with 0 < d <= 99
     and store numerators/denominators in arrays num and denom */ 

  for (j = 1; j < 100; j++){
    for (i = 1; i < j; i++){
      if (gcd(i,j) == 1){
	num[k] = i;
	denom[k] = j;
	k++;
      }
    }
  }

  /* Sort the fractions using quicksort */
  qsort(num,denom,0,k);
  
  /* Read length of input sequence */
  scanf("%d",&len);

  /* For each input fraction */
  for (i = 0; i < len; i++){
    scanf("%d %d",&n,&d);

    /* Find the bracketing pair using binary search and print it out */
    pos = bsearch(num,denom,0,k-1,n,d);
    printf("%d %d %d %d\n",num[pos],denom[pos],num[pos+1],denom[pos+1]);

  }

}

/* Auxiliary functions defined below */

/* gcd */

int gcd(int a, int b){
  if (a == b){
    return a;
  }

  if (a < b){
    return gcd(a,b-a);
  }else{
    return gcd(b,a-b);
  }
}

/* comparing two fractions */

int compare(int a, int b, int x, int y){
  if (a*y == b*x) { return 0; }
  if (a*y < b*x) {return -1; }
  return 1;
}

/* swapping a pair of fractions */

int swap(int *a, int *b, int i, int j){

  int temp1, temp2;

  temp1 = a[i];
  temp2 = b[i];

  a[i] = a[j];
  b[i] = b[j];
  
  a[j] = temp1;
  b[j] = temp2;
}

/* Quick sort */
  
void qsort(int *a, int *b, int lower, int upper){
  int boundary = lower+1;
  int limit = lower+1;
  int result;

  if ((upper-lower) <= 1){
    return;
  }

  while(limit < upper){
    result = compare(a[limit],b[limit],a[lower],b[lower]);

    if (result < 0){
      swap(a,b,boundary,limit);
      boundary++;
      limit++;
    }else{
      limit++;
    }
  }

  swap(a,b,lower,boundary-1);

  qsort(a,b,lower,boundary-1);
  qsort(a,b,boundary,upper);
}

/* Binary search */

int bsearch(int *a, int *b, int lower, int upper, int n, int d){
  int midpos;
  int result;

  if (upper < lower) {return upper;}

  midpos = (upper + lower)/2;

  result = compare(a[midpos],b[midpos],n,d);

  if (result == 0){
    return midpos;
  }

  if (result > 0){
    return bsearch(a,b,lower,midpos-1,n,d);
  }else{
    return bsearch(a,b,midpos+1,upper,n,d);
  }
}

  
  

