#include <bits/stdc++.h>
using namespace std;
int N, M, K, F[111][111][30], G[30][30];
char S[111];
int main() {
  scanf("%s", S);
  N = strlen(S);
  scanf("%d", &K);
  scanf("%d", &M);
  for (int i = 0, k; i < M; i++) {
    char s1[11], s2[11];
    scanf("%s%s%d", s1, s2, &k);
    G[s1[0] - 'a'][s2[0] - 'a'] = k;
  }
  memset(F, -2, sizeof(F));
  for (int i = 0; i < 26; i++) F[0][1][i] = 0;
  F[0][0][S[0] - 'a'] = 0;
  for (int i = 1; i < N; i++)
    for (int j = 0; j <= K; j++)
      for (int u = 0; u < 26; u++)
        for (int v = 0; v < 26; v++) {
          int d = (v + 'a' != S[i]);
          F[i][j + d][v] = max(F[i][j + d][v], F[i - 1][j][u] + G[u][v]);
        }
  int Ret = -1000000000;
  for (int i = 0; i <= K; i++)
    for (int j = 0; j < 26; j++) Ret = max(Ret, F[N - 1][i][j]);
  cout << Ret << endl;
  return 0;
}
