#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const vector<int> mods = {998244353, 1000000007, 1000000009};
const long long inf = 3e18;
const double pi = acos(-1.0);
const double eps = 0.0000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class... K>
using umap = unordered_map<K...>;
template <class... K>
using uset = unordered_set<K...>;
long long t, n;
vector<long long> a;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.precision(12);
  cin >> t;
  for (long long _t = (0); _t < (t); ++_t) {
    cin >> n;
    a.assign(n, 0);
    for (long long i = (0); i < (n); ++i) cin >> a[i];
    long long res = 0, cur = 0;
    for (long long i = (1); i < (n); ++i) {
      if (a[i - 1] > a[i]) {
        if (a[i] + cur < a[i - 1]) {
          long long ad = a[i - 1] - a[i] - cur;
          cur += ad;
          res += ad;
        } else {
          cur = a[i - 1] - a[i];
        }
        a[i] = a[i - 1];
      } else {
        cur = 0;
      }
    }
    cout << res << "\n";
  }
  return 0;
}
