#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector<vector<int>> edges(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            edges[u].push_back(v);
            edges[v].push_back(u);
        }

        vector<int> ks(n + 1);
        long long ans = 0;

        for (int x = 1; x <= n; x++) {
            if (edges[x].size() == 1) {
                ans += n - 2;
                int y = edges[x][0];
                ks[y]++;
            }
        }

        for (int x = 0; x <= n; x++) {
            ans -= 1LL * ks[x] * (ks[x] - 1) / 2;
            if (edges[x].size() == 2 && ks[x] == 0) ans++;
        }

        cout << 1LL * n * (n - 1) * (n - 2) / 6 - ans << '\n';
    }
}
