#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct state {
int x, y, z, t;
state() {
x = y = z = t = -1;
}
state(int x, int y, int z, int t) :
x(x), y(y), z(z), t(t) {
}
bool operator<(const state &e) const {
return t < e.t;
}
};
const int N = 100;
int n, m, k;
int firstToSolve[N];
vector<state> sub;
int WA[N][N], FAC[N];
int main(int argc, char **argv) {
int T;
scanf("%d", &T);
while (T-- != 0) {
scanf("%d%d%d", &n, &m, &k);
int x, y, z, tm, ts;
sub.clear();
for (int i = 0; i < k; ++i) {
scanf("%d%d%d%d:%d", &x, &y, &z, &tm, &ts);
sub.push_back(state(x - 1, y - 1, z, tm * 60 + ts));
}
sort(sub.begin(), sub.end());
memset(firstToSolve, -1, sizeof firstToSolve);
memset(WA, 0, sizeof WA);
memset(FAC, 0, sizeof FAC);
int ep = -1, sg = -1;
pair<int, int> sp(-1, 0), rp(-1, 0);
for (int i = 0; i < k; ++i) {
if (sub[i].z == 0)
++WA[sub[i].x][sub[i].y];
else {
if (firstToSolve[sub[i].x] == -1)
firstToSolve[sub[i].x] = sub[i].y + 1;
if (ep == -1)
ep = sub[i].y + 1;
sg = sub[i].y + 1;
if (WA[sub[i].x][sub[i].y] == 0)
++FAC[sub[i].y];
if (FAC[sub[i].y] > sp.first || (FAC[sub[i].y] == sp.first && sub[i].y + 1 < sp.second))
sp = make_pair(FAC[sub[i].y], sub[i].y + 1);
if (WA[sub[i].x][sub[i].y] > rp.first || (WA[sub[i].x][sub[i].y] == rp.first && sub[i].y + 1 < rp.second))
rp = make_pair(WA[sub[i].x][sub[i].y], sub[i].y + 1);
}
}
for (int i = 0; i < n; ++i)
printf("%s%d", i ? " " : "", firstToSolve[i]);
puts("");
printf("%d %d %d %d\n", ep, sg, sp.second, rp.second);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCnN0cnVjdCBzdGF0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