#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<long long> h(n);
    int M = 0;
    for (int i = 0; i < n; ++i) {
        cin >> h[i];
        if (h[i] > h[M]) {
            M = i;
        }
    }

    vector<long long> B(n - 1);
    for (int i = 0; i < n - 1; ++i) {
        B[i] = h[(M + 1 + i) % n];
    }

    vector<long long> fL(n), fR(n);
    vector<pair<long long, long long>> st;
    long long current_sum = 0;

    fL[0] = 0;
    for (int i = 1; i < n; ++i) {
        long long x = B[i - 1];
        long long cnt = 1;
        while (!st.empty() && st.back().first <= x) {
            current_sum -= st.back().first * st.back().second;
            cnt += st.back().second;
            st.pop_back();
        }
        st.push_back({x, cnt});
        current_sum += x * cnt;
        fL[i] = current_sum;
    }

    st.clear();
    current_sum = 0;
    fR[n - 1] = 0;
    for (int i = n - 2; i >= 0; --i) {
        long long x = B[i];
        long long cnt = 1;
        while (!st.empty() && st.back().first <= x) {
            current_sum -= st.back().first * st.back().second;
            cnt += st.back().second;
            st.pop_back();
        }
        st.push_back({x, cnt});
        current_sum += x * cnt;
        fR[i] = current_sum;
    }

    vector<long long> ans(n);
    for (int i = 0; i < n; ++i) {
        ans[(M + 1 + i) % n] = fL[i] + fR[i];
    }

    for (int i = 0; i < n; ++i) {
        cout << ans[i] << (i == n - 1 ? "" : " ");
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    if (cin >> t) {
        while (t--) {
            solve();
        }
    }
    return 0;
}