#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define sz(a) (int) (a).size()
#define int long long
using namespace std;
vector<int> brute(int n, const vector<int>& a) {
vector<int> ans;
for (int pf = 1; pf <= n; pf++) {
vector<int> b(a.begin(), a.begin() + pf);
int cnt = 0;
for (int i = 0; i < pf; i++) {
for (int j = 0; j < pf; j++) {
if (b[i] < b[j]) {
swap(b[i], b[j]);
cnt++;
}
}
}
ans.push_back(cnt);
}
return ans;
}
const int N = 1e5 + 11;
struct BIT{
int bit[N];
void add(int p, int v) {
for (int i = p; i < N; i += i & -i) {
bit[i] += v;
}
}
int qry(int p) {
int res = 0;
for (int i = p; i; i -= i & -i) {
res += bit[i];
}
return res;
}
} bit;
vector<int> calc(int n, const vector<int>& a) {
vector<int> ans(n);
int inv = 0, chain = 0, lst = -1, cur_max = 0, tot_num_eq = 0, num_eq = 0, have_eq = false;
for (int i = 0; i < n; i++) {
inv += bit.qry(n) - bit.qry(a[i]);
if (bit.qry(a[i]) == bit.qry(a[i] - 1))
bit.add(a[i], 1);
if (a[i] > cur_max) {
cur_max = a[i], chain++;
int gap = i - lst - 1; lst = i;
tot_num_eq += num_eq;
num_eq = 0; have_eq = false;
}
else if (a[i] == cur_max) have_eq = true;
if (have_eq) num_eq++;
ans[i] = inv + (chain - 1) * 2 + tot_num_eq;
// cout << inv << ' ' << num_eq << ' ' << chain << endl;
}
set<int> s (all(a));
for (auto v : s) {
bit.add(v, -1);
}
return ans;
}
mt19937 rng(42);
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// vector<int> a(10); int n = 10;
// for (int i = 0; i < 10; i++) {
// a[i] = rng() % 10 + 1;
// }
vector<int> ans = calc(n, a);
// vector<int> other = brute(n, a);
// for (auto x : a) cout << x << ' '; cout << endl;
// for (auto x : ans) cout << x << ' '; cout << endl;
// for (auto x : other) cout << x << ' '; cout << endl;
assert(ans == other);
// for (int i = 0; i < n; i++) {
// cout << ans[i] << " \n"[i == n - 1];
// }
}
int32_t main() {
cin.tie(0)->sync_with_stdio(false);
int t = 1; cin >> t;
while (t--) {
solve();
}
}