#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int cmod = 998244353;
const long long int N = 2e5 + 5;
const long long int inf = 1e16 + 2;
void solve(long long int Case) {
  long long int n;
  cin >> n;
  long long int a[n], su[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      su[i] = a[i];
    else
      su[i] = max(a[i], su[i + 1]);
  }
  long long int b[n], mx = 0, ans = 0;
  for (long long int i = 0; i < n; i++) {
    mx = max(mx, a[i]);
    b[i] = mx - a[i];
  }
  long long int f = 0, l = 0, g = 0, mn = 0;
  for (long long int i = 1; i < n; i++) {
    if (b[i] > b[i - 1]) {
      ans += b[i] - b[i - 1];
    }
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int testcase = 1;
  cin >> testcase;
  long long int Case = 1;
  while (testcase--) {
    solve(Case++);
  }
  return 0;
}
