#include <bits/stdc++.h>
using namespace std;
struct pri {
  long long v;
  int num;
} q[100], les[100];
long long t, n, V = 1;
int cnt;
struct tyc {
  int g[100], id;
  long long q;
} e[100010];
const int TC = 1e9;
long long ansa, ansb, ansc, z;
int num[100];
long long minn = 8e18;
long long fast_pow(long long x, int a) {
  long long ans = 1;
  for (int i = a; i; i >>= 1, x = x * x)
    if (i & 1) ans *= x;
  return ans;
}
void dfs(int i, long long v) {
  if (i == n + 1) {
    cnt++;
    for (int j = 1; j <= n; j++) e[cnt].g[j] = num[j];
    e[cnt].q = v;
    e[cnt].id = cnt;
    return;
  }
  for (int j = 0; j <= q[i].num; j++) {
    num[i] = j;
    dfs(i + 1, v);
    v *= q[i].v;
    if (v > TC) break;
    if (v * v * v > V) break;
  }
}
void dfs2(int i, long long v, long long A) {
  if (i == n + 1) {
    z = V / (v * A);
    if (minn > v * A + v * z + A * z) {
      minn = v * A + v * z + A * z;
      ansa = A;
      ansb = v;
      ansc = z;
    }
    return;
  }
  for (int j = 0; j <= les[i].num; j++) {
    num[i] = j;
    dfs2(i + 1, v, A);
    v *= q[i].v;
    if (v * v * A > V) break;
  }
}
bool cmp(tyc a, tyc b) { return a.q < b.q; }
int main() {
  scanf("%d", &t);
  while (t--) {
    V = 1;
    cnt = 0;
    minn = 8e18;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      scanf("%I64d%d", &q[i].v, &q[i].num), V *= fast_pow(q[i].v, q[i].num);
    dfs(1, 1);
    for (int i = cnt; i >= 1; i--) {
      double C = sqrt(1.0 * V / e[i].q);
      if (2 * e[i].q * C + C * C >= minn) continue;
      for (int j = 1; j <= n; j++) les[j].num = q[j].num - e[i].g[j];
      dfs2(1, 1, e[i].q);
    }
    printf("%I64d %I64d %I64d %I64d\n", minn * 2, ansa, ansb, ansc);
  }
}
