// cheese-cracker [2022-02-12]

void solve(){
    int n = scan!int;
    auto arr = scanArray;
    long summ = 0;
    for(int i = 1; i <= n; ++i){
        summ += i * (n + 1 - i);
    }
    for(int i = 1; i <= n; ++i){
        if(arr[i-1] == 0){
            summ += i * (n + 1 - i);
        }
    }
    writeln(summ);
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
