#include <bits/stdc++.h>
using namespace std;
int i, j, k, t, n, m;
long long p[100], q[100], v, min1, ans[4], a1, b1, c1, max1, max2, w, sum;
void dfs2(int t, long long now) {
  if (now > max2) return;
  if (t > n) {
    if (now < ans[1]) return;
    ans[2] = now;
    w = v / ans[1] / ans[2];
    sum = ans[1] * ans[2] + ans[1] * w + ans[2] * w;
    if (sum < min1) {
      min1 = sum;
      a1 = ans[1];
      b1 = ans[2];
      c1 = w;
    }
    return;
  }
  if (q[t]) {
    q[t]--;
    dfs2(t, now * p[t]);
    q[t]++;
  }
  dfs2(t + 1, now);
  return;
}
void dfs1(int t, long long now) {
  if (now > max1) return;
  if (t > n) {
    if (v / now + 2 * now * sqrt(v / now) > min1) return;
    ans[1] = now;
    max2 = sqrt(v / now) + 1;
    dfs2(1, 1);
    return;
  }
  if (q[t]) {
    q[t]--;
    dfs1(t, now * p[t]);
    q[t]++;
  }
  dfs1(t + 1, now);
  return;
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    v = 1;
    for (i = 1; i <= n; i++) {
      scanf("%I64d %I64d", &p[i], &q[i]);
      for (j = 1; j <= q[i]; j++) v *= p[i];
    }
    max1 = pow(v, 1.0 / 3) + 1;
    min1 = 9000000000000000000ll;
    dfs1(1, 1);
    printf("%I64d %I64d %I64d %I64d\n", 2 * min1, a1, b1, c1);
  }
  return 0;
}
