#include <bits/stdc++.h>
using namespace std;
const long long inf = 9e18;
const int K = 100;
long long all[K], p[K], ans, ansa, ansb, ansc, cura, curb, N;
int d[K], k, t;
long long S(long long a, long long b, long long c) {
  return 2 * (a * b + a * c + b * c);
}
void go_b(int i) {
  if (curb > N) return;
  if (i == k) {
    if (S(cura, curb, N) < ans) {
      ans = S(cura, curb, N);
      ansa = cura, ansb = curb, ansc = N;
    }
    return;
  }
  int curd = d[i];
  d[i] = 0, curb *= all[i], N /= all[i], all[i] = 1;
  while (curd >= 0) {
    go_b(i + 1);
    if (curd) {
      curb /= p[i];
      N *= p[i];
      all[i] *= p[i];
      ++d[i];
    }
    --curd;
  }
}
void go_a(int i) {
  if (cura > N / cura) return;
  if (i == k) {
    long long good = sqrt(N);
    if (S(cura, good, good) < ans) go_b(0);
    return;
  }
  int curd = d[i];
  d[i] = 0, cura *= all[i], N /= all[i], all[i] = 1;
  while (curd >= 0) {
    go_a(i + 1);
    if (curd) {
      cura /= p[i];
      N *= p[i];
      all[i] *= p[i];
      ++d[i];
    }
    --curd;
  }
}
int main() {
  scanf("%d", &t);
  for (int it = 0; it < t; ++it) {
    scanf("%d", &k);
    N = 1, ans = inf, ansa = ansb = ansc = -1;
    for (int i = 0; i < k; ++i) {
      scanf("%I64d%d", &p[i], &d[i]);
      all[i] = 1;
      for (int j = 0; j < d[i]; ++j) all[i] *= p[i];
      N *= all[i];
    }
    cura = curb = 1;
    go_a(0);
    printf("%I64d %I64d %I64d %I64d\n", ans, ansa, ansb, ansc);
  }
  return 0;
}
