#include <bits/stdc++.h>
using namespace std;
int _c, n, m;
int e[70];
long long p[70][70], V, A, B, C, ans, G, F;
void dfs2(const long long b, int x) {
  if (F / b < b) return;
  if (x > n) {
    long long c = F / b;
    if (G * b + b * c + c * G < ans)
      ans = G * b + b * c + c * G, A = G, B = b, C = c;
    return;
  }
  for (int E = e[x]; E >= 0; --E) dfs2(b * p[x][E], x + 1);
}
void dfs(const long long a, int x) {
  if (V / a / a < a) return;
  if (x > n) {
    double T = sqrt(V / a);
    G = a, F = V / a;
    if (T * a * 2 + V / a < ans) dfs2(1, 1);
    return;
  }
  int E = e[x];
  e[x] = 0;
  for (; E >= 0; --E, ++e[x]) dfs(a * p[x][E], x + 1);
  --e[x];
}
void work() {
  scanf("%d", &n);
  V = 1;
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d%d", &p[i][1], &e[i]);
    p[i][0] = 1;
    for (int j = 2; j <= e[i]; ++j) p[i][j] = p[i][1] * p[i][j - 1];
    V *= p[i][e[i]];
  }
  ans = V * 2 + 1;
  A = V, B = C = 1;
  dfs(1, 1);
  printf("%I64d %I64d %I64d %I64d\n", ans << 1, A, B, C);
}
int main() {
  for (scanf("%d", &_c); _c--;) work();
  return 0;
}
