#include <bits/stdc++.h>
using namespace std;
long long A, V, S, x, y, z, a[105], b[105];
int t, n, T;
void dfs(int q, int t, long long B) {
  if (B * B > V / (q ? A : B)) return;
  if (t > n) {
    long long C = V / A / B;
    if (q == 0) {
      A = B;
      if (A * A <= C && (C + 2 * sqrt(C) * B < S)) dfs(1, 1, 1);
      A = 1;
    } else if (A * B + A * C + B * C < S)
      S = A * B + A * C + B * C, x = A, y = B, z = C;
    return;
  }
  for (int i = 1; i <= b[t]; i++) B *= a[t];
  for (int i = b[t]; i >= 0; i--)
    b[t] -= i, dfs(q, t + 1, B), B /= a[t], b[t] += i;
}
int main() {
  cin >> t;
  while (t--) {
    cin >> n, V = A = 1;
    for (int i = 1; i <= n; i++) {
      cin >> a[i] >> b[i];
      for (int j = 1; j <= b[i]; j++) V *= a[i];
    }
    S = 2 * V + 2, dfs(0, 1, 1);
    cout << S * 2 << " " << x << " " << y << " " << z << endl;
  }
  return 0;
}
