#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<long long> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    stack<long long> stk;
    long long ans = 0, tm = 0;
    for (int i = n - 1; i >= 0; i--) {
      while (!stk.empty() && stk.top() < v[i]) {
        tm = max(tm, v[i] - stk.top());
        stk.pop();
      }
      stk.push(v[i]);
      if (tm > 0) {
        ans += tm;
        tm = 0;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
