#include <bits/stdc++.h>
using namespace std;
int n, t, a[210];
long long v, s, p[210];
long long ansa, ansb, ansc;
long long calc(long long x) { return 2 * sqrt(x) * sqrt(v) + v / x; }
void dfs2(int now, long long aa, long long bb) {
  if ((long double)bb * bb * aa - v > 1e-6) return;
  if (now > n) {
    if (aa * bb + v / aa + v / bb < s) {
      s = aa * bb + v / aa + v / bb;
      ansa = aa;
      ansb = bb;
      ansc = v / aa / bb;
    }
    return;
  }
  long long pro = 1;
  for (int i = 1; i <= a[now]; ++i) pro *= p[now];
  for (int i = a[now]; i >= 1; --i) {
    a[now] -= i;
    dfs2(now + 1, aa, bb * pro);
    a[now] += i;
    pro /= p[now];
  }
  dfs2(now + 1, aa, bb);
}
void dfs1(int now, long long aa) {
  if ((long double)aa * aa * aa - v > 1e-6) return;
  if (now > n) {
    if (calc(aa) < s) dfs2(1, aa, 1);
    return;
  }
  long long pro = 1;
  for (int i = 1; i <= a[now]; ++i) pro *= p[now];
  for (int i = a[now]; i >= 1; --i) {
    a[now] -= i;
    dfs1(now + 1, aa * pro);
    a[now] += i;
    pro /= p[now];
  }
  dfs1(now + 1, aa);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    v = 1;
    s = 4e18;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%lld%d", &p[i], &a[i]);
      for (int j = 1; j <= a[i]; ++j) v *= p[i];
    }
    dfs1(1, 1);
    printf("%lld %lld %lld %lld\n", 2 * s, ansa, ansb, ansc);
  }
}
