//StockExchange
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int testcase, i, j, n;
freopen("Testcase.txt", "r", stdin);
cin >> testcase;
for(int i = 1; i <= testcase; i++)
{
cin >> n;
int sum = 0;
int price[30];
for(j = 0; j < n; j++)
cin >> price[j];
for(j = 0; j < n; j++)
{
int sell = 0, buy = 0;
int k, max = 0, posMax;
for(k = j; k < n; k++)
if(max < price[k])
{
max = price[k];
posMax = k;
}
for(k = j; k < posMax; k++)
{
buy += price[k];
}
sell = (k-j) * price[posMax];
sum += (sell - buy);
j = k;
}
cout << "#" << i << " " << sum << endl;
}
getch();
return 0;
}