import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Main11501 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
int N = Integer.parseInt(br.readLine());
int[] price = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
price[i] = Integer.parseInt(st.nextToken());
}
long answer = 0;
long max = 0;
for (int i = N - 1; i >= 0; i--) {
if (price[i] > max) {
max = price[i];
continue;
}
answer += max - price[i];
}
System.out.println(answer);
}
}
}
휴지통/알고리즘 & 자료구조