#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    long long a[N];
    for (int i = 0; i < N; i++) cin >> a[i];
    long long ans = 0, v = a[0];
    for (int i = 1; i < N; i++) {
      if (a[i] >= v) {
        v = a[i];
        continue;
      }
      if (a[i] <= a[i - 1]) {
        ans += (a[i - 1] - a[i]);
      }
    }
    cout << ans << "\n";
  }
}
