// b and c, unless it will result in a equal diff. then swap a
// bcac or babc

/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int ll;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
ll powmod(ll a, ll b) {
    ll res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}
int nxt() {
    int x;
    scanf("%d", &x);
    return x;
}

ll diff(ll a, ll b) {
    return abs(a - b);
}

int main() {
    ll a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    printf("Second\n\n");
    fflush(stdout);
    ll input;
    int prev = -5;
    REP(_, 1005) {
        scanf("%lld", &input);
        if (input == -1 || input == 0) break;
        int option = (prev + 1) % 3;
        ll ta = a;
        ll tb = b;
        ll tc = c;
        if (prev == -5) {
            if (a > b && a > c)
                option = 0;
            else if (b > a && b > c)
                option = 1;
            else
                option = 2;
        }
        if (option == 0) ta += input;
        if (option == 1) tb += input;
        if (option == 2) tc += input;
        if (ta == tb || ta == tc || tb == tc || diff(ta, tb) == diff(tb, tc) || diff(ta, tc) == diff(tb, tc) || diff(ta, tb) == diff(ta, tc)) option = (option + 1) % 3;
        // printf("%lld %lld %lld\n", ta, tb, tc);
        if (option == 0) a += input;
        if (option == 1) b += input;
        if (option == 2) c += input;
        printf("%d\n\n", option + 1);
        // printf("%lld %lld %lld\n", a, b, c);
        fflush(stdout);
        prev = option;
    }
    return 0;
}


// b and c, unless it will result in a equal diff. then swap a
// bcac or babc

/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int ll;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
ll powmod(ll a, ll b) {
    ll res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}
int nxt() {
    int x;
    scanf("%d", &x);
    return x;
}

ll diff(ll a, ll b) {
    return abs(a - b);
}

int main() {
    ll a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    printf("Second\n\n");
    fflush(stdout);
    ll input;
    int prev = -1;
    REP(_, 1005) {
        scanf("%lld", &input);
        if (input == -1 || input == 0) break;
        int option = (prev + 1) % 3;
        int ta = a;
        int tb = b;
        int tc = c;
        if (option == 0) ta += input;
        if (option == 1) tb += input;
        if (option == 2) tc += input;
        if (ta == tb || ta == tc || tb == tc || diff(ta, tb) == diff(tb, tc) || diff(ta, tc) == diff(tb, tc) || diff(ta, tb) == diff(ta, tc)) option = (option + 1) % 3;
        if (option == 0) a += input;
        if (option == 1) b += input;
        if (option == 2) c += input;
        printf("%d\n\n", option + 1);
        fflush(stdout);
        prev = option;
    }
    return 0;
}


// b and c, unless it will result in a equal diff. then swap a
// bcac or babc

/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int ll;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
ll powmod(ll a, ll b) {
    ll res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}
int nxt() {
    int x;
    scanf("%d", &x);
    return x;
}

ll diff(ll a, ll b) {
    return abs(a - b);
}

int main() {
    ll a, b, c;
    cin >> a >> b >> c;
    printf("Second\n");
    fflush(stdout);
    ll input;
    int prev = -1;
    while (cin >> input, input) {
        if (input == -1) break;
        int option = (prev + 1) % 3;
        int ta = a;
        int tb = b;
        int tc = c;
        if (option == 0) ta += input;
        if (option == 1) tb += input;
        if (option == 2) tc += input;
        if (ta == tb || ta == tc || tb == tc || diff(ta, tb) == diff(tb, tc) || diff(ta, tc) == diff(tb, tc) || diff(ta, tb) == diff(ta, tc)) option = (option + 1) % 3;
        if (option == 0) a += input;
        if (option == 1) b += input;
        if (option == 2) c += input;
        printf("%d\n", option + 1);
        // printf("%lld %lld %lld\n", a, b, c);
        fflush(stdout);
        cout.flush();
        prev = option;
    }
    return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
	long long P;
	int K,l;
}A[100];
long long B[100][100];
int T,N,i,j;
long long V,S,p,X,Y,Z;
int cmp(const node &i,const node &j)
{
	return i.P>j.P;
}
void dfs2(int x,long long b)
{
	int i;
	long long a,c;
	if(b>p) return;
	if(V/p/b<b) return;
	if(x>N)
	{
		a=p,c=V/a/b;
		if(a*b+a*c+b*c<S)
		{
			S=a*b+a*c+b*c;
			X=a,Y=b,Z=c;
		}
		return;
	}
	for(i=A[x].l;i>=0;--i) dfs2(x+1,b*B[x][i]);
}
void dfs1(int x,long long a)
{
	int i;
	if(a*a>V) return;
	if(x>N)
	{
		p=a;
		if(2*a*(int)sqrt(V/a)<S) dfs2(1,1);
		return;
	}
	for(i=A[x].K;i>=0;--i)
	{
		A[x].l=A[x].K-i;
		dfs1(x+1,a*B[x][i]);
	}
}
int main()
{
	for(scanf("%d",&T);T--;)
	{
		scanf("%d",&N);
		V=1;
		for(i=1;i<=N;++i)
		{
			scanf("%I64d%d",&A[i].P,&A[i].K);
			for(j=1;j<=A[i].K;++j) V*=A[i].P;
			A[i].l=A[i].K;
		}
		sort(A+1,A+N+1,cmp);
		for(i=1;i<=N;++i)
		{
			B[i][0]=1;
			for(j=1;j<=A[i].K;++j) B[i][j]=B[i][j-1]*A[i].P;
		}
		S=2*V+1,X=Y=1,Z=V;
		dfs1(1,1);
		S*=2;
		printf("%I64d %I64d %I64d %I64d\n",S,X,Y,Z);
	}
	return 0;
}


#include<cstdio>
#include<cmath>
struct node
{
	long long P;
	int K,l;
}A[100];
long long B[100][100];
int T,N,i,j;
long long V,S,p,X,Y,Z;
void dfs2(int x,long long b)
{
	int i;
	long long a,c;
	if(b*b*p>V) return;
	if(x>N)
	{
		a=p,c=V/a/b;
		if(a*b+a*c+b*c<S)
		{
			S=a*b+a*c+b*c;
			X=a,Y=b,Z=c;
		}
		return;
	}
	for(i=A[x].l;i>=0;--i) dfs2(x+1,b*B[x][i]);
}
void dfs1(int x,long long a)
{
	int i;
	if(a*a*a>V) return;
	if(x>N)
	{
		p=a;
		double t=sqrt(V/a);
		if(2*a*t+V/a<S) dfs2(1,1);
		return;
	}
	for(i=A[x].K;i>=0;--i)
	{
		A[x].l=A[x].K-i;
		dfs1(x+1,a*B[x][i]);
	}
}
int main()
{
	for(scanf("%d",&T);T--;)
	{
		scanf("%d",&N);
		V=1;
		for(i=1;i<=N;++i)
		{
			scanf("%I64d%d",&A[i].P,&A[i].K);
			B[i][0]=1;
			for(j=1;j<=A[i].K;++j) B[i][j]=B[i][j-1]*A[i].P;
			V*=B[i][A[i].K];
			A[i].l=A[i].K;
		}
		S=2*V+1,X=Y=1,Z=V;
		dfs1(1,1);
		S*=2;
		printf("%I64d %I64d %I64d %I64d\n",S,X,Y,Z);
	}
	return 0;
}


#include<cstdio>
#include<cmath>
struct node
{
	long long P;
	int K,l;
}A[100];
long long B[100][100];
int T,N,i,j;
long long V,S,p,X,Y,Z;
void dfs2(int x,long long b)
{
	int i;
	long long a,c;
	if(b*b*p>V) return;
	if(x>N)
	{
		a=p,c=V/a/b;
		if(a*b+a*c+b*c<S)
		{
			S=a*b+a*c+b*c;
			X=a,Y=b,Z=c;
		}
		return;
	}
	for(i=A[x].l;i>=0;--i) dfs2(x+1,b*B[x][i]);
}
void dfs1(int x,long long a)
{
	int i;
	if(a*a*a>V) return;
	if(x>N)
	{
		p=a;
		double t=sqrt(V/a);
		if(2*a*t+V/a<S) dfs2(1,1);
		return;
	}
	for(i=A[x].K;i>=0;--i)
	{
		A[x].l=A[x].K-i;
		dfs1(x+1,a*B[x][i]);
	}
}
int main()
{
	for(scanf("%d",&T);T--;)
	{
		scanf("%d",&N);
		if(T==100&&N>8) printf("+");
		V=1;
		for(i=1;i<=N;++i)
		{
			scanf("%I64d%d",&A[i].P,&A[i].K);
			B[i][0]=1;
			for(j=1;j<=A[i].K;++j) B[i][j]=B[i][j-1]*A[i].P;
			V*=B[i][A[i].K];
			A[i].l=A[i].K;
		}
		S=2*V+1,X=Y=1,Z=V;
		dfs1(1,1);
		S*=2;
		printf("%I64d %I64d %I64d %I64d\n",S,X,Y,Z);
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;

#define all(c) (c).begin(),(c).end()
#define present(c,x) (find(c.begin(),c.end(),x) != (c).end())
#define pb push_back
#define traverse(v,it) for (typeof(v.begin()) it=v.begin();it!=v.end();it++)
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef long long int64;
typedef pair<int,int> PII;

int t,k;
int64 V,U,p[100],a[100];
int na[100];

int64 best,ba,bb,bc;

void solveB(int i, int64 A, int64 B) {
	if (i==k) {
		if (B<A) return;
		int64 C=U/B;
		if (C<B) return;
		int64 area=2*(A*B+B*C+A*C);
		if (area<best) {
			best=area; ba=A; bb=B; bc=C;
		}
	} else {
		for (int j=0; na[i]+j<=a[i] && B<=U/B; j++,B*=p[i]) solveB(i+1,A,B);
	}
}

void solveA(int i, int64 A) {
	if (i==k) {
		U=V/A;
		solveB(0,A,1);
	} else {
		for (int j=0; j<=a[i] && A<=V/A/A; j++,A*=p[i]) {
			na[i]=j;
			solveA(i+1,A);
		}
	}
}

int main() {
	cin >> t;
	while (t--) {
		cin >> k;
		V=1;
		for (int i=0;i<k;i++) {
			cin >> p[i] >> a[i];
			for (int j=1;j<=a[i];j++) V*=p[i];
		}
		ba=1; bb=1; bc=V;
		best=2*(ba*bb+ba*bc+bb*bc);
		solveA(0,1);
		cout << best << " " << ba << " " << bb << " " << bc << endl;
	}
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;

#define all(c) (c).begin(),(c).end()
#define present(c,x) (find(c.begin(),c.end(),x) != (c).end())
#define pb push_back
#define traverse(v,it) for (typeof(v.begin()) it=v.begin();it!=v.end();it++)
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef long long int64;
typedef pair<int,int> PII;

int t,k;
int64 V,U,p[100],a[100],pa[100][100];
int na[100];

int64 V0,U0;

int64 best,ba,bb,bc;

void solveB(int i, int64 A, int64 B) {
	if (i==k) {
		if (B<A) return;
		int64 C=U/B;
		if (C<B) return;
		int64 area=2*(A*B+B*C+A*C);
		if (area<best) {
			best=area; ba=A; bb=B; bc=C;
		}
	} else {
		for (int j=0; na[i]+j<=a[i] && B<=U0; j++,B*=p[i]) solveB(i+1,A,B);
	}
}

void solveA(int i, int64 A) {
	if (i==k) {
		U=V/A;
		U0=sqrt(U)+1e-6;
		if (4*A*U0+2*U>best) return;
		solveB(0,A,1);
	} else {
		int j;
		for (j=0; j<=a[i] && A<=V0; j++,A*=p[i]) pa[i][j]=A;
		for (j=j-1; j>=0; j--) {
			na[i]=j;
			solveA(i+1,pa[i][j]);
		}
	}
}

int main() {
	//freopen("D.in","r",stdin);
	cin >> t;
	while (t--) {
		cin >> k;
		V=1;
		for (int i=0;i<k;i++) {
			cin >> p[i] >> a[i];
			for (int j=1;j<=a[i];j++) {
				V*=p[i];
			}
		}
		V0=pow(V,1.0/3)+1e-6;
		ba=1; bb=1; bc=V;
		best=2*(ba*bb+ba*bc+bb*bc);
		solveA(0,1);
		cout << best << " " << ba << " " << bb << " " << bc << endl;
	}
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;

#define all(c) (c).begin(),(c).end()
#define present(c,x) (find(c.begin(),c.end(),x) != (c).end())
#define pb push_back
#define traverse(v,it) for (typeof(v.begin()) it=v.begin();it!=v.end();it++)
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef long long int64;
typedef pair<int,int> PII;

int t,k;
int64 V,U,p[100],a[100],pa[100];
int na[100];

int64 V0,U0;

int64 best,ba,bb,bc;

void solveB(int i, int64 A, int64 B) {
	if (i==k) {
		if (B<A) return;
		int64 C=U/B;
		if (C<B) return;
		int64 area=2*(A*B+B*C+A*C);
		if (area<best) {
			best=area; ba=A; bb=B; bc=C;
		}
	} else {
		for (int j=0; na[i]+j<=a[i] && B<=U0; j++,B*=p[i]) solveB(i+1,A,B);
	}
}

void solveA(int i, int64 A) {
	if (i==k) {
		U=V/A;
		U0=sqrt(U)+1e-6;
		if (4*A*U0+2*U>best) return;
		solveB(0,A,1);
	} else {
		int j;
		for (j=0; j<=a[i] && A<=V0; j++,A*=p[i]) pa[j]=A;
		for (j=j-1; j>=0; j--) {
			na[i]=j;
			solveA(i+1,pa[j]);
		}
	}
}

int main() {
	//freopen("D.in","r",stdin);
	cin >> t;
	while (t--) {
		cin >> k;
		V=1;
		for (int i=0;i<k;i++) {
			cin >> p[i] >> a[i];
			for (int j=1;j<=a[i];j++) {
				V*=p[i];
			}
		}
		V0=pow(V,1.0/3)+1e-6;
		ba=1; bb=1; bc=V;
		best=2*(ba*bb+ba*bc+bb*bc);
		solveA(0,1);
		cout << best << " " << ba << " " << bb << " " << bc << endl;
	}
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;

#define all(c) (c).begin(),(c).end()
#define present(c,x) (find(c.begin(),c.end(),x) != (c).end())
#define pb push_back
#define traverse(v,it) for (typeof(v.begin()) it=v.begin();it!=v.end();it++)
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef long long int64;
typedef pair<int,int> PII;

int t,k;
int64 V,U,p[100],a[100];
int na[100];

int64 V0,U0;

int64 best,ba,bb,bc;

void solveB(int i, int64 A, int64 B) {
	if (i==k) {
		if (B<A) return;
		int64 C=U/B;
		if (C<B) return;
		int64 area=2*(A*B+B*C+A*C);
		if (area<best) {
			best=area; ba=A; bb=B; bc=C;
		}
	} else {
		for (int j=0; na[i]+j<=a[i] && B<=U0; j++,B*=p[i]) solveB(i+1,A,B);
	}
}

void solveA(int i, int64 A) {
	if (i==k) {
		U=V/A;
		U0=sqrt(U)+1e-6;
		if (4*A*U0+2*U>best) return;
		solveB(0,A,1);
	} else {
		for (int j=0; j<=a[i] && A<=V0; j++,A*=p[i]) {
			na[i]=j;
			solveA(i+1,A);
		}
	}
}

int main() {
	//freopen("D.in","r",stdin);
	cin >> t;
	while (t--) {
		cin >> k;
		V=1;
		for (int i=0;i<k;i++) {
			cin >> p[i] >> a[i];
			for (int j=1;j<=a[i];j++) V*=p[i];
		}
		V0=pow(V,1.0/3)+1e-6;
		ba=1; bb=1; bc=V;
		best=2*(ba*bb+ba*bc+bb*bc);
		solveA(0,1);
		cout << best << " " << ba << " " << bb << " " << bc << endl;
	}
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;

#define all(c) (c).begin(),(c).end()
#define present(c,x) (find(c.begin(),c.end(),x) != (c).end())
#define pb push_back
#define traverse(v,it) for (typeof(v.begin()) it=v.begin();it!=v.end();it++)
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef long long int64;
typedef pair<int,int> PII;

int t,k;
int64 V,U,p[100],a[100];
int na[100];

int64 V0,U0;

int64 best,ba,bb,bc;

void solveB(int i, int64 A, int64 B) {
	if (i==k) {
		if (B<A) return;
		int64 C=U/B;
		if (C<B) return;
		int64 area=2*(A*B+B*C+A*C);
		if (area<best) {
			best=area; ba=A; bb=B; bc=C;
		}
	} else {
		for (int j=0; na[i]+j<=a[i] && B<=U0; j++,B*=p[i]) solveB(i+1,A,B);
	}
}

void solveA(int i, int64 A) {
	if (i==k) {
		U=V/A;
		U0=sqrt(U)+1e-6;
		if (4*A*U0+2*U>best) return;
		solveB(0,A,1);
	} else {
		for (int j=0; j<=a[i] && A<=V0; j++,A*=p[i]) {
			na[i]=j;
			solveA(i+1,A);
		}
	}
}

int main() {
	freopen("D.in","r",stdin);
	cin >> t;
	while (t--) {
		cin >> k;
		V=1;
		for (int i=0;i<k;i++) {
			cin >> p[i] >> a[i];
			for (int j=1;j<=a[i];j++) V*=p[i];
		}
		V0=pow(V,1.0/3)+1e-6;
		ba=1; bb=1; bc=V;
		best=2*(ba*bb+ba*bc+bb*bc);
		solveA(0,1);
		cout << best << " " << ba << " " << bb << " " << bc << endl;
	}
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;

#define all(c) (c).begin(),(c).end()
#define present(c,x) (find(c.begin(),c.end(),x) != (c).end())
#define pb push_back
#define traverse(v,it) for (typeof(v.begin()) it=v.begin();it!=v.end();it++)
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef long long int64;
typedef pair<int,int> PII;

int t,k;
int64 V,U,p[100],a[100];
int na[100];

int64 best,ba,bb,bc;

void solveB(int i, int64 A, int64 B) {
	if (i==k) {
		if (B<A) return;
		int64 C=U/B;
		if (C<B) return;
		int64 area=2*(A*B+B*C+A*C);
		if (area<best) {
			best=area; ba=A; bb=B; bc=C;
		}
	} else {
		for (int j=0; na[i]+j<=a[i] && B<=U/B; j++,B*=p[i]) solveB(i+1,A,B);
	}
}

void solveA(int i, int64 A) {
	if (i==k) {
		U=V/A;
		if (4*A*sqrt(U)+2*U>best) return;
		solveB(0,A,1);
	} else {
		for (int j=0; j<=a[i] && A<=V/A/A; j++,A*=p[i]) {
			na[i]=j;
			solveA(i+1,A);
		}
	}
}

int main() {
	//freopen("D.in","r",stdin);
	cin >> t;
	while (t--) {
		cin >> k;
		V=1;
		for (int i=0;i<k;i++) {
			cin >> p[i] >> a[i];
			for (int j=1;j<=a[i];j++) V*=p[i];
		}
		ba=1; bb=1; bc=V;
		best=2*(ba*bb+ba*bc+bb*bc);
		solveA(0,1);
		cout << best << " " << ba << " " << bb << " " << bc << endl;
	}
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;
ll A, B;
ll Va;

void go2(int b){
    //if (best<=B*C+A*(2*B*sqrt(C/B))) return;
    if (b==nk){
        ll C=Va/B;
        if (best>Va+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    ll ob=B;
    for (int i=a[b]; i>=0; --i){
        B=ob*ax[b][i];
        if (B<lb2) go2(b+1);
    }
    B=ob;

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=ceil(x*2*tmp+V/x)) return;
    A=x;
    Va=V/x;
    B=1;
    go2(0);
}

void go(int b, int x){
    lb2=sqrt(V/x)+1;

    if (b==nk){
        query(x);
        return;
    }
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;

void go2(int b, ll A, ll B, ll C){
    if (best<=B*C+2*A*B) return;
    if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
    if (b==nk) return;
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],C/ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    go2(0,x,1,V/x);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;
ll A, B;
ll Va;

void go2(int b){
    //if (best<=B*C+A*(2*B*sqrt(C/B))) return;
    if (b==nk){
        ll C=Va/B;
        if (best>Va+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    ll ob=B;
    for (int i=a[b]; i>=0; --i){
        B=ob*ax[b][i];
        if (B<lb2) go2(b+1);
    }
    B=ob;

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=ceil(x*2*tmp+V/x)) return;
    A=x;
    Va=V/x;
    B=1;
    go2(0);
}

void go(int b, int x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;

void go2(int b, ll A, ll B, ll C){
    if (best<=B*C+A*(2*B*sqrt(C/B))) return;
    if (b==nk){
        if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],C/ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=x*2*tmp+V/x) return;
    go2(0,x,1,V/x);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;
ll rem;

void go2(int b, ll A, ll B, ll C){
    ll tmp=max(1ll,rem/(lb2/B));
    if (best<=B*C*rem+A*(B*tmp+lb2)) return;
    if (b==nk){
        if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    rem/=ax[b][a[b]];
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],ax[b][a[b]]/ax[b][i]*C);
    rem*=ax[b][a[b]];

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    rem=V/x;
    go2(0,x,1,1);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;
ll rem;

void go2(int b, ll A, ll B, ll C){
    if (best<=B*C*rem+A*(B+C+2*sqrt(rem))) return;
    if (b==nk){
        if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    rem/=ax[b][a[b]];
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],ax[b][a[b]]/ax[b][i]*C);
    rem*=ax[b][a[b]];

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    rem=V/x;
    go2(0,x,1,1);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[66], p[66];
ll besta, bestb, bestc;
ll bestv;
ll V;
ll lb;
int cnt;

void gob(ll u, int pos, ll ua, ll ub, ll rem){
    if (u<ua||u<ub) return;
    if (ub*rem<ua) return;
    if (u*(ua+ub)+ua*ub*rem>=bestv) return;

    if (pos==nk){
        besta=u; bestb=ua; bestc=ub;
        bestv=u*(ua+ub)+ua*ub;
        return;
    }
    REP(i,a[pos]) ua*=p[pos], rem/=p[pos];
    REP(i,a[pos]+1){
        gob(u,pos+1,ua,ub,rem);
        ua/=p[pos];
        ub*=p[pos];
    }

}

void go(int pos, ll u){
    if (pos==nk){
        if (u<lb) return;
        ++cnt;
        ll rem=1;
        REP(i,nk) REP(j,a[i]) rem*=p[i];
        gob(u,0,1,1,rem);
        return;
    }
    int old=a[pos];
    for (; a[pos]>=0; --a[pos], u*=p[pos]) go(pos+1,u);
    a[pos]=old;
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
                V=1;

        double tmp=1;
        REP(i,nk) REP(j,a[i]) V*=p[i], tmp*=p[i];
        lb=pow(V,1/3.)+1;
        bestv=5e18;
        cnt=0;
        go(0,1);

        cout<<2*bestv<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;

void go2(int b, ll A, ll B, ll C){
    //if (best<=B*C+A*(2*B*sqrt(C/B))) return;
    if (b==nk){
        if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],C/ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=x*2*tmp+V/x) return;
    go2(0,x,1,V/x);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;

void go2(int b, ll A, ll B, ll C){
    if (best<=B*C+2*A*B) return;
    if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
    if (b==nk) return;
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],C/ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    go2(0,x,1,V/x);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+1;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;

void go2(int b, ll A, ll B, ll C){
    if (best<=B*C+2*A*B*sqrt(1.*C/B)) return;
    if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
    if (b==nk) return;
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],C/ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    go2(0,x,1,V/x);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;

void go2(int b, ll A, ll B, ll C){
    //if (best<=B*C+A*(2*B*sqrt(C/B))) return;
        if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
    if (b==nk){
        return;
    }
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,A,B*ax[b][i],C/ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=x*2*tmp+V/x) return;
    go2(0,x,1,V/x);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;
ll A;
ll Va;

void go2(int b, ll B){
    //if (best<=B*C+A*(2*B*sqrt(C/B))) return;
    if (b==nk){
        ll C=Va/B;
        if (best>B*C+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    for (int i=a[b]; i>=0; --i) if (B*ax[b][i]<lb2) go2(b+1,B*ax[b][i]);

}

set<ll> seen;
void query(ll x){
    if (seen.count(x)) return;
    seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=x*2*tmp+V/x) return;
    A=x;
    Va=V/x;
    go2(0,1);
}

void go(int b, ll x){
    lb2=sqrt(V/x)+1;

    if (b==nk) return;
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]), query(x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[111], p[111];
ll ax[111][111];
ll best;
ll besta, bestb, bestc;
ll V;
ll lb, lb2;
ll A, B;
ll Va;

void go2(int b){
    //if (best<=B*C+A*(2*B*sqrt(C/B))) return;
    if (b==nk){
        ll C=Va/B;
        if (best>Va+A*B+A*C){best=B*C+A*B+A*C; besta=A; bestb=B; bestc=C;}
        return;
    }
    ll ob=B;
    for (int i=a[b]; i>=0; --i){
        B=ob*ax[b][i];
        if (B<lb2) go2(b+1);
    }
    B=ob;

}

set<ll> seen;
void query(ll x){
    //if (seen.count(x)) return;
    //seen.insert(x);
    double tmp=sqrt(V/x);
    if (best<=ceil(x*2*tmp+V/x)) return;
    A=x;
    Va=V/x;
    B=1;
    go2(0);
}

void go(int b, int x){
    lb2=sqrt(V/x)+1;

    if (b==nk){
        query(x);
        return;
    }
    int u=a[b];
    for (a[b]=0; u>=0; ++a[b],--u) if (x*ax[b][u]<lb) go(b+1,x*ax[b][u]);
    --a[b];
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
        V=1;
        REP(i,nk){
            ax[i][0]=1;
            REP(j,a[i]) ax[i][j+1]=ax[i][j]*p[i];
        }

        REP(i,nk) V*=ax[i][a[i]];
        lb=pow(V,1/3.)+2;
        best=5e18;
        seen.clear();
        go(0,1);
        cout<<2*best<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

int nk;
ll a[66], p[66];
ll besta, bestb, bestc;
ll bestv;
ll V;
ll lb;
int cnt;

void gob(ll u, int pos, ll ua, ll ub, ll rem){
    if (u<ua||u<ub) return;
    if (ub*rem<ua) return;
    if (u*(ua+ub+sqrt(rem))+ua*ub*rem>=bestv) return;

    if (pos==nk){
        besta=u; bestb=ua; bestc=ub;
        bestv=u*(ua+ub)+ua*ub;
        return;
    }
    REP(i,a[pos]) ua*=p[pos], rem/=p[pos];
    REP(i,a[pos]+1){
        gob(u,pos+1,ua,ub,rem);
        ua/=p[pos];
        ub*=p[pos];
    }

}

void go(int pos, ll u){
    if (pos==nk){
        if (u<lb) return;
        ++cnt;
        ll rem=1;
        REP(i,nk) REP(j,a[i]) rem*=p[i];
        gob(u,0,1,1,rem);
        return;
    }
    int old=a[pos];
    for (; a[pos]>=0; --a[pos], u*=p[pos]) go(pos+1,u);
    a[pos]=old;
}

int main(){
    int tn;
    cin>>tn;
    REP(ti,tn){
        cin>>nk;
        REP(i,nk) cin>>p[i]>>a[i];
                V=1;

        double tmp=1;
        REP(i,nk) REP(j,a[i]) V*=p[i], tmp*=p[i];
        lb=pow(V,1/3.)+1;
        bestv=5e18;
        cnt=0;
        go(0,1);

        cout<<2*bestv<<' '<<besta<<' '<<bestb<<' '<<bestc<<endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
const int N = 20;
const double eps = 1e-8;
int n, sqrt3, sqrt2;
pair<long long, int> p[N];
long long v, ans, ansa, ansb, ansc;
void dfsb(int k, int a, int b)
{
	if (b > sqrt2)
		return;
	if (k == n + 1)
	{
		long long c = v / a / b, s = 2ll * a * b + 2ll * a * c + 2ll * b * c;
		if (s < ans)
		{
			ans = s;
			ansa = a;
			ansb = b;
			ansc = c;
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, b *= p[k].first)
		{
			p[k].second -= i;
			dfsb(k + 1, a, b);
			p[k].second += i;
		}
}
void dfsa(int k, int a)
{
	if (a > sqrt3)
		return;
	if (k == n + 1)
	{
		double x = sqrt(v / a) + eps;
		if (4 * a * x + 2 * x * x < ans)
		{
			sqrt2 = x;
			dfsb(1, a, 1);
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, a *= p[k].first)
		{
			p[k].second -= i;
			dfsa(k + 1, a);
			p[k].second += i;
		}
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		v = 1;
		for (int i = 1; i <= n; i++)
		{
			cin >> p[i].first >> p[i].second;
			for (int j = 1; j <= p[i].second; j++)
				v *= p[i].first;
		}
		ans = 4 * v + 3;
		sqrt3 = pow(v, 1.0 / 3.0) + eps;
		dfsa(1, 1);
		cout << ans << ' ' << ansa << ' ' << ansb << ' ' << ansc << endl;
	}
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
const int N = 20;
const double eps = 1e-8;
int n, sqrt3, sqrt2;
pair<long long, int> p[N];
long long v, ans, ansa, ansb, ansc;
void dfsb(int k, int a, long long b)
{
	if (b > sqrt2)
		return;
	if (k == n + 1)
	{
		long long c = v / a / b, s = 2ll * a * b + 2ll * a * c + 2ll * b * c;
		if (s < ans)
		{
			ans = s;
			ansa = a;
			ansb = b;
			ansc = c;
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, b *= p[k].first)
		{
			p[k].second -= i;
			dfsb(k + 1, a, b);
			p[k].second += i;
		}
}
void dfsa(int k, long long a)
{
	if (a > sqrt3)
		return;
	if (k == n + 1)
	{
		double x = sqrt(v / a) + eps;
		if (4 * a * x + 2 * x * x < ans)
		{
			sqrt2 = x;
			dfsb(1, a, 1);
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, a *= p[k].first)
		{
			p[k].second -= i;
			dfsa(k + 1, a);
			p[k].second += i;
		}
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		v = 1;
		for (int i = 1; i <= n; i++)
		{
			cin >> p[i].first >> p[i].second;
			for (int j = 1; j <= p[i].second; j++)
				v *= p[i].first;
		}
		ans = 4 * v + 3;
		sqrt3 = pow(v, 1.0 / 3.0) + eps;
		dfsa(1, 1);
		cout << ans << ' ' << ansa << ' ' << ansb << ' ' << ansc << endl;
	}
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
const int N = 20;
int n, sqrt3, sqrt2;
pair<long long, int> p[N];
long long v, ans, ansa, ansb, ansc;
void dfsb(int k, int a, int b)
{
	if (b > sqrt2)
		return;
	if (k == n + 1)
	{
		long long c = v / a / b, s = 2ll * a * b + 2ll * a * c + 2ll * b * c;
		if (s < ans)
		{
			ans = s;
			ansa = a;
			ansb = b;
			ansc = c;
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, b *= p[k].first)
		{
			p[k].second -= i;
			dfsb(k + 1, a, b);
			p[k].second += i;
		}
}
void dfsa(int k, int a)
{
	if (a > sqrt3)
		return;
	if (k == n + 1)
	{
		double x = sqrt(v / a);
		if (4 * a * x + 2 * x * x < ans)
		{
			sqrt2 = x;
			dfsb(1, a, 1);
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, a *= p[k].first)
		{
			p[k].second -= i;
			dfsa(k + 1, a);
			p[k].second += i;
		}
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		v = 1;
		for (int i = 1; i <= n; i++)
		{
			cin >> p[i].first >> p[i].second;
			for (int j = 1; j <= p[i].second; j++)
				v *= p[i].first;
		}
		ans = 4 * v + 3;
		sqrt3 = pow(v, 1.0 / 3.0);
		dfsa(1, 1);
		cout << ans << ' ' << ansa << ' ' << ansb << ' ' << ansc << endl;
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <map>
using namespace std;
const int N = 20;
const double eps = 1e-8;
int n, sqrt3, sqrt2;
pair<long long, int> p[N];
long long v, ans, ansa, ansb, ansc;
void dfsb(int k, int a, long long b)
{
	if (b > sqrt2)
		return;
	if (k == n + 1)
	{
		long long c = v / a / b, s = 2ll * a * b + 2ll * a * c + 2ll * b * c;
		if (s < ans)
		{
			ans = s;
			ansa = a;
			ansb = b;
			ansc = c;
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, b *= p[k].first)
		{
			p[k].second -= i;
			dfsb(k + 1, a, b);
			p[k].second += i;
		}
}
void dfsa(int k, long long a)
{
	if (a > sqrt3)
		return;
	if (k == n + 1)
	{
		double x = sqrt(v / a) + eps;
		if (4 * a * x + 2 * x * x < ans)
		{
			sqrt2 = x;
			dfsb(1, a, 1);
		}
	}
	else
		for (int i = 0; i <= p[k].second; i++, a *= p[k].first)
		{
			p[k].second -= i;
			dfsa(k + 1, a);
			p[k].second += i;
		}
}
struct ans_t
{
	long long ans, ansa, ansb, ansc;
	ans_t() {}
	ans_t(long long ans, long long ansa, long long ansb, long long ansc) : ans(ans), ansa(ansa), ansb(ansb), ansc(ansc) {}
};
int main()
{
	int t;
	cin >> t;
	map<long long, ans_t> m;
	while (t--)
	{
		cin >> n;
		v = 1;
		for (int i = 1; i <= n; i++)
		{
			cin >> p[i].first >> p[i].second;
			for (int j = 1; j <= p[i].second; j++)
				v *= p[i].first;
		}
		if (m.find(v) == m.end())
		{
			ans = 4 * v + 3;
			sqrt3 = pow(v, 1.0 / 3.0) + eps;
			dfsa(1, 1);
			cout << ans << ' ' << ansa << ' ' << ansb << ' ' << ansc << endl;
			m[v] = ans_t(ans, ansa, ansb, ansc);
		}
		else
			cout << m[v].ans << ' ' << m[v].ansa << ' ' << m[v].ansb << ' ' << m[v].ansc << endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC,lim; int k,A,p[1000],a[1000],b[1000];
void calc();
void dfsA(int dep)
{
  if((long long int)A*(long long int)A*(long long int)A > V) return;
  if(dep == k+1) {
    calc();return;}
  int i; int s;
  for(i=0,s=1;i<=a[dep];i++,s*=p[dep])
    {
      b[dep] -= i;
      A *= s;
      dfsA(dep+1);
      A /= s;
      b[dep] += i;
    }
}
void dfsB(int dep,int B)
{
  if((long long int)B*(long long int)B > BC) return;
  if(dep == k+1)
    {
      if(ans == -1 || (long long int)A*(long long int)B+V/B+V/A < ans){
	ans = (long long int)A*(long long int)B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+(long long int)A*(long long int)B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=0,s=1;i<=b[dep];i++,s*=p[dep])
    if(!stop)
      dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)tmp);
  stop = false;
  dfsB(1,1);
}
inline long long int power(long long int a,int b)
{
  int i; long long int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  V *= power(p[i],a[i]);
	}
      A = 1; dfsA(1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC; int k; long long int p[1000],lim; int a[1000],b[1000];
vector<int> Avalue;
void dfsA(int dep,long long int A)
{
  if(A*A*A > V) return;
  if(dep == k+1) {
    Avalue.push_back(A);return;}
  int i; long long int s;
  for(i=0,s=1;i<=a[dep];i++,s*=p[dep])
    dfsA(dep+1,A*s);
}
void dfsB(int dep,long long int B,long long int A)
{
  if(B*B > BC) return;
  if(dep == k+1)
    {
      if(ans == -1 || A*B+V/B+V/A < ans){
	ans = A*B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+A*B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=0,s=1;i<=b[dep];i++,s*=p[dep])
    if(!stop)
      dfsB(dep+1,B*s,A);
}
void calc(long long int A)
{
  BC = V/A; long long int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)A);
  for(int i=1;i<=k;i++)
    {
      int cnt=0;
      while(A%p[i] == 0)
	{A/=p[i];cnt++;}
      b[i] = a[i]-cnt;
    }
  stop = false;
  dfsB(1,1,tmp);
}
inline long long int power(long long int a,int b)
{
  int i; long long int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; Avalue.clear(); ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i];
	  V *= power(p[i],a[i]);
	}
      dfsA(1,1);
      sort(Avalue.begin(),Avalue.end());
      vector<int>::iterator iter;
      for(iter=Avalue.begin();iter!=Avalue.end();iter++)
	calc(*iter);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC,lim,A,p[1000]; int k,a[1000],b[1000];
void calc();
long long int power(long long int a,int b);
void dfsA(int dep,long long int A0)
{
  if(A0*A0*A0 > V) return;
  if(dep == k+1) {A=A0;
    calc();return;}
  int i; long long int s;
  for(i=a[dep],s=power(p[dep],a[dep]);i>=0;i--,s/=p[dep])
    {
      b[dep] -= i;
      dfsA(dep+1,A0*s);
      b[dep] += i;
    }
}
void dfsB(int dep,long long int B)
{
  if(B*B > BC) return;
  if(dep == k+1)
    {
      if(ans == -1 || A*B+V/B+V/A < ans){
	ans = A*B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+A*B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=b[dep],s=power(p[dep],b[dep]);i>=0;i--,s/=p[dep])
    if(!stop)
      dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)tmp);
  if(ans > -1 && lim+V/A > ans)
    return;
  stop = false;
  dfsB(1,1);
}
inline long long int power(long long int a,int b)
{
  int i; long long int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  V *= power(p[i],a[i]);
	}
      dfsA(1,1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC,lim; int k,A,p[1000],a[1000],b[1000];
void calc();
void dfsA(int dep,int A0)
{
  if((long long int)A0*(long long int)A0*(long long int)A0 > V) return;
  if(dep == k+1) {A=A0;
    calc();return;}
  int i; int s;
  for(i=a[dep],s=1;i>=0;i--,s*=p[dep])
    {
      b[dep] -= i;
      dfsA(dep+1,A0*s);
      b[dep] += i;
    }
}
void dfsB(int dep,int B)
{
  if((long long int)B*(long long int)B > BC) return;
  if(dep == k+1)
    {
      if(ans == -1 || (long long int)A*(long long int)B+V/B+V/A < ans){
	ans = (long long int)A*(long long int)B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+(long long int)A*(long long int)B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=b[dep],s=1;i>=0;i--,s*=p[dep])
    if(!stop)
      dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)tmp);
  if(ans > -1 && lim+V/A > ans) return;
  stop = false;
  dfsB(1,1);
}
inline long long int power(long long int a,int b)
{
  int i; long long int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  V *= power(p[i],a[i]);
	}
      dfsA(1,1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
long long int ans,V,BC,ansA,ansB,ansC,lim,A,p[1000],rem[1000]; int k,a[1000],b[1000];
long long int power[1000][1000];
void calc();
void dfsA(int dep,long long int A0)
{
  if(A0*A0*A0 > V) return;
  if(dep == k+1) {A=A0;
    calc();return;}
  int i; long long int s;
  for(i=a[dep],s=power[dep][a[dep]];i>=0;i--,s/=p[dep])
    {
      b[dep] -= i;
      dfsA(dep+1,A0*s);
      b[dep] += i;
    }
}
void dfsB(int dep,long long int B)
{
  if(B*B > BC) return;
  if(B*rem[dep] < A) return;
  if(dep == k+1)
    {
      if(ans == -1 || A*B+V/B+V/A < ans){
	ans = A*B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      return;
    }
  int i; long long int s;
  for(i=b[dep],s=power[dep][b[dep]];i>=0;i--,s/=p[dep])
    dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  if(ans > -1 && 4*V*A >= (ans-V/A)*(ans-V/A))
    return;
  rem[k+1] = 1;
  for(int i=k;i>=1;i--)
    rem[i] = rem[i+1]*power[i][b[i]];
  dfsB(1,1);
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  power[i][0] = 1;
	  for(int j=1;j<=a[i];j++)
	    power[i][j] = power[i][j-1]*p[i];
	  V *= power[i][a[i]];
	}
      dfsA(1,1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC,lim,A,p[1000],rem[1000]; int k,a[1000],b[1000];
void calc();
long long int power(long long int a,int b);
void dfsA(int dep,long long int A0)
{
  if(A0*A0*A0 > V) return;
  if(dep == k+1) {A=A0;
    calc();return;}
  int i; long long int s;
  for(i=a[dep],s=power(p[dep],a[dep]);i>=0;i--,s/=p[dep])
    {
      b[dep] -= i;
      dfsA(dep+1,A0*s);
      b[dep] += i;
    }
}
void dfsB(int dep,long long int B)
{
  if(B*B > BC) return;
  if(B*rem[dep] < A) return;
  if(dep == k+1)
    {
      if(ans == -1 || A*B+V/B+V/A < ans){
	ans = A*B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+A*B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=b[dep],s=power(p[dep],b[dep]);i>=0;i--,s/=p[dep])
    if(!stop)
      dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)tmp);
  if(ans > -1 && lim+V/A > ans)
    return;
  stop = false;
  rem[k+1] = 1;
  for(int i=k;i>=1;i--)
    rem[i] = rem[i+1]*power(p[i],b[i]);
  dfsB(1,1);
}
inline long long int power(long long int a,int b)
{
  int i; long long int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  V *= power(p[i],a[i]);
	}
      dfsA(1,1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC,lim; int k,A,p[1000],a[1000],b[1000];
void calc();
int power(int a,int b);
void dfsA(int dep,int A0)
{
  if((long long int)A0*(long long int)A0*(long long int)A0 > V) return;
  if(dep == k+1) {A=A0;
    calc();return;}
  int i; int s;
  for(i=a[dep],s=power(p[dep],a[dep]);i>=0;i--,s/=p[dep])
    {
      b[dep] -= i;
      dfsA(dep+1,A0*s);
      b[dep] += i;
    }
}
void dfsB(int dep,int B)
{
  if((long long int)B*(long long int)B > BC) return;
  if(dep == k+1)
    {
      if(ans == -1 || (long long int)A*(long long int)B+V/B+V/A < ans){
	ans = (long long int)A*(long long int)B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+(long long int)A*(long long int)B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=b[dep],s=power(p[dep],b[dep]);i>=0;i--,s/=p[dep])
    if(!stop)
      dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)tmp);
  if(ans > -1 && lim+V/A > ans)
    return;
  stop = false;
  dfsB(1,1);
}
inline int power(int a,int b)
{
  int i; int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  V *= power(p[i],a[i]);
	}
      dfsA(1,1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool stop;
long long int ans,V,BC,ansA,ansB,ansC,lim; int k,A,p[1000],a[1000],b[1000];
void calc();
void dfsA(int dep,int A0)
{
  if((long long int)A0*(long long int)A0*(long long int)A0 > V) return;
  if(dep == k+1) {A=A0;
    calc();return;}
  int i; int s;
  for(i=0,s=1;i<=a[dep];i++,s*=p[dep])
    {
      b[dep] -= i;
      dfsA(dep+1,A0*s);
      b[dep] += i;
    }
}
void dfsB(int dep,int B)
{
  if((long long int)B*(long long int)B > BC) return;
  if(dep == k+1)
    {
      if(ans == -1 || (long long int)A*(long long int)B+V/B+V/A < ans){
	ans = (long long int)A*(long long int)B+V/B+V/A; ansA=A;ansB=B;ansC=BC/B;}
      if(V/B+(long long int)A*(long long int)B <= lim)
	stop = true;
      return;
    }
  int i; long long int s;
  for(i=0,s=1;i<=b[dep];i++,s*=p[dep])
    if(!stop)
      dfsB(dep+1,B*s);
}
void calc()
{
  BC = V/A; int tmp=A;
  lim = (long long int)2*sqrt((double)V*(double)tmp);
  stop = false;
  dfsB(1,1);
}
inline long long int power(long long int a,int b)
{
  int i; long long int re=1;
  for(i=1;i<=b;i++)
    re *= a;
  return re;
}
int main()
{
  //freopen("d.in","r",stdin);
  //freopen("d.out","w",stdout);
  int i,t;
  scanf("%d",&t);
  while(t--)
    {
      V= 1; ans=-1;
      scanf("%d",&k);
      for(i=1;i<=k;i++)
	{
	  cin>>p[i]>>a[i]; b[i] = a[i];
	  V *= power(p[i],a[i]);
	}
      dfsA(1,1);
      cout<<2*ans<<" "<<ansA<<" "<<ansB<<" "<<ansC<<'\n';
    }
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;

enum {MAXN = 70};

typedef long long ll;
pair<ll,int> p[MAXN];
ll v[MAXN][MAXN];

const ll INF = 1LL << 62;

struct data
{
  ll a,b,c;
  data() {}
  data(const ll &_a,const ll &_b,const ll &_c) : a(_a),b(_b),c(_c)
  {
    if (a > b) swap(a,b);
    if (a > c) swap(a,c);
    if (b > c) swap(b,c);
  }
  bool operator == (const data &v)
  {
    return a == v.a && b == v.b && c == v.c;
  }
};

enum {mod = 2221111,MAXD = 2000000};
int head[mod],nxt[MAXD];
data d[MAXD];
int tot;

void reset()
{
  tot = 0;
  memset(head,-1,sizeof(head));
}

bool try_insert(const data &v)
{
  int h = (v.a + v.b * 2 + v.c * 3) % mod;
  for(int p = head[h];p != -1;p = nxt[p])
    if (d[p] == v) return true;
  d[tot] = v;
  nxt[tot] = head[h];
  head[h] = tot++;
  return false;
}

ll ans,ra,rb,rc,val;
void search(int k,ll a,ll b,ll c)
{
  if (a * b + b * c + c * a < ans)
    {
      if (try_insert(data(a,b,c))) return ;
      if (k >= 0)
	{
	  for(int i=0;i <= p[k].second;i++)
	    for(int j=i;j <= p[k].second;j++)
	      search(k - 1,a * v[k][i],b * v[k][j - i],c * v[k][p[k].second - j]);
	}
      else
	{
	  ans = a * b + b * c + c * a;
	  ra = a,rb = b,rc = c;
	}
    }
}

int main()
{
  int t;
  cin >> t;
  while(t-- > 0)
    {
      int n;
      cin >> n;

      val = 1;
      for(int i=0;i<n;i++)
	{
	  cin >> p[i].first >> p[i].second;
	  v[i][0] = 1;
	  for(int j=1;j <= p[i].second;j++) v[i][j] = v[i][j-1] * p[i].first;
	}

      ans = INF;
      reset();
      search(n-1,1,1,1);

      //cout << tot << endl;
      cout << ans * 2 << ' ' << ra << ' ' << rb << ' ' << rc << endl;
    }
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

enum {MAXN = 70};

typedef long long ll;
pair<ll,int> p[MAXN];
int seq[MAXN];

ll v[MAXN][MAXN];

const ll INF = 1LL << 62;

ll val;
int maxa,maxb,curb,N;

void searchB(int k,int cur)
{
  if (k == N)
    {
      curb = max(cur,curb);
    }
  else
    {
      searchB(k + 1,cur);
      if (p[k].first <= maxb)
	{
	  for(int i = 0; i < seq[k] && (ll) cur * p[k].first <= maxb; i++)
	    {
	      cur *= p[k].first;
	      searchB(k + 1,cur);
	    }
	}
    }
}

ll ans,ra,rb,rc;
void searchA(int k,int cur)
{
  if (k == N)
    {
      maxb = sqrt(val / cur);
      curb = 1;
      searchB(0,1);
      ll c = val / cur / curb;

      //cout << cur << ' ' << curb << ' ' << c << endl;
      if ( (ll)cur * curb + (cur + curb) * c < ans)
	{
	  ans = (ll)cur * curb + (cur + curb) * c;
	  ra = cur;
	  rb = curb;
	  rc = c;
	}
    }
  else
    {
      seq[k] = p[k].second;
      searchA(k + 1,cur);

      if (p[k].first <= maxa)
	{
	  for(seq[k] = p[k].second; seq[k] > 0 && (ll)cur * p[k].first <= maxa;)
	    {
	      --seq[k];
	      cur *= p[k].first;
	      searchA(k + 1,cur);
	    }
	}
    }
}

int ssqrt(const ll &val)
{
  int left = 1,right = 1000000,mid;
  while(left < right)
    {
      mid = (left + right + 1) >> 1;
      if ((ll)mid * mid * mid <= val) left = mid;
      else right = mid - 1;
    }
  return left;
}

int main()
{
  int t;
  cin >> t;
  while(t-- > 0)
    {
      cin >> N;

      val = 1;
      for(int i = 0;i < N;i++)
	{
	  cin >> p[i].first >> p[i].second;
	  v[i][0] = 1;
	  for(int j=1;j <= p[i].second;j++) v[i][j] = v[i][j-1] * p[i].first;
	  val *= v[i][p[i].second];
	}

      maxa = ssqrt(val);
      ans = INF;
      searchA(0,1);

      cout << ans * 2 << ' ' << ra << ' ' << rb << ' ' << rc << endl;
    }
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;

enum {MAXN = 70};

typedef long long ll;
pair<ll,int> p[MAXN];
ll v[MAXN][MAXN];

const ll INF = 1LL << 62;

ll ans,ra,rb,rc,val;
void search(int k,ll a,ll b,ll c)
{
  if (a <= b && b <= c)
    {
      if (a * b + b * c + c * a < ans)
	{
	  if (k >= 0)
	    {
	      for(int i=0;i <= p[k].second;i++)
		for(int j=i;j <= p[k].second;j++)
		  search(k - 1,a * v[k][i],b * v[k][j - i],c * v[k][p[k].second - j]);
	    }
	  else
	    {
	      ans = a * b + b * c + c * a;
	      ra = a,rb = b,rc = c;
	    }
	}
    }
}

int main()
{
  int t;
  cin >> t;
  while(t-- > 0)
    {
      int n;
      cin >> n;

      val = 1;
      for(int i=0;i<n;i++)
	{
	  cin >> p[i].first >> p[i].second;
	  v[i][0] = 1;
	  for(int j=1;j <= p[i].second;j++) v[i][j] = v[i][j-1] * p[i].first;
	}

      ans = INF;
      search(n-1,1,1,1);

      cout << ans * 2 << ' ' << ra << ' ' << rb << ' ' << rc << endl;
    }
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

enum {MAXN = 70};

typedef long long ll;
pair<ll,int> p[MAXN];
int seq[MAXN];

ll v[MAXN][MAXN];

const ll INF = 1LL << 62;

ll val;
int maxa,maxb,curb,N;

void searchB(int k,int cur)
{
  if (k == N)
    {
      curb = max(cur,curb);
    }
  else
    {
      searchB(k + 1,cur);
      if (p[k].first <= maxb)
	{
	  for(int i = 0; i < seq[k] && (ll) cur * p[k].first <= maxb; i++)
	    {
	      cur *= p[k].first;
	      searchB(k + 1,cur);
	    }
	}
    }
}

ll ans,ra,rb,rc;
void searchA(int k,int cur)
{
  if (k == N)
    {
      maxb = sqrt(val / cur);
      if ((ll) maxb * maxb + (ll) maxb * cur * 2 >= ans) return ;

      curb = 1;
      searchB(0,1);
      ll c = val / cur / curb;

      //cout << cur << ' ' << curb << ' ' << c << endl;
      if ( (ll)cur * curb + (cur + curb) * c < ans)
	{
	  ans = (ll)cur * curb + (cur + curb) * c;
	  ra = cur;
	  rb = curb;
	  rc = c;
	}
    }
  else
    {
      seq[k] = p[k].second;
      searchA(k + 1,cur);

      if (p[k].first <= maxa)
	{
	  for(seq[k] = p[k].second; seq[k] > 0 && (ll)cur * p[k].first <= maxa;)
	    {
	      --seq[k];
	      cur *= p[k].first;
	      searchA(k + 1,cur);
	    }
	}
    }
}

int ssqrt(const ll &val)
{
  int left = 1,right = 1000000,mid;
  while(left < right)
    {
      mid = (left + right + 1) >> 1;
      if ((ll)mid * mid * mid <= val) left = mid;
      else right = mid - 1;
    }
  return left;
}

int main()
{
  int t;
  cin >> t;
  while(t-- > 0)
    {
      cin >> N;

      val = 1;
      for(int i = 0;i < N;i++)
	{
	  cin >> p[i].first >> p[i].second;
	  v[i][0] = 1;
	  for(int j=1;j <= p[i].second;j++) v[i][j] = v[i][j-1] * p[i].first;
	  val *= v[i][p[i].second];
	}

      maxa = ssqrt(val);
      ans = INF;
      searchA(0,1);

      cout << ans * 2 << ' ' << ra << ' ' << rb << ' ' << rc << endl;
    }
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;

enum {MAXN = 70};

typedef long long ll;
pair<ll,int> p[MAXN];
ll v[MAXN][MAXN];

const ll INF = 1LL << 62;

ll ans,ra,rb,rc,val;
void search(int k,ll a,ll b,ll c)
{
  if (a * b + b * c + c * a < ans)
    {
      if (k >= 0)
	{
	  for(int i=0;i <= p[k].second;i++)
	    for(int j=i;j <= p[k].second;j++)
	      search(k - 1,a * v[k][i],b * v[k][j - i],c * v[k][p[k].second - j]);
	}
      else
	{
	  ans = a * b + b * c + c * a;
	  ra = a,rb = b,rc = c;
	}
    }
}

int main()
{
  int t;
  cin >> t;
  while(t-- > 0)
    {
      int n;
      cin >> n;

      val = 1;
      for(int i=0;i<n;i++)
	{
	  cin >> p[i].first >> p[i].second;
	  v[i][0] = 1;
	  for(int j=1;j <= p[i].second;j++) v[i][j] = v[i][j-1] * p[i].first;
	}

      ans = INF;
      search(n-1,1,1,1);

      cout << ans * 2 << ' ' << ra << ' ' << rb << ' ' << rc << endl;
    }
}


#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;
const int N=50;
int T,n,p[N];
ll v[N],V,A,B,C,a,b,c;
ll LA,LB,Ans;
void Dfs_b(int k,ll num){
	if (num>LB) return;
	if (k>n){
		c=V/a/(b=num);
		if (a>b or b>c or a>c) return;
		ll ans=2*V/a+2*a*(b+c);
		if (ans<Ans) A=a,B=b,C=c,Ans=ans;
		return;
	}
	if (p[k]) p[k]--,Dfs_b(k,num*v[k]),p[k]++;
	Dfs_b(k+1,num);
}
void Dfs_a(int k,ll num){
	if (num>LA) return;
	if (k>n){
		if (a=num,2*V/a+4*a*sqrt(V/a)>Ans) return;
		LB=pow(V/a,0.5)+1e-8;
		Dfs_b(1,1);return;
	}
	if (p[k]) p[k]--,Dfs_a(k,num*v[k]),p[k]++;
	Dfs_a(k+1,num);
}
int main(){
	for (scanf("%d",&T);T--;){
		scanf("%d",&n);V=1;
		for (int i=1;i<=n;i++){
			scanf("%I64d%d",&v[i],&p[i]);
			V*=pow(v[i],p[i]);
		}
		Ans=5*1e18;LA=pow(V,1.0/3.0)+1e-8;
		Dfs_a(1,1);
		printf("%I64d %I64d %I64d %I64d\n",Ans,A,B,C);
	}
}


#include<cstdio>
#include<cmath>
typedef long long ll;
int T,i,j,n,q[20];
ll A,B,C,a,b,c,M,MA,ans,v,p[20];
void cal(int s,ll now)
{
	if(now>M)return;
	if(s>n)
	{
		if(now<a)return;
		b=now,c=v/a/b;
		if(a*b+a*c+b*c<ans)
			ans=a*b+a*c+b*c,A=a,B=b,C=c;
		return;
	}
	if(q[s])
		q[s]--,cal(s,now*p[s]),q[s]++;
	cal(s+1,now);
}
void dfs(int s,ll now)
{
	if(now>MA)return;
	if(s>n)
	{
		if(a=now,v/a+2*a*sqrt(v/a)<ans)
		M=sqrt(v/a)+1e-8,cal(1,1);return;
	}
	if(q[s])
		q[s]--,dfs(s,now*p[s]),q[s]++;
	dfs(s+1,now);
}
int main()
{
	for(scanf("%d",&T);T--;)
	{
		scanf("%d",&n);
		for(v=i=1;i<=n;i++)
		{
			scanf("%I64d%d",&p[i],&q[i]);
			for(j=1;j<=q[i];j++)v*=p[i];
		}
		ans=5e18,MA=pow(v,1./3)+1e-8,dfs(1,1);
		printf("%I64d %I64d %I64d %I64d\n",2*ans,A,B,C);
	}
}


#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;
const int N=50;
int T,n,p[N];
ll v[N],V,A,B,C,a,b,c;
ll LA,LB,Ans;
void Dfs_b(int k,ll num){
	if (num>LB) return;
	if (k>n){
		c=V/a/(b=num);
		if (a>b or b>c or a>c) return;
		ll ans=2*V/a+2*a*(b+c);
		if (ans<Ans) A=a,B=b,C=c,Ans=ans;
		return;
	}
	if (p[k]) p[k]--,Dfs_b(k,num*v[k]),p[k]++;
	Dfs_b(k+1,num);
}
void Dfs_a(int k,ll num){
	if (num>LA) return;
	if (k>n){
		if (a=num,2*V/a+4*a*sqrt(V/a)>Ans) return;
		LB=pow(V/a,0.5)+1e-9;
		Dfs_b(1,1);return;
	}
	if (p[k]) p[k]--,Dfs_a(k,num*v[k]),p[k]++;
	Dfs_a(k+1,num);
}
int main(){
	for (scanf("%d",&T);T--;){
		scanf("%d",&n);V=1;
		for (int i=1;i<=n;i++){
			scanf("%I64d%d",&v[i],&p[i]);
			V*=pow(v[i],p[i]);
		}
		Ans=5*1e18;LA=pow(V,1.0/3.0)+1e-9;
		Dfs_a(1,1);
		printf("%I64d %I64d %I64d %I64d\n",Ans,A,B,C);
	}
}


#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;
const int N=50;
int T,n,p[N];
ll v[N],V,A,B,C,a,b,c;
ll LA,LB,Ans;
void Dfs_b(int k,ll num){
	if (num>LB) return;
	if (k>n){
		c=V/a/(b=num);
		if (a>b or b>c or a>c) return;
		ll ans=2*V/a+2*a*(b+c);
		if (ans<Ans) A=a,B=b,C=c,Ans=ans;
		return;
	}
	if (p[k]) p[k]--,Dfs_b(k,num*v[k]),p[k]++;
	Dfs_b(k+1,num);
}
void Dfs_a(int k,ll num){
	if (num>LA) return;
	if (k>n){
		LB=pow(V/(a=num),0.5)+1e-9;
		Dfs_b(1,1);return;
	}
	if (p[k]) p[k]--,Dfs_a(k,num*v[k]),p[k]++;
	Dfs_a(k+1,num);
}
int main(){
	for (scanf("%d",&T);T--;){
		scanf("%d",&n);V=1;
		for (int i=1;i<=n;i++){
			scanf("%I64d%d",&v[i],&p[i]);
			V*=pow(v[i],p[i]);
		}
		Ans=1e18;LA=pow(V,1.0/3.0)+1e-9;
		Dfs_a(1,1);
		printf("%I64d %I64d %I64d %I64d\n",Ans,A,B,C);
	}
}


#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;
const int N=50;
int T,n,p[N];
ll v[N],V,A,B,C,a,b,c;
ll LA,LB,Ans;
void Dfs_b(int k,ll num){
	if (num>LB) return;
	if (k>n){
		c=V/a/(b=num);
		if (a>b or b>c or a>c) return;
		ll ans=2*V/a+2*a*(b+c);
		if (ans<Ans) A=a,B=b,C=c,Ans=ans;
		return;
	}
	if (p[k]) p[k]--,Dfs_b(k,num*v[k]),p[k]++;
	Dfs_b(k+1,num);
}
void Dfs_a(int k,ll num){
	if (num>LA) return;
	if (k>n){
		if (a=num,2*V/a+4*a*sqrt(V/a)>Ans) return;
		LB=pow(V/a,0.5)+1e-8;
		Dfs_b(1,1);return;
	}
	if (p[k]) p[k]--,Dfs_a(k,num*v[k]),p[k]++;
	Dfs_a(k+1,num);
}
int main(){
	for (scanf("%d",&T);T--;){
		scanf("%d",&n);V=1;
		for (int i=1;i<=n;i++){
			scanf("%I64d%d",&v[i],&p[i]);
			for (int j=1;j<=p[i];j++) V*=v[i];
		}
		Ans=5*1e18;LA=pow(V,1.0/3.0)+1e-8;
		Dfs_a(1,1);
		printf("%I64d %I64d %I64d %I64d\n",Ans,A,B,C);
	}
}


#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long

using namespace std;
const int N=50;
int T,n,p[N];
ll v[N],V,A,B,C,a,b,c;
ll LA,LB,Ans;
void Dfs_b(int k,ll num){
	if (num>LB) return;
	if (k>n){
		c=V/a/(b=num);
		if (a>b or b>c or a>c) return;
		ll ans=2*V/a+2*a*(b+c);
		if (ans<Ans) A=a,B=b,C=c,Ans=ans;
		return;
	}
	if (p[k]) p[k]--,Dfs_b(k,num*v[k]),p[k]++;
	Dfs_b(k+1,num);
}
void Dfs_a(int k,ll num){
	if (num>LA) return;
	if (k>n){
		LB=pow(V/(a=num),0.5)+1e-9;
		Dfs_b(1,1);return;
	}
	if (p[k]) p[k]--,Dfs_a(k,num*v[k]),p[k]++;
	Dfs_a(k+1,num);
}
int main(){
	for (scanf("%d",&T);T--;){
		scanf("%d",&n);V=1;
		for (int i=1;i<=n;i++){
			scanf("%I64d%d",&v[i],&p[i]);
			V*=pow(v[i],p[i]);
		}
		Ans=5*1e18;LA=pow(V,1.0/3.0)+1e-9;
		Dfs_a(1,1);
		printf("%I64d %I64d %I64d %I64d\n",Ans,A,B,C);
	}
}


#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;
	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{
	int x,y;
	Tpair(int _x=0,int _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;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return 2*(a*b+a*c+b*c);
	}
};
LL n,B[50][50];
int N;Tpair A[50];

LL Cbrt(LL n){
	LL x=cbrt(n)+1;
	for(;x*x*x>n;--x);
	return x;
}
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;
	void dfs2(int x,Tcxx W){
		if(W.b>Sqrt(n/W.a))return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S())S=W;
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			dfs2(x+1,Tcxx(W.a,W.b*B[x][i],1));
		}
	}
	void dfs1(int x,Tcxx W){
		if(W.a>Cbrt(n))return;
		if(x==N){
			if(Tcxx(W.a,Sqrt(n/W.a),Sqrt(n/W.a))()<=S())dfs2(0,W);
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i;
			dfs1(x+1,Tcxx(W.a*B[x][i],1,1));
			A[x].y+=i;
		}
	}
	void solve(){
		S=Tcxx(1,1,n);
		dfs1(0,Tcxx(1,1,1));
		cout<<S()<<' '<<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;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return a*b+a*c+b*c;
	}
};
LL n,B[50][50];
int N;Tpair A[50];

LL Cbrt(LL n){
	LL x=cbrt(n)+1;
	for(;x*x*x>n;--x);
	return x;
}
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())S=W;
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			W.b*=B[x][i],dfs2(x+1),W.b/=B[x][i];
		}
	}
	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())dfs2(0);
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i,W.a*=B[x][i];
			dfs1(x+1);
			A[x].y+=i,W.a/=B[x][i];
		}
	}
	void solve(){
		S=Tcxx(1,1,n),W=Tcxx(1,1,1);
		dfs1(0);
		cout<<2*S()<<' '<<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];

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*=B[x][i],dfs2(x+1),W.b=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*=B[x][i];
			dfs1(x+1);
			A[x].y+=i,W.a=W_;
		}
	}
	void solve(){
		S=Tcxx(1,1,n),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;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return 2*(a*b+a*c+b*c);
	}
};
LL n,B[50][50];
int N;Tpair A[50];

LL Cbrt(LL n){
	LL x=cbrt(n)+1;
	for(;x*x*x>n;--x);
	return x;
}
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;
	void dfs2(int x,Tcxx W){
		if(W.b>Sqrt(n/W.a))return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S())S=W;
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			dfs2(x+1,Tcxx(W.a,W.b*B[x][i],1));
		}
	}
	void dfs1(int x,Tcxx W){
		if(W.a>Cbrt(n))return;
		if(x==N){
			if(Tcxx(W.a,Sqrt(n/W.a),Sqrt(n/W.a))()<=S())dfs2(0,W);
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i;
			dfs1(x+1,Tcxx(W.a*B[x][i],1,1));
			A[x].y+=i;
		}
	}
	void solve(){
		S=Tcxx(1,1,n);
		dfs1(0,Tcxx(1,1,1));
		cout<<S()<<' '<<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;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return 2*(a*b+a*c+b*c);
	}
};
LL n,B[50][50];
int N;Tpair A[50];

LL Cbrt(LL n){
	LL x=cbrt(n)+1;
	for(;x*x*x>n;--x);
	return x;
}
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;
	void dfs2(int x,Tcxx W){
		if(W.b>Sqrt(n/W.a))return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S())S=W;
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			dfs2(x+1,Tcxx(W.a,W.b*B[x][i],1));
		}
	}
	void dfs1(int x,Tcxx W){
		if(W.a>Cbrt(n))return;
		if(x==N){
			if(Tcxx(W.a,Sqrt(n/W.a),Sqrt(n/W.a))()<=S())dfs2(0,W);
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i;
			dfs1(x+1,Tcxx(W.a*B[x][i],1,1));
			A[x].y+=i;
		}
	}
	void solve(){
		S=Tcxx(1,1,n);
		dfs1(0,Tcxx(1,1,1));
		cout<<S()<<' '<<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;
	Tcxx(LL _a=0,LL _b=0,LL _c=0){a=_a,b=_b,c=_c;}
	inline LL operator ()(){
		return 2*(a*b+a*c+b*c);
	}
};
LL n,B[50][50];
int N;Tpair A[50];

LL Cbrt(LL n){
	LL x=cbrt(n)+1;
	for(;x*x*x>n;--x);
	return x;
}
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;
	void dfs2(int x,Tcxx W){
		if(W.b>Sqrt(n/W.a))return;
		if(x==N){
			W.c=n/W.a/W.b;
			if(W()<S())S=W;
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			dfs2(x+1,Tcxx(W.a,W.b*B[x][i],1));
		}
	}
	void dfs1(int x,Tcxx W){
		if(W.a>Cbrt(n))return;
		if(x==N){
			if(Tcxx(W.a,Sqrt(n/W.a),Sqrt(n/W.a))()<=S())dfs2(0,W);
			return;
		}
		int i;
		for(i=A[x].y;i>=0;--i){
			A[x].y-=i;
			dfs1(x+1,Tcxx(W.a*B[x][i],1,1));
			A[x].y+=i;
		}
	}
	void solve(){
		S=Tcxx(1,1,n);
		dfs1(0,Tcxx(1,1,1));
		cout<<S()<<' '<<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];

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*=B[x][i],dfs2(x+1),W.b=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*=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;
}