#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x = x * f;
  return;
}
void write(int x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
  return;
}
int t, k;
long long x[1001], y[1001], v, ansa, ansb, ansc, a, b, c, lma, lmb, ans;
long long quickpow(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1) ret *= x;
    x *= x;
    y >>= 1;
  }
  return ret;
}
void dfs2(int dep, long long sum) {
  if (sum > lmb) return;
  if (dep > k) {
    if (sum < a) return;
    b = sum;
    c = v / a / b;
    if (a * b + b * c + a * c < ans) {
      ans = a * b + b * c + a * c;
      ansa = a;
      ansb = b;
      ansc = c;
    }
    return;
  }
  if (y[dep]) {
    y[dep]--;
    dfs2(dep, sum * x[dep]);
    y[dep]++;
  }
  dfs2(dep + 1, sum);
  return;
}
void dfs1(int dep, long long sum) {
  if (sum > lma) return;
  if (dep > k) {
    a = sum;
    if (v / a + a * 2 * sqrt(v / a) < ans) {
      lmb = sqrt(v / a) + 1e-8;
      dfs2(1, 1);
    }
    return;
  }
  if (y[dep]) {
    y[dep]--;
    dfs1(dep, sum * x[dep]);
    y[dep]++;
  }
  dfs1(dep + 1, sum);
  return;
}
int main() {
  read(t);
  for (register int(p) = (1); (p) <= (t); p++) {
    read(k);
    v = 1;
    for (register int(i) = (1); (i) <= (k); i++) {
      read(x[i]), read(y[i]);
      v *= quickpow(x[i], y[i]);
    }
    lma = pow(v, 1.0 / 3) + 1e-8;
    ansa = 0;
    ansb = 0;
    ansc = 0;
    ans = 0x7fffffffffffffff;
    dfs1(1, 1);
    printf("%lld %lld %lld %lld\n", ans << 1, ansa, ansb, ansc);
  }
  return 0;
}
