#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read(register T& t) {
  register T f = 1;
  register char ch = getchar();
  t = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar();
  t *= f;
  return t;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
long long V;
int n;
long long P[505];
int Q[505];
long long PP[505][505];
long long ans;
int used[505];
long long AnsA, AnsB, AnsC;
long long MB;
long long MA;
void dfs2(register int now, register long long B) {
  if ((double)B * B > V / MA) return;
  if (now > n) return MB = max(MB, B), void();
  for (register int i = Q[now]; i >= used[now]; i--)
    dfs2(now + 1, B * PP[now][i - used[now]]);
}
void dfs1(register int now, register long long A) {
  if ((double)A * A * A > V) return;
  if (now > n) {
    double Tp = V / A;
    if (4 * A * sqrt(Tp) + Tp * 2 > ans) return;
    MB = 0;
    MA = A;
    dfs2(1, 1);
    long long C = V / A / MB;
    long long tp = (A * MB + MB * C + A * C) << 1;
    if (tp < ans) {
      ans = tp;
      AnsA = A;
      AnsB = MB;
      AnsC = C;
    }
    return;
  }
  for (used[now] = Q[now]; used[now] >= 0; used[now]--)
    dfs1(now + 1, A * PP[now][used[now]]);
}
int main() {
  int T;
  read(T);
  while (T--) {
    V = 1;
    read(n);
    for (int i = 1; i <= n; i++) {
      read(P[i], Q[i]);
      PP[i][0] = 1;
      for (register int j = 1; j <= Q[i]; j++) PP[i][j] = PP[i][j - 1] * P[i];
      V *= PP[i][Q[i]];
    }
    ans = 4e18;
    dfs1(1, 1);
    printf("%lld %lld %lld %lld\n", ans, AnsA, AnsB, AnsC);
  }
  return 0;
}
