#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int test_case;
int T;
cin >> T;
for (test_case = 1; test_case <= T; ++test_case) {
int N;
vector<int> v;
long long ans = 0;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
v.push_back(tmp);
}
int max_budget = v[N - 1]; //최고가
for (int j = N - 1; j >= 0; j--) {
if (max_budget > v[j]) { //현재가가 최고가보다 낮으면
ans += max_budget - v[j]; //팔아서 이득을 남김
}
else {
max_budget = v[j]; //현재가가 최고가
}
}
cout << "#" << test_case << " " << ans << endl;
}
return 0;
}