#include <bits/stdc++.h>
using namespace std;
long long int n, t, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  while (t--) {
    cin >> n;
    long long int a[n + 1];
    long long int mx = 0, ans = 0, tans = 0;
    a[n] = INT_MAX;
    for (int i = 0; i <= n; i++) {
      if (i < n) cin >> a[i];
      if (a[i] >= mx && i > 0)
        ans += (mx - a[i - 1]);
      else if (i > 0 && a[i] > a[i - 1] && mx > a[i])
        ans += (a[i] - a[i - 1]);
      mx = max(mx, a[i]);
    }
    cout << ans << endl;
  }
}
