#include <bits/stdc++.h>
using namespace std;
int sz;
const int maxn = 123;
long long p[maxn], n[maxn], nn[maxn];
int T;
long long V;
long long ans[3];
long long S, ansS;
vector<long long> fac;
long long cmp0;
vector<long long> fac2;
int boom;
void solve2() {
  long long cntV = V / cmp0;
  long long ttt = cntV;
  fac2.clear();
  fac2.push_back(1);
  for (int i = 0; i < sz; i++) {
    int mm = fac2.size();
    long long ppp = 1;
    while (cntV % p[i] == 0) {
      ppp *= p[i];
      for (int j = 0; j < mm; j++) {
        long long gg = fac2[j] * ppp;
        if (ttt / gg < gg) continue;
        fac2.push_back(gg);
      }
      cntV /= p[i];
    }
  }
  sort(fac2.begin(), fac2.end());
  for (int i = fac2.size() - 1; i >= 0; i--) {
    if (fac2[i] < cmp0) return;
    long long cmp1 = fac2[i];
    long long cmp2 = V / cmp0 / cmp1;
    S = cmp0 * cmp1 + cmp1 * cmp2 + cmp2 * cmp0;
    if (S < ansS) {
      ansS = S;
      ans[0] = cmp0;
      ans[1] = cmp1;
      ans[2] = cmp2;
    }
    return;
  }
}
void solve(long long minn) {
  fac.clear();
  fac.push_back(1);
  for (int i = 0; i < sz; i++) {
    int nn = fac.size();
    long long now = p[i];
    for (int j = 0; j < n[i]; j++) {
      for (int k = 0; k < nn; k++) {
        if (now * fac[k] <= minn) fac.push_back(now * fac[k]);
      }
      now *= p[i];
    }
  }
  sort(fac.begin(), fac.end());
  boom = -1;
  for (int i = fac.size() - 1; i >= 0; i--) {
    cmp0 = fac[i];
    solve2();
    if (ansS != 1LL << 61) {
      if (boom == -1) {
        boom = 6;
      } else
        boom--;
      if (boom == 0) break;
    }
  }
  printf("%I64d %I64d %I64d %I64d\n", 2 * ansS, ans[0], ans[1], ans[2]);
}
int main() {
  T = 1;
  scanf("%d", &T);
  while (T--) {
    ansS = 1LL << 61;
    V = 1;
    ans[0] = ans[1] = ans[2] = 1;
    scanf("%d", &sz);
    int tot = 0;
    for (int i = 0; i < sz; i++) {
      tot += n[i];
      scanf("%I64d%I64d", &p[i], &n[i]);
    }
    int cnt = tot;
    for (int i = sz - 1; i >= 0; i--) {
      for (int j = 0; j < n[i]; j++) {
        V *= p[i];
      }
    }
    long long min_ = (long long)pow(V, 1.0 / 3) + 1;
    solve(min_);
  }
  return 0;
}
