#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int x = 0, f = 1;
  register 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;
}
inline void write(int x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
long long nA, nB, nC, P[505][505], n, V, s, B, v[10001];
void dfs2(long long i, long long b, long long a) {
  long long j;
  if ((double)b * b > V / a) {
    return;
  }
  if (i > n) {
    B = max(B, b);
    return;
  }
  for (j = v[i]; j >= 0; j--) {
    dfs2(i + 1, b * P[i][j], a);
  }
}
void dfs1(long long i, long long a) {
  long long j, t, Sm, c, A;
  if ((double)a * a * a > V) {
    return;
  }
  if (i > n) {
    t = V / a;
    Sm = 4 * a * sqrt(t) + 2 * t;
    if (Sm >= s) {
      return;
    }
    B = 0;
    dfs2(1, 1, a);
    c = V / a / B;
    A = (a * B + B * c + c * a) << 1;
    if (A < s) {
      s = A;
      nA = a;
      nB = B;
      nC = c;
    }
    return;
  }
  for (j = v[i]; j >= 0; --j) {
    v[i] -= j;
    dfs1(i + 1, a * P[i][j]);
    v[i] += j;
  }
}
long long a, b;
int main() {
  int T = read();
  while (T--) {
    cin >> n;
    V = 1;
    for (long long i = 1; i <= n; i++) {
      cin >> a >> b;
      v[i] = b;
      P[i][0] = 1;
      for (long long j = 1; j <= b; j++) {
        P[i][j] = P[i][j - 1] * a;
      }
      V *= P[i][b];
    }
    s = 1ll << 62;
    dfs1(1, 1);
    cout << s << " " << nA << " " << nB << " " << nC << "\n";
  }
  return 0;
}
