#include <bits/stdc++.h>
using namespace std;
long long v, ans, A, B, C, va, vb, vc, lim, limm, a[65], b[65], n;
void dfs2(int x, long long y) {
  if (y > limm) return;
  if (x > n) {
    vb = y;
    vc = v / va / vb;
    if (2 * (va * vb + va * vc + vb * vc) <= ans)
      ans = 2 * (va * vb + va * vc + vb * vc), A = va, B = vb, C = vc;
    return;
  }
  if (b[x]) --b[x], dfs2(x, y * a[x]), ++b[x];
  dfs2(x + 1, y);
}
void dfs1(int x, long long y) {
  if (y > lim) return;
  if (x > n) {
    va = y;
    limm = pow(v / y, 1.0 / 2) + 1e-8;
    if (2 * (limm * limm + limm * y + limm * y) < ans) dfs2(1, 1);
    return;
  }
  if (b[x]) --b[x], dfs1(x, y * a[x]), ++b[x];
  dfs1(x + 1, y);
}
void solve() {
  v = 1;
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) {
    scanf("%lld%lld", &a[i], &b[i]);
    for (int j = (int)(1); j <= (int)(b[i]); j++) v *= a[i];
  }
  ans = 1ll << 62;
  lim = pow(v, 1.0 / 3) + 1e-8;
  dfs1(1, 1);
  printf("%lld %lld %lld %lld\n", ans, A, B, C);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
}
