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

#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define F first
#define S second
#define all(c) (c).begin(), (c).end()

int tc;

signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    cin >> tc;
    while (tc--)
    {
        int n;
        cin >> n;
        map<char, int> first_pos;
        map<char, int> second_pos;

        vector<string> a(n);

        map<string, int> cntr;

        for (auto &x : a)
            cin >> x;

        int ans = 0;

        for (int i = n - 1; i >= 0; i--)
        {

            int temp = first_pos[a[i][0]] + second_pos[a[i][1]] - 2 * cntr[a[i]];
            ans += temp;

            cntr[a[i]]++;
            first_pos[a[i][0]]++;
            second_pos[a[i][1]]++;
        }

        cout << ans << '\n';
    }

    return 0;
}

Generated URL:

Output:

No Output
Report Bug