#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
int rnd(int mod) {
  unsigned long long ans = 0;
  for (int i = 0; i < 5; i++) ans = ans << 15 ^ rand();
  return ans % (mod - 1) + 1;
}
int quickpow(long long bb, long long pp, long long kk) {
  long long b = bb, p = pp, k = kk, ans = 1;
  while (p > 1) {
    if (p % 2 == 1) ans = (ans % k) * (b % k) % k;
    p /= 2;
    b = (b % k) * (b % k) % k;
  }
  ans = ans * (b % k) % k;
  return ans;
}
long long na, nb, nc, n, B;
long long v = 1, a[10008], b[10008], s, c;
long long p[10008 / 2][10008 / 2];
long long t, k;
void dfs2(long long i, long long b1, long long a1) {
  long long j;
  if ((double)b1 * b1 > v / a1) {
    return;
  }
  if (i > n) {
    B = max(B, b1);
    return;
  }
  for (j = b[i]; j >= 0; j--) {
    dfs2(i + 1, b1 * p[i][j], a1);
  }
}
void dfs1(long long i, long long a1) {
  long long t, sm, c, A;
  if ((double)a1 * a1 * a1 > v) return;
  if (i > n) {
    t = v / a1;
    sm = 4 * a1 * sqrt(t) + 2 * t;
    if (sm >= s) return;
    B = 0;
    dfs2(1, 1, a1);
    c = v / a1 / B;
    A = (a1 * B + B * c + c * a1) << 1;
    if (A < s) {
      s = A;
      na = a1;
      nb = B;
      nc = c;
    }
    return;
  }
  long long j;
  for (j = b[i]; j >= 0; j--) {
    b[i] -= j;
    dfs1(i + 1, a1 * p[i][j]);
    b[i] += j;
  }
}
int main() {
  long long T, i, j;
  cin >> T;
  while (T--) {
    n = read();
    v = 1;
    for (i = 1; i <= n; i++) {
      a[i] = read();
      b[i] = read();
      p[i][0] = 1;
      for (j = 1; j <= b[i]; j++) {
        p[i][j] = p[i][j - 1] * a[i];
      }
      v *= p[i][b[i]];
    }
    s = 1ll << 62;
    dfs1(1, 1);
    printf("%lld %lld %lld %lld\n", s, na, nb, nc);
  }
  return 0;
}
