#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int i = 1, j, mn, val;
    long long k = 0;
    while (i < n) {
      mn = INT_MAX;
      if (arr[i] < arr[i - 1]) {
        j = i;
        val = arr[i - 1];
        while (j < n && arr[j] <= arr[j - 1]) {
          mn = min(mn, arr[j]);
          j++;
        }
      }
      if (mn < INT_MAX) {
        k += val - mn;
        i = j - 1;
      }
      i++;
    }
    cout << k << "\n";
  }
  return 0;
}
