#include <bits/stdc++.h>
using namespace std;
enum { MAXN = 70 };
pair<long long, int> p[MAXN];
int seq[MAXN];
long long v[MAXN][MAXN];
const long long INF = 1LL << 62;
long long val;
int maxa, maxb, curb, N;
void searchB(int k, int cur) {
  if (k == N) {
    curb = max(cur, curb);
  } else {
    searchB(k + 1, cur);
    if (p[k].first <= maxb) {
      for (int i = 0; i < seq[k] && (long long)cur * p[k].first <= maxb; i++) {
        cur *= p[k].first;
        searchB(k + 1, cur);
      }
    }
  }
}
long long ans, ra, rb, rc;
void searchA(int k, int cur) {
  if (k == N) {
    maxb = sqrt(val / cur);
    if ((long long)maxb * maxb + (long long)maxb * cur * 2 >= ans) return;
    curb = 1;
    searchB(0, 1);
    long long c = val / cur / curb;
    if ((long long)cur * curb + (cur + curb) * c < ans) {
      ans = (long long)cur * curb + (cur + curb) * c;
      ra = cur;
      rb = curb;
      rc = c;
    }
  } else {
    seq[k] = p[k].second;
    searchA(k + 1, cur);
    if (p[k].first <= maxa) {
      for (seq[k] = p[k].second;
           seq[k] > 0 && (long long)cur * p[k].first <= maxa;) {
        --seq[k];
        cur *= p[k].first;
        searchA(k + 1, cur);
      }
    }
  }
}
int ssqrt(const long long &val) {
  int left = 1, right = 1000000, mid;
  while (left < right) {
    mid = (left + right + 1) >> 1;
    if ((long long)mid * mid * mid <= val)
      left = mid;
    else
      right = mid - 1;
  }
  return left;
}
int main() {
  int t;
  cin >> t;
  while (t-- > 0) {
    cin >> N;
    val = 1;
    for (int i = 0; i < N; i++) {
      cin >> p[i].first >> p[i].second;
      v[i][0] = 1;
      for (int j = 1; j <= p[i].second; j++) v[i][j] = v[i][j - 1] * p[i].first;
      val *= v[i][p[i].second];
    }
    maxa = ssqrt(val);
    ans = INF;
    searchA(0, 1);
    cout << ans * 2 << ' ' << ra << ' ' << rb << ' ' << rc << endl;
  }
}
