#include <bits/stdc++.h>
using namespace std;
int T;
int s, P[30];
long long n, N[30], ans, sa, sb, sc;
void dfs2(int x, long long a, long long b, long long lim) {
  if (x == 0) {
    long long c = n / a / b;
    if (2 * a * b + 2 * a * c + 2 * b * c < ans) {
      ans = 2 * a * b + 2 * a * c + 2 * b * c;
      sa = a;
      sb = b;
      sc = c;
    }
    return;
  }
  int mx = 0;
  long long now = b;
  for (int i = 1; i <= P[x]; i++) {
    now *= N[x];
    if (now > lim) {
      now /= N[x];
      break;
    }
    mx = i;
  }
  for (int i = mx; i >= 0; i--) {
    dfs2(x - 1, a, now, lim);
    now /= N[x];
  }
}
void dfs(int x, long long a, long long lim) {
  if (x == 0) {
    double p = sqrt(n / a);
    if (4 * a * p + 2 * (n / a) - 1e-9 > ans) return;
    dfs2(s, a, 1, (long long)(sqrt(n / a)) + 1);
    return;
  }
  int mx = 0;
  long long now = a;
  for (int i = 1; i <= P[x]; i++) {
    now *= N[x];
    if (now > lim) {
      now /= N[x];
      break;
    }
    mx = i;
  }
  for (int i = mx; i >= 0; i--) {
    P[x] -= i;
    dfs(x - 1, now, lim);
    P[x] += i;
    now /= N[x];
  }
}
int main() {
  scanf("%d", &T);
  for (int i = 1; i <= T; i++) {
    n = 1;
    ans = 0x7FFFFFFFFFFFFFFF;
    scanf("%d", &s);
    for (int j = 1; j <= s; j++) {
      scanf("%I64d%d", &N[j], &P[j]);
      for (int k = 1; k <= P[j]; k++) n *= N[j];
    }
    dfs(s, 1, (long long)(pow(n, 1.0 / 3.0)) + 1);
    printf("%I64d %I64d %I64d %I64d\n", ans, sa, sb, sc);
  }
}
