#include <bits/stdc++.h>
using namespace std;
long long n, i, t, j, l, y, k, r, a[301301], b[301000], k1, k2, ma, mi, sum, s1,
    in, jm, f, xx, cur, x, jj, ii, m, z, mia, mib, ans, sum1;
string s, s2;
double te;
char c[101][101];
long long power(long long x, unsigned long long y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
map<long long, long long> mr;
map<long long, long long> mc;
int main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  ;
  cin >> t;
  while (t--) {
    ans = 0;
    ma = 0;
    cin >> n;
    for (i = 0; i < n; i++) b[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    ma = a[0];
    for (i = 1; i < n; i++) {
      if (ma <= a[i])
        ma = a[i];
      else
        b[i] = ma - a[i];
    }
    ma = 0;
    for (i = n; i > 0; i--) {
      if (b[i]) {
        if (ma == 0)
          ma = b[i];
        else if (ma <= b[i])
          ma = max(ma, b[i]);
        else if (ma > b[i]) {
          ans += abs((b[i] - ma));
          ma = b[i];
        }
      } else
        ans += ma, ma = 0;
    }
    if (b[1]) ans += ma;
    for (i = 1; i < n; i++) b[i] = 0;
    cout << ans;
    cout << "\n";
  }
}
