#include <bits/stdc++.h>
const double eps = 1e-9;
long long p[65], q[65][65], a1, b1, c1, v, v1, ans, a2, b2, c2;
int f[65], fv[65], N;
void dfs_b(int i, long long b, long long a) {
  if (b > b1) return;
  if (i > N) {
    long long c = v / a / b;
    long long t = 2 * (a * b + b * c + c * a);
    if (t < ans) {
      ans = t;
      a2 = a;
      b2 = b;
      c2 = c;
    }
    return;
  }
  for (int j = f[i] - fv[i]; j >= 0; j--) dfs_b(i + 1, b * q[i][j], a);
}
void dfs_a(int i, long long a) {
  if (a > a1) return;
  if (i > N) {
    v1 = v / a;
    b1 = floor(sqrtl((long double)v1 + eps));
    if (4 * a * b1 + 2 * b1 * b1 < ans) dfs_b(1, 1, a);
    return;
  }
  for (int j = f[i]; j >= 0; j--) fv[i] = j, dfs_a(i + 1, a * q[i][j]);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) scanf("%I64d%d", p + i, f + i);
    v = 1;
    for (int i = 1; i <= N; i++) {
      q[i][0] = 1;
      for (int j = 1; j <= f[i]; j++) q[i][j] = q[i][j - 1] * p[i];
      v *= q[i][f[i]];
    }
    ans = 4 * v + 2;
    a2 = 1;
    b2 = 1;
    c2 = v;
    a1 = floor(powl((long double)v + eps, (long double)1 / 3));
    dfs_a(1, 1);
    printf("%I64d %I64d %I64d %I64d\n", ans, a2, b2, c2);
  }
  return 0;
}
