#include <bits/stdc++.h>
using namespace std;
vector<long long int> arr(1000001);
void trozen() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> v1(n);
    for (auto &i : v1) cin >> i;
    long long int maxx = v1[0];
    long long int minn = v1[0];
    bool flag = false;
    long long int ans = 0;
    for (long long int i = 1; i < n; i++) {
      if (v1[i] < v1[i - 1]) {
        maxx = max(maxx, v1[i - 1]);
        minn = min(minn, v1[i]);
      } else {
        ans += abs(maxx - minn);
        maxx = v1[i];
        minn = v1[i];
      }
    }
    ans += abs(maxx - minn);
    cout << ans << "\n";
  }
}
int32_t main() {
  cin.tie(NULL);
  cout.tie(NULL);
  std::ios_base::sync_with_stdio(false);
  trozen();
  return 0;
}
