#include <bits/stdc++.h>
const double pi = acos(-1);
const int maxn = 200050;
const int maxm = 200050;
using namespace std;
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2>& p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
int n;
int x[maxn];
int pre[maxn];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int i;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) scanf("%d", &x[i]);
    x[0] = 0;
    for (i = 1; i <= n; i++) pre[i] = max(pre[i - 1], x[i]);
    i = n;
    long long ans = 0;
    while (i > 1) {
      if (x[i] >= pre[i - 1]) {
        i--;
        continue;
      }
      int tmp = pre[i - 1] - x[i];
      ans += (long long)tmp;
      while (i > 1 && tmp >= 0) {
        if (x[i] >= x[i - 1]) {
          i--;
        } else {
          int j = x[i - 1] - x[i];
          if (j > tmp) {
            x[i] += tmp;
            break;
          } else {
            tmp -= j;
            i--;
          }
        }
      }
    }
    printf("%lld\n", ans);
  }
}
