#include <bits/stdc++.h>
using namespace std;
struct point {
  long long x, a;
} p[100];
int n;
long long key[100][100];
long long k[100];
long long s[3], ans;
long long v;
void getb(long long ab, long long b, int cur) {
  if (ab < b * b) return;
  if (cur > n + 1) return;
  if (cur == n + 1) {
    long long a = ab / b;
    long long c = v / ab;
    long long tmp = a * c + ab + b * c;
    if (tmp < ans) {
      ans = tmp;
      s[0] = a, s[1] = b, s[2] = c;
    }
    return;
  }
  int t = p[cur].a - k[cur];
  for (int i = t; i >= 0; i--) {
    getb(ab, b * key[cur][i], cur + 1);
  }
}
void dfs(long long c, int cur) {
  if (v / c < c * c || cur > n + 1) return;
  if (cur == n + 1) {
    long long ab = v / c;
    if (ab + 2 * sqrt(1.0 * v * c) <= ans) {
      getb(ab, 1, 1);
    }
    return;
  }
  for (int i = p[cur].a; i >= 0; i--) {
    k[cur] = i;
    dfs(c * key[cur][i], cur + 1);
  }
}
int main() {
  int cas;
  long long suma, sumb;
  scanf("%d", &cas);
  while (cas--) {
    scanf("%d", &n);
    v = 1;
    for (int i = 1; i <= n; i++) {
      cin >> p[i].x >> p[i].a;
      long long t = 1;
      key[i][0] = 1;
      for (int j = 1; j <= p[i].a; j++) {
        t *= p[i].x;
        key[i][j] = t;
      }
      v *= t;
    }
    ans = 2 * v + 1;
    s[0] = v, s[1] = s[2] = 1;
    dfs(1, 1);
    ans *= 2;
    cout << ans << ' ' << s[0] << ' ' << s[1] << ' ' << s[2] << endl;
  }
  return 0;
}
