#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  long long count = 0;
  for (int i = 0; i < n - 1; i++) {
    if (v[i] > v[i + 1]) {
      int j = i + 1;
      while (v[j + 1] <= v[j] && j < n - 1) {
        j++;
      }
      count += v[i] - v[j];
      i = j;
    }
  }
  cout << count << endl;
  return;
}
int main() {
  int t = 1;
  cin >> t;
  while (t) {
    solve();
    t--;
  }
}
