#include <iostream>
#include <vector>
using namespace std;
template <typename T>
static void Swap(T* data, int index1, int index2)
{
if (index1 == index2)
return;
T temp = *(data + index1);
*(data + index1) = *(data + index2);
*(data + index2) = temp;
}
template <typename T, typename GreaterThanFunc>
static int Partition(T* data, int left, int right, GreaterThanFunc greaterThanFunc)
{
T& ref = data[right];
int lastSmallerThanRefIndex = left - 1;
for (int i = left; i < right; ++i)
{
if (greaterThanFunc(ref, *(data + i)))
{
++lastSmallerThanRefIndex;
Swap(data, lastSmallerThanRefIndex, i);
}
}
++lastSmallerThanRefIndex;
Swap(data, lastSmallerThanRefIndex, right);
return lastSmallerThanRefIndex;
}
template <typename T, typename GreaterThanFunc>
static void QuickSort(T* data, int left, int right, GreaterThanFunc greaterThanFunc)
{
if (left >= right)
return;
if ((right - left) == 1)
{
if ((*(data + left)) > (*(data + right)))
Swap(data, left, right);
return;
}
int mid = Partition(data, left, right, greaterThanFunc);
QuickSort(data, left, mid - 1, greaterThanFunc);
QuickSort(data, mid + 1, right, greaterThanFunc);
}
template <typename T>
class Comparer
{
public:
bool operator()(const T& a, const T& b)
{
return (a > b);
}
};
struct CrossPair
{
public:
CrossPair(int x, int y) : m_X(x), m_Y(y) {}
CrossPair(int x) : m_X(x), m_Y(-1) {}
void Display()
{
cout << m_X;
if (m_Y > 0)
cout << ' ' << m_Y;
cout << endl;
}
private:
int m_X;
int m_Y;
};
static int CrossBridge(int* people, int cnt, vector<CrossPair>& crossPairs)
{
if (cnt == 0)
return 0;
if (cnt == 1)
{
crossPairs.push_back(CrossPair(people[0]));
return people[0];
}
if (cnt == 2)
{
crossPairs.push_back(CrossPair(people[0], people[1]));
return people[1];
}
if (cnt == 3)
{
crossPairs.push_back(CrossPair(people[0], people[2]));
crossPairs.push_back(CrossPair(people[0]));
crossPairs.push_back(CrossPair(people[0], people[1]));
return people[0] + people[1] + people[2];
}
if (cnt == 3)
{
crossPairs.push_back(CrossPair(people[0], people[2]));
crossPairs.push_back(CrossPair(people[0]));
crossPairs.push_back(CrossPair(people[0], people[1]));
return people[0] + people[1] + people[2];
}
if ((people[0] + people[cnt - 2]) > (2 * people[1]))
{
crossPairs.push_back(CrossPair(people[0], people[1]));
crossPairs.push_back(CrossPair(people[0]));
crossPairs.push_back(CrossPair(people[cnt - 2], people[cnt - 1]));
crossPairs.push_back(CrossPair(people[1]));
return people[1] * 2 + people[0] + people[cnt - 1] + CrossBridge(people, cnt - 2, crossPairs);
}
else
{
crossPairs.push_back(CrossPair(people[0], people[cnt - 1]));
crossPairs.push_back(CrossPair(people[0]));
crossPairs.push_back(CrossPair(people[0], people[cnt - 2]));
crossPairs.push_back(CrossPair(people[0]));
return people[0] * 2 + people[cnt - 2] + people[cnt - 1] + CrossBridge(people, cnt - 2, crossPairs);
}
}
static void RunTestCase()
{
int cnt;
cin >> cnt;
if (cnt <= 0)
{
cout << 0 << endl;
return;
}
int* people = new int[cnt];
for (int i = 0; i < cnt; ++i)
cin >> people[i];
QuickSort(people, 0, cnt - 1, Comparer<int>());
vector<CrossPair> crossPairs;
int time = CrossBridge(people, cnt, crossPairs);
cout << time << endl;
for (size_t i = 0; i < crossPairs.size(); ++i)
crossPairs[i].Display();
delete[] people;
}
static void Test()
{
int testCnt;
cin >> testCnt;
for (int i = 0; i < testCnt; ++i)
{
RunTestCase();
if (i < (testCnt - 1))
cout << endl;
}
}
int main(int argc, char* argv[])
{
Test();
return 0;
}