#include <bits/stdc++.h>
using namespace std;
const int MAX = 30;
int main() {
#ifdef wxh010910
freopen("input.txt", "r", stdin);
#endif
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
vector<vector<pair<int, int>>> edges(MAX);
while (q--) {
int from, to, cost;
scanf("%d %d %d", &from, &to, &cost);
--from;
--to;
for (int i = cost; i < MAX; ++i) {
edges[i].emplace_back(from, to);
}
}
vector<long long> answer(m);
for (int w = 0; w < MAX; ++w) {
vector<int> p(n << 1);
vector<long long> tag(m);
for (int i = 0; i < n << 1; ++i) {
p[i] = i;
}
auto find = [&](int x) {
while (x != p[x]) {
x = p[x] = p[p[x]];
}
return x;
};
vector<pair<int, int>> useful;
for (auto e : edges[w]) {
int x = find(e.first), y = find(e.second + n);
if (x == y) {
continue;
}
if (x > y) {
swap(x, y);
}
p[x] = y;
++tag[0];
if (x >= n && y >= n) {
useful.emplace_back(x - n, y - n);
}
}
for (int i = 1; i < m && !useful.empty(); ++i) {
vector<pair<int, int>> new_useful;
for (auto e : useful) {
int x = find(e.first), y = find(e.second);
if (x == y) {
--tag[i];
} else {
if (x > y) {
swap(x, y);
}
p[x] = y;
if (x >= n && y >= n) {
new_useful.emplace_back(x - n, y - n);
}
}
}
swap(useful, new_useful);
}
for (int i = 1; i < m; ++i) {
tag[i] += tag[i - 1];
}
for (int i = 1; i < m; ++i) {
tag[i] += tag[i - 1];
}
for (int i = 0; i < m; ++i) {
answer[i] += (long long)n * (i + 2) - 1 - tag[i];
}
}
for (int i = 0; i < m; ++i) {
printf("%lld\n", answer[i]);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWCA9IDMwOwoKaW50IG1haW4oKSB7CiNpZmRlZiB3eGgwMTA5MTAKICBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKI2VuZGlmCiAgaW50IG4sIG0sIHE7CiAgc2NhbmYoIiVkICVkICVkIiwgJm4sICZtLCAmcSk7CiAgdmVjdG9yPHZlY3RvcjxwYWlyPGludCwgaW50Pj4+IGVkZ2VzKE1BWCk7CiAgd2hpbGUgKHEtLSkgewogICAgaW50IGZyb20sIHRvLCBjb3N0OwogICAgc2NhbmYoIiVkICVkICVkIiwgJmZyb20sICZ0bywgJmNvc3QpOwogICAgLS1mcm9tOwogICAgLS10bzsKICAgIGZvciAoaW50IGkgPSBjb3N0OyBpIDwgTUFYOyArK2kpIHsKICAgICAgZWRnZXNbaV0uZW1wbGFjZV9iYWNrKGZyb20sIHRvKTsKICAgIH0KICB9CiAgdmVjdG9yPGxvbmcgbG9uZz4gYW5zd2VyKG0pOwogIGZvciAoaW50IHcgPSAwOyB3IDwgTUFYOyArK3cpIHsKICAgIHZlY3RvcjxpbnQ+IHAobiA8PCAxKTsKICAgIHZlY3Rvcjxsb25nIGxvbmc+IHRhZyhtKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbiA8PCAxOyArK2kpIHsKICAgICAgcFtpXSA9IGk7CiAgICB9CiAgICBhdXRvIGZpbmQgPSBbJl0oaW50IHgpIHsKICAgICAgd2hpbGUgKHggIT0gcFt4XSkgewogICAgICAgIHggPSBwW3hdID0gcFtwW3hdXTsKICAgICAgfQogICAgICByZXR1cm4geDsKICAgIH07CiAgICB2ZWN0b3I8cGFpcjxpbnQsIGludD4+IHVzZWZ1bDsKICAgIGZvciAoYXV0byBlIDogZWRnZXNbd10pIHsKICAgICAgaW50IHggPSBmaW5kKGUuZmlyc3QpLCB5ID0gZmluZChlLnNlY29uZCArIG4pOwogICAgICBpZiAoeCA9PSB5KSB7CiAgICAgICAgY29udGludWU7CiAgICAgIH0KICAgICAgaWYgKHggPiB5KSB7CiAgICAgICAgc3dhcCh4LCB5KTsKICAgICAgfQogICAgICBwW3hdID0geTsKICAgICAgKyt0YWdbMF07CiAgICAgIGlmICh4ID49IG4gJiYgeSA+PSBuKSB7CiAgICAgICAgdXNlZnVsLmVtcGxhY2VfYmFjayh4IC0gbiwgeSAtIG4pOwogICAgICB9CiAgICB9CiAgICBmb3IgKGludCBpID0gMTsgaSA8IG0gJiYgIXVzZWZ1bC5lbXB0eSgpOyArK2kpIHsKICAgICAgdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBuZXdfdXNlZnVsOwogICAgICBmb3IgKGF1dG8gZSA6IHVzZWZ1bCkgewogICAgICAgIGludCB4ID0gZmluZChlLmZpcnN0KSwgeSA9IGZpbmQoZS5zZWNvbmQpOwogICAgICAgIGlmICh4ID09IHkpIHsKICAgICAgICAgIC0tdGFnW2ldOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoeCA+IHkpIHsKICAgICAgICAgICAgc3dhcCh4LCB5KTsKICAgICAgICAgIH0KICAgICAgICAgIHBbeF0gPSB5OwogICAgICAgICAgaWYgKHggPj0gbiAmJiB5ID49IG4pIHsKICAgICAgICAgICAgbmV3X3VzZWZ1bC5lbXBsYWNlX2JhY2soeCAtIG4sIHkgLSBuKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgc3dhcCh1c2VmdWwsIG5ld191c2VmdWwpOwogICAgfQogICAgZm9yIChpbnQgaSA9IDE7IGkgPCBtOyArK2kpIHsKICAgICAgdGFnW2ldICs9IHRhZ1tpIC0gMV07CiAgICB9CiAgICBmb3IgKGludCBpID0gMTsgaSA8IG07ICsraSkgewogICAgICB0YWdbaV0gKz0gdGFnW2kgLSAxXTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICAgIGFuc3dlcltpXSArPSAobG9uZyBsb25nKW4gKiAoaSArIDIpIC0gMSAtIHRhZ1tpXTsKICAgIH0KICB9CiAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyArK2kpIHsKICAgIHByaW50ZigiJWxsZFxuIiwgYW5zd2VyW2ldKTsKICB9CiAgcmV0dXJuIDA7Cn0K