#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 65536
#define INF 0x3f3f3f3f
long long sum[MAXN], ssum[MAXN], isum[MAXN], issum[MAXN];
int BIT[MAXN<<1];
int query(int x) {
int ret = 0;
while(x) {
ret = max(ret, BIT[x]);
x -= x&(-x);
}
return ret;
}
void modify(int x, int v, int L) {
while(x <= L) {
BIT[x] = max(BIT[x], v);
x += x&(-x);
}
}
int query2(int x) {
int ret = INF;
while(x) {
ret = min(ret, BIT[x]);
x -= x&(-x);
}
return ret;
}
void modify2(int x, int v, int L) {
while(x <= L) {
BIT[x] = min(BIT[x], v);
x += x&(-x);
}
}
int cases = 0, preA[MAXN], sufA[MAXN], start[MAXN], cover[MAXN<<1];
int main() {
int testcase, n, A[MAXN];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &A[i]), start[i] = 0, cover[A[i] + MAXN] = -1;
int L = 50000 + MAXN;
for(int i = 0; i <= L; i++)
BIT[i] = 0;
for(int i = 1; i <= n; i++) {
preA[i] = query(A[i] + MAXN);
modify(A[i]+MAXN, i, L);
if(preA[i] <= cover[A[i] + MAXN])
start[i] = cover[A[i] + MAXN] + 1;
cover[A[i] + MAXN] = i;
}
for(int i = 0; i <= L; i++)
BIT[i] = n + 1;
for(int i = n; i >= 1; i--) {
sufA[i] = query2(A[i] + MAXN);
modify2(A[i]+MAXN+1, i, L);
}
for(int i = 1; i <= n; i++)
sum[i] = sum[i-1] + A[i];
for(int i = 1; i <= n; i++)
ssum[i] = ssum[i-1] + sum[i];
isum[n+1] = issum[n+1] = 0;
for(int i = n; i >= 1; i--)
isum[i] = isum[i+1] + A[i];
for(int i = n; i >= 1; i--)
issum[i] = issum[i+1] + isum[i];
long long ret = 0;
for(int i = 1; i <= n; i++) {
long long suma, a, sumb, b;
if(start[i]) {
suma = issum[start[i]] - issum[i], a = i - start[i];
sumb = ssum[sufA[i]-1] - ssum[i], b = sufA[i] - i - 1;
suma -= isum[i] * a;
sumb -= sum[i] * b;
} else {
suma = issum[preA[i]+1] - issum[i], a = i - preA[i] - 1;
sumb = ssum[sufA[i]-1] - ssum[i], b = sufA[i] - i - 1;
suma -= isum[i] * a;
sumb -= sum[i] * b;
}
ret += (suma - A[i]*a*(a+1)/2) * (b + 1) + (sumb - A[i]*b*(b+1)/2) * (a + 1);
}
printf("Case %d: %lld\n", ++cases, ret);
}
return 0;
}