#include <bits/stdc++.h>
using namespace std;
long long pw[20][120], p[120], V, B, fA, fB, fC, ans;
int num[120], n, T, i, j;
void dfs_B(int l, long long x) {
  if ((double)x * x > V) return;
  if (l == n + 1) {
    B = max(B, x);
    return;
  }
  for (int i = 0; i <= num[l]; ++i) dfs_B(l + 1, x * pw[l][i]);
}
void dfs_A(int l, long long A) {
  if ((double)A * A * A > V) return;
  if (l == n + 1) {
    long long x = (int)sqrt(V / A);
    if (2 * A * x + V / A >= ans) return;
    V /= A;
    B = 0;
    dfs_B(1, 1);
    if (A * B + A * (V / B) + V < ans) {
      ans = A * B + A * (V / B) + V;
      fA = A;
      fB = B;
      fC = V / B;
    }
    V *= A;
    return;
  }
  for (int i = num[l]; i >= 0; --i) {
    num[l] -= i;
    dfs_A(l + 1, A * pw[l][i]);
    num[l] += i;
  }
}
int main() {
  for (scanf("%d", &T); T--;) {
    scanf("%d", &n);
    V = 1;
    for (i = 1; i <= n; ++i) {
      scanf("%I64d%d", &p[i], &num[i]);
      pw[i][0] = 1;
      for (j = 1; j <= num[i]; ++j) pw[i][j] = pw[i][j - 1] * p[i];
      V *= pw[i][num[i]];
    }
    ans = 1ll << 62;
    dfs_A(1, 1);
    printf("%I64d %I64d %I64d %I64d\n", ans * 2, fA, fB, fC);
  }
}
