#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<int> h(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }

    int operations = 0;
    int target = h[n - 1]; // Start from the last pillar

    for (int i = n - 2; i >= 0; i--) {
        if (h[i] > target) {
            operations += (h[i] - target);
        } else {
            target = h[i];
        }
    }

    cout << operations << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}