#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    vector<long long> pts;
    long long ans = 0;
    long long n;
    cin >> n;
    long long a[n + 2];
    a[0] = INT_MIN;
    a[n + 1] = INT_MAX;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
      if (a[i] <= a[i - 1] && a[i] <= a[i + 1])
        pts.push_back(a[i]);
      else if (a[i] >= a[i - 1] && a[i] >= a[i + 1])
        pts.push_back(a[i]);
    }
    for (int i = 1; i < pts.size(); i++) {
      if (pts[i] < pts[i - 1]) ans += pts[i - 1] - pts[i];
    }
    cout << ans << endl;
  }
  return 0;
}
