#include <iostream>
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];
        }
        for(int i=n-1;i>=0;i--){
            if(a[i-1]>a[i]){
                a[i-1]=a[i];
            }
        }
        long long int sum=0;
        for(int i=0;i<n;i++){
            sum+=a[i];
        }
        cout<<sum<<"\n";
    }
    return 0;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: