#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100 + 10;
const int MAX_CH = 130;
const int INF = (1 << 30);
int N, K;
string S;
int D[MAX_N][MAX_N][MAX_CH];
int W[MAX_CH][MAX_CH];
int Ans = -INF - 1;
void init() {
  for (int i = 0; i < MAX_N; i++)
    for (int j = 0; j < MAX_N; j++)
      for (int k = 0; k < MAX_CH; k++) D[i][j][k] = -INF;
  D[0][0][(int)'z' + 1] = 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> S >> K;
  cin >> N;
  for (int i = 0; i < N; i++) {
    char a, b;
    int c;
    cin >> a >> b >> c;
    W[(int)a][(int)b] = c;
  }
  init();
  for (int i = 1; i <= (int)S.size(); i++)
    for (int j = 0; j <= i && j <= K; j++)
      for (int now = (int)'a'; now <= (int)'z'; now++)
        for (int prev = (int)'a'; prev <= (int)'z' + 1; prev++) {
          if (now == S[i - 1]) {
            D[i][j][now] = max(D[i - 1][j][prev] + W[prev][now], D[i][j][now]);
          } else if (j != 0) {
            D[i][j][now] =
                max(D[i - 1][j - 1][prev] + W[prev][now], D[i][j][now]);
          }
        }
  for (int i = 0; i <= K; i++)
    for (int ch = (int)'a'; ch <= (int)'z'; ch++)
      Ans = max(Ans, D[(int)S.size()][i][ch]);
  cout << Ans << endl;
  return 0;
}
