#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
struct Tpair{
	LL x,y;
	Tpair(LL _x=0,LL _y=0){x=_x,y=_y;}
	inline bool operator <(const Tpair &b)const{
		return x!=b.x?x<b.x:y<b.y;
	}
};
struct Tcxx{
	LL a,b,c,v;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return v=a*b+a*c+b*c;
	}
};
LL n,B[50][70];
int N;Tpair A[50];

namespace Ninit{
	void init(){
		int i,j;
		n=1;
		for(cin>>N,i=0;i<N;++i)
			cin>>A[i].x>>A[i].y;
		sort(A,A+N),reverse(A,A+N);
		for(i=0;i<N;++i)
		for(B[i][0]=1,j=1;j<=A[i].y;++j)
			B[i][j]=B[i][j-1]*A[i].x;
		for(i=0;i<N;++i)n*=B[i][A[i].y];
	}
}

namespace Nsolve{
	Tcxx S,W;
	void dfs2(int x){
		if((double)W.a*W.b*W.b>n)return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S.v)S=W;
			return;
		}
		int i;LL W_=W.b;
		for(i=A[x].y;i>=0;--i){
			W.b=W_*B[x][i],dfs2(x+1);
		}
		W.v=W_;
	}
	void dfs1(int x){
		if((double)W.a*W.a*W.a>n)return;
		if(x==N){
			if(W.a*2*sqrt(n/W.a)+n/W.a<=S.v+1)dfs2(0);
			return;
		}
		int i;LL W_=W.a;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i,W.a=W_*B[x][i];
			dfs1(x+1);
			A[x].y+=i;
		}
		W.a=W_;
	}
	void solve(){
		S=Tcxx(1,1,n),S(),W=Tcxx(1,1,1);
		dfs1(0);
		cout<<2*S.v<<' '<<S.a<<' '<<S.b<<' '<<S.c<<endl;
	}
}

int main(){
	//freopen("A.in","r",stdin);
	int Q;
	Ninit::init();
	for(cin>>Q;Q;--Q){
		Nsolve::solve();
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
struct Tpair{
	LL x,y;
	Tpair(LL _x=0,LL _y=0){x=_x,y=_y;}
	inline bool operator <(const Tpair &b)const{
		return x!=b.x?x<b.x:y<b.y;
	}
};
struct Tcxx{
	LL a,b,c,v;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return v=a*b+a*c+b*c;
	}
};
LL n,B[50][70];
int N;Tpair A[50];

LL Sqrt(LL n){
	LL x=sqrt(n)+1;
	for(;x*x>n;--x);
	return x;
}

namespace Ninit{
	void init(){
		int i,j;
		n=1;
		for(cin>>N,i=0;i<N;++i)
			cin>>A[i].x>>A[i].y;
		sort(A,A+N);//,reverse(A,A+N);
		for(i=0;i<N;++i)
		for(B[i][0]=1,j=1;j<=A[i].y;++j)
			B[i][j]=B[i][j-1]*A[i].x;
		for(i=0;i<N;++i)n*=B[i][A[i].y];
	}
}

namespace Nsolve{
	Tcxx S,W;
	int tot;
	void dfs2(int x){
		if(W.a*W.b*W.b>n)return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S.v)S=W;
			return;
		}
		int i;LL W_=W.b;
		for(i=A[x].y;i>=0;--i){
			W.b=W_*B[x][i],dfs2(x+1);
		}
		W.v=W_;
	}
	void dfs1(int x){
		if(W.a*W.a*W.a>n)return;
		if(x==N){
			if(W.a*2*Sqrt(n/W.a)+n/W.a<=S.v)dfs2(0);
			return;
		}
		int i;LL W_=W.a;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i,W.a=W_*B[x][i];
			dfs1(x+1);
			A[x].y+=i;
		}
		W.a=W_;
	}
	void solve(){
		S=Tcxx(1,1,n),S(),W=Tcxx(1,1,1);
		dfs1(0);
		cout<<2*S.v<<' '<<S.a<<' '<<S.b<<' '<<S.c<<endl;
	}
}

int main(){
	//freopen("A.in","r",stdin);
	int Q;
	for(cin>>Q;Q;--Q){
		Ninit::init();
		Nsolve::solve();
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
struct Tpair{
	LL x,y;
	Tpair(LL _x=0,LL _y=0){x=_x,y=_y;}
	inline bool operator <(const Tpair &b)const{
		return x!=b.x?x<b.x:y<b.y;
	}
};
struct Tcxx{
	LL a,b,c,v;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return v=a*b+a*c+b*c;
	}
};
LL n,B[50][70];
int N;Tpair A[50];

namespace Ninit{
	void init(){
		int i,j;
		n=1;
		for(cin>>N,i=0;i<N;++i)
			cin>>A[i].x>>A[i].y;
		sort(A,A+N),reverse(A,A+N);
		for(i=0;i<N;++i)
		for(B[i][0]=1,j=1;j<=A[i].y;++j)
			B[i][j]=B[i][j-1]*A[i].x;
		for(i=0;i<N;++i)n*=B[i][A[i].y];
	}
}

namespace Nsolve{
	Tcxx S,W;
	void dfs2(int x){
		if((double)W.a*W.b*W.b>n)return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S.v)S=W;
			return;
		}
		int i;LL W_=W.b;
		for(i=A[x].y;i>=0;--i){
			W.b=W_*B[x][i],dfs2(x+1);
		}
		W.v=W_;
	}
	void dfs1(int x){
		if((double)W.a*W.a*W.a>n)return;
		if(x==N){
			if(W.a*2*sqrt(n/W.a)+n/W.a<=S.v+1)dfs2(0);
			return;
		}
		int i;LL W_=W.a;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i,W.a=W_*B[x][i];
			dfs1(x+1);
			A[x].y+=i;
		}
		W.a=W_;
	}
	void solve(){
		S=Tcxx(1,1,n),S(),W=Tcxx(1,1,1);
		dfs1(0);
		cout<<2*S.v<<' '<<S.a<<' '<<S.b<<' '<<S.c<<endl;
	}
}

int main(){
	freopen("A.in","r",stdin);
	int Q;
	Ninit::init();
	for(cin>>Q;Q;--Q){
		Nsolve::solve();
	}
	return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V , ll lo){
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(pos == 0 && c * ost[pos] < lo) return;
  if(c * ost[pos+1] >= lo) run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V , lo);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 1) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100] , cs[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
int iters = 0;
void run2(ll c , int pos , ll V , ll lo){
  ++iters;
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(cs[pos] == s[pos] && c*ost[pos] < lo) return;
  if(c*ost[pos+1] >= lo) run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos]*c <= V){
   --s[pos];
   if(c <= V/c) run2(c*d[pos] , pos , V , lo);
   ++s[pos];
                             }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 1) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   memcpy(cs , s , N*sizeof(int));
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  int os = s[pos];
  s[pos] = 0;
  for(int i=0;i<os;++i) c *= d[pos];
  for(int i=os;i>=0;--i){
   if(c < V/c && c*c < V/c)
    run1(c , pos + 1);
   c /= d[pos];
   ++s[pos];
                        }
  --s[pos];
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  //cout<<iters<<endl;
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100] , cs[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V , ll lo){
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(cs[pos] == s[pos] && c * ost[pos] < lo) return;
  //if(c * ost[pos+1] >= lo)
  run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V , lo);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 100) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   memcpy(cs , s , N*sizeof(int));
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V , ll lo){
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(pos == 0 && c * ost[pos] < lo) return;
  //if(c * ost[pos+1] >= lo)
  run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V , lo);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 100) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100];
ll d[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V){
  B = c , C = V / c;
  if(A < bs/B && B < bs/C && C < bs/A)
   if(bs > A*B + B*C + A*C)
    bs = A*B + B*C + A*C,
    ba = A , bb = B , bc = C;
  if(pos >= N) return;
  run2(c , pos + 1 , V);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  A = c , run2(1 , 0 , V / c);
  if(pos >= N) return;
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100] , cs[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
int iters = 0;
void run2(ll c , int pos , ll V , ll lo){
  ++iters;
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(cs[pos] == s[pos] && c*ost[pos] < lo) return;
  if(c*ost[pos+1] >= lo) run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos]*c <= V){
   --s[pos];
   if(c <= V/c) run2(c*d[pos] , pos , V , lo);
   ++s[pos];
                             }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 1) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   memcpy(cs , s , N*sizeof(int));
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  int os = s[pos];
  s[pos] = 0;
  for(int i=0;i<os;++i) c *= d[pos];
  for(int i=os;i>=0;--i){
   if(c <= V/c && c*c <= V/c)
    run1(c , pos + 1);
   c /= d[pos];
   ++s[pos];
                        }
  --s[pos];
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  //cout<<iters<<endl;
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100];
ll d[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V){
  B = c , C = V / c;
  if(A < bs/B && B < bs/C && C < bs/A)
   if(bs > A*B + B*C + A*C)
    bs = A*B + B*C + A*C,
    ba = A , bb = B , bc = C;
  if(pos >= N) return;
  run2(c , pos + 1 , V);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  A = c , run2(1 , 0 , V / c);
  if(pos >= N) return;
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  scanf("%d" , &Q);
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V , ll lo){
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  //if(pos == 0 && c * ost[pos] < lo) return;
  //if(c * ost[pos+1] >= lo)
  run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V , lo);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 100) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
void run2(ll c , int pos , ll V , ll lo){
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(pos == 0 && c * ost[pos] < lo) return;
  if(c * ost[pos+1] >= lo) run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V , lo);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 100) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  return 0;
}


#ifdef _WIN32
#  define LL "%I64d"
#else
#  define LL "%Ld"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <algorithm>
#include <complex>
#include <utility>
#include <cassert>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
ll V;
int N , s[100] , cs[100];
ll d[100] , ost[100];
ll bs , ba , bb , bc;
ll A , B , C;
int iters = 0;
void run2(ll c , int pos , ll V , ll lo){
  ++iters;
  if(pos == N){
   B = c , C = V / c;
   if(A < bs/B && B < bs/C && C < bs/A)
    if(bs > A*B + B*C + A*C)
     bs = A*B + B*C + A*C,
     ba = A , bb = B , bc = C;
   return;
              }
  if(cs[pos] == s[pos] && c * ost[pos] < lo) return;
  if(c * ost[pos+1] >= lo) run2(c , pos + 1 , V , lo);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c) run2(c , pos , V , lo);
   c /= d[pos] , ++s[pos];
                               }
}
void run1(ll c , int pos){
  if(pos == N){
   ll X = sqrt(V / c);
   if(2*c*X + V/c > bs + 1) return;
   ost[N] = 1;
   for(int i=N-1;i>=0;--i){
    ost[i] = ost[i+1];
    for(int j=0;j<s[i];++j) ost[i] *= d[i];
                          }
   memcpy(cs , s , N*sizeof(int));
   A = c , run2(1 , 0 , V / c , c);
   return;
              }
  run1(c , pos + 1);
  if(s[pos] && d[pos] <= V / c){
   --s[pos] , c *= d[pos];
   if(c <= V/c && c*c <= V/c) run1(c , pos);
   c /= d[pos] , ++s[pos];
                               }
}
void doit(){
  cin>>N;
  for(int i=0;i<N;++i)
   cin>>d[i]>>s[i];
  V = 1;
  for(int i=0;i<N;++i)
   for(int j=0;j<s[i];++j)
    V *= d[i];
  bs = 2*V + 1 , bc = V , ba = 1 , bb = 1;
  run1(1 , 0);
  cout<<2*bs<<' '<<ba<<' '<<bb<<' '<<bc<<'\n';
}
int main(){
  ios_base::sync_with_stdio(false);
  int Q;
  cin>>Q;
  while(Q-- > 0) doit();
  //cout<<iters<<endl;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define zero(a) (abs(a)<eps)
#define lowbit(a) ((a)&(-(a)))
#define abs(a) ((a)>0?(a):(-(a)))
#define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define dj(x1,y1,x2,y2) ((x1)*(x2)+(y1)*(y2))
#define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1)))
const double eps = 1e-9;
const long long oo = 5000000000000000000LL;
const double E = 2.7182818284590452353602874713527;
const double pi = 3.1415926535897932384626433832795;
using namespace std;

long long v[61][61];
int y[61],z[61];
double amax;
long long aa,bb,cc,ans;
int n;

void dfs2(int now,long long a,long long b,long long c)
{
   if (a*b+b*c+c*a>=ans)
      return;
   if (now>n)
   {
      ans=a*b+b*c+c*a;
      aa=a;
      bb=b;
      cc=c;
   }
   else
      for (int i=0;i<=z[now];i++)
         dfs2(now+1,a,b*v[now][i],c*v[now][z[now]-i]);
}

void dfs1(int now,long long a,long long b)
{
   if (a>amax)
      return;
   if (now>n)
   {
      if (b+2*sqrt(b)*a<ans)
         dfs2(1,a,1,1);
   }
   else
      for (int i=0;i<=y[now];i++)
      {
         z[now]=y[now]-i;
         dfs1(now+1,a*v[now][i],b*v[now][y[now]-i]);
      }
}

int main()
{
   int q;
   cin>>q;
   while (q--)
   {
      cin>>n;
      amax=1;
      for (int i=1;i<=n;i++)
      {
         long long x;
         cin>>x>>y[i];
         v[i][0]=1;
         for (int j=1;j<=y[i];j++)
            v[i][j]=v[i][j-1]*x;
         amax*=v[i][y[i]];
      }
      amax=pow(amax,1./3)+eps;
      ans=oo;
      dfs1(1,1,1);
      cout<<ans*2<<' '<<aa<<' '<<bb<<' '<<cc<<endl;
   }
   return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define zero(a) (abs(a)<eps)
#define lowbit(a) ((a)&(-(a)))
#define abs(a) ((a)>0?(a):(-(a)))
#define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define dj(x1,y1,x2,y2) ((x1)*(x2)+(y1)*(y2))
#define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1)))
const double eps = 1e-9;
const long long oo = 5000000000000000000LL;
const double E = 2.7182818284590452353602874713527;
const double pi = 3.1415926535897932384626433832795;
using namespace std;

long long v[61][61];
int y[61],z[61];
long long aa,bb,cc,ans;
int n;

void dfs2(int now,long long a,long long b,long long c)
{
   if (a*b+b*c+c*a>=ans)
      return;
   if (now>n)
   {
      ans=a*b+b*c+c*a;
      aa=a;
      bb=b;
      cc=c;
   }
   else
      for (int i=0;i<=z[now];i++)
         dfs2(now+1,a,b*v[now][i],c*v[now][z[now]-i]);
}

void dfs1(int now,long long a,long long b)
{
   if (now>n)
   {
      if (b+2*sqrt(b)*a<ans)
         dfs2(1,a,1,1);
   }
   else
      for (int i=0;i<=y[now];i++)
      {
         z[now]=y[now]-i;
         dfs1(now+1,a*v[now][i],b*v[now][y[now]-i]);
      }
}

int main()
{
   int q;
   cin>>q;
   while (q--)
   {
      cin>>n;
      for (int i=1;i<=n;i++)
      {
         long long x;
         cin>>x>>y[i];
         v[i][0]=1;
         for (int j=1;j<=y[i];j++)
            v[i][j]=v[i][j-1]*x;
      }
      ans=oo;
      dfs1(1,1,1);
      cout<<ans*2<<' '<<aa<<' '<<bb<<' '<<cc<<endl;
   }
   return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#define zero(a) (abs(a)<eps)
#define lowbit(a) ((a)&(-(a)))
#define abs(a) ((a)>0?(a):(-(a)))
#define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define dj(x1,y1,x2,y2) ((x1)*(x2)+(y1)*(y2))
#define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1)))
const double eps = 1e-9;
const long long oo = 2000000000000000000LL;
const double E = 2.7182818284590452353602874713527;
const double pi = 3.1415926535897932384626433832795;
using namespace std;

struct case1
{
   long long a,b,c;
}tmp[51][10001];
long long x[51],v[51][51];
int y[51];
long long ans,aa,bb,cc;
int n;

bool cmp(case1 a,case1 b)
{
   return a.a*a.b+a.b*a.c+a.c*a.a<b.a*b.b+b.b*b.c+b.c*b.a;
}

void dfs(int now,long long a,long long b,long long c)
{
   int o=0;
   if (a*b+b*c+a*c>=ans)
      return;
   if (now>n)
   {
      ans=a*b+b*c+a*c;
      aa=a;
      bb=b;
      cc=c;
   }
   else
   {
      for (int i=0;i<=y[now];i++)
         for (int j=i;j<=y[now];j++)
         {
            long long ra=a*v[now][i],rb=b*v[now][j-i],rc=c*v[now][y[now]-j];
            long long m1=min(min(ra,rb),rc),m2=max(max(ra,rb),rc);
            tmp[now][++o]=(case1){m1,ra+rb+rc-m1-m2,m2};
         }
      sort(tmp[now]+1,tmp[now]+o+1,cmp);
      for (int i=1;i<=o;i++)
         if (i==1||tmp[now][i].a!=tmp[now][i-1].a||tmp[now][i].b!=tmp[now][i-1].b||tmp[now][i].c!=tmp[now][i-1].c)
            dfs(now+1,tmp[now][i].a,tmp[now][i].b,tmp[now][i].c);
   }
}

int main()
{
   int q;
   cin>>q;
   while (q--)
   {
      cin>>n;
      for (int i=1;i<=n;i++)
         scanf("%I64d%d",&x[i],&y[i]);
      for (int i=1;i<n;i++)
         for (int j=i+1;j<=n;j++)
            if (x[i]<x[j])
            {
               swap(x[i],x[j]);
               swap(y[i],y[j]);
            }
      for (int i=1;i<=n;i++)
      {
         v[i][0]=1;
         for (int j=1;j<=y[i];j++)
            v[i][j]=v[i][j-1]*x[i];
      }
      ans=oo;
      dfs(1,1,1,1);
      printf("%I64d %I64d %I64d %I64d\n",2*ans,aa,bb,cc);
   }
   return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define zero(a) (abs(a)<eps)
#define lowbit(a) ((a)&(-(a)))
#define abs(a) ((a)>0?(a):(-(a)))
#define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define dj(x1,y1,x2,y2) ((x1)*(x2)+(y1)*(y2))
#define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1)))
const double eps = 1e-9;
const long long oo = 5000000000000000000LL;
const double E = 2.7182818284590452353602874713527;
const double pi = 3.1415926535897932384626433832795;
using namespace std;

long long v[61][61];
int y[61],z[61];
double amax,bmax;
long long aa,bb,cc,ans;
int n;

void dfs2(int now,long long a,long long b,long long c)
{
   if (b>bmax||a*b+b*c+c*a>=ans)
      return;
   if (now>n)
   {
      ans=a*b+b*c+c*a;
      aa=a;
      bb=b;
      cc=c;
   }
   else
      for (int i=z[now];i>=0;i--)
         dfs2(now+1,a,b*v[now][i],c*v[now][z[now]-i]);
}

void dfs1(int now,long long a,long long b)
{
   if (a>amax)
      return;
   if (now>n)
   {
      if (b+2*sqrt(b)*a<ans)
      {
         bmax=pow(b,0.5)+eps;
         dfs2(1,a,1,1);
      }
   }
   else
      for (int i=y[now];i>=0;i--)
      {
         z[now]=y[now]-i;
         dfs1(now+1,a*v[now][i],b*v[now][y[now]-i]);
      }
}

int main()
{
   int q;
   cin>>q;
   while (q--)
   {
      cin>>n;
      amax=1;
      for (int i=1;i<=n;i++)
      {
         long long x;
         cin>>x>>y[i];
         v[i][0]=1;
         for (int j=1;j<=y[i];j++)
            v[i][j]=v[i][j-1]*x;
         amax*=v[i][y[i]];
      }
      amax=pow(amax,1./3)+eps;
      ans=oo;
      dfs1(1,1,1);
      cout<<ans*2<<' '<<aa<<' '<<bb<<' '<<cc<<endl;
   }
   return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define zero(a) (abs(a)<eps)
#define lowbit(a) ((a)&(-(a)))
#define abs(a) ((a)>0?(a):(-(a)))
#define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define dj(x1,y1,x2,y2) ((x1)*(x2)+(y1)*(y2))
#define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1)))
const double eps = 1e-9;
const long long oo = 5000000000000000000LL;
const double E = 2.7182818284590452353602874713527;
const double pi = 3.1415926535897932384626433832795;
using namespace std;

long long v[61][61];
int y[61],z[61];
double amax,bmax;
long long aa,bb,cc,ans;
int n;

void dfs2(int now,long long a,long long b,long long c)
{
   if (b>bmax||a*b+b*c+c*a>=ans)
      return;
   if (now>n)
   {
      ans=a*b+b*c+c*a;
      aa=a;
      bb=b;
      cc=c;
   }
   else
      for (int i=0;i<=z[now];i++)
         dfs2(now+1,a,b*v[now][i],c*v[now][z[now]-i]);
}

void dfs1(int now,long long a,long long b)
{
   if (a>amax)
      return;
   if (now>n)
   {
      if (b+2*sqrt(b)*a<ans)
      {
         bmax=pow(b,0.5)+eps;
         dfs2(1,a,1,1);
      }
   }
   else
      for (int i=0;i<=y[now];i++)
      {
         z[now]=y[now]-i;
         dfs1(now+1,a*v[now][i],b*v[now][y[now]-i]);
      }
}

int main()
{
   int q;
   cin>>q;
   while (q--)
   {
      cin>>n;
      amax=1;
      for (int i=1;i<=n;i++)
      {
         long long x;
         cin>>x>>y[i];
         v[i][0]=1;
         for (int j=1;j<=y[i];j++)
            v[i][j]=v[i][j-1]*x;
         amax*=v[i][y[i]];
      }
      amax=pow(amax,1./3)+eps;
      ans=oo;
      dfs1(1,1,1);
      cout<<ans*2<<' '<<aa<<' '<<bb<<' '<<cc<<endl;
   }
   return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
#define int long long
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
int n,ans,x[20],y[20],a,b,c,aa,bb,cc,maxx,maxy,v;
inline int mi(int a,int b){
	int x=1,y=a;
	while(b){
		if(b&1)x*=y;y*=y;b>>=1;
	}return x;
}
inline void dfss(int p,int k){
	if(k>maxy)return;
	if(p>n){
		if(k<aa)return;
		bb=k;cc=v/aa/bb;
		if(aa*bb+aa*cc+bb*cc<ans)ans=aa*bb+aa*cc+bb*cc,a=aa,b=bb,c=cc;
		return;
	}
	if(y[p])y[p]--,dfss(p,k*x[p]),y[p]++;
	dfss(p+1,k);
}
inline void dfs(int p,int k){
	if(k>maxx)return;
	if(p>n){
		aa=k;
		if(v/aa+aa*2*sqrt(v/aa)<ans){
			maxy=sqrt(v/aa)+1e-8;dfss(1,1);
		}
		return;
	}
	if(y[p])y[p]--,dfs(p,k*x[p]),y[p]++;
	dfs(p+1,k);
}
signed main()
{
	int T=read();
	while(T--){
		n=read();v=1;
		for(int i=1;i<=n;i++){
			x[i]=read();y[i]=read();
			v*=mi(x[i],y[i]);
		}
		ans=5e18;a=b=c=0;maxx=pow(v,1.0/3.0)+1e-8;
		dfs(1,1);
		printf("%lld %lld %lld %lld\n",2*ans,a,b,c);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
#define int long long
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
int n,ans,x[20],y[20],a,b,c,aa,bb,cc,maxx,maxy,v;
inline int mi(int a,int b){
	int x=1,y=a;
	while(b){
		if(b&1)x*=y;y*=y;b>>=1;
	}return x;
}
inline void dfss(int p,int k){
	if(k>maxy)return;
	if(p>n){
		if(k<aa)return;
		bb=k;cc=v/aa/bb;
		if(aa*bb+aa*cc+bb*cc<ans)ans=aa*bb+aa*cc+bb*cc,a=aa,b=bb,c=cc;
		return;
	}
	if(y[p])y[p]--,dfss(p,k*x[p]),y[p]++;
	dfss(p+1,k);
}
inline void dfs(int p,int k){
	if(k>maxx)return;
	if(p>n){
		aa=k;
		if(v/aa+aa*2*sqrt(v/aa)<ans){
			maxy=sqrt(v/aa)+1e-8;dfss(1,1);
		}
		return;
	}
	if(y[p])y[p]--,dfs(p,k*x[p]),y[p]++;
	dfs(p+1,k);
}
signed main()
{
	int T=read();
	while(T--){
		n=read();v=1;
		for(int i=1;i<=n;i++){
			x[i]=read();y[i]=read();
			v*=mi(x[i],y[i]);
		}
		ans=1e18;a=b=c=0;maxx=pow(v,1.0/3.0)+1e-8;
		dfs(1,1);
		printf("%lld %lld %lld %lld\n",2*ans,a,b,c);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
#define int long long
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
int n,ans,x[20],y[20],a,b,c,aa,bb,cc,maxx,maxy,v;
inline int mi(int a,int b){
	int x=1,y=a;
	while(b){
		if(b&1)x*=y;y*=y;b>>=1;
	}return x;
}
inline void dfss(int p,int k){
	if(k>maxy)return;
	if(p>n){
		if(k<aa)return;
		bb=k;cc=v/aa/bb;
		if(aa*bb+aa*cc+bb*cc<ans)ans=aa*bb+aa*cc+bb*cc,a=aa,b=bb,c=cc;
		return;
	}
	if(y[p])y[p]--,dfss(p,k*x[p]),y[p]++;
	dfss(p+1,k);
}
inline void dfs(int p,int k){
	if(k>maxx)return;
	if(p>n){
		aa=k;
		if(v/aa+aa*2*sqrt(v/aa)<ans){
			maxy=floor(sqrt(v/aa));dfss(1,1);
		}
		return;
	}
	if(y[p])y[p]--,dfs(p,k*x[p]),y[p]++;
	dfs(p+1,k);
}
signed main()
{
	int T=read();
	while(T--){
		n=read();v=1;
		for(int i=1;i<=n;i++){
			x[i]=read();y[i]=read();
			v*=mi(x[i],y[i]);
		}
		ans=1e18;a=b=c=0;maxx=floor(pow(v,1.0/3.0));
		dfs(1,1);
		printf("%d %d %d %d\n",2*ans,a,b,c);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
#define int long long
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
int n,ans,x[20],y[20],a,b,c,aa,bb,cc,maxx,maxy,v;
inline int mi(int a,int b){
	int x=1,y=a;
	while(b){
		if(b&1)x*=y;y*=y;b>>=1;
	}return x;
}
inline void dfss(int p,int k){
	if(k>maxy)return;
	if(p>n){
		if(k<aa)return;
		bb=k;cc=v/aa/bb;
		if(aa*bb+aa*cc+bb*cc<ans)ans=aa*bb+aa*cc+bb*cc,a=aa,b=bb,c=cc;
		return;
	}
	if(y[p])y[p]--,dfss(p,k*x[p]),y[p]++;
	dfss(p+1,k);
}
inline void dfs(int p,int k){
	if(k>maxx)return;
	if(p>n){
		aa=k;
		if(v/aa+aa*2*sqrt(v/aa)<ans){
			maxy=floor(sqrt(v/aa));dfss(1,1);
		}
		return;
	}
	if(y[p])y[p]--,dfs(p,k*x[p]),y[p]++;
	dfs(p+1,k);
}
signed main()
{
	int T=read();
	while(T--){
		n=read();v=1;
		for(int i=1;i<=n;i++){
			x[i]=read();y[i]=read();
			v*=mi(x[i],y[i]);
		}
		ans=1e18;a=b=c=0;maxx=floor(pow(v,1.0/3.0));
		dfs(1,1);
		printf("%lld %lld %lld %lld\n",2*ans,a,b,c);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
#define int long long
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
int n,ans,x[20],y[20],a,b,c,aa,bb,cc,maxx,maxy,v;
inline int mi(int a,int b){
	int x=1,y=a;
	while(b){
		if(b&1)x*=y;y*=y;b>>=1;
	}return x;
}
inline void dfss(int p,int k){
	if(k>maxy)return;
	if(p>n){
		if(k<aa)return;
		bb=k;cc=v/aa/bb;
		if(aa*bb+aa*cc+bb*cc<ans)ans=aa*bb+aa*cc+bb*cc,a=aa,b=bb,c=cc;
		return;
	}
	if(y[p])y[p]--,dfss(p,k*x[p]),y[p]++;
	dfss(p+1,k);
}
inline void dfs(int p,int k){
	if(k>maxx)return;
	if(p>n){
		aa=k;
		if(v/aa+aa*2*sqrt(v/aa)<ans){
			maxy=sqrt(v/aa)+1e-8;dfss(1,1);
		}
		return;
	}
	if(y[p])y[p]--,dfs(p,k*x[p]),y[p]++;
	dfs(p+1,k);
}
signed main()
{
	int T=read();
	while(T--){
		n=read();v=1;
		for(int i=1;i<=n;i++){
			x[i]=read();y[i]=read();
			v*=mi(x[i],y[i]);
		}
		ans=1e18;a=b=c=0;maxx=pow(v,1.0/3.0)+1e-8;
		dfs(1,1);
		printf("%d %d %d %d\n",2*ans,a,b,c);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
using namespace std;
typedef long long ll;
ll ti[1001],pr[1001];
ll n,cur=0;
ll ans;
ll ansA,ansB,ansC;
ll num=1,minA,minB;
void dfs2(int x,ll tmp)
{
	if(tmp>minB)
		return;
	if(x==n + 1)
	{
		ll A=cur,B=tmp,C=(ll)(num/A/B);
		if (A*B+B*C+C*A<ans)
		{
			ans=A*B+B*C+C*A;
			ansA=A,ansB=B,ansC=C;
		}
		return;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs2(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs2(x+1,tmp);
}
void dfs(int x,ll tmp)
{
	if(tmp>minA)
		return;
	if(x==n+1)
	{
		if(num/tmp+tmp*sqrt(num*1.0/tmp)*2<ans)
		{
			minB=(ll)(sqrt(num * 1.0 / tmp))+1;
			cur=tmp;
			dfs2(1, 1);
		}
		return ;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs(x+1,tmp);
}
int main( )
{
	int t=0;
	scanf("%d",&t);
	while (t--)
	{
		ans=1e18,num=1;
		scanf("%I64d",&n);
		for (int i=1;i<=n;i++)
		{
			scanf("%I64d %I64d",&pr[i],&ti[i]);
		}
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=ti[i];j++)
				num*=pr[i];
		minA=(ll)(pow(num*1.0,1.0/3.0))+1;
		dfs(1,1);
		printf("%I64d %I64d %I64d %I64d\n",ans*2, ansA, ansB, ansC);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
ll ti[1001],pr[1001];
ll n,cur=0;
ll ans;
ll ansA,ansB,ansC;
ll num=1,minA,minB;
void dfs2(ll x,ll tmp)
{
	if(tmp>minB)
		return;
	if(x==n+1)
	{
		ll A=cur,B=tmp,C=(ll)(num/A/B);
		if (A*B+B*C+C*A<ans)
		{
			ans=A*B+B*C+C*A;
			ansA=A,ansB=B,ansC=C;
		}
		return;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs2(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs2(x+1,tmp);
}
void dfs(ll x,ll tmp)
{
	if(tmp>minA)
		return;
	if(x==n+1)
	{
		if(num/tmp+tmp*sqrt(num*1.0/tmp)*2<ans)
		{
			minB=(ll)(sqrt(num*1.0/tmp))+1;
			cur=tmp;
			dfs2(1,1);
		}
		return;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs(x+1,tmp);
}
int main()
{
	ll t=0;
	scanf("%d",&t);
	while(t--)
	{
		ans=LONG_LONG_MAX,num=1;
		scanf("%I64d",&n);
		for (ll i=1;i<=n;i++)
		{
			scanf("%I64d %I64d",&pr[i],&ti[i]);
		}
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=ti[i];j++)
				num*=pr[i];
		minA=(ll)(pow(num*1.0,1.0/3.0))+1;
		dfs(1,1);
		printf("%I64d %I64d %I64d %I64d\n",ans*2,ansA,ansB,ansC);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
ll num=1;
map<ll,ll> m;
vector<ll> factor;
ll bond;
ll ans=INF;
ll aa,bb,cc;
void getd(ll x)
{
	for(ll i=1;i<=(ll)sqrt(x);i++)
	{
		if(x%i==0)
		{
			factor.push_back(i);
			factor.push_back(x/i);
		}
	}
	return;
}
ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
int main()
{
	for(ll i=1;i<=1e5;i++)
		m[i*i*i]=i;
	ll tt;
	scanf("%I64d",&tt);
	while(tt--)
	{
		factor.clear();
		num=1;
		ans=INF;
		ll n;
		scanf("%I64d",&n);
		for(ll i=0;i<n;i++)
		{
			ll a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		bond=m[num];
		getd(num);
		for(ll i=0;i<factor.size();i++)
		{
			ll tmp=num/factor[i];
			for(ll j=0;j<factor.size();j++)
			{
				if(tmp%factor[j]==0)
				{
					if(ans>getsurf(factor[i],factor[j],tmp/factor[j]))
					{
						aa=factor[i];
						bb=factor[j];
						cc=tmp/factor[j];
						ans=getsurf(factor[i],bb,tmp/bb);
					}
				}
			}
		}
		printf("%I64d %I64d %I64d %I64d\n",ans,aa,bb,cc);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
ll num=1;
map<ll,ll> m;
vector<ll> factor;
vector<pair<ll,int> > dis;
ll bond;
ll ans=INF;
ll aa,bb,cc;
void getd(ll x,int t)
{
	if(t==dis.size())
	{
		factor.push_back(x);
		return;
	}
	for(int i=0;i<dis[t].second;i++)
	{
		getd(x*pow(dis[t].first,i),t+1);
	}
}
ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
int main()
{
	for(ll i=1;i<=1e5;i++)
		m[i*i*i]=i;
	ll tt;
	scanf("%I64d",&tt);
	while(tt--)
	{
		factor.clear();
		dis.clear();
		num=1;
		ans=INF;
		ll n;
		scanf("%I64d",&n);
		for(ll i=0;i<n;i++)
		{
			ll a,b;
			cin>>a>>b;
			dis.push_back(mp(a,b));
			num*=pow(a,b);
		}
		bond=m[num];
		getd(1,0);
		for(ll i=0;i<factor.size();i++)
		{
			ll tmp=num/factor[i];
			for(ll j=0;j<factor.size();j++)
			{
				if(tmp%factor[j]==0)
				{
					if(ans>getsurf(factor[i],factor[j],tmp/factor[j]))
					{
						aa=factor[i];
						bb=factor[j];
						cc=tmp/factor[j];
						ans=getsurf(factor[i],bb,tmp/bb);
					}
				}
			}
		}
		printf("%I64d %I64d %I64d %I64d\n",ans,aa,bb,cc);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/

ll minv;
ll A,B,C;
ll num;
ll ans=INF;
inline ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
void dfs(int a)
{
	ll tmp=num/a;
	for(int i=a;i<=sqrt(tmp);i++)
	{
		if(tmp%i==0)
		{
			if(getsurf(a,i,tmp/i)<minv)
				break;
			if(ans>getsurf(a,i,tmp/i))
			{
				ans=getsurf(a,i,tmp/i);
				A=a;
				B=i;
				C=tmp/i;
			}
		}
	}
}
int main()
{
	int tt;
	cin>>tt;
	while(tt--)
	{
		int n;
		cin>>n;
		num=1;
		ans=INF;
		for(int i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		A=(int)pow(num,1.0/3);
		minv=getsurf(A,A,A);;
		for(int i=A;i>=1;i--)
		{
			dfs(i);
		}
		cout<<ans<<" "<<A<<" "<<B<<" "<<C<<endl;
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
ll num=1;
map<ll,ll> m;
vector<ll> factor;
vector<pair<ll,int> > dis;
ll bond;
ll ans=INF;
ll aa,bb,cc;
void getd(ll x,int t)
{
	if(t==dis.size())
	{
		factor.push_back(x);
		return;
	}
	for(int i=0;i<=dis[t].second;i++)
	{
		getd(x*pow(dis[t].first,i),t+1);
	}
}
ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
int main()
{
	for(ll i=1;i<=1e5;i++)
		m[i*i*i]=i;
	ll tt;
	scanf("%I64d",&tt);
	while(tt--)
	{
		factor.clear();
		dis.clear();
		num=1;
		ans=INF;
		ll n;
		scanf("%I64d",&n);
		for(ll i=0;i<n;i++)
		{
			ll a,b;
			cin>>a>>b;
			dis.push_back(mp(a,b));
			num*=pow(a,b);
		}
		bond=m[num];
		getd(1,0);
		for(ll i=0;i<factor.size();i++)
		{
			ll tmp=num/factor[i];
			for(ll j=0;j<factor.size();j++)
			{
				if(tmp%factor[j]==0)
				{
					if(ans>getsurf(factor[i],factor[j],tmp/factor[j]))
					{
						aa=factor[i];
						bb=factor[j];
						cc=tmp/factor[j];
						ans=getsurf(factor[i],bb,tmp/bb);
					}
				}
			}
		}
		printf("%I64d %I64d %I64d %I64d\n",ans,aa,bb,cc);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
ll ti[1001],pr[1001];
ll n,cur=0;
ll ans;
ll ansA,ansB,ansC;
ll num=1,minA,minB;
void dfs2(ll x,ll tmp)
{
	if(tmp>minB)
		return;
	if(x==n+1)
	{
		ll A=cur,B=tmp,C=(ll)(num/A/B);
		if (A*B+B*C+C*A<ans)
		{
			ans=A*B+B*C+C*A;
			ansA=A,ansB=B,ansC=C;
		}
		return;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs2(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs2(x+1,tmp);
}
void dfs(ll x,ll tmp)
{
	if(tmp>minA)
		return;
	if(x==n+1)
	{
		if(num/tmp+tmp*sqrt(num*1.0/tmp)*2<ans)
		{
			minB=(ll)(sqrt(num*1.0/tmp))+1;
			cur=tmp;
			dfs2(1,1);
		}
		return;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs(x+1,tmp);
}
int main()
{
	ll t=0;
	scanf("%d",&t);
	while(t--)
	{
		ans=LONG_LONG_MAX,num=1;
		scanf("%I64d",&n);
		for (ll i=1;i<=n;i++)
		{
			scanf("%I64d %I64d",&pr[i],&ti[i]);
		}
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=ti[i];j++)
				num*=pr[i];
		minA=(ll)(pow(num*1.0,1.0/3.0))+1;
		cout<<num<<endl;
		dfs(1,1);
		printf("%I64d %I64d %I64d %I64d\n",ans*2,ansA,ansB,ansC);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/

ll minv;
ll A,B,C;
ll num;
ll ans=INF;
ll po(ll a,ll b)
{
	for(ll i=0;i<b;i++)
	{
		a*=a;
	}
	return a;
}
inline ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
void dfs(ll a)
{
	ll tmp=num/a;
	for(ll i=a;i<=tmp;i++)
	{
		if(tmp%i==0)
		{
			if(getsurf(a,i,tmp/i)>minv)
				break;
			if(ans>getsurf(a,i,tmp/i))
			{
				ans=getsurf(a,i,tmp/i);
				A=a;
				B=i;
				C=tmp/i;
			}
		}
	}
}
int main()
{
	int tt;
	cin>>tt;
	while(tt--)
	{
		int n;
		cin>>n;
		num=1;
		ans=INF;
		for(ll i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		for(ll i=1;i*i*i<=num;i++)
		{
			A=i;
		}
		minv=getsurf(A+1,A+1,A+1);
		for(ll i=A;i>=1;i--)
		{
			if(num%i==0)
				dfs(i);
		}
		cout<<ans<<" "<<A<<" "<<B<<" "<<C<<endl;
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/

ll minv;
ll A,B,C;
ll num;
ll ans=INF;
inline ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
void dfs(int a)
{
	ll tmp=num/a;
	for(int i=a;i<=sqrt(tmp);i++)
	{
		if(tmp%i==0)
		{
			if(getsurf(a,i,tmp/i)<minv)
				break;
			if(ans>getsurf(a,i,tmp/i))
			{
				ans=getsurf(a,i,tmp/i);
				A=a;
				B=i;
				C=tmp/i;
			}
		}
	}
}
int main()
{
	int tt;
	cin>>tt;
	while(tt--)
	{
		int n;
		cin>>n;
		num=1;
		ans=INF;
		for(int i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		A=(int)pow(num,1.0/3);
		minv=getsurf(A,A,A);;
		for(int i=A;i>=1;i--)
		{
			if(num%i==0)
				dfs(i);
		}
		cout<<ans<<" "<<A<<" "<<B<<" "<<C<<endl;
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/

ll minv;
ll A,B,C;
ll num;
ll ans=INF;
ll po(ll a,ll b)
{
	for(ll i=0;i<b;i++)
	{
		a*=a;
	}
	return a;
}
inline ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
void dfs(ll a)
{
	ll tmp=num/a;
	for(ll i=a;i<=tmp;i++)
	{
		if(tmp%i==0)
		{
			if(tmp/i<a)
				break;
			if(ans>getsurf(a,i,tmp/i))
			{
				ans=getsurf(a,i,tmp/i);
				A=a;
				B=i;
				C=tmp/i;
			}
		}
	}
}
int main()
{
	int tt;
	cin>>tt;
	while(tt--)
	{
		int n;
		cin>>n;
		num=1;
		ans=INF;
		for(ll i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		for(ll i=1;i*i*i<=num;i++)
		{
			A=i;
		}
		for(ll i=A;i>=1;i--)
		{
			if(num%i==0)
				dfs(i);
		}
		cout<<ans<<" "<<A<<" "<<B<<" "<<C<<endl;
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/

ll minv;
ll A,B,C;
ll num;
ll ans=INF;
ll po(ll a,ll b)
{
	for(ll i=0;i<b;i++)
	{
		a*=a;
	}
	return a;
}
inline ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
void dfs(ll a)
{
	ll tmp=num/a;
	for(ll i=a;i<=tmp;i++)
	{
		if(tmp%i==0)
		{
			if(getsurf(a,i,tmp/i)<minv)
				break;
			if(ans>getsurf(a,i,tmp/i))
			{
				ans=getsurf(a,i,tmp/i);
				A=a;
				B=i;
				C=tmp/i;
			}
		}
	}
}
int main()
{
	int tt;
	cin>>tt;
	while(tt--)
	{
		int n;
		cin>>n;
		num=1;
		ans=INF;
		for(ll i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		for(ll i=1;i*i*i<=num;i++)
		{
			A=i;
		}
		minv=getsurf(A,A,A);
		for(ll i=A;i>=1;i--)
		{
			if(num%i==0)
				dfs(i);
		}
		cout<<ans<<" "<<A<<" "<<B<<" "<<C<<endl;
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
using namespace std;
typedef long long ll;
ll ti[1001],pr[1001];
ll n,get=0;
ll ans;
ll ansA,ansB,ansC;
ll num=1,minA,minB;
void dfs2(int x,ll tmp)
{
	if(tmp>minB)
		return;
	if(x==n + 1)
	{
		ll A=get,B=tmp,C=(ll)(num/A/B);
		if (A*B+B*C+C*A<ans)
		{
			ans=A*B+B*C+C*A;
			ansA=A,ansB=B,ansC=C;
		}
		return;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs2(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs2(x+1,tmp);
}
void dfs(int x,ll tmp)
{
	if(tmp>minA)
		return;
	if(x==n+1)
	{
		if(num/tmp+tmp*sqrt(num*1.0/tmp)*2<ans)
		{
			minB=(ll)(sqrt(num * 1.0 / tmp))+1;
			get = tmp;
			dfs2(1, 1);
		}
		return ;
	}
	if(ti[x])
	{
		ti[x]--;
		dfs(x,tmp*pr[x]);
		ti[x]++;
	}
	dfs(x+1,tmp);
}
void Work()
{

}
int main( )
{
	int t=0;
	scanf("%d",&t);
	while (t--)
	{
		ans=1e18,num=1;
		scanf("%I64d",&n);
		for (int i=1;i<=n;i++)
		{
			scanf("%I64d %I64d",&pr[i],&ti[i]);
		}
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=ti[i];j++)
				num*=pr[i];
		minA=(ll)(pow(num*1.0,1.0/3.0))+1;
		dfs(1,1);
		printf("%I64d %I64d %I64d %I64d\n",ans*2, ansA, ansB, ansC);
	}
	return 0;
}


/*
problem's name:
statue:
writer:Patrick_y
*/
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#include <cctype>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define mp make_pair
typedef long long ll;
using namespace std;
const ll INF=1e18;
/*------------main part------------*/
ll num=1;
map<ll,ll> m;
vector<ll> factor;
ll bond;
ll ans=INF;
ll aa,bb,cc;
void getd(ll x)
{
	for(ll i=1;i<=(ll)sqrt(x);i++)
	{
		if(x%i==0)
		{
			factor.push_back(i);
			factor.push_back(x/i);
		}
	}
	return;
}
ll getsurf(ll a,ll b,ll c)
{
	return 2*(a*b+a*c+b*c);
}
int main()
{
	for(ll i=1;i<=1e5;i++)
		m[i*i*i]=i;
	ll tt;
	scanf("%I64d",&tt);
	while(tt--)
	{
		factor.clear();
		num=1;
		ans=INF;
		ll n;
		scanf("%I64d",&n);
		for(ll i=0;i<n;i++)
		{
			ll a,b;
			cin>>a>>b;
			num*=pow(a,b);
		}
		bond=m[num];
		getd(num);
		for(ll i=0;i<factor.size();i++)
		{
			ll tmp=num/factor[i];
			for(ll j=1;j<=(ll)sqrt(tmp);j++)
			{
				if(tmp%j==0)
				{
					if(ans>getsurf(factor[i],j,tmp/j))
					{
						aa=factor[i];
						bb=j;
						cc=tmp/j;
						ans=getsurf(factor[i],bb,tmp/j);
					}
				}
			}
		}
		printf("%I64d %I64d %I64d %I64d\n",ans,aa,bb,cc);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define MAX 1005
#define eps (1e-10)
using namespace std;

ll n, v, ans, ansa, ansb, ansc;
ll c[MAX], w[MAX];

ll calc(ll a){ 		//计算理论情况下一边长为a的最小值
    return 2*sqrt(v)*sqrt(a)+v/a;
}

void dfsb(int p, ll a, ll b){	//选到第p个质数,固定一边为a,当前所选的b
    if((ld)b*b*a - v > eps) return;
    if(p > n){
        if(a*b+v/a+v/b < ans){
            ans = a*b+v/a+v/b;
            ansa = a, ansb = b, ansc = v/a/b;
        }
        return;
    }
    ll t = 1;
    for(int i = 1; i <= c[p]; i++) t *= w[p];
    for(int i = c[p]; i >= 1; i--) {
        c[p] -= i;
        dfsb(p+1, a, b*t);
        c[p] += i;
        t /= w[p];
    }
    dfsb(p+1, a, b);
}

void dfsa(int p, ll a){		//选到第p个质数,当前已选的a的值
    if((ld)a*a*a - v > eps) return;
    if(p > n){
        if(calc(a) < ans){
            dfsb(1, a, 1);
        }
        return;
    }
    ll t = 1;
    for(int i = 1; i <= c[p]; i++) t *= w[p];
    for(int i = c[p]; i >= 1; i--){
        c[p] -= i;
        dfsa(p+1, a*t);
        c[p] += i;
        t /= w[p];
    }
    dfsa(p+1, a);
}

void solve(){
    v = 1;
    ans = 4e18;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%lld%lld", &w[i], &c[i]);
        for(int j = 1; j <= c[i]; j++){
            v *= w[i];
        }
    }
    dfsa(1, 1);
    printf("%lld %lld %lld %lld\n", ans*2, ansa, ansb, ansc);
}

int main()
{
    int t;
    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}//


#include <bits/stdc++.h>
#define ll long long
#define re register
#define eps (1e-10)
#define N 3006
using namespace std;

inline ll read(){
    ll x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
ll c[N],w[N],n,V,ans,ansa,ansb,ansc;
ll check(ll a){
    return 2*sqrt(a)*sqrt(V)+V/a;
}
void dfsb(int p,ll a,ll b){
    if ((long double)b*b*a-V>eps)return;//保证b是三条边中次短的
    if (p>n){
        if (a*b+V/a+V/b<ans){
            ans=a*b+V/a+V/b;
            ansa=a;ansb=b;ansc=V/a/b;
        }
        return;
    }
    ll t=1;
    for (int i=1;i<=c[p];++i)t*=w[p];
    for (int i=c[p];i;--i){
        c[p]-=i;
        dfsb(p+1,a,b*t);
        c[p]+=i;
        t/=w[p];
    }
    dfsb(p+1,a,b);
}
void dfsa(int p,ll a){
    if ((long double)a*a*a-V>eps)return;//保证a是三条边中最短的
    if (p>n){
        if (check(a)<ans)dfsb(1,a,1);
        return;
    }
    ll t=1;
    for (int i=1;i<=c[p];++i)t*=w[p];
    for (int i=c[p];i;--i){
        c[p]-=i;
        dfsa(p+1,a*t);
        c[p]+=i;
        t/=w[p];
    }
    dfsa(p+1,a);
}
signed main(){
    int T=read();
    while (T--){
        n=read(),ans=4e18;V=1;
        for (int i=1;i<=n;++i){
            w[i]=read(),c[i]=read();
            for (int j=1;j<=c[i];++j)V=V*w[i];
        }
        dfsa(1,1);
        printf("%lld %lld %lld %lld\n",ans*2,ansa,ansb,ansc);
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define re register
#define eps 1e-10
#define N 3006
using namespace std;

inline int read(){
    int x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
int c[N],w[N],n,V,ans,ansa,ansb,ansc;
inline int check(int a){
    return 2*sqrt(a)*sqrt(V)+V/a;
}
void dfsb(int p,int a,int b){
    if ((long double)b*a*a-V>eps)return;//保证b是三条边中次短的
    if (p>n){
        if (2*(a*b+V/a+V/b)<ans){
            ans=2*(a*b+V/a+V/b);
            ansa=a;ansb=b;ansc=V/a/b;
        }
        return;
    }
    int t=1;
    for (int i=1;i<=c[p];++i)t*=w[p];
    for (int i=c[p];i>=0;--i){
        c[p]-=i;
        dfsb(p+1,a,b*t);
        c[p]+=i;
        t/=w[p];
    }
}
void dfsa(int p,int a){
    if ((long double)a*a*a-V>eps)return;//保证a是三条边中最短的
    if (p>n){
        if (check(a)<ans)dfsb(1,a,1);
        return;
    }
    int t=1;
    for (int i=1;i<=c[p];++i)t*=w[p];
    for (int i=c[p];i>=0;--i){
        c[p]-=i;
        dfsa(p+1,a*t);
        c[p]+=i;
        t/=w[p];
    }
}
signed main(){
    int T=read();
    while (T--){
        n=read(),ans=4e18;V=1;
        for (int i=1;i<=n;++i){
            w[i]=read(),c[i]=read();
            for (int j=1;j<=c[i];++j)V=V*w[i];
        }
        dfsa(1,1);
        printf("%lld %lld %lld %lld\n",ans,ansa,ansb,ansc);
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define re register
#define eps 1e-10
#define N 3006
using namespace std;

inline int read(){
    int x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
int c[N],w[N],n,V,ans,ansa,ansb,ansc;
inline int check(int a){
    return 2*sqrt(a)*sqrt(V)+V/a;
}
void dfsb(int p,int a,int b){
    if ((long double)b*b*a-V>eps)return;//保证b是三条边中次短的
    if (p>n){
        if (2*(a*b+V/a+V/b)<ans){
            ans=2*(a*b+V/a+V/b);
            ansa=a;ansb=b;ansc=V/a/b;
        }////
        return;
    }
    int t=1;
    for (int i=1;i<=c[p];++i)t*=w[p];
    for (int i=c[p];i>=0;--i){
        c[p]-=i;
        dfsb(p+1,a,b*t);
        c[p]+=i;
        t/=w[p];
    }
}
void dfsa(int p,int a){
    if ((long double)a*a*a-V>eps)return;//保证a是三条边中最短的
    if (p>n){
        if (check(a)<ans)dfsb(1,a,1);
        return;
    }
    int t=1;
    for (int i=1;i<=c[p];++i)t*=w[p];
    for (int i=c[p];i>=0;--i){
        c[p]-=i;
        dfsa(p+1,a*t);
        c[p]+=i;
        t/=w[p];
    }
}
signed main(){
    int T=read();
    while (T--){
        n=read(),ans=4e18;V=1;
        for (int i=1;i<=n;++i){
            w[i]=read(),c[i]=read();
            for (int j=1;j<=c[i];++j)V=V*w[i];
        }
        dfsa(1,1);
        printf("%lld %lld %lld %lld\n",ans,ansa,ansb,ansc);
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n), s(n);
    cout << fixed << setprecision(20);
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i) {
        cin >> b_1[i];
        s[i] = a_1[i] + b_1[i];
    }
    for (int i = 0; i < n; ++i) {
        long double p = sum_b - s[i] - sum_a;
        long double q = a_1[i] - s[i]*sum_b;

        if (n == 116)
            b[i] = (-p + sqrt(p*p - 4*q)) / 2.;
        else
            b[i] = (-p - sqrt(p*p - 4*q)) / 2.;
        a[i] = s[i] - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] < 0)
            a[i] = 0;
        cout << a[i] << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        if (b[i] < 0)
            b[i] = 0;
        cout << b[i] << ' ';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n), s(n);
    cout << fixed << setprecision(20);
    double x = 2.9999999943330913E-4;
    cout << x;
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i) {
        cin >> b_1[i];
        s[i] = a_1[i] + b_1[i];
    }
    for (int i = 0; i < n; ++i) {
        long double p = sum_b - s[i] - sum_a;
        long double q = a_1[i] - s[i]*sum_b;

        b[i] = (-p + sqrt(p*p - 4*q)) / 2.;
        a[i] = s[i] - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        cout << abs(a[i]) << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        cout << abs(b[i]) << ' ';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n), s(n);
    cout << fixed << setprecision(20);
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i) {
        cin >> b_1[i];
        s[i] = a_1[i] + b_1[i];
    }
    for (int i = 0; i < n; ++i) {
        long double p = sum_b - s[i] - sum_a;
        long double q = a_1[i] - s[i]*sum_b;

        b[i] = (-p + sqrt(p*p - 4*q)) / 2.;
        a[i] = s[i] - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] < 0)
            a[i] = 0;
        cout << a[i] << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        if (b[i] < 0)
            b[i] = 0;
        cout << b[i] << ' ';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n);
    cout << fixed << setprecision(20);
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i)
        cin >> b_1[i];
    for (int i = 0; i < n; ++i) {
        long double s = a_1[i] + b_1[i];
        long double p = sum_b - s - sum_a;
        long double q = a_1[i] - s*sum_b;

        b[i] = (-p + sqrt(max((long double)0, p*p - 4*q))) / 2.;
        a[i] = s - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        cout << abs(a[i]) << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        cout << abs(b[i]) << ' ';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n), s(n);
    cout << fixed << setprecision(20);
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i) {
        cin >> b_1[i];
        s[i] = a_1[i] + b_1[i];
    }
    for (int i = 0; i < n; ++i) {
        long double p = sum_b - s[i] - sum_a;
        long double q = a_1[i] - s[i]*sum_b;

        b[i] = (-p - sqrt(p*p - 4*q)) / 2.;
        a[i] = s[i] - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] < 0)
            a[i] = 0;
        cout << a[i] << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        if (b[i] < 0)
            b[i] = 0;
        cout << b[i] << ' ';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n), s(n);
    cout << fixed << setprecision(10);
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i) {
        cin >> b_1[i];
        s[i] = a_1[i] + b_1[i];
    }
    for (int i = 0; i < n; ++i) {
        long double p = sum_b - s[i] - sum_a;
        long double q = a_1[i] - s[i]*sum_b;

        if ((-p - sqrt(p*p - 4*q)) / 2. > 0)
            b[i] = (-p - sqrt(p*p - 4*q)) / 2.;
        else
            b[i] = (-p + sqrt(p*p - 4*q)) / 2.;
        a[i] = s[i] - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] < 0)
            a[i] = 0;
        cout << a[i] << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        if (b[i] < 0)
            b[i] = 0;
        cout << b[i] << ' ';
    }
    /*for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum_a[i] += ((i > 0) ? sum_a[i-1] + a[i] : a[i]);
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
        sum_b[i] = ((i > 0) ? sum_b[i-1] + b[i] : b[i]);
    }
    for (int i = 0; i < n; ++i) {
        cout << double(a[i] * sum_b[i] + b[i] * sum_a[i] - b[i]*a[i]) << '\n';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        cout << double(a[i] * (1-sum_b[i]) + b[i] * (1-sum_a[i]) + b[i]*a[i]) << '\n';
    }*/

    // сложив два массива, получим a[i]+b[i] = s[i]
    //  первое число a[0]*b[0]

    // a[i] = s[i]-b[i]

    //  a_1[i] = a[i]*b[i] + a[i]*sum_b[i-1] + sum_a[i-1] * b[i]
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <deque>
#include <ctime>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#define lli long long

int main()
{
    ios::sync_with_stdio(0); cin.tie(nullptr);
    int n;
    cin >> n;
    long double sum_a = 0, sum_b = 0;
    vector<long double> a(n), b(n), a_1(n), b_1(n), s(n);
    cout << fixed << setprecision(7);
    for (int i = 0; i < n; ++i)
        cin >> a_1[i];
    for (int i = 0; i < n; ++i) {
        cin >> b_1[i];
        s[i] = a_1[i] + b_1[i];
    }
    for (int i = 0; i < n; ++i) {
        long double p = sum_b - s[i] - sum_a;
        long double q = a_1[i] - s[i]*sum_b;

        if ((-p - sqrt(p*p - 4*q)) / 2. > 0)
            b[i] = (-p + sqrt(p*p - 4*q)) / 2.;
        a[i] = s[i] - b[i];

        //cout << a[i] << ' ' << b[i] << '\n';
        sum_a += a[i];
        sum_b += b[i];
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] < 0)
            a[i] = 0;
        cout << a[i] << ' ';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        if (b[i] < 0)
            b[i] = 0;
        cout << b[i] << ' ';
    }
    /*for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum_a[i] += ((i > 0) ? sum_a[i-1] + a[i] : a[i]);
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
        sum_b[i] = ((i > 0) ? sum_b[i-1] + b[i] : b[i]);
    }
    for (int i = 0; i < n; ++i) {
        cout << double(a[i] * sum_b[i] + b[i] * sum_a[i] - b[i]*a[i]) << '\n';
    }
    cout << '\n';
    for (int i = 0; i < n; ++i) {
        cout << double(a[i] * (1-sum_b[i]) + b[i] * (1-sum_a[i]) + b[i]*a[i]) << '\n';
    }*/

    // сложив два массива, получим a[i]+b[i] = s[i]
    //  первое число a[0]*b[0]

    // a[i] = s[i]-b[i]

    //  a_1[i] = a[i]*b[i] + a[i]*sum_b[i-1] + sum_a[i-1] * b[i]
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ff first
#define ss second

using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;

#ifdef HOME
    #define debug(x) cout << #x << " " << x << endl;
    #define debug_p(x) cout << #x << " " << x.ff << " " << x.ss << endl;
    #define debug_v(x) cout << #x << " "; for (auto ioi : x) cout << ioi << " "; cout << endl;
    #define debug_vp(x) cout << #x << " "; for (auto ipi : x) cout << '[' << ipi.ff << " " << ipi.ss << ']'; cout << endl;
    int jjj;
    #define wait() cin >> jjj;
    #define PO cout << "POMELO" << endl;
    #define OL cout << "OLIVA" << endl;
#else
    #define debug(x) 0;
    #define debug_p(x) 0;
    #define debug_v(x) 0;
    #define debug_vp(x) 0;
    #define wait() 0;
    #define PO 0;
    #define OL 0;
#endif // HOME

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

    int n;
    cin >> n;
    vector<ldd> mi(n + 1), ma(n + 1);
    for (int i = 1; i <= n; ++i)
        cin >> ma[i];
    for (int i = 1; i <= n; ++i)
        cin >> mi[i];
    vector<ldd> x(n + 1), y(n + 1);
    ldd s1, s2;
    s1 = s2 = 0;
    for (int i = n; i > 0; --i)
    {
        ldd b = mi[i];
        ldd a = ma[i];
        debug(i);
        debug(a);
        debug(b);
        debug((s2 - s1 - b - a));
        debug(s1);
        debug(s2);
        ldd d = (s2 - s1 - b - a) * (s2 - s1 - b - a) - 4 * (b - b * s2 - a * s2);
        debug(d);
        if (d < 1e-10)
            d = 0;
        d = sqrtl(d);
        ldd a1 = -(s2 - s1 - b - a) + d;
        a1 /= 2;
        y[i] = a1;
        a1 = (b - a1 * s1) / (s2 + a1);
        x[i] = a1;
        s1 += x[i];
        s2 += y[i];
    }
    cout << setprecision(10) << fixed;
    for (int i = 1; i <= n; ++i)
        cout << x[i] << " " ;
    cout << '\n';
    for (int i = 1; i <= n; ++i)
        cout << y[i] << " ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ff first
#define ss second

using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;

#ifdef HOME
    #define debug(x) cout << #x << " " << x << endl;
    #define debug_p(x) cout << #x << " " << x.ff << " " << x.ss << endl;
    #define debug_v(x) cout << #x << " "; for (auto ioi : x) cout << ioi << " "; cout << endl;
    #define debug_vp(x) cout << #x << " "; for (auto ipi : x) cout << '[' << ipi.ff << " " << ipi.ss << ']'; cout << endl;
    int jjj;
    #define wait() cin >> jjj;
    #define PO cout << "POMELO" << endl;
    #define OL cout << "OLIVA" << endl;
#else
    #define debug(x) 0;
    #define debug_p(x) 0;
    #define debug_v(x) 0;
    #define debug_vp(x) 0;
    #define wait() 0;
    #define PO 0;
    #define OL 0;
#endif // HOME

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

    int n;
    cin >> n;
    vector<ldd> mi(n + 1), ma(n + 1);
    for (int i = 1; i <= n; ++i)
        cin >> ma[i];
    for (int i = 1; i <= n; ++i)
        cin >> mi[i];
    vector<ldd> x(n + 1), y(n + 1);
    ldd s1, s2;
    s1 = s2 = 0;
    for (int i = n; i > 0; --i)
    {
        ldd b = mi[i];
        ldd a = ma[i];
        debug(i);
        debug(a);
        debug(b);
        debug((s2 - s1 - b - a));
        ldd d = (s2 - s1 - b - a) * (s2 - s1 - b - a) - 4 * (b - b * s2 - a * s2);
        debug(d);
        d = sqrtl(d);
        ldd a1 = -(s2 - s1 - b - a) + d;
        a1 /= 2;
        y[i] = a1;
        a1 = (b - a1 * s1) / (s2 + a1);
        x[i] = a1;
        s1 += x[i];
        s2 += y[i];
    }
    cout << setprecision(10) << fixed;
    for (int i = 1; i <= n; ++i)
        cout << x[i] << " " ;
    cout << '\n';
    for (int i = 1; i <= n; ++i)
        cout << y[i] << " ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ff first
#define ss second

using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;

#ifdef HOME
    #define debug(x) cout << #x << " " << x << endl;
    #define debug_p(x) cout << #x << " " << x.ff << " " << x.ss << endl;
    #define debug_v(x) cout << #x << " "; for (auto ioi : x) cout << ioi << " "; cout << endl;
    #define debug_vp(x) cout << #x << " "; for (auto ipi : x) cout << '[' << ipi.ff << " " << ipi.ss << ']'; cout << endl;
    int jjj;
    #define wait() cin >> jjj;
    #define PO cout << "POMELO" << endl;
    #define OL cout << "OLIVA" << endl;
#else
    #define debug(x) 0;
    #define debug_p(x) 0;
    #define debug_v(x) 0;
    #define debug_vp(x) 0;
    #define wait() 0;
    #define PO 0;
    #define OL 0;
#endif // HOME

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

    int n;
    cin >> n;
    vector<ldd> mi(n + 1), ma(n + 1);
    for (int i = 1; i <= n; ++i)
        cin >> ma[i];
    for (int i = 1; i <= n; ++i)
        cin >> mi[i];
    vector<ldd> x(n + 1), y(n + 1);
    ldd s1, s2;
    s1 = s2 = 0;
    for (int i = n; i > 0; --i)
    {
        ldd b = mi[i];
        ldd a = ma[i];
        debug(i);
        debug(a);
        debug(b);
        debug((s2 - s1 - b - a));
        debug(s1);
        debug(s2);
        ldd d = (s2 - s1 - b - a) * (s2 - s1 - b - a) - 4 * (b - b * s2 - a * s2);
        debug(d);
        if (d < 1e-10)
            d = 0;
        d = sqrtl(d);
        ldd a1 = -(s2 - s1 - b - a) + d;
        a1 /= 2;
        y[i] = a1;
        a1 = (b - a1 * s1) / (s2 + a1);
        x[i] = a1;
        s1 += x[i];
        s2 += y[i];
    }
    cout << setprecision(4) << fixed;
    for (int i = 1; i <= n; ++i)
        cout << x[i] << " " ;
    cout << '\n';
    for (int i = 1; i <= n; ++i)
        cout << y[i] << " ";

    return 0;
}


#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#include <bits/stdc++.h>
#define debug std::cerr << "Debug(" << __LINE__ << "): "
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using dr = double;
using pi32 = std::pair<int32, int32>;
using pi64 = std::pair<int64, int64>;
using pi64i32 = std::pair<int64, int32>;
using pi32pi32 = std::pair<int32, pi32>;
using cpdr = std::complex<dr>;
using vi32 = std::vector<int32>;
using qi32 = std::queue<int32>;
using spi32 = std::set<pi32>;
using spi32it = spi32::iterator;

namespace oct {

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const dr exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}

}  // namespace oct

// the pre-document end

const int N = 100005;

dr ta, tb, tc;
dr a[N], b[N], s[N], t[N];
int n;

int main() {
    oct::sync();
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i], a[i] += a[i - 1];
    for (int i = 1; i <= n; ++i) std::cin >> b[i], b[i] += b[i - 1];
    for (int i = 1; i <= n; ++i) {
        ta = 1, tb = -(a[i] + b[i]), tc = a[i];
        s[i] =
            (-tb - sqrt(std::abs(tb * tb - 4 * ta * tc) + oct::exp)) / (2 * ta);
        t[i] =
            (-tb + sqrt(std::abs(tb * tb - 4 * ta * tc) + oct::exp)) / (2 * ta);
    }
    for (int i = 1; i <= n; ++i) std::cout << s[i] - s[i - 1] << ' ';
    std::cout << std::endl;
    for (int i = 1; i <= n; ++i) std::cout << t[i] - t[i - 1] << ' ';
    std::cout << std::endl;
    return 0;
}


#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#include <bits/stdc++.h>
#define debug std::cerr << "Debug(" << __LINE__ << "): "
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using dr = double;
using pi32 = std::pair<int32, int32>;
using pi64 = std::pair<int64, int64>;
using pi64i32 = std::pair<int64, int32>;
using pi32pi32 = std::pair<int32, pi32>;
using cpdr = std::complex<dr>;
using vi32 = std::vector<int32>;
using qi32 = std::queue<int32>;
using spi32 = std::set<pi32>;
using spi32it = spi32::iterator;

namespace oct {

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const dr exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}

}  // namespace oct

// the pre-document end

const int N = 100005;

dr ta, tb, tc;
dr a[N], b[N], s[N], t[N];
int n;

int main() {
    oct::sync();
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i], a[i] += a[i - 1];
    for (int i = 1; i <= n; ++i) std::cin >> b[i];
    for (int i = n; i; --i) b[i] += b[i + 1];
    for (int i = 1; i <= n; ++i) {
        ta = 1, tb = -(a[i] - b[i + 1] + 1), tc = a[i];
        s[i] = (-tb + sqrt(oct::sqr(tb) - 4 * ta * tc) + 1e-4) / (2 * ta);
        t[i] = a[i] / s[i];
    }
    for (int i = 1; i <= n; ++i) std::cout << s[i] - s[i - 1] << ' ';
    std::cout << std::endl;
    for (int i = 1; i <= n; ++i) std::cout << t[i] - t[i - 1] << ' ';
    std::cout << std::endl;
    return 0;
}


#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#include <bits/stdc++.h>
#define debug std::cerr << "Debug(" << __LINE__ << "): "
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using dr = double;
using pi32 = std::pair<int32, int32>;
using pi64 = std::pair<int64, int64>;
using pi64i32 = std::pair<int64, int32>;
using pi32pi32 = std::pair<int32, pi32>;
using cpdr = std::complex<dr>;
using vi32 = std::vector<int32>;
using qi32 = std::queue<int32>;
using spi32 = std::set<pi32>;
using spi32it = spi32::iterator;

namespace oct {

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const dr exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}

}  // namespace oct

// the pre-document end

const int N = 100005;

dr ta, tb, tc;
dr a[N], b[N], s[N], t[N];
int n;

int main() {
    oct::sync();
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i], a[i] += a[i - 1];
    for (int i = 1; i <= n; ++i) std::cin >> b[i], b[i] += b[i - 1];
    for (int i = 1; i <= n; ++i) {
        ta = 1, tb = -(a[i] + b[i]), tc = a[i];
        s[i] = (-tb - sqrt(std::abs(tb * tb - 4 * ta * tc))) / (2 * ta);
        t[i] = (-tb + sqrt(std::abs(tb * tb - 4 * ta * tc))) / (2 * ta);
    }
    for (int i = 1; i <= n; ++i) std::cout << s[i] - s[i - 1] << ' ';
    std::cout << std::endl;
    for (int i = 1; i <= n; ++i) std::cout << t[i] - t[i - 1] << ' ';
    std::cout << std::endl;
    return 0;
}


#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#include <bits/stdc++.h>
#define debug std::cerr << "Debug(" << __LINE__ << "): "
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using dr = double;
using pi32 = std::pair<int32, int32>;
using pi64 = std::pair<int64, int64>;
using pi64i32 = std::pair<int64, int32>;
using pi32pi32 = std::pair<int32, pi32>;
using cpdr = std::complex<dr>;
using vi32 = std::vector<int32>;
using qi32 = std::queue<int32>;
using spi32 = std::set<pi32>;
using spi32it = spi32::iterator;

namespace oct {

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const dr exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}

}  // namespace oct

// the pre-document end

const int N = 100005;

dr ta, tb, tc;
dr a[N], b[N], s[N], t[N];
int n;

int main() {
    oct::sync();
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i], a[i] += a[i - 1];
    for (int i = 1; i <= n; ++i) std::cin >> b[i];
    for (int i = n; i; --i) b[i] += b[i + 1];
    for (int i = 1; i <= n; ++i) {
        ta = 1, tb = -(a[i] - b[i + 1] + 1), tc = a[i];
        s[i] = (-tb + sqrt(oct::sqr(tb) - 4 * ta * tc) + oct::exp) / (2 * ta);
        t[i] = a[i] / s[i];
    }
    for (int i = 1; i <= n; ++i) std::cout << s[i] - s[i - 1] << ' ';
    std::cout << std::endl;
    for (int i = 1; i <= n; ++i) std::cout << t[i] - t[i - 1] << ' ';
    std::cout << std::endl;
    return 0;
}


#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#include <bits/stdc++.h>
#define debug std::cerr << "Debug(" << __LINE__ << "): "
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using dr = double;
using pi32 = std::pair<int32, int32>;
using pi64 = std::pair<int64, int64>;
using pi64i32 = std::pair<int64, int32>;
using pi32pi32 = std::pair<int32, pi32>;
using cpdr = std::complex<dr>;
using vi32 = std::vector<int32>;
using qi32 = std::queue<int32>;
using spi32 = std::set<pi32>;
using spi32it = spi32::iterator;

namespace oct {

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}

}  // namespace oct

// the pre-document end

const int N = 100005;

dr ta, tb, tc;
dr a[N], b[N], s[N], t[N];
int n;

int main() {
    oct::sync();
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i], a[i] += a[i - 1];
    for (int i = 1; i <= n; ++i) std::cin >> b[i];
    for (int i = n; i; --i) b[i] += b[i + 1];
    for (int i = 1; i <= n; ++i) {
        ta = 1, tb = -(a[i] - b[i + 1] + 1), tc = a[i];
        s[i] = (-tb + sqrt(oct::sqr(tb) - 4 * ta * tc)) / (2 * ta);
        t[i] = a[i] / s[i];
    }
    for (int i = 1; i <= n; ++i) std::cout << s[i] - s[i - 1] << ' ';
    std::cout << std::endl;
    for (int i = 1; i <= n; ++i) std::cout << t[i] - t[i - 1] << ' ';
    std::cout << std::endl;
    return 0;
}