#include <bits/stdc++.h>
using namespace std;
const int N = 50;
int T, n, p[N];
long long v[N], V, A, B, C, a, b, c;
long long LA, LB, Ans;
void Dfs_b(int k, long long num) {
  if (num > LB) return;
  if (k > n) {
    c = V / a / (b = num);
    if (a > b or b > c or a > c) return;
    long long ans = 2 * V / a + 2 * a * (b + c);
    if (ans < Ans) A = a, B = b, C = c, Ans = ans;
    return;
  }
  if (p[k]) p[k]--, Dfs_b(k, num * v[k]), p[k]++;
  Dfs_b(k + 1, num);
}
void Dfs_a(int k, long long num) {
  if (num > LA) return;
  if (k > n) {
    if (a = num, 2 * V / a + 4 * a * sqrt(V / a) > Ans) return;
    LB = pow(V / a, 0.5) + 1e-8;
    Dfs_b(1, 1);
    return;
  }
  if (p[k]) p[k]--, Dfs_a(k, num * v[k]), p[k]++;
  Dfs_a(k + 1, num);
}
int main() {
  for (scanf("%d", &T); T--;) {
    scanf("%d", &n);
    V = 1;
    for (int i = 1; i <= n; i++) {
      scanf("%I64d%d", &v[i], &p[i]);
      for (int j = 1; j <= p[i]; j++) V *= v[i];
    }
    Ans = 5 * 1e18;
    LA = pow(V, 1.0 / 3.0) + 1e-8;
    Dfs_a(1, 1);
    printf("%I64d %I64d %I64d %I64d\n", Ans, A, B, C);
  }
}
