#include <bits/stdc++.h>
using namespace std;
const int N = 127;
long long w[N], S, A, B, C, v;
int c[N], n, t;
long long alpha(long long a) { return 2 * sqrt(a) * sqrt(v) + v / a; }
void dfs2(int p, long long a, long long b) {
  if ((double)a * b * b > v) return;
  if (p > n) {
    long long c = v / a / b;
    if (a * b + b * c + a * c < S) {
      S = a * b + b * c + a * c;
      A = a;
      B = b;
      C = c;
    }
    return;
  }
  long long sum = 1;
  for (int i = 1; i <= c[p]; i++) sum *= w[p];
  for (int i = c[p]; i >= 0; i--) {
    dfs2(p + 1, a, b * sum);
    sum /= w[p];
  }
}
void dfs(int p, long long a) {
  if ((double)a * a * a > v) return;
  if (p > n) {
    if (alpha(a) < S) dfs2(1, a, 1);
    return;
  }
  long long sum = 1;
  for (int i = 1; i <= c[p]; i++) sum *= w[p];
  for (int i = c[p]; i >= 0; i--) {
    c[p] -= i;
    dfs(p + 1, a * sum);
    c[p] += i;
    sum /= w[p];
  }
}
int main() {
  scanf("%d", &t);
  while (t--) {
    S = 3e18;
    v = 1;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%I64d%d", &w[i], &c[i]);
      for (int j = 1; j <= c[i]; j++) v *= w[i];
    }
    dfs(1, 1);
    printf("%I64d %I64d %I64d %I64d\n", S * 2, A, B, C);
  }
}
