#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int> >& adjList, vector<bool>& explored, int src);
void bfs(vector<vector<int> >& adjList, vector<bool>& explored, int src);
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    long long int min = 0, prev = 0;
    long long int ans = 0;
    int ind = 0;
    for (int i = 1; i < n; i++) {
      if (a[i] < a[i - 1]) {
        ans += prev - min;
        prev = a[i - 1];
        min = a[i];
        ind = 1;
      } else {
        if (ind == 1) {
          if (min > a[i]) min = a[i];
        }
      }
    }
    ans += prev - min;
    cout << ans << endl;
  }
  return 0;
}
void dfs(vector<vector<int> >& adjList, vector<bool>& explored, int src) {
  explored.at(src - 1) = true;
  for (int i = 0, j = adjList.at(src - 1).size(); i < j; i++) {
    if (!explored.at(adjList.at(src - 1).at(i) - 1)) {
      dfs(adjList, explored, adjList.at(src - 1).at(i));
    }
  }
  return;
}
void bfs(vector<vector<int> >& adjList, vector<bool>& explored, int src) {
  explored.at(src - 1) = true;
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    for (int i = 0, j = adjList.at(temp - 1).size(); i < j; i++) {
      if (!explored.at(adjList.at(temp - 1).at(i) - 1)) {
        explored.at(adjList.at(temp - 1).at(i) - 1) = true;
        q.push(adjList.at(temp - 1).at(i));
      }
    }
  }
}
