#include <bits/stdc++.h>
using namespace std;
const long long LLinf = 7000000000000000000LL;
const int maxn = 205;
long long ppow[maxn][maxn];
pair<long long, int> p[maxn];
long long V, V1, ans, na, nb, b;
int n;
void dfs2(int u, long long x) {
  if (x > V1) return;
  if (u == 0) {
    b = max(x, b);
    return;
  }
  for (int i = p[u].second; i >= 0; --i) dfs2(u - 1, x * ppow[u][i]);
}
void dfs(int u, long long a) {
  if ((double)a * a * a > V) return;
  if (u == 0) {
    long long x = V / a, tmp;
    V1 = int(sqrt(x) + 0.5);
    if (2 * V1 * a + x < ans) {
      b = 1;
      dfs2(n, 1);
      tmp = (b + x / b) * a + x;
      if (tmp < ans) ans = tmp, na = a, nb = b;
    }
    return;
  }
  int i, lim = p[u].second;
  for (i = lim; i >= 0; --i) {
    p[u].second -= i;
    dfs(u - 1, a * ppow[u][i]);
    p[u].second += i;
  }
}
void work() {
  int i, j;
  scanf("%d", &n);
  ans = LLinf;
  for (i = 1; i <= n; ++i) scanf("%I64d%d", &p[i].first, &p[i].second);
  sort(p + 1, p + n + 1);
  for (i = 1; i <= n; ++i)
    for (j = 1, ppow[i][0] = 1; j <= p[i].second; ++j)
      ppow[i][j] = ppow[i][j - 1] * p[i].first;
  for (i = 1, V = 1; i <= n; ++i) V *= ppow[i][p[i].second];
  dfs(n, 1);
  printf("%I64d %I64d %I64d %I64d\n", ans * 2, na, nb, V / na / nb);
}
int main() {
  int test;
  scanf("%d", &test);
  while (test--) work();
  return 0;
}
