OIG XI - sej

// https://szkopul.edu.pl/problemset/problem/Plha_6BH8_14ZptrZOdschts/site/?key=statement
// OIG XI (3 etap, zawody drużynowe)

#include <iostream>

typedef long long ll;

constexpr int sizik = 1000 * 1001;

int wiersz[sizik], kolumna[sizik];

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int n, m;
    std::cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        wiersz[i] = i;
        kolumna[i] = i;
    }

    for (; m > 0; m--) {
        char z;
        int a, b;
        std::cin >> z >> a >> b;

        if (z == 'W') {
            std::swap(wiersz[a], wiersz[b]);
        } else if (z == 'K') {
            std::swap(kolumna[a], kolumna[b]);
        } else if (z == 'P') {
            ll ans = (ll)(wiersz[a] - 1) * n + kolumna[b];

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

    return 0;
}