#include typedef struct List { struct List *next; int v, id, cost, x; } list; const long long sup = 1LL << 60; void chmin(long long* a, long long b) { if (*a > b) *a = b; } long long DFS(list* adj[], int u, int t, int flag[], int label) { if (u == t) { if (label == 0) return sup; else return 0; } else flag[u] = 1; int w; long long ans = sup; list *p; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (flag[w] != 0) continue; chmin(&ans, DFS(adj, w, t, flag, label ^ p->x) + p->cost); } flag[u] = 0; return ans; } int main() { int i, k, N, M, K, u, w, c, x, bit[31]; char X[31]; list *adj[51] = {}, e[1001]; scanf("%d %d %d", &N, &M, &K); if (N > 50 || M > 500) return 0; for (k = 1, bit[0] = 1; k < K; k++) bit[k] = bit[k-1] << 1; for (i = 0; i < M; i++) { scanf("%d %d %d %s", &u, &w, &c, X); for (k = 0, x = 0; k < K; k++) if (X[k] == '1') x |= bit[k]; e[i*2].v = w; e[i*2+1].v = u; e[i*2].id = i; e[i*2+1].id = i; e[i*2].cost = c; e[i*2+1].cost = c; e[i*2].x = x; e[i*2+1].x = x; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); } long long ans, flag[51]; for (u = 1; u < N; u++) { ans = DFS(adj, u, N, flag, 0); printf("%lld\n", (ans == sup)? -1: ans); } fflush(stdout); return 0; }