#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long readll() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int num[505], n, used[505];
long long v, x, y, z, pri[505], ans, Pri[505][505];
void work(long long ab, long long b, int k) {
  if (ab < b * b) return;
  if (k == n + 1) {
    long long a = ab / b, c = v / ab;
    long long tmp = a * c + ab + b * c;
    if (tmp < ans) {
      ans = tmp;
      x = a;
      y = b;
      z = c;
    }
    return;
  }
  int t = num[k] - used[k];
  for (int i = t; i >= 0; i--) work(ab, b * Pri[k][i], k + 1);
}
void dfs(long long s, int k) {
  if (v / s < s * s) return;
  if (k == n + 1) {
    long long ss = v / s;
    if (ss + 2 * sqrt((double)v * s) <= ans) work(ss, 1, 1);
    return;
  }
  for (int i = num[k]; i >= 0; i--) {
    used[k] = i;
    dfs(s * Pri[k][i], k + 1);
  }
}
int main() {
  int T = read();
  while (T--) {
    n = read();
    v = 1;
    for (int i = 1; i <= n; i++) {
      pri[i] = readll();
      num[i] = read();
      Pri[i][0] = 1;
      for (int j = 1; j <= num[i]; j++) Pri[i][j] = Pri[i][j - 1] * pri[i];
      v *= Pri[i][num[i]];
    }
    x = v;
    y = 1;
    z = 1;
    ans = 2 * v + 1;
    dfs(1, 1);
    ans *= 2;
    printf("%I64d %I64d %I64d %I64d\n", ans, x, y, z);
  }
  return 0;
}
