#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x * 10) + (ch - '0');
    ch = getchar();
  }
  return x * f;
}
long long Pow[25][505], n, V, ans, B;
long long val[505];
void dfs2(long long i, long long b, long long a) {
  if ((double)b * b > V / a) {
    return;
  }
  if (i > n) {
    B = max(B, b);
    return;
  }
  for (register long long j = val[i]; j >= 0; --j) {
    dfs2(i + 1, b * Pow[i][j], a);
  }
}
long long ansA, ansB, ansC;
void dfs1(long long i, long long a) {
  if ((double)a * a * a > V) {
    return;
  }
  if (i > n) {
    const long long t = V / a;
    long long Smax = 4 * a * sqrt(t) + 2 * t;
    if (Smax >= ans) return;
    B = 0, dfs2(1, 1, a);
    long long c = V / a / B, Ans = (a * B + B * c + c * a) << 1;
    if (Ans < ans) {
      ans = Ans;
      ansA = a, ansB = B, ansC = c;
    }
    return;
  }
  for (register long long j = val[i]; j >= 0; --j) {
    val[i] -= j;
    dfs1(i + 1, a * Pow[i][j]);
    val[i] += j;
  }
}
int main() {
  long long T;
  cin >> T;
  while (T--) {
    cin >> n;
    V = 1;
    for (register long long i = 1; i <= n; ++i) {
      long long a, b;
      cin >> a >> b;
      val[i] = b;
      Pow[i][0] = 1;
      for (register long long j = 1; j <= b; ++j) {
        Pow[i][j] = Pow[i][j - 1] * a;
      }
      V *= Pow[i][b];
    }
    ans = 1ll << 62;
    dfs1(1, 1);
    ;
    ;
    ;
    ;
    ;
    ;
    ;
    cout << ans << " " << ansA << " " << ansB << " " << ansC << endl;
  }
}
