#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t, n, a[N], diff[N], L[N], R[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    long long ans = 0;
    for (int i = 0, cur = 0; i < n; i++) {
      cin >> a[i];
      cur = max(cur, a[i]);
      diff[i] = cur - a[i], L[i] = i - 1;
      while (~L[i] && diff[L[i]] > diff[i]) L[i] = L[L[i]];
    }
    for (int i = n - 1; ~i; i--) {
      R[i] = i + 1;
      while (R[i] < n && diff[R[i]] >= diff[i]) R[i] = R[R[i]];
      ans += diff[i] - max(~L[i] ? diff[L[i]] : 0, R[i] < n ? diff[R[i]] : 0);
    }
    cout << ans << endl;
  }
  return 0;
}
