#include <bits/stdc++.h>
using namespace std;
struct fastIO {
  inline fastIO operator>>(int& num) {
    num = 0;
    char c = getchar();
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    bool foo = 0;
    if (c == '-') {
      foo = 1;
      c = getchar();
    }
    while (c >= '0' && c <= '9') {
      num = (num << 3) + (num << 1) + c - '0';
      c = getchar();
    }
    if (foo) num = -num;
    return *this;
  }
  inline fastIO operator>>(long long& num) {
    num = 0;
    char c = getchar();
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    bool foo = 0;
    if (c == '-') {
      foo = 1;
      c = getchar();
    }
    while (c >= '0' && c <= '9') {
      num = (num << 3) + (num << 1) + c - '0';
      c = getchar();
    }
    if (foo) num = -num;
    return *this;
  }
  inline fastIO operator>>(float& num) {
    scanf("%f", &num);
    return *this;
  }
  inline fastIO operator>>(double& num) {
    scanf("%lf", &num);
    return *this;
  }
  inline fastIO operator>>(long double& num) {
    scanf("%Lf", &num);
    return *this;
  }
  inline fastIO operator>>(char& num) {
    num = getchar();
    while (num == ' ' || num == '\n') num = getchar();
    return *this;
  }
  inline fastIO operator>>(char* num) {
    int cnt = 0;
    char c = getchar();
    while (c == ' ' || c == '\n') c = getchar();
    while (c != ' ' && c != '\n') {
      num[cnt++] = c;
      c = getchar();
    }
    num[cnt] = 0;
    return *this;
  }
  inline fastIO operator>>(string& num) {
    cin >> num;
    return *this;
  }
  inline void printInt(const int& num) {
    if (num < 10) {
      putchar(num + '0');
      return;
    }
    printInt(num / 10);
    putchar((num % 10) + '0');
  }
  inline void printINT(const long long& num) {
    if (num < 10) {
      putchar(num + '0');
      return;
    }
    printINT(num / 10);
    putchar((num % 10) + '0');
  }
  inline fastIO operator<<(const int& num) {
    if (num < 0)
      putchar('-'), printInt(-num);
    else
      printInt(num);
    return *this;
  }
  inline fastIO operator<<(const long long& num) {
    if (num < 0)
      putchar('-'), printINT(-num);
    else
      printINT(num);
    return *this;
  }
  inline fastIO operator<<(const float& num) {
    printf("%.10f", num);
    return *this;
  }
  inline fastIO operator<<(const double& num) {
    printf("%.16lf", num);
    return *this;
  }
  inline fastIO operator<<(const long double& num) {
    printf("%.20Lf", num);
    return *this;
  }
  inline fastIO operator<<(const char& num) {
    putchar(num);
    return *this;
  }
  inline fastIO operator<<(const char* num) {
    for (int i = 0; num[i]; i++) putchar(num[i]);
    return *this;
  }
  inline fastIO operator<<(const string& num) {
    cout << num;
    return *this;
  }
} fio;
map<long long, pair<pair<long long, long long>, pair<long long, long long> > >
    mem;
long long k, a[107], p[107];
long long used[107];
long long V, rs, ra, rb, rc;
long long A, B, C, NN;
void dfs(long long rx, long long x, long long now, long long zl) {
  if (now == k || rx < a[now]) {
    if (zl) {
      B = x;
      C = NN / B;
      long long s13 = A * (B + C) + NN;
      if (s13 < rs) {
        rs = s13;
        ra = A;
        rb = B;
        rc = C;
      }
    } else {
      A = x;
      NN = V / A;
      long long BB = sqrt(NN);
      if (2 * A * BB + NN < rs) {
        for (int i = 0; i < k; i++) {
          p[i] -= used[i];
        }
        dfs(BB + 1, 1, 0, 1);
        for (int i = 0; i < k; i++) {
          p[i] += used[i];
        }
      }
    }
    return;
  }
  long long fo = 0, RX[64], X[64];
  while (rx >= 1 && fo <= p[now]) {
    RX[fo] = rx;
    X[fo] = x;
    rx /= a[now];
    x *= a[now];
    fo++;
  }
  for (int i = fo - 1; i >= 0; i--) {
    if (!zl) {
      used[now] = i;
    }
    dfs(RX[i], X[i], now + 1, zl);
  }
}
int main() {
  int T;
  fio >> T;
  while (T--) {
    fio >> k;
    V = 1;
    for (int i = 0; i < k; i++) {
      fio >> a[i] >> p[i];
      for (int j = 0; j < p[i]; j++) {
        V *= a[i];
      }
    }
    for (int i = 0; i < k; i++) {
      for (int j = i + 1; j < k; j++) {
        if (a[i] > a[j]) {
          swap(a[i], a[j]);
          swap(p[i], p[j]);
        }
      }
    }
    if (mem.count(V)) {
      pair<pair<long long, long long>, pair<long long, long long> > fuck =
          mem[V];
      fio << 2 * fuck.first.first << ' ' << fuck.first.second << ' '
          << fuck.second.first << ' ' << fuck.second.second << '\n';
    } else {
      rs = ((long long)0x3f3f3f3f3f3f3f3f);
      dfs(pow(V, 1.0 / 3) + 1, 1, 0, 0);
      mem[V] = make_pair(make_pair(rs, ra), make_pair(rb, rc));
      fio << 2 * rs << ' ' << ra << ' ' << rb << ' ' << rc << '\n';
    }
  }
  return 0;
}
