#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int MxN = 200005;
const long long mod = 1e9 + 7;
void pe(int a) { cout << a << "\n"; }
void pe(long long a) { cout << a << "\n"; }
void pe(string a) { cout << a << "\n"; }
void ps(int a) { cout << a << ' '; }
void ps(string a) { cout << a << " "; }
void ps(long long a) { cout << a << " "; }
long long nCr(long long n, long long r) {
  long long res = 1;
  if (r > n) return 0;
  if (r > n - r) r = n - r;
  for (long long i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  vector<int> ar;
  for (int i = 1; i < n; ++i) {
    ar.push_back(a[i] - a[i - 1]);
  }
  long long ans = 0, res = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (ar[i] <= 0) {
      res += ar[i];
    } else {
      ans += abs(res);
      res = 0;
    }
  }
  ans += abs(res);
  pe(ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
