#include <bits/stdc++.h>
using namespace std;
int T, n;
long long V, Y, u, ans = 0, ax, ay, az;
struct Point {
  long long p;
  int k;
} a[27];
inline bool cmp(Point x, Point y) { return x.p > y.p; }
long long P[27];
void dfs1(int x, long long v) {
  if (v * v > u) return;
  if (x > n) {
    Y = max(Y, v);
    return;
  }
  if (v * P[x] <= Y) return;
  int i;
  long long o = 1;
  for (i = a[x].k; i >= 0; i--) {
    if (v * o <= 1e9)
      dfs1(x + 1, v * o);
    else
      break;
    o *= a[x].p;
  }
}
inline void calc() {
  Y = 1;
  P[n + 1] = 1;
  int i, j;
  for (i = n; i >= 1; i--) {
    P[i] = P[i + 1];
    for (j = 1; j <= a[i].k; j++) P[i] *= a[i].p;
  }
  dfs1(1, 1);
}
void dfs(int x, int v) {
  if (1ll * v * v * v > V) return;
  if (x > n) {
    u = V / v;
    long long mi = 2ll * v * sqrt(u) + u;
    if (mi >= ans) return;
    calc();
    long long Z = u / Y, o = Y * v + Z * v + Y * Z;
    if (o < ans) {
      ans = o;
      ax = v;
      ay = Y;
      az = Z;
    }
    return;
  }
  int i, tm = a[x].k;
  long long o = 1;
  for (i = a[x].k; i >= 0; i--) {
    if (1ll * v * o <= 1e6)
      dfs(x + 1, v * o);
    else
      break;
    o *= a[x].p;
    --a[x].k;
  }
  a[x].k = tm;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    int i, j;
    for (i = 1; i <= n; i++) scanf("%lld%d", &a[i].p, &a[i].k);
    sort(a + 1, a + n + 1, cmp);
    V = 1;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= a[i].k; j++) V *= a[i].p;
    ax = ay = 1;
    az = V;
    ans = V << 1 | 1;
    dfs(1, 1);
    printf("%lld\n%lld %lld %lld\n", ans << 1, ax, ay, az);
  }
  return 0;
}
