#include <bits/stdc++.h>
using namespace std;
int64_t A, B, C, S, V;
int N, T;
int64_t P[80];
int M[80];
void dfsB(int64_t a, int pIdx, int64_t b) {
  int64_t ab = a * b;
  if (b * ab > V) return;
  if (pIdx >= N) {
    int64_t curS = ab + V / a + V / b;
    if (curS < S) {
      S = curS;
      A = a;
      B = b;
      C = V / (ab);
    }
    return;
  }
  int64_t pm = 1;
  for (int i = 0; i < M[pIdx]; i++) pm *= P[pIdx];
  for (int i = M[pIdx]; i >= 1; i--) {
    M[pIdx] -= i;
    dfsB(a, pIdx + 1, b * pm);
    M[pIdx] += i;
    pm /= P[pIdx];
  }
  dfsB(a, pIdx + 1, b);
}
void dfsA(int pIdx, int64_t a) {
  if (a * a * a > V) {
    return;
  }
  if (pIdx >= N) {
    int64_t x = (int64_t)(sqrt(V) * sqrt(a));
    int64_t curS = V / a + 2 * x;
    if (curS < S) dfsB(a, 0, 1);
    return;
  }
  int64_t pm = 1;
  for (int i = 0; i < M[pIdx]; i++) pm *= P[pIdx];
  for (int i = M[pIdx]; i >= 1; i--) {
    M[pIdx] -= i;
    dfsA(pIdx + 1, a * pm);
    M[pIdx] += i;
    pm /= P[pIdx];
  }
  dfsA(pIdx + 1, a);
}
typedef struct _Sdata {
  int64_t S;
  int64_t A;
  int64_t B;
  int64_t C;
} SDATA;
map<int64_t, SDATA> mCache;
int main() {
  cin >> T;
  clock_t t0 = clock();
  SDATA sData;
  while (T--) {
    scanf("%d", &N);
    V = 1;
    for (int i = 0; i < N; i++) {
      scanf("%lld %d", &P[i], &M[i]);
      for (int j = 0; j < M[i]; j++) {
        V *= P[i];
      }
    }
    auto iCache = mCache.find(V);
    if (iCache != mCache.end()) {
      sData = iCache->second;
    } else {
      S = (int64_t)3e18;
      dfsA(0, 1);
      sData = {S << 1, A, B, C};
      mCache[V] = sData;
    }
    printf("%lld %lld %lld %lld\n", sData.S, sData.A, sData.B, sData.C);
  }
  return 0;
}
