#include<stdio.h>
#include<string.h>
#define m1 50
#define m2 50
char b3[m1][m2], a1[m1][m2], b2[m2][m2], b4[m1], a5[m2];
int judge(int x, int x3) {
int y1, y2;
if (strlen(b3[x]) > strlen(a1[x3]))
y2 = strlen(b3[x]);
else y2 = strlen(a1[x3]);
for (y1 = 0; y1 < y2; y1++)
if (b3[x][y1] != a1[x3][y1])
break;
if (y1 == y2)
return 1;
else return 0;
}
int judge1(int x3)
{
int y1, y2;
if (strlen(a5) > strlen(b2[x3]))
y2 = strlen(a5);
else y2 = strlen(b2[x3]);
for (y1 = 0; y1 < y2; y1++)
if (a5[y1] != b2[x3][y1])
break;
if (y1 == y2)
return 1;
else return 0;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int x1, x2, x3, x, n, m, k, i = 1, a3[m2] = { 0 }, a4[m2][m1] = { 0 },j,m3,k1;
char ch, b1[m2][m1], a2[700];
scanf("%d %d %d", &n, &m, &k);
m3 = m;
k1 = k;
ch = getchar();
while (m--)
{
gets(a1[i]);
i++;
}
i = 1;
while (k--)
{
gets(a2);
for (x = 0; ;x++)
{
if (a2[x] == ' ') {
b2[i][x] = '\0';
break;
}
b2[i][x] = a2[x];
}
x += 3;
for ( x1=0,x2=1; x < strlen(a2); x++,x1++)
{
if (a2[x] == ' ') {
b3[x2][x1] = '\0';
b4[x2] = a2[x + 1];
x2++;
x += 2;
x1 = -1;
continue;
}
b3[x2][x1] = a2[x];
}
for (x = 1; x < x2; x++)
{
for (x3 = 1; x3 <= m3; x3++) {
j = judge(x, x3);
if(j)
a4[i][x3] = b4[x] - '0';
}
}
for (x3 = 1; x3 <= m3; x3++)
a4[i][0] += a4[i][x3];
i++;
}
while (n--)
{
gets(a2);
for (x3 = 0; x3 < strlen(a2); x3++) {
if (a2[x3] == ' ') {
ch = a2[x3 + 1];
a5[x3] = '\0';
break;
}
a5[x3] = a2[x3];
}
for (x3 = 1; x3 <= k1; x3++) {
j = judge1(x3);
if (j) {
for (x = 1; x <= m3; x++) {
if (ch > '0' && a4[x3][0] <= 10)
a3[x] += a4[x3][x] * 2;
else a3[x] += a4[x3][x];
}
}
}
}
for (x = 1; x <= m3; x++) {
printf("%d", a3[x]);
if (x != m3)
printf(" ");
}
printf("\n");
}
return 0;
}