#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long ans, num[30][61], tot, a, b, c, BC, ansA, ansB, ansC;
int n, cnt[30];
void getB(int l) {
  if (b * b > BC) return;
  if (l == n) {
    c = BC / b;
    if (ans > a * b + b * c + a * c) {
      ans = a * b + b * c + a * c;
      ansA = a;
      ansB = b;
      ansC = c;
    }
    return;
  }
  for (int i = cnt[l]; i >= 0; i--) {
    cnt[l] -= i;
    b *= num[l][i];
    getB(l + 1);
    b /= num[l][i];
    cnt[l] += i;
  }
}
void getA(int l) {
  if (a * a > tot / a) return;
  if (l == n) {
    BC = tot / a;
    if (2 * a * sqrt(BC) + BC >= ans) return;
    getB(0);
    return;
  }
  for (int i = cnt[l]; i >= 0; i--) {
    cnt[l] -= i;
    a *= num[l][i];
    getA(l + 1);
    a /= num[l][i];
    cnt[l] += i;
  }
}
int main() {
  long long p;
  int t, i, j;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (i = 0, tot = 1; i < n; i++) {
      scanf("%I64d%d", &p, cnt + i);
      for (j = num[i][0] = 1; j <= cnt[i]; j++) num[i][j] = p * num[i][j - 1];
      tot *= num[i][cnt[i]];
    }
    ans = 1LL << 62;
    a = b = 1;
    getA(0);
    printf("%I64d %I64d %I64d %I64d\n", ans << 1, ansA, ansB, ansC);
  }
  return 0;
}
