#include <bits/stdc++.h>
using namespace std;
int K;
long long p[21], a[21], aLeft[21];
long long N, Nq3;
long long minArea = 3000000000000000000LL;
long long ansA, ansB, ansC;
long long curA;
long long leftMul[21];
long long BCq, BC, nowBest;
void dfs2(int cur, long long carry) {
  if (carry * leftMul[cur] < BCq) return;
  if (carry > nowBest) return;
  if (carry >= BCq) {
    nowBest = carry;
    return;
  }
  if (cur == K + 1) return;
  dfs2(cur + 1, carry);
  for (int i = 1; i <= aLeft[cur]; i++) {
    carry *= p[cur];
    dfs2(cur + 1, carry);
  }
}
void calc(long long _curA) {
  nowBest = 3000000000000000000LL;
  curA = _curA;
  BC = N / curA;
  double each = sqrt(double(BC));
  if (curA * 2 * each + BC > minArea + 10) return;
  leftMul[K + 1] = 1;
  for (int i = K; i >= 1; i--) {
    leftMul[i] = leftMul[i + 1];
    for (int j = 1; j <= aLeft[i]; j++) leftMul[i] *= p[i];
  }
  BCq = sqrt(double(BC - 1));
  while (BCq * BCq < BC) BCq++;
  dfs2(1, 1);
  long long curB = nowBest, curC = BC / curB;
  if (curA * (curB + curC) + BC < minArea) {
    minArea = curA * (curB + curC) + BC;
    ansA = curA;
    ansB = curB;
    ansC = curC;
  }
}
void dfs(int cur, long long carry) {
  if (cur == K + 1)
    calc(carry);
  else {
    aLeft[cur] = a[cur];
    dfs(cur + 1, carry);
    for (int i = 1; i <= a[cur]; i++) {
      carry *= p[cur];
      if (carry > Nq3) return;
      aLeft[cur] = a[cur] - i;
      dfs(cur + 1, carry);
    }
  }
}
void solve() {
  minArea = 3000000000000000000LL;
  for (int iteration = 1; iteration <= K; iteration++)
    for (int i = 1; i < K; i++)
      if (p[i] < p[i + 1]) {
        swap(p[i], p[i + 1]);
        swap(a[i], a[i + 1]);
      }
  N = 1;
  for (int i = 1; i <= K; i++)
    for (int j = 1; j <= a[i]; j++) N *= p[i];
  Nq3 = pow(double(N - 1), 1 / 3.0);
  while (Nq3 * Nq3 * Nq3 < N) Nq3++;
  dfs(1, 1);
  cout << 2 * minArea << " " << ansA << " " << ansB << " " << ansC << endl;
}
int main() {
  ios ::sync_with_stdio(false);
  int TestCase;
  cin >> TestCase;
  for (int CaseID = 1; CaseID <= TestCase; CaseID++) {
    cin >> K;
    for (int i = 1; i <= K; i++) cin >> p[i] >> a[i];
    solve();
  }
  return 0;
}
