CSES - Shared codeLink to this code: https://cses.fi/paste/4af0cd10fc8228471690ba/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    long long x, y;
    cin >> t;
    while (t--)
    {
        cin >> x >> y;
        long long m = max(x, y);
        // value of diagonal element - m,m
        long long ele = (long long)pow(m, 2) - (m - 1);

        if (x == y)
        {
            cout << ele << "\n";
            continue;
        }
        // find whether column same or row same?
        bool col = false;
        bool col_inc = false;
        if (y == m)
            col = true;

        // increasing values in column or not ?
        if (m % 2 == 0)
            col_inc = true;
        if (col)
        {
            // same column
            if (col_inc)
            {
                cout << ele - (m - x);
            }
            else
            {
                cout << ele + (m - x); // 7+ 2 - 1
            }
        }
        else
        {
            // same row
            if (!col_inc)
            {
                cout << ele - (m - y);
            }
            else
            {
                cout << ele + (m - y);
            }
        }
        cout << "\n";
    }

    return 0;
}