#include <bits/stdc++.h>
int n, q[20];
long long A, B, C, a, b, c, m, m1, ans, v, p[20];
void dfs1(register int s, long long now) {
  if (now > m) return;
  if (s > n) {
    if (now < a) return;
    b = now;
    c = v / a / b;
    if (a * b + a * c + b * c < ans)
      ans = a * b + a * c + b * c, A = a, B = b, C = c;
    return;
  }
  if (q[s]) q[s]--, dfs1(s, now * p[s]), q[s]++;
  dfs1(s + 1, now);
}
void dfs2(register int s, long long now) {
  if (now > m1) return;
  if (s > n) {
    if (a = now, v / a + 2 * a * sqrt(v / a) < ans)
      m = sqrt(v / a) + 1e-8, dfs1(1, 1);
    return;
  }
  if (q[s]) q[s]--, dfs2(s, now * p[s]), q[s]++;
  dfs2(s + 1, now);
}
int main() {
  for (register int T, x = scanf("%d", &T); T--;) {
    scanf("%d", &n);
    for (register int i = v = 1; i <= n; i++) {
      scanf("%lld%d", &p[i], &q[i]);
      for (register int j = 1; j <= q[i]; j++) v *= p[i];
    }
    ans = 5e18;
    A = 0;
    m1 = pow(v, 1. / 3) + 1e-8;
    dfs2(1, 1);
    printf("%lld %lld %lld %lld\n", ans * 2, A, B, C);
  }
  return 0;
}
