static int day;
static long profit;
static int[] stock;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
StringTokenizer st;
for (int t = 1; t <= testCase; t++) {
day = Integer.parseInt(br.readLine());
stock = new int[day];
profit = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < day; i++) {
stock[i] = Integer.parseInt(st.nextToken());
}
int max = stock[day - 1];
calculateProfit(max);
System.out.println(profit);
}
}
static void calculateProfit(int max) {
for (int i = day - 2; i >= 0; i--) {
if (stock[i] < max) {
profit += max - stock[i];
} else {
max = stock[i];
}
}
}
출처:https://www.acmicpc.net/problem/11501