Rain Water Trapping

Code: https://ide.codingblocks.com/s/412931
Problem: https://hack.codingblocks.com/app/contests/1975/327/problem

Not giving correct output for second test case.

int main(){
    int n,t;
    cin>>t;
    //cin>>n;not here
    
    //int a[n];not here 
    for(int i=0;i<t;i++){
        cin>>n;//here bec diff tc
        int a[n];//here
        for(int i=0;i<n;i++)
           cin>>a[i];
        int l[n];
        l[0]=a[0];
        for(int i=1;i<n;i++)
           l[i]=max(l[i-1],a[i]);
    
        int r[n];
   
        r[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--)
           r[i]=max(r[i+1],a[i]);
    
        int ans =0;
        for(int i=0;i<n;i++)
           ans +=(min(l[i],r[i]) -a[i]);
    
        cout << ans << endl;
    }
    
}