#include <bits/stdc++.h>
using namespace std;
const int maxk = 100;
int k;
long long ps[maxk];
int pows[maxk];
int powsn[maxk];
long long V;
long long ans, ansa, ansb, ansc;
long long s, a, b, c;
void work(int left, int now, int it, int iter) {
  if (it == k || ps[it] > left) {
    if (iter) {
      a = now;
      int newleft = (int)(sqrt(V / a) + (1E-9));
      if (ans > 2 * (a * (2 * newleft) + V / a)) {
        for (int i = 0; i < k; i++) pows[i] -= powsn[i];
        work(newleft, 1, 0, 0);
        for (int i = 0; i < k; i++) pows[i] += powsn[i];
      }
    } else {
      b = now, c = V / a / b;
      s = 2 * (a * b + b * c + a * c);
      if (s < ans) {
        ans = s;
        ansa = a, ansb = b, ansc = c;
      }
    }
    return;
  }
  int _left[64], _now[64];
  int m;
  for (m = 0; m <= pows[it] && left; m++) {
    _left[m] = left, _now[m] = now;
    left /= ps[it], now *= ps[it];
  }
  while (m--) {
    if (iter) powsn[it] = m;
    work(_left[m], _now[m], it + 1, iter);
  }
}
void solve() {
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k - 1; j++)
      if (ps[j] > ps[j + 1]) {
        swap(ps[j], ps[j + 1]);
        swap(pows[j], pows[j + 1]);
      }
  V = 1;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < pows[i]; j++) V *= ps[i];
  ans = (long long)9e18;
  ansa = -1, ansb = -1, ansc = -1;
  work((int)(pow(V, 1.0 / 3) + (1E-9)), 1, 0, 1);
  printf("%I64d %I64d %I64d %I64d\n", ans, ansa, ansb, ansc);
}
int main() {
  int t;
  while (scanf("%d", &t) >= 1) {
    for (int it = 0; it < t; it++) {
      scanf("%d", &k);
      for (int i = 0; i < k; i++) scanf("%I64d%d", &ps[i], &pows[i]);
      solve();
    }
  }
  return 0;
}
