#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=105*2;
struct Search_t{
int n;
int G[N][N];
int w[N][N];
int op[N];
int v[N];
int used[N];
int tmp_u[N];
int ans,now;
bool Can_choose(int k){
for (int i=1;i<=n;i++){
if (v[i] && !used[i] && G[k][i]) return false;
if (v[i] && used[i] && G[i][op[k]]) return false;
}
return true;
}
int greedy(int k){
int ret=0;
for (int i=1;i<=n;i++) tmp_u[i]=used[i];
for (int i=k;i<=n;i++)
if (!v[i]){
bool check=true;
for (int j=1;j<=n;j++)
if (v[j] && !used[j] && G[i][j]) { check=false; break; }
if (!check) continue;
ret+=w[i][i];
for (int j=1;j<=n;j++)
if (tmp_u[j])
ret+=w[i][j];
tmp_u[i]=1;
}
return ret;
}
void Choose(int k){
v[k]=1;
v[op[k]]=1;
used[k]=1;
now+=w[k][k];
for (int i=1;i<=n;i++)
if (v[i] && used[i] && i!=k) now+=w[i][k];
}
void Remove(int k){
v[k]=0;
v[op[k]]=0;
used[k]=0;
now-=w[k][k];
for (int i=1;i<=n;i++)
if (v[i] && used[i] && i!=k) now-=w[i][k];
}
void dfs(int dep){
if (dep>n){
if (now>ans) ans=now;
return;
}
if (now+greedy(dep)<=ans)
return;
if (v[dep]) dfs(dep+1);
else{
bool s1=Can_choose(dep);
bool s2=Can_choose(op[dep]);
if (s1 && s2){
Choose(dep);
int w1=now+greedy(dep+1);
Remove(dep);
Choose(op[dep]);
int w2=now+greedy(dep+1);
Remove(op[dep]);
if (w1>w2){
Choose(dep);
dfs(dep+1);
Remove(dep);
Choose(op[dep]);
dfs(dep+1);
Remove(op[dep]);
}
else{
Choose(op[dep]);
dfs(dep+1);
Remove(op[dep]);
Choose(dep);
dfs(dep+1);
Remove(dep);
}
}
else
if (s1){
Choose(dep);
dfs(dep+1);
Remove(dep);
}
else
if (s2){
Choose(op[dep]);
dfs(dep+1);
Remove(op[dep]);
}
}
}
void gao(){
memset(v,0,sizeof(v));
memset(used,0,sizeof(used));
now=0;
ans=0;
dfs(1);
printf("%d\n",ans);
}
}Search;
#define op(x) ((x)>n?(x)-n:(x)+n)
int m,n,Cn;
int mat[N][N];
int G[N][N];
int Only[N][N];
int Color[N];
int P;
void solve(){
memset(G,0,sizeof(G));
for (int i=1;i<=2*n;i++)
for (int j=1;j<=2*n;j++)
if (mat[i][j] && Color[i]!=Color[j])
G[Color[i]][Color[j]]=1;
memset(Only,0,sizeof(Only));
for (int i=1;i<=n;i++){
Only[Color[i]][Color[op(i)]]=1;
Only[Color[op(i)]][Color[i]]=1;
}
memcpy(Search.G,G,sizeof(G));
memset(Search.w,0,sizeof(Search.w));
for (int i=1;i<=Cn;i++)
for (int j=1;j<=Cn;j++)
if (Only[i][j]){
Search.op[i]=j;
Search.op[j]=i;
}
Search.n=Cn;
for (int i=0;i<P;i++){
int a,b,x,y,w;
scanf("%d%d%d%d%d",&a,&x,&b,&y,&w);
if (x==0) a+=n;
if (y==0) b+=n;
Search.w[Color[a]][Color[b]]+=w;
if (Color[a]!=Color[b]) Search.w[Color[b]][Color[a]]+=w;
}
Search.gao();
}
int main(){
while (scanf("%d%d%d",&n,&m,&P)!=EOF){
memset(mat,0,sizeof(mat));
for (int i=0;i<m;i++){
int x,y,c;
char oper[10];
char Tmp[10];
scanf("%d %s %d %s %d",&x,oper,&y,Tmp,&c);
switch (oper[0]){
case 'x':
if (c){
mat[x][op(y)]=1;
mat[y][op(x)]=1;
mat[op(x)][y]=1;
mat[op(y)][x]=1;
}
else{
mat[x][y]=1;
mat[y][x]=1;
mat[op(x)][op(y)]=1;
mat[op(y)][op(x)]=1;
}
break;
case 'o':
if (c){
mat[op(x)][y]=1;
mat[op(y)][x]=1;
}
else{
mat[x][op(x)]=1;
mat[y][op(y)]=1;
}
break;
case 'a':
if (c){
mat[op(x)][x]=1;
mat[op(y)][y]=1;
}
else{
mat[x][op(y)]=1;
mat[y][op(x)]=1;
}
break;
}
}
for (int k=1;k<=2*n;k++)
for (int i=1;i<=2*n;i++)
for (int j=1;j<=2*n;j++)
mat[i][j]|=mat[i][k]&mat[k][j];
Cn=0;
memset(Color,0,sizeof(Color));
for (int i=1;i<=2*n;i++)
if (!Color[i]){
Color[i]=++Cn;
for (int j=1;j<=2*n;j++)
if (mat[i][j] && mat[j][i])
Color[j]=Cn;
}
bool check=true;
for (int i=1;i<=n;i++)
if (Color[i]==Color[op(i)]) check=false;
if (!check) puts("No solution");
else solve();
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBOPTEwNSoyOwoKc3RydWN0IFNlYXJjaF90ewoJaW50IG47CglpbnQgR1tOXVtOXTsKCWludCB3W05dW05dOwoJaW50IG9wW05dOwoJaW50IHZbTl07CglpbnQgdXNlZFtOXTsKCWludCB0bXBfdVtOXTsKCWludCBhbnMsbm93OwoKCWJvb2wgQ2FuX2Nob29zZShpbnQgayl7CgkJZm9yIChpbnQgaT0xO2k8PW47aSsrKXsKCQkJaWYgKHZbaV0gJiYgIXVzZWRbaV0gJiYgR1trXVtpXSkgcmV0dXJuIGZhbHNlOwoJCQlpZiAodltpXSAmJiB1c2VkW2ldICYmIEdbaV1bb3Bba11dKSByZXR1cm4gZmFsc2U7CgkJfQoJCXJldHVybiB0cnVlOwoJfQoKCWludCBncmVlZHkoaW50IGspewoJCWludCByZXQ9MDsKCQlmb3IgKGludCBpPTE7aTw9bjtpKyspIHRtcF91W2ldPXVzZWRbaV07CiAgICAgICAgZm9yIChpbnQgaT1rO2k8PW47aSsrKQogICAgICAgICAgICBpZiAoIXZbaV0pewogICAgICAgICAgICAgICAgYm9vbCBjaGVjaz10cnVlOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaj0xO2o8PW47aisrKQogICAgICAgICAgICAgICAgICAgIGlmICh2W2pdICYmICF1c2VkW2pdICYmIEdbaV1bal0pIHsgY2hlY2s9ZmFsc2U7IGJyZWFrOyB9CiAgICAgICAgICAgICAgICBpZiAoIWNoZWNrKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIHJldCs9d1tpXVtpXTsKICAgICAgICAgICAgICAgIGZvciAoaW50IGo9MTtqPD1uO2orKykKICAgICAgICAgICAgICAgICAgICBpZiAodG1wX3Vbal0pCiAgICAgICAgICAgICAgICAgICAgICAgIHJldCs9d1tpXVtqXTsKICAgICAgICAgICAgICAgIHRtcF91W2ldPTE7CiAgICAgICAgICAgIH0KCQlyZXR1cm4gcmV0OwoJfQoKCXZvaWQgQ2hvb3NlKGludCBrKXsKCQl2W2tdPTE7CgkJdltvcFtrXV09MTsKCQl1c2VkW2tdPTE7CgkJbm93Kz13W2tdW2tdOwoJCWZvciAoaW50IGk9MTtpPD1uO2krKykKCQkJaWYgKHZbaV0gJiYgdXNlZFtpXSAmJiBpIT1rKSBub3crPXdbaV1ba107Cgl9CgoJdm9pZCBSZW1vdmUoaW50IGspewoJCXZba109MDsKCQl2W29wW2tdXT0wOwoJCXVzZWRba109MDsKCQlub3ctPXdba11ba107CgkJZm9yIChpbnQgaT0xO2k8PW47aSsrKQoJCQlpZiAodltpXSAmJiB1c2VkW2ldICYmIGkhPWspIG5vdy09d1tpXVtrXTsKCX0KCgl2b2lkIGRmcyhpbnQgZGVwKXsKCQlpZiAoZGVwPm4pewoJCQlpZiAobm93PmFucykgYW5zPW5vdzsKCQkJcmV0dXJuOwoJCX0KCQlpZiAobm93K2dyZWVkeShkZXApPD1hbnMpCiAgICAgICAgICAgIHJldHVybjsKCQlpZiAodltkZXBdKSBkZnMoZGVwKzEpOwoJCWVsc2V7CgkJCWJvb2wgczE9Q2FuX2Nob29zZShkZXApOwoJCQlib29sIHMyPUNhbl9jaG9vc2Uob3BbZGVwXSk7CgkJCWlmIChzMSAmJiBzMil7CgkJCQlDaG9vc2UoZGVwKTsKCQkJCWludCB3MT1ub3crZ3JlZWR5KGR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