#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e9;
int n, m;
int r;
long long p[30];
int w[30], ww[30];
int q[100010], g[100010][30];
int u[30], P[100010];
long long v, a, b, c, ans;
void dfs1(int i, int x) {
  if (i == n) {
    for (int j = 0; j < n; j++) g[r][j] = u[j];
    P[r] = r;
    q[r++] = x;
    return;
  }
  for (int j = 0; j <= w[i]; j++) {
    u[i] = j;
    dfs1(i + 1, x);
    if (j < w[i] && x <= MAXN / p[i])
      x *= p[i];
    else
      break;
    if (x > v / x / x) break;
  }
  return;
}
void dfs2(int i, int x, int y, long long z) {
  if (i == n) {
    if (x >= y) {
      z /= x;
      long long tmp = (long long)x * y + y * z + z * x;
      if (tmp < ans) {
        ans = tmp;
        a = x;
        b = y;
        c = z;
      }
    }
    return;
  }
  for (int j = 0; j <= ww[i]; j++) {
    dfs2(i + 1, x, y, z);
    if (j < ww[i] && x <= MAXN / p[i])
      x *= p[i];
    else
      break;
    if (x > z / x) break;
  }
  return;
}
bool cmp(int i, int j) { return q[i] < q[j]; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    v = 1;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%lld%d", &p[i], &w[i]);
      for (int j = 1; j <= w[i]; j++) v *= p[i];
    }
    ans = 4e18;
    r = 0;
    dfs1(0, 1);
    sort(P, P + r, cmp);
    for (int num = r - 1; num >= 0; num--) {
      int i = P[num];
      double ppp = sqrt(v / q[i] + 0.0);
      if (2 * q[i] * ppp + ppp * ppp >= ans) continue;
      for (int j = 0; j < n; j++) ww[j] = w[j] - g[i][j];
      dfs2(0, 1, q[i], v / q[i]);
    }
    cout << ans * 2 << ' ' << a << ' ' << b << ' ' << c << ' ' << endl;
  }
  return 0;
}
