#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
		if(pk.at(i-1)>pk.at(i))
		{
		ll x=*min_element(pk.begin()+i, pk.end());

		ll q=min_element(pk.begin()+i,pk.end()) - pk.begin();
		ans+=pk.at(i-1)-x;
		// cout<<x<<" "<<q<<" "<<ans<<" \n";
		pk.at(q)=pk.at(i-1);

		}
	}
	// for(auto x:pk)
	// {
	// 	cout<<x<<" ";
	// }
	// cout<<"\n";
	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░


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

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i;
        cin>>n;
        ll ar[n+1];
        for(i=1;i<=n;i++)
            cin>>ar[i];
        ll ans=0;
        for(i=1;i<n;i++)
        {
            if(ar[i]>ar[i+1])
                ans+=ar[i]-ar[i+1];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(pk.at(i-1)>pk.at(i))
		{
			ans+=pk.at(i-1)-pk.at(i);
			ll x=pk.at(i-1)-pk.at(i);
			ff(j,i,pk.size())
			{
				pk.at(j)+=x;
			}
		}
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
	}
	// for(auto x:pk)
	// {
	// 	cout<<x<<" ";
	// }
	// cout<<"\n";
	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
		if(pk.at(i-1)>pk.at(i))
		{
		ll x=*min_element(pk.begin()+i, pk.end());
		ans+=pk.at(i-1)-x;
		break;
		}
	}
	// for(auto x:pk)
	// {
	// 	cout<<x<<" ";
	// }
	// cout<<"\n";
	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        for(int &i:data) cin>>i;
        int prev_maxi  = 0;

        for(int i= 1;i<n;){
            while(data[i]>=data[i-1]){
                ++i;
            }
            if(i>=n) break;
            prev_maxi = max(prev_maxi,data[i-1]);
            int prev = prev_maxi;
            int maxi = 0;
            while(i<n&&data[i]<=data[i-1]){
                maxi = max(prev-data[i],maxi);
                ++i;
            }
//            if(i>=n){
//                ans+=maxi;
//                break;
//            }
//            while(i<n&&data[i]<prev&&data[i]>=data[i-1]){
//                maxi = max(prev-data[i],maxi);
//                ++i;
//            }
            ans+=maxi;
            if(i>=n) break;
        }
        cout<<ans<<"\n";
    }
}


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        for(int &i:data) cin>>i;
        int prev_maxi  = 0;

        for(int i= 1;i<n;){
            while(data[i]>=data[i-1]){
                ++i;
            }
            if(i>=n) break;
            prev_maxi = max(prev_maxi,data[i-1]);
            int prev = prev_maxi;
            int maxi = 0;
            while(i<n&&data[i]<=data[i-1]){
                maxi = max(prev-data[i],maxi);
                ++i;
            }
            if(i>=n){
                ans+=maxi;
                break;
            }
            while(i<n&&data[i]<prev&&data[i]>=data[i-1]){
                maxi = max(prev-data[i],maxi);
                ++i;
            }
            ans+=maxi;
            if(i>=n) break;
        }
        cout<<ans<<"\n";
    }
}


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        for(int &i:data) {
            cin >> i;
        }
        int prev=  0;
        for(int i = 0;i<n;++i){
            if(data[i]-prev<0){
                ans+=abs(data[i]-prev);
            }
            prev = data[i];
        }
        cout<<ans<<"\n";
    }
}


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        for(int &i:data) cin>>i;
        int prev_maxi  = 0;

        for(int i= 1;i<n;){
            while(data[i]>=data[i-1]){
                ++i;
            }
            if(i>=n) break;
            prev_maxi = max(prev_maxi,data[i-1]);
            int prev = prev_maxi;
            int maxi = 0;
            while(i<n&&data[i]<=prev_maxi){
                maxi = max(prev-data[i],maxi);
                ++i;
                prev_maxi = max(prev_maxi,data[i]);
            }
//            if(i>=n){
//                ans+=maxi;
//                break;
//            }
//            while(i<n&&data[i]<prev&&data[i]>=data[i-1]){
//                maxi = max(prev-data[i],maxi);
//                ++i;
//            }
            ans+=maxi;
            if(i>=n) break;
        }
        cout<<ans<<"\n";
    }
}


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        int maxi = 0;
        for(int &i:data) {
            cin >> i;
//            maxi =max(maxi,i);
        }
        vector<int> calc;
        for(int i:data){
            maxi = max(maxi,i);
            calc.push_back(maxi-i);
        }
        for(int i = 1;i<n;){
            int flag= 0;
            while(i<n&&calc[i]>calc[i-1]) {
                ++i;
                flag =1;
            }
            if(flag)
            ans+=calc[i-1];
            if(!flag) ++i;
        }
        cout<<ans<<"\n";
    }
}


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        int maxi = 0;
        for(int &i:data) {
            cin >> i;
//            maxi =max(maxi,i);
        }
        vector<int> calc;
        for(int i:data){
            maxi = max(maxi,i);
            calc.push_back(maxi-i);
        }
        int prev = calc[0];
        for(int i = 1;i<n;){
            int flag= 0;
            while(i<n&&calc[i]>prev) {
                prev=  max(prev,calc[i]);
                ++i;
                flag =1;
            }
            if(flag) {
                ans += calc[i - 1];
                prev= calc[i-1];
            }
            if(!flag) ++i;
        }
        cout<<ans<<"\n";
    }
}


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

int32_t main() {
    int t;
    cin>>t;
    while(t--){

        int n;
        cin>>n;
        vector<int> data(n);
        int ans = 0;
        int maxi = 0;
        for(int &i:data) {
            cin >> i;
            maxi =max(maxi,i);
        }
        vector<int> calc;
        for(int i:data){
            calc.push_back(maxi-i);
        }
        for(int i = 1;i<n;){
            int flag= 0;
            while(i<n&&calc[i]>calc[i-1]) {
                ++i;
                flag =1;
            }
            if(flag)
            ans+=calc[i-1];
            if(!flag) ++i;
        }
        cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<ll> arr(n);
		for (auto &i : arr)cin >> i;
		ll mx = 0, curr = 0, ans = 0, i = 0;
		while (i < n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] <= mx) {
				curr = max(curr, mx - arr[j]);
				j++;
			}
			i = j;
			ans += curr;
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<ll> arr(n), ans;
		for (auto &i : arr)cin >> i;
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i < n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] <= mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		while (i < n) {
			ll j = i + 1;
			while (j < n && ans[j] > ans[j - 1])j++;
			res += ans[j - 1];
			i = j;
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<ll> arr(n), ans;
		for (auto &i : arr)cin >> i;
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i < n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] <= mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		// for (auto i : ans) cout << i << " ";
		// cout << "\n";
		while (i < n) {
			ll j = i + 1;
			curr = 0;
			while (j < n && ans[j] != 0)j++, curr = max(curr, ans[j]);
			res += curr;
			i = j;
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		ll n, x;
		cin >> n;
		vector<ll> arr, ans;
		for (int i = 0; i < n; i++) {
			cin >> x;
			arr.pb(x);
		}
		arr.pb(1e18);
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i <= n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] <= mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		// for (auto i : ans) cout << i << " ";
		// cout << "\n";
		while (i < n) {
			ll j = i;
			curr = 0;
			while (j < n && ans[j] == 0)j++;
			while (j < n && ans[j] != 0)curr = max(curr, ans[j++]);
			res += curr;
			i = j;
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		ll n, x;
		cin >> n;
		vector<ll> arr, ans;
		for (int i = 0; i < n; i++) {
			cin >> x;
			arr.pb(x);
		}
		ll px = 1e18;
		arr.pb(px);
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i <= n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] < mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		for (i = 0; i <= n; i++) {
			if (ans[i + 1] - ans[i] > 0) res += ans[i + 1] - ans[i];
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<ll> arr(n);
		for (auto &i : arr)cin >> i;
		ll mx = arr[0], curr = 0, ans = 0, i = 1;
		while (i < n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				continue;
			}
			int j = i;
			curr = 0;
			while (j < n && arr[j] < mx) {
				curr = max(curr, mx - arr[j]);
				j++;
			}
			i = j;
			ans += curr;
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		ll n, x;
		cin >> n;
		vector<ll> arr, ans;
		for (int i = 0; i < n; i++) {
			cin >> x;
			arr.pb(x);
		}
		arr.pb(1e18);
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i <= n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] < mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		for (int i = 0; i <= n; i++) {
			if (ans[i + 1] - ans[i] > 0) res += ans[i + 1] - ans[i];
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<ll> arr(n), ans;
		for (auto &i : arr)cin >> i;
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i < n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] <= mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		// for (auto i : ans) cout << i << " ";
		// cout << "\n";
		while (i < n) {
			ll j = i;
			curr = 0;
			while (j < n && ans[j] == 0)j++;
			while (j < n && ans[j] != 0)curr = max(curr, ans[j++]);
			res += curr;
			i = j;
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<ll> arr(n), ans;
		for (auto &i : arr)cin >> i;
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i < n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] <= mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		// for (auto i : ans) cout << i << " ";
		// cout << "\n";
		while (i < n) {
			ll j = i + 1;
			while (j < n && ans[j] >= ans[j - 1])j++;
			res += ans[j - 1];
			i = j;
		}
		cout << res << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b) {ll res = 1; a = a % mod; while (b) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b /= 2;} return res;}
ll invmod(ll a) {return power(a, mod - 2);}
int main(void)
{
	FIO
	int t;
	cin >> t;
	while (t--)
	{
		ll n, x;
		cin >> n;
		vector<ll> arr, ans;
		for (int i = 0; i < n; i++) {
			cin >> x;
			arr.pb(x);
		}
		ll px = 1e18;
		arr.pb(px);
		ll mx = 0, curr = 0, res = 0, i = 0;
		while (i <= n) {
			if (arr[i] >= mx) {
				mx = arr[i++];
				ans.pb(0);
				continue;
			}
			ll j = i;
			curr = 0;
			while (j < n && arr[j] < mx) {
				ans.pb(mx - arr[j]);
				j++;
			}
			i = j;
		}
		i = 0;
		for (i = 0; i < n; i++) {
			if (ans[i + 1] - ans[i] > 0) res += ans[i + 1] - ans[i];
		}
		cout << res << "\n";
	}
	return 0;
}


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

long steps(vector<long> &v,long s,long e,long &sum)
{   long pos=e,mn,mx=0;

    for(long i=0;i<=e;i++)
        if(v[i]>=mx)
         { mx=v[i];
           pos=i;

         }
    if(pos==e && pos-1>=0 && e>=0)
            steps(v,0,pos-1,sum);

    else
    {
      for(long i=e;i>=s;i--)
       {
        if(v[e]<v[i])
        {
            pos=i;
            break;
        }
       }
      mn=v[pos];
      for(long i=pos;i<=e;i++)
        if(v[i]<mn)
            mn=v[i];

        sum+=v[pos]-mn;
        if(pos-1>=0 && e>=0)
        steps(v,0,pos,sum);
    }
      return sum;

}

int main()
{  vector<vector<long> >v;
   long n,r,sum;
   cin>>r;
   long temp;
   for(long j=0;j<r;j++)
   {
      v.push_back(vector<long>());
      cin>>n;
      for(long i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(long i=0;i<r;i++)
   {   sum=0;
       cout<<steps(v[i],0,v[i].size()-1,sum)<<"\n";
   }

}


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

int steps(vector<int> &v,int s,int e,int sum)
{  int mx=0,pos,mn;
   for(int i=0;i<=e;i++)
    if(v[i]>mx)
    {  mx=v[i];
       pos=i;
    }

    if(pos==e && pos-1>=s)
        steps(v,0,pos-1,sum);
    else
    {   mn=mx;
        for(int i=pos+1;i<=e;i++)
           { if(mn>v[i])
                mn=v[i];

           }
        sum+=mx-mn;

       if(pos-1>=s)
        steps(v,0,pos-1,sum);

    }
    if(pos==0)
        return sum;

}

int main()
{  vector<vector<int> >v;
   int n,r;
   cin>>r;
   int temp;
   for(int j=0;j<r;j++)
   {
      v.push_back(vector<int>());
      cin>>n;
      for(int i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(int i=0;i<r;i++)
   {
       cout<<steps(v[i],0,v[i].size()-1,0)<<"\n";
   }

}


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

long steps(vector<long> &v,long s,long e,long sum)
{  long mx=0,pos,mn;
   for(long i=0;i<=e;i++)
    if(v[i]>mx)
    {  mx=v[i];
       pos=i;
    }

    if(pos==e && pos-1>=s)
        steps(v,0,pos-1,sum);
    else
    {   mn=mx;
        for(long i=pos+1;i<=e;i++)
           { if(mn>v[i])
                mn=v[i];

           }
        sum+=mx-mn;
        cout<<mn<<" "<<sum<<" "<<pos<<" "<<v[pos]<<"\n";

       if(pos-1>=s)
        steps(v,0,pos-1,sum);

    }
    if(pos==0)
        return sum;

}

int main()
{  vector<vector<long> >v;
   long n,r;
   cin>>r;
   long temp;
   for(long j=0;j<r;j++)
   {
      v.push_back(vector<long>());
      cin>>n;
      for(long i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(long i=0;i<r;i++)
   {
       cout<<steps(v[i],0,v[i].size()-1,0)<<"\n";
   }

}


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

int64_t steps(vector<int64_t> &v,int64_t s,int64_t e,int64_t &sum)
{   int64_t pos=e,mn,mx=0;

    for(int64_t i=0;i<=e;i++)
        if(v[i]>=mx)
         { mx=v[i];
           pos=i;

         }
    if(pos==e && pos-1>=0 && e>=0)
            steps(v,0,pos-1,sum);

    else
    {
      for(int64_t i=e-1;i>=s;i--)
       {
        if(v[e]<=v[i])
        {
            pos=i;
            break;
        }
       }
      mn=v[pos];
      for(int64_t i=pos;i<=e;i++)
        if(v[i]<mn)
            mn=v[i];

        sum+=v[pos]-mn;
        if(pos-1>=0 && e>=0)
        steps(v,0,pos,sum);
    }
      return sum;

}

int main()
{  vector<vector<int64_t> >v;
   int64_t n,r,sum;
   cin>>r;
   int64_t temp;
   for(int64_t j=0;j<r;j++)
   {
      v.push_back(vector<int64_t>());
      cin>>n;
      for(int64_t i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(int64_t i=0;i<r;i++)
   {   sum=0;
       cout<<steps(v[i],0,v[i].size()-1,sum)<<"\n";
   }

}


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

long steps(vector<long> &v,long s,long e,long sum)
{  long mx=0,pos,mn;
   for(long i=0;i<=e;i++)
    if(v[i]>mx)
    {  mx=v[i];
       pos=i;
    }

    if(pos==e && pos-1>=s)
        steps(v,0,pos-1,sum);
    else
    {   mn=mx;
        for(long i=pos+1;i<=e;i++)
           { if(mn>v[i])
                mn=v[i];

           }
        sum+=mx-mn;

       if(pos-1>=s)
        steps(v,0,pos-1,sum);

    }
    if(pos==0)
        return sum;

}

int main()
{  vector<vector<long> >v;
   long n,r;
   cin>>r;
   long temp;
   for(long j=0;j<r;j++)
   {
      v.push_back(vector<int>());
      cin>>n;
      for(long i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(long i=0;i<r;i++)
   {
       cout<<steps(v[i],0,v[i].size()-1,0)<<"\n";
   }

}


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

long steps(vector<long> &v,long s,long e,long sum)
{  long mx=0,pos,mn;
   for(long i=0;i<=e;i++)
    if(v[i]>mx)
    {  mx=v[i];
       pos=i;
    }

    if(pos==e && pos-1>=s)
        steps(v,0,pos-1,sum);
    else
    {   mn=mx;
        for(long i=pos+1;i<=e;i++)
           { if(mn>v[i])
                mn=v[i];

           }
        sum+=mx-mn;

       if(pos-1>=s)
        steps(v,0,pos-1,sum);

    }
    if(pos==0)
        return sum;

}

int main()
{  vector<vector<long> >v;
   long n,r;
   cin>>r;
   long temp;
   for(long j=0;j<r;j++)
   {
      v.push_back(vector<long>());
      cin>>n;
      for(long i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(long i=0;i<r;i++)
   {
       cout<<steps(v[i],0,v[i].size()-1,0)<<"\n";
   }

}


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

int64_t steps(vector<int64_t> &v,int64_t s,int64_t e,int64_t &sum)
{   int64_t pos=e,mn,mx=0;

    for(int64_t i=0;i<=e;i++)
        if(v[i]>=mx)
         { mx=v[i];
           pos=i;

         }
    if(pos==e && pos-1>=0 && e>=0)
            steps(v,0,pos-1,sum);

    else
    {
      for(int64_t i=e;i>=s;i--)
       {
        if(v[e]<v[i])
        {
            pos=i;
            break;
        }
       }
      mn=v[pos];
      for(int64_t i=pos;i<=e;i++)
        if(v[i]<mn)
            mn=v[i];

        sum+=v[pos]-mn;
        if(pos-1>=0 && e>=0)
        steps(v,0,pos,sum);
    }
      return sum;

}

int main()
{  vector<vector<int64_t> >v;
   int64_t n,r,sum;
   cin>>r;
   int64_t temp;
   for(int64_t j=0;j<r;j++)
   {
      v.push_back(vector<int64_t>());
      cin>>n;
      for(int64_t i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(int64_t i=0;i<r;i++)
   {   sum=0;
       cout<<steps(v[i],0,v[i].size()-1,sum)<<"\n";
   }

}


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

long steps(vector<long> &v,long s,long e,long sum)
{  long mx=0,pos,mn;
   for(long i=0;i<=e;i++)
    if(v[i]>mx)
    {  mx=v[i];
       pos=i;

    }

    if(pos==e && pos-1>=s)
        steps(v,0,pos-1,sum);
    else
    {   mn=mx;
        for(long i=pos+1;i<=e;i++)
           { if(mn>v[i])
                mn=v[i];

           }
        sum+=mx-mn;

       if(pos-1>=s)
        steps(v,0,pos-1,sum);

    }
    if(pos==0)
        return sum;

}

int main()
{  vector<vector<long> >v;
   long n,r;
   cin>>r;
   long temp;
   for(long j=0;j<r;j++)
   {
      v.push_back(vector<long>());
      cin>>n;
      for(long i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(long i=0;i<r;i++)
   {
       cout<<steps(v[i],0,v[i].size()-1,0)<<"\n";
   }

}


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

long steps(vector<int> &v,int s,int e,long sum)
{  int mx=0,pos,mn;
   for(int i=0;i<=e;i++)
    if(v[i]>mx)
    {  mx=v[i];
       pos=i;
    }

    if(pos==e && pos-1>=s)
        steps(v,0,pos-1,sum);
    else
    {   mn=mx;
        for(int i=pos+1;i<=e;i++)
           { if(mn>v[i])
                mn=v[i];

           }
        sum+=mx-mn;

       if(pos-1>=s)
        steps(v,0,pos-1,sum);

    }
    if(pos==0)
        return sum;

}

int main()
{  vector<vector<int> >v;
   int n,r;
   cin>>r;
   int temp;
   for(int j=0;j<r;j++)
   {
      v.push_back(vector<int>());
      cin>>n;
      for(int i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(int i=0;i<r;i++)
   {
       cout<<steps(v[i],0,v[i].size()-1,0)<<"\n";
   }

}


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

int64_t steps(vector<int64_t> &v,int64_t s,int64_t e,int64_t &sum)
{
    int64_t i,j,mx=0,mn=v[0];
    for(i=e;i>0;i--)
    {
        if(v[i-1]>v[i])
            sum+=v[i-1]-v[i];
    }

    return sum;

}

int main()
{  vector<vector<int64_t> >v;
   int64_t n,r,sum;
   cin>>r;
   int64_t temp;
   for(int64_t j=0;j<r;j++)
   {
      v.push_back(vector<int64_t>());
      cin>>n;
      for(int64_t i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(int64_t i=0;i<r;i++)
   {   sum=0;
       cout<<steps(v[i],0,v[i].size()-1,sum)<<"\n";
   }

}


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

double long steps(vector<double long> &v,double long s,double long e,double long &sum)
{   double long pos=e,mn,mx=0;

    for(double long i=0;i<=e;i++)
        if(v[i]>=mx)
         { mx=v[i];
           pos=i;

         }
    if(pos==e && pos-1>=0 && e>=0)
            steps(v,0,pos-1,sum);

    else
    {
      for(double long i=e;i>=s;i--)
       {
        if(v[e]<v[i])
        {
            pos=i;
            break;
        }
       }
      mn=v[pos];
      for(double long i=pos;i<=e;i++)
        if(v[i]<mn)
            mn=v[i];

        sum+=v[pos]-mn;
        if(pos-1>=0 && e>=0)
        steps(v,0,pos,sum);
    }
      return sum;

}

int main()
{  vector<vector<double long> >v;
   double long n,r,sum;
   cin>>r;
   double long temp;
   for(double long j=0;j<r;j++)
   {
      v.push_back(vector<double long>());
      cin>>n;
      for(double long i=0;i<n;i++)
      {
       cin>>temp;
       v[j].push_back(temp);
      }
   }

   for(double long i=0;i<r;i++)
   {   sum=0;
       cout<<steps(v[i],0,v[i].size()-1,sum)<<"\n";
   }

}


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

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				sort(less.begin(),less.end(),greater<int>());
				int n1 = less.size();
				for(int j=0;j<n1;j++){
					//cout<<"aca "<<less[j]<<endl;
					cambios += (ant-less[j]);
					ant = less[j];
				}
				less.clear();
				ant = a[i];
			}
		}

		sort(less.begin(),less.end(),greater<int>());
		int n1 = less.size();
		for(int j=0;j<n1;j++){
			//cout<<j<<" "<<less[j]<<endl;
			//cout<<"cambios "<<cambios<<endl;
			cambios += (ant-less[j]);
			ant = less[j];
		}
		less.clear();
		cout<<cambios<<endl;
	}
	return 0;
}


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

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				sort(less.begin(),less.end(),greater<int>());
				int n1 = less.size();
				for(int j=0;j<n1;j++){
					//cout<<"aca "<<less[j]<<endl;
					cambios += (ant-less[j]);
					ant = less[j];
				}
				less.clear();
				ant = a[i];
			}
		}
		sort(less.begin(),less.end());
				int n1 = less.size();
				for(int j=0;j<n1;j++){
					cambios += (ant-less[j]);
					ant = less[j];
				}
				less.clear();
		cout<<cambios<<endl;
	}
	return 0;
}


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

long long f(vector<int> &less, int obj){
	long long res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<cambios<<endl;
	}
	return 0;
}


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

long long f(vector<int> &less, int obj){
	int res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<"res "<<cambios<<endl;
	}
	return 0;
}


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

long long f(vector<int> &less, int obj){
	int res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<cambios<<endl;
	}
	return 0;
}


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

long long f(vector<long long> &less, int obj){
	long long res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return 	res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		long long a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		long long ant = a[0];
		long long cambios = 0;
		vector<long long> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<cambios<<endl;
	}
	return 0;
}


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

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		long long k;
		cin>>n>>k;
		int a[n];
		int maximo = - (1<<31)+1;
		for(int i =0;i<n;i++){
			cin>>a[i];
			maximo = max(maximo,a[i]);
		}
		int b[n];
		k--;
		int nuevoMax = - (1<<31)+1;
		for(int i =0;i<n;i++){
			b[i] = maximo - a[i];
			nuevoMax = max(nuevoMax,b[i]);
		}
		if(k % 2 == 1){
				for(int i =0;i<n;i++){
					b[i] =  - b[i];
				}
		}
		for(int i =0;i<n;i++){
			cout<<b[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}


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

long long f(vector<int> &less, int obj){
	long long res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return 	res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<cambios<<endl;
	}
	return 0;
}


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

long long f(vector<int> &less, int obj){
	int res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		int ant = a[0];
		long long cambios = 0;
		vector<int> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<<<cambios<<endl;
	}
	return 0;
}


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

long long f(vector<long long> &less, int obj){
	long long res = 0;
	sort(less.begin(),less.end(),greater<int>());
	int n = less.size();
	for(int i =0;i<n;i++){
		res += (obj-less[i]);
		obj = less[i];
	}
	return 	res;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		long long ant = a[0];
		long long cambios = 0;
		vector<long long> less;
		for(int i =1;i<n;i++){

			if(a[i] < ant){
				less.push_back(a[i]);
			}
			else{
				cambios += f(less,ant);
				less.clear();
				ant = a[i];
			}
		}
		cambios += f(less,ant);
		cout<<cambios<<endl;
	}
	return 0;
}


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

long long max(long long a,long long b){
	if(a > b) return a;
	return b;
}

int main(){

	int q;
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		long long a[n];
		for(int i =0;i<n;i++) cin>>a[i];

		long long res = 0;
		for(int i =0;i<n-1;i++){
			res += max(0,a[i]-a[i+1]);
		}

		cout<<res<<endl;
	}

	return 0;
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        int ans=0;
        for(int i=n-2;i>=0;i--){
            ans=ans+max(a[i]-a[i+1],0);
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        int count=0;
        for(int i=0;i<n-1;i++){
            if(a[i+1]<a[i]){
                int j=i+1;
                int maxi=-1;
                int mini=1e9+5;
                while(a[j]<a[i] && j<n){
                    maxi=max(maxi,a[j]);
                    mini=min(mini,a[j]);
                    j++;
                }
                count=count+(maxi-mini)+(a[i]-maxi);
                i=j-1;
            }
        }
        cout<<count<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        int count=0;
        for(int i=0;i<n-1;i++){
            if(a[i+1]<a[i]){
                int j=i;
                int maxi=-1;
                int mini=1e9+5;
                while(a[j]>=a[j+1] && j+1<n){

                    count+=(a[j]-a[j+1]);
                    j++;
                }
                //maxi=a[i];
                //mini=a[j-1];
                //count=count+(maxi-mini)+(a[i]-maxi);
                i=j-1;
            }
        }
        cout<<count<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        long long int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        long long int ans=0;
        for(int i=n-2;i>=0;i--){
            ans=ans+max(a[i]-a[i+1],0);
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        int count=0;
        for(int i=0;i<n-1;i++){
            if(a[i+1]<a[i]){
                int j=i;
                int maxi=-1;
                int mini=1e9+5;
                while(a[j]>a[j+1] && j+1<n){

                    count+=(a[j]-a[j+1]);
                    j++;
                }
                //maxi=a[i];
                //mini=a[j-1];
                //count=count+(maxi-mini)+(a[i]-maxi);
                i=j-1;
            }
        }
        cout<<count<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        long long int ans=0;
        for(int i=n-2;i>=0;i--){
            ans=ans+max(a[i]-a[i+1],0);
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
          cin>>a[i];
        int count=0;
        for(int i=0;i<n-1;i++){
            if(a[i+1]<a[i]){
                int j=i;
                int maxi=-1;
                int mini=1e9+5;
                while(a[j]>=a[j+1] && j+1<n){

                    count+=(a[j]-a[j+1]);

                    j++;
                }
                if(j+1<n && a[j+1]<=a[i])
                count=count+(a[i]-a[j+1]);
                //maxi=a[i];
                //mini=a[j-1];
                //count=count+(maxi-mini)+(a[i]-maxi);
                i=j-1;
            }
        }
       // for(int i=0;i<n;i++)
      //    cout<<a[i]<<" ";
      //  cout<<endl;
        cout<<count<<endl;
    }
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<ll> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	int dp[n];
  	memset(dp,0,sizeof(dp));
  	ll mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		dp[i]=mx;
  		mx=max(mx,v[i]);
  	}
  	ll start_idx=1,val=0;
  	for(int i=0;i<n;)	{
  		ll temp=dp[i],max_req=0;
  		int j=i;
  		while(dp[j]==temp&&j<n)	{
  			max_req=max(max_req,dp[j]-v[j]);
  			j++;
  		}
  		ans+=max_req;
  		i=j;
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<ll> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	ll dp[n];
  	memset(dp,0,sizeof(dp));
  	ll mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		dp[i]=mx;
  		mx=max(mx,v[i]);
  	}
  	ll start_idx=1,val=0;
  	for(int i=0;i<n;)	{
  		ll temp=dp[i],max_req=0;
  		int j=i;
  		while(j<n&&dp[j]==temp)	{
  			max_req=max(max_req,dp[j]-v[j]);
  			j++;
  		}
  		ans+=max_req;
  		i=j;
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/

int main()
// {
//   freopen("input.txt","r",stdin);
//   freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<int> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	int mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		if(v[i]<mx)	{
  			ans=max(ans,mx-v[i]);
  		}
  		else	{
  			mx=v[i];
  		}
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	long long ans = 0;
	for(int i=1;i<n;i++){
		if(a[i]<a[i-1]) ans+= a[i-1]-a[i];
	}
	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<ll> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	ll dp[n];
  	memset(dp,0,sizeof(dp));
  	ll mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		dp[i]=mx;
  		mx=max(mx,v[i]);
  	}
  	ll start_idx=1,val=0;
  	for(int i=0;i<n;)	{
  		ll temp=dp[i],max_req=0;
  		int j=i;
  		while(dp[j]==temp&&j<n)	{
  			max_req=max(max_req,dp[j]-v[j]);
  			j++;
  		}
  		ans+=max_req;
  		i=j;
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<int> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	int dp[n];
  	memset(dp,0,sizeof(dp));
  	int mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		dp[i]=mx;
  		mx=max(mx,v[i]);
  	}
  	int start_idx=1,val=0;
  	for(int i=0;i<n;)	{
  		int temp=dp[i],max_req=0;
  		int j=i;
  		while(dp[j]==temp&&j<n)	{
  			max_req=max(max_req,dp[j]-v[j]);
  			j++;
  		}
  		ans+=max_req;
  		i=j;
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<int> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	int dp[n];
  	memset(dp,0,sizeof(dp));
  	long long int mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		dp[i]=mx;
  		mx=max(mx,v[i]);
  	}
  	int start_idx=1,val=0;
  	for(int i=0;i<n;)	{
  		int temp=dp[i],max_req=0;
  		int j=i;
  		while(dp[j]==temp&&j<n)	{
  			max_req=max(max_req,dp[j]-v[j]);
  			j++;
  		}
  		ans+=max_req;
  		i=j;
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/
int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	ll v[n];
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	ll dp[n];
  	memset(dp,0,sizeof(dp));
  	ll mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		dp[i]=mx;
  		mx=max(mx,v[i]);
  	}
  	int i=0;
  	while(i<n)	{
  		ll temp=dp[i],max_req=0;
  		while(i<n&&dp[i]==temp)	{
  			max_req=max(max_req,dp[i]-v[i]);
  			i++;
  		}
  		ans+=max_req;
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


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

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fi first
#define se second
#define merge(a,b) a##b //concat a and b
#define mkstr(a) #a  //convert a to string a. a is not variable
#define flush fflush(stdout);
#define debug(x) cout<<#x<<endl;
#define debug1(x) cout<<x<<endl;
#define inf 1e18
#define endl "\n"
#define epsilon 1e-9

typedef long long int ll;
typedef long double   ld;
typedef unsigned long long int ull;

/************************************TEMPLATE**********************************/

ll modpower(ll x,ll y)
{
  ll res=1;
  x=x%mod;
  if(x==0) return 0;
  while(y>0)
  {
    if(y&1)
      res=(res*x)%mod;
    y=y>>1;
    x=(x*x)%mod;
  }
  return res;
}

/*************************************CODE*************************************/

int main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fastIO
  ll test_case=1;
  cin>>test_case;
  while(test_case--)  {
  	int n;
  	cin>>n;
  	vector<int> v(n);
  	for(int i=0;i<n;i++)	{
  		cin>>v[i];
  	}
  	int mx=0,ans=0;
  	for(int i=0;i<n;i++)	{
  		if(v[i]<mx)	{
  			ans=max(ans,mx-v[i]);
  		}
  		else	{
  			mx=v[i];
  		}
  	}
  	cout<<ans<<endl;
  }
  return 0;
}


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll need[n]={0};
				ll mx=arr[0];
				for(ll i=1;i<n;i++)
				{
					if(arr[i]<mx)
					{
						need[i]=abs(arr[i]-mx);

					}
					mx=max(mx,arr[i]);
					//  cout<<need[i]<<" ";
				}
				// cout<<endl;
				// for(ll i=0;i<n;i++)
				// cin>>need[i];

				ll ans=0;
				ll cs=0;
				ll mn=inf;
				ll len=0;
				for(ll i=1;i<n;i++)
				{
					if(need[i]>=need[i-1])
					cs=need[i];
					else
					{
						ans+=cs;
						cs=need[i];
					}
					if(i==n-1 and need[i]>=need[i-1])
					ans+=need[i];
				}
				cout<<ans<<endl;

			}

			return 0;
		 }


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll need[n]={0};

				for(ll i=1;i<n;i++)
				{
					if(arr[i]<arr[i-1])
					{
						need[i]=abs(arr[i]-(arr[i-1]+need[i-1]));
					}
					// cout<<need[i]<<" ";
				}
				// cout<<endl;

				ll ans=0;
				ll cs=0;
				ll mn=inf;
				ll len=0;
				for(ll i=1;i<n;i++)
				{
					if(need[i]>0)
					{
						cs+=need[i];
						mn=min(mn,need[i]);
						len+=1;
					}
					else if(need[i]==0 and cs>0)
					{
						cs+=mn;
						cs-=(len*mn);
						ans+=cs;
						cs=0;
						len=0;
						mn=inf;
					}
					if(cs>0 and i==n-1)
					{
						cs+=mn;
						cs-=(len*mn);
						ans+=cs;
					}
				}

				cout<<ans<<endl;

			}

			return 0;
		 }


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll need[n]={0};
				ll mx=arr[0];
				for(ll i=1;i<n;i++)
				{
					if(arr[i]<mx)
					{
						need[i]=abs(arr[i]-mx);

					}
					mx=max(mx,arr[i]);
					//  cout<<need[i]<<" ";
				}
				// cout<<endl;

				ll ans=0;
				ll cs=0;
				ll res=0;
				ll len=0;
				for(ll i=1;i<n;i++)
				{
						if(need[i]<=res)
						{
							res=need[i];
						}
						else
						{
							ll diff=need[i]-res;
							cs+=diff;
							res=diff;
						}
				}
				ans+=cs;

				cout<<ans<<endl;

			}

			return 0;
		 }


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll need[n]={0};
				ll mx=arr[0];
				for(ll i=1;i<n;i++)
				{
					if(arr[i]<mx)
					{
						need[i]=abs(arr[i]-mx);

					}
					mx=max(mx,arr[i]);
					//  cout<<need[i]<<" ";
				}
				// cout<<endl;

				ll ans=0;
				ll cs=0;
				ll res=0;
				ll len=0;
				for(ll i=1;i<n;i++)
				{
					if(need[i]==0 and cs>0)
					{
						ans+=cs;
						cs=0;
						res=0;
					}
					else
					{
						if(need[i]<=res)
						{
							res=need[i];
						}
						else
						{
							ll diff=need[i]-res;
							cs+=diff;
							res=diff;
						}
					}
					if(i==n-1 and cs>0)
					{
						ans+=cs;
					}
				}

				cout<<ans<<endl;

			}

			return 0;
		 }


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll need[n]={0};
				ll mx=arr[0];
				for(ll i=1;i<n;i++)
				{
					if(arr[i]<mx)
					{
						need[i]=abs(arr[i]-mx);

					}
					mx=max(mx,arr[i]);
					//  cout<<need[i]<<" ";
				}
				// cout<<endl;
				// for(ll i=0;i<n;i++)
				// cin>>need[i];

				ll ans=0;
				ll cs=0;
				ll mn=inf;
				ll len=0;
				for(ll i=1;i<n;i++)
				{
					if(need[i]>=need[i-1])
					cs=need[i];
					else
					{
						ans+=cs;
						cs=need[i];
					}
					if(i==n-1 and need[i]>=need[i-1])
					ans+=cs;
				}
				cout<<ans<<endl;

			}

			return 0;
		 }


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll need[n]={0};
				ll mx=arr[0];
				for(ll i=1;i<n;i++)
				{
					if(arr[i]<mx)
					{
						need[i]=abs(arr[i]-mx);

					}
					mx=max(mx,arr[i]);
					//  cout<<need[i]<<" ";
				}
				// cout<<endl;
				// for(ll i=0;i<n;i++)
				// cin>>need[i];

				ll ans=0;
				ll cs=0;
				ll mn=inf;
				ll len=0;
				for(ll i=1;i<n;i++)
				{
					if(need[i]>need[i-1])
					cs=need[i];
					else
					{
						ans+=cs;
						cs=need[i];
					}
					if(i==n-1 and need[i]>need[i-1])
					ans+=need[i];
				}
				cout<<ans<<endl;

			}

			return 0;
		 }


/**
 *    author:  accesss_denied
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;
ll inf=1e18;

int main() {
			fast
			#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
			#endif

			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll arr[n];

				for(ll i=0;i<n;i++)
				cin>>arr[i];

				ll ans=0;
				for(ll i=1;i<n;i++)
				{
					if(arr[i]<arr[i-1])
					ans+=arr[i-1]-arr[i];
				}
				cout<<ans<<endl;

			}

			return 0;
		 }


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                while(i+1<n&&vec[start]>vec[i+1]){
                    minv=vec[i+1];
                    if(vec[i]>vec[i+1]){
                        while(i+1<n&&vec[i]>vec[i+1]){
                            minv=vec[i+1];
                            i++;
                        }
                        if(i+1<n)ans+=vec[i+1]-minv;
                        else ans+=vec[start]-minv;
                    }else{
                        while(i+1<n&&vec[i]<vec[i+1]){
                            i++;
                            if(vec[i]>=vec[start])break;
                        }
                        if(i+1<n) ans+=min(vec[start],vec[i])-minv;
                        else ans+=vec[start]-minv;
                    }
                }
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv,maxv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                i++;
                while(i<n&&vec[start]>vec[i]){
                    minv=vec[i];
                    while(i+1<n&&vec[i]>vec[i+1]){
                        i++;
                        minv=vec[i];
                    }
                    maxv=vec[i+1];
                    while(i+1<n&&vec[i]<=vec[i+1]){
                        i++;
                        maxv=vec[i];
                    }
                    if(vec[i]>=vec[start])break;
                    else ans+=vec[i]-minv;
                    minv=vec[i];
                    if(i>=n-1)break;
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                i++;
                while(i<n&&vec[start]>vec[i]){
                    minv=vec[i];
                    if(vec[i]>vec[i+1]){
                        while(i+1<n&&vec[i]>vec[i+1]){
                            i++;
                            minv=vec[i];
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }else{
                        while(i<n+1&&vec[i]<vec[i+1]){
                            i++;
                            if(vec[i]>=vec[start])break;
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        LL(n);
        vector<ll> vec(n);
        in(vec);
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1])ans+=vec[i]-vec[i+1];
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv;
        ll ans=0;
        for(int i=0;i<n;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                while(i+1<n&&vec[start]>vec[i+1]){
                    minv=vec[i+1];
                    if(vec[i]>vec[i+1]){
                        while(i+1<n&&vec[i]>vec[i+1]){
                            i++;
                            minv=vec[i];
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }else{
                        while(i+1<n&&vec[i]<=vec[i+1]){
                            i++;
                            if(vec[i]>=vec[start])break;
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                while(i+1<n&&vec[start]>vec[i+1]){
                    minv=vec[i+1];
                    if(vec[i]>vec[i+1]){
                        while(i+1<n&&vec[i]>vec[i+1]){
                            minv=vec[i+1];
                            i++;
                        }
                        if(i+1<n)ans+=vec[i+1]-minv;
                        else ans+=vec[start]-minv;
                    }else{
                        while(i+1<n&&vec[i]<vec[i+1]){
                            i++;
                            if(vec[i]>=vec[start])break;
                        }
                        ans+=min(vec[start],vec[i])-minv;
                    }
                }
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv,maxv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                while(i+1<n&&vec[start]>vec[i+1]){
                    minv=vec[i+1];
                    while(i+1<n&&vec[i]>vec[i+1]){
                        i++;
                        minv=vec[i];
                    }
                    maxv=vec[i+1];
                    while(i+1<n&&vec[i]<=vec[i+1]){
                        i++;
                        maxv=vec[i];
                    }
                    if(vec[i]>=vec[start])break;
                    else ans+=vec[i]-minv;
                    minv=vec[i];
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                i++;
                while(i<n&&vec[start]>vec[i]){
                    minv=vec[i];
                    if(vec[i]>vec[i+1]){
                        while(i+1<n&&vec[i]>vec[i+1]){
                            i++;
                            minv=vec[i];
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }else{
                        while(i<n&&vec[i]<vec[i+1]){
                            i++;
                            if(vec[i]>=vec[start])break;
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv,maxv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                i++;
                while(i<n&&vec[start]>vec[i]){
                    minv=vec[i];
                    while(i+1<n&&vec[i]>vec[i+1]){
                        i++;
                        minv=vec[i];
                    }
                    maxv=vec[i+1];
                    while(i+1<n&&vec[i]<=vec[i+1]){
                        i++;
                        maxv=vec[i];
                    }
                    if(vec[i]>=vec[start])break;
                    else ans+=vec[i]-minv;
                    minv=vec[i];
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                i++;
                while(i<n&&vec[start]>vec[i]){
                    minv=vec[i];
                    if(vec[i]>vec[i+1]){
                        while(i+1<n&&vec[i]>vec[i+1]){
                            i++;
                            minv=vec[i];
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }else{
                        while(i<n+1&&vec[i]<=vec[i+1]){
                            i++;
                            if(vec[i]>=vec[start])break;
                        }
                        if(i+1<n&&vec[i+1]<vec[start]) ans+=vec[i+1]-minv;
                    }
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}

int main(){
    codefor{
        INT(n);
        vector<int> vec(n);
        in(vec);
        int start,end,minv,maxv;
        ll ans=0;
        for(int i=0;i<n-1;i++){
            if(vec[i]>vec[i+1]){
                start=i;
                i++;
                while(i<n&&vec[start]>vec[i]){
                    minv=vec[i];
                    while(i+1<n&&vec[i]>vec[i+1]){
                        i++;
                        minv=vec[i];
                    }
                    maxv=vec[i+1];
                    while(i+1<n&&vec[i]<=vec[i+1]){
                        i++;
                        maxv=vec[i];
                    }
                    if(i+1>=n||vec[i]>=vec[start])break;
                    else ans+=vec[i]-minv;
                }
                ans+=vec[start]-minv;
            }
        }
        out(ans);
    }
}


#include <bits/stdc++.h>
using namespace std;
# define ll long long
# define endl "\n"
# define str string
# define vll vector<ll>
# define pb push_back
# define mll map<ll,ll>
# define mp make_pair
# define ff first
# define ss second
# define pll pair<ll,ll>
# define f(x,y) for(int x=0;x<y;x++)
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll x;
    cin>>x;
    while(x--){
        ll y;
        cin>>y;
        ll a[y];
        f(i,y){
            cin>>a[i];
        }
        ll k=a[0];
        ll ans=0;
        ll k1=10000000000;
        ll k2=10000000000;
        ll flg=1;
        for(ll i=1;i<y;i++){
            if(a[i]<=a[i-1]){
                k1=min(k1,a[i]);
            }
            else{
                if(k1!=10000000000){
                    if(k2==10000000000){
                        ans+=k-k1;
                        k2=k1;

                    }
                    else if(k2!=10000000000){
                        if(k2<k1){
                            flg=0;
                        }
                        else{
                            ans+=k-k1;
                            k2=k1;
                            flg=1;
                        }
                    }
                    k1=10000000000;
                }
            }
            k=max(k,a[i]);
        }
        if(k1!=10000000000 ){
            ans+=k-k1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define ll long long
# define endl "\n"
# define str string
# define vll vector<ll>
# define pb push_back
# define mll map<ll,ll>
# define mp make_pair
# define ff first
# define ss second
# define pll pair<ll,ll>
# define f(x,y) for(int x=0;x<y;x++)
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll x;
    cin>>x;
    while(x--){
        ll y;
        cin>>y;
        ll a[y];
        f(i,y){
            cin>>a[i];
        }
        ll k=a[0];
        ll ans=0;
        ll k1=10000000000;
        for(ll i=1;i<y;i++){
            k=max(k,a[i]);
            if(a[i]<a[i-1]){
                k1=min(k1,a[i]);
            }
            else{
                if(k1!=10000000000){
                    ans+=k-k1;
                    k1=10000000000;
                }
            }
        }
        if(k1!=10000000000){
            ans+=k-k1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define ll long long
# define endl "\n"
# define str string
# define vll vector<ll>
# define pb push_back
# define mll map<ll,ll>
# define mp make_pair
# define ff first
# define ss second
# define pll pair<ll,ll>
# define f(x,y) for(int x=0;x<y;x++)
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll x;
    cin>>x;
    while(x--){
        ll y;
        cin>>y;
        ll a[y];
        f(i,y){
            cin>>a[i];
        }
        ll k=a[0];
        ll ans=0;
        f(i,y){
            if(a[i]<k){
                ans=max(ans,k-a[i]);
            }
            else{
                k=a[i];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define ll long long
# define endl "\n"
# define str string
# define vll vector<ll>
# define pb push_back
# define mll map<ll,ll>
# define mp make_pair
# define ff first
# define ss second
# define pll pair<ll,ll>
# define f(x,y) for(int x=0;x<y;x++)
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll x;
    cin>>x;
    while(x--){
        ll y;
        cin>>y;
        ll a[y];
        f(i,y){
            cin>>a[i];
        }
        ll k=a[0];
        ll ans=0;
        ll k1=10000000000;
        ll flg=0;
        f(i,y){
            if(a[i]<k){
                k1=min(k1,a[i]);
                flg=1;
            }
            else{
                if(flg==1){
                    ans+=k-k1;
                    flg=0;
                    k1=10000000000;
                }
                k=a[i];
            }
        }
        if(k1!=10000000000){
            ans+=k-k1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define ll long long
# define endl "\n"
# define str string
# define vll vector<ll>
# define pb push_back
# define mll map<ll,ll>
# define mp make_pair
# define ff first
# define ss second
# define pll pair<ll,ll>
# define f(x,y) for(int x=0;x<y;x++)
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll x;
    cin>>x;
    while(x--){
        ll y;
        cin>>y;
        ll a[y];
        f(i,y){
            cin>>a[i];
        }
        ll k=a[0];
        ll ans=0;
        ll k1=10000000000;
        for(ll i=1;i<y;i++){
            if(a[i]<=a[i-1]){
                k1=min(k1,a[i]);
            }
            else{
                if(k1!=10000000000){
                    ans+=k-k1;
                    k1=10000000000;
                }
            }
             k=max(k,a[i]);
        }
        if(k1!=10000000000){
            ans+=k-k1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include <bitset>
#include <map>
#include <set>
#include <list>
#include<fstream>
#include<sstream>
#include<stdio.h>
#include<iomanip>
#include<climits>
using namespace std;
int main(){
	long long mx=0;
int n,len=0,d=1,ans=0;cin>>n;
vector<long long>v(n);
mx=abs(v[0]-v[1]);
int *a=new int[n-1];
for(int i=0;i<n;i++){
cin>>v[i];
}
for(int k=0;k<n-1;k++){
	a[k]=abs(v[k]-v[k+1]);
}ans=0;
for(int k=0;k<n-1;k++){
for(int j=k;j<n-1;j++){
ans=ans+a[j]*d;
d=d*-1;
if(mx<ans)mx=ans;
}
d=1,ans=0;
}

cout<<mx<<endl;
}


#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define repi(a, i, n) for( int i = a ; i < n ; i++)
#define repj(a, j, n) for( int j = a ; j < n ; j++)
#define rrep(a, i, n) for( int i = n ; i >= a; i--)
#define mp make_pair
#define pb push_back
#define S second
#define F first
#define inf 0x3f
#define MAX 1000001
#define teste cout << "testando **********\n"
#define ll  long long
#define ld  long double
#define mod 1000000007LL
typedef  vector<int> vi;
typedef  vector<double> vd;
typedef pair<int, int> pii;
typedef  pair<double, double> pdd;
typedef  pair< int, double> pid;
typedef  pair< double, int> pdi;
typedef map<int, int> mii;
ll abss(ll a, ll b){
    ll ans = a - b;
    if( ans < 0)
        return -ans;
    return ans;
}

void kadane(vector<ll> &v, ll &n, ll &max_total){
 ll max_atual, menos_max_atual;
 max_atual = menos_max_atual = 0;
 max_total = -11111111111111;
 for(ll i=0;i<n-1;i++){
        ll ans=(i&1LL)?-1:1;
        max_atual = max_atual + abss( v[i], v[i+1])*ans;
        menos_max_atual = menos_max_atual + abss( v[i], v[i+1])*(-ans);
        if(max_atual < 0){
            max_atual = 0;
        }
        if(menos_max_atual < 0)
            menos_max_atual=0;
        if(max_atual > max_total){
            max_total = max_atual;
        }
        if(menos_max_atual > max_total){
            max_total = menos_max_atual;
        }
 }
}
int main(){
 ll n, ma;
 cin >> n;
 vector<ll>v(n);
 repi(0,i,n)cin>>v[i];
 kadane(v,n,ma);
 cout << ma << endl;
 return 0;
}