#include <bits/stdc++.h>
long long a, b, c, e[505], f[10], g[505][3], h[505][505], i, j, k;
long double An;
long long an[10];
long long V;
long long T;
long double BMJ(long double x, long double y, long double z) {
  return x * y + y * z + z * x;
}
void sososo(long long x, long long y) {
  long long ii, jj, kk;
  if (V / f[1] / y < y || V / f[1] / y < f[1]) return;
  if (x > a) {
    if (y < f[1]) return;
    c = V / f[1];
    c /= y;
    if (BMJ((long double)f[1], (long double)y, (long double)c) < An) {
      An = BMJ((long double)f[1], (long double)y, (long double)c);
      an[1] = f[1];
      an[2] = y;
      an[3] = c;
    }
    return;
  }
  kk = g[x][1];
  for (ii = kk; ii >= 0; ii--) {
    g[x][1] = kk - ii;
    sososo(x + 1, y * h[x][ii]);
    g[x][1] = kk;
  }
}
void soso(long long x, long long y) {
  long long ii, jj, kk;
  long double u, v, w;
  if (V / y / y < y) return;
  if (x > a) {
    u = sqrt((long double)V / (long double)y);
    if (BMJ((long double)y, u, u) > An) return;
    f[1] = y;
    sososo(1, 1);
    return;
  }
  kk = g[x][1];
  for (ii = kk; ii >= 0; ii--) {
    g[x][1] = kk - ii;
    soso(x + 1, y * h[x][ii]);
    g[x][1] = kk;
  }
}
int main() {
  scanf("%I64d", &T);
  while (T--) {
    scanf("%I64d", &a);
    V = 1;
    for (i = 1; i <= a; i++) {
      scanf("%I64d%I64d", &g[i][0], &g[i][1]);
      h[i][0] = 1;
      for (j = 1; j <= g[i][1]; j++) {
        h[i][j] = h[i][j - 1] * g[i][0];
        V *= g[i][0];
      }
    }
    An = 1e30;
    soso(1, 1);
    printf("%I64d ", (long long)An * 2);
    printf("%I64d %I64d %I64d\n", an[1], an[2], an[3]);
  }
  return 0;
}
