#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 10000 + 5, M = 30 + 5;
struct submission {
int p, s, t;
bool ac;
submission(int P=0, int S=0, int T=0, bool AC=false) {
p=P, s=S, t=T, ac=AC;
}
bool operator < (const submission &o) const {
return t < o.t;
}
} sub[N];
int firstAC[M], WAs[M][M], ACs[M];
bool AC[M][M];
int main() {
int n, m, k;
int p, s, ac, t1, t2;
int extreme, steadfast, solid, relentless;
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &m, &k);
swap(n, m);
for(int i=0; i<k; ++i) {
scanf("%d %d %d %d:%d", &p, &s, &ac, &t1, &t2);
t1 = t1*60+t2;
sub[i] = submission(p, s, t1, ac);
}
sort(sub, sub+k);
memset(firstAC, -1, sizeof firstAC);
memset(WAs, 0, sizeof WAs);
memset(AC, 0, sizeof AC);
memset(ACs, 0, sizeof ACs);
extreme = steadfast = solid = relentless = -1;
for(int i=0; i<k; ++i) {
p = sub[i].p;
s = sub[i].s;
ac = sub[i].ac;
if(ac) {
if(firstAC[p] == -1)
firstAC[p] = s;
if(extreme == -1)
extreme = s;
steadfast = s;
if(WAs[s][p] == 0)
++ACs[s];
AC[s][p] = true;
} else {
++WAs[s][p];
}
}
int mxWA = -1;
for(int i=1; i<=n; ++i)
for(int j=1;j<=m;++j) {
if(AC[i][j] && mxWA < WAs[i][j]) {
mxWA = WAs[i][j];
relentless = i;
}
}
int mxAC = -1;
for(int i=1; i<=n; ++i) {
if(mxAC < ACs[i]) {
mxAC = ACs[i];
solid = i;
}
}
printf("%d", firstAC[1]);
for(int i=2; i<=m; ++i)
printf(" %d", firstAC[i]);
puts("");
printf("%d %d %d %d\n", extreme, steadfast, solid, relentless);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdHlwZWRlZiBsb25nIGxvbmcgbGw7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gMTAwMDAgKyA1LCBNID0gMzAgKyA1OwoKc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