#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v(n);
    for (long long &ele : v) cin >> ele;
    long long reuse = 0;
    long long ans = 0;
    for (long long i = 1; i < n; i++) {
      long long diff = v[i - 1] - v[i];
      if (diff > reuse) {
        long long cur = diff - reuse;
        ans += cur;
        reuse += cur;
      }
      reuse = max(0ll, diff);
      v[i] = max(v[i], v[i - 1]);
    }
    cout << ans << "\n";
  }
}
