#include <iostream>
#include <vector>
using namespace std;
int min_operations_to_non_decreasing(vector<int>& heights) {
int operations = 0;
for (int i = 1; i < heights.size(); ++i) {
if (heights[i] < heights[i - 1]) {
// Increase heights[i] to heights[i - 1] to maintain non-decreasing order
operations++;
heights[i] = heights[i - 1];
}
}
return operations;
}
int main() {
int t;
cin >> t; // Number of test cases
while (t--) {
int n;
cin >> n; // Number of pillars
vector<int> heights(n);
for (int i = 0; i < n; ++i) {
cin >> heights[i];
}
int result = min_operations_to_non_decreasing(heights);
cout << result << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1pbl9vcGVyYXRpb25zX3RvX25vbl9kZWNyZWFzaW5nKHZlY3RvcjxpbnQ+JiBoZWlnaHRzKSB7CiAgICBpbnQgb3BlcmF0aW9ucyA9IDA7CiAgICBmb3IgKGludCBpID0gMTsgaSA8IGhlaWdodHMuc2l6ZSgpOyArK2kpIHsKICAgICAgICBpZiAoaGVpZ2h0c1tpXSA8IGhlaWdodHNbaSAtIDFdKSB7CiAgICAgICAgICAgIC8vIEluY3JlYXNlIGhlaWdodHNbaV0gdG8gaGVpZ2h0c1tpIC0gMV0gdG8gbWFpbnRhaW4gbm9uLWRlY3JlYXNpbmcgb3JkZXIKICAgICAgICAgICAgb3BlcmF0aW9ucysrOwogICAgICAgICAgICBoZWlnaHRzW2ldID0gaGVpZ2h0c1tpIC0gMV07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIG9wZXJhdGlvbnM7Cn0KCmludCBtYWluKCkgewogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsgIC8vIE51bWJlciBvZiB0ZXN0IGNhc2VzCiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG47CiAgICAgICAgY2luID4+IG47ICAvLyBOdW1iZXIgb2YgcGlsbGFycwogICAgICAgIHZlY3RvcjxpbnQ+IGhlaWdodHMobik7CiAgICAgICAgCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICAgICAgY2luID4+IGhlaWdodHNbaV07CiAgICAgICAgfQoKICAgICAgICBpbnQgcmVzdWx0ID0gbWluX29wZXJhdGlvbnNfdG9fbm9uX2RlY3JlYXNpbmcoaGVpZ2h0cyk7CiAgICAgICAgY291dCA8PCByZXN1bHQgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=