#include <bits/stdc++.h>
int b[100], _A[100], n;
long long Ans, AnsA, AnsB, AnsC, a[100], c[100][100], LastA, V, V2;
void DfsB(int l, long long B) {
  if (!(V2 / B / B)) return;
  if (l == n) {
    long long C = V2 / B;
    if ((V / LastA + V / B + V / C) < Ans) {
      Ans = V / LastA + V / B + V / C;
      AnsA = LastA;
      AnsB = B;
      AnsC = C;
    }
    return;
  }
  for (int i = b[l] - _A[l]; i >= 0; i--) DfsB(l + 1, B * c[l][i]);
}
void DfsA(int l, long long A) {
  if (!(V / A / A / A)) return;
  if (l == n) {
    long long B = (long long)sqrt(V / A), C = (V / A + B - 1) / B;
    if ((V / A + V / B + V / C) >= Ans) return;
    LastA = A;
    V2 = V / LastA;
    DfsB(0, 1);
    return;
  }
  for (_A[l] = b[l]; _A[l] >= 0; _A[l]--) DfsA(l + 1, A * c[l][_A[l]]);
}
int main() {
  int i, j, TestCase;
  for (scanf("%d", &TestCase); TestCase--;) {
    scanf("%d", &n);
    V = 1;
    for (i = n - 1; i >= 0; i--) {
      scanf("%I64d%d", a + i, b + i);
      c[i][0] = 1;
      for (j = 0; j < b[i]; j++) {
        c[i][j + 1] = c[i][j] * a[i];
        V *= a[i];
      }
    }
    Ans = 2 * V + 2;
    DfsA(0, 1);
    printf("%I64d %I64d %I64d %I64d\n", 2 * Ans, AnsA, AnsB, AnsC);
  }
  return 0;
}
