#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long Hashimoto = 0;
  bool Kanna = 1;
  char I_Love = getchar();
  while (I_Love < '0' || I_Love > '9') {
    if (I_Love == '-') Kanna = 0;
    I_Love = getchar();
  }
  while (I_Love >= '0' && I_Love <= '9') {
    Hashimoto = Hashimoto * 10 + I_Love - '0';
    I_Love = getchar();
  }
  return (Kanna ? Hashimoto : -Hashimoto);
}
template <typename T1, typename T2>
inline void Umax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T1, typename T2>
inline void Umin(T1 &a, T2 b) {
  if (a > b) a = b;
}
int n;
long long v;
long long x[22], y[22];
long long ans, ansa, ansb, ansc;
long long bound_a, bound_b;
long long fuckU_a;
void dfsb(int pos, long long b) {
  if (pos == n) {
    long long a = fuckU_a;
    long long c = v / a / b;
    long long s = 2 * (a * b + b * c + c * a);
    if (s < ans) {
      ans = s;
      ansa = a;
      ansb = b;
      ansc = c;
    }
    return;
  }
  int ub = 0;
  long long curb = b;
  while (curb * x[pos] <= bound_b && ub < y[pos]) {
    curb *= x[pos];
    ub++;
  }
  for (int i = ub; i >= 0; --i) {
    dfsb(pos + 1, curb);
    curb /= x[pos];
  }
}
void dfsa(int pos, long long a) {
  if (pos == n) {
    bound_b = sqrt(v / a);
    if (2ll * (2ll * a * bound_b + v / a) >= ans) return;
    fuckU_a = a;
    dfsb(0, 1);
    return;
  }
  int ub = 0;
  long long cura = a;
  while (cura * x[pos] <= bound_a && ub < y[pos]) {
    cura *= x[pos];
    ub++;
  }
  y[pos] -= ub;
  for (int i = ub; i >= 0; --i) {
    dfsa(pos + 1, cura);
    cura /= x[pos];
    y[pos]++;
  }
  y[pos]--;
}
void get_fucked_ans() {
  int p[20];
  for (int i = 0; i < (n); ++i) p[i] = i;
  for (int tot = 0; tot < (1000); ++tot) {
    long long a = 1, b = 1, c = 1;
    for (int fuck = 0; fuck < (n); ++fuck) {
      int i = p[fuck];
      for (int j = 0; j < (y[i]); ++j) {
        long long mn = min(min(a, b), c);
        if (mn == a) {
          a *= x[i];
        } else if (mn == b) {
          b *= x[i];
        } else
          c *= x[i];
      }
    }
    long long s = 2 * (a * b + b * c + c * a);
    if (s < ans) {
      ans = s;
      ansa = a;
      ansb = b;
      ansc = c;
    }
    random_shuffle(p, p + n);
  }
}
void solve() {
  ans = 4 * (long long)1e18 + 233;
  n = read();
  v = 1;
  for (int i = 0; i < (n); ++i) {
    x[i] = read();
    y[i] = read();
    for (int j = 0; j < (y[i]); ++j) {
      v *= x[i];
    }
  }
  bound_a = pow(v, 1.0 / 3) + 1;
  get_fucked_ans();
  for (int i = 0; i < (n); ++i) {
    for (int j = i + 1; j < (n); ++j) {
      if (x[i] < x[j]) {
        swap(x[i], x[j]);
        swap(y[i], y[j]);
      }
    }
  }
  dfsa(0, 1);
  printf("%I64d %I64d %I64d %I64d\n", ans, ansa, ansb, ansc);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  srand(19990203);
  int T = read();
  while (T--) solve();
  return 0;
}
