#include <bits/stdc++.h>
using namespace std;
const int min_ans = -10000000;
string S;
int K, N, i, j, s[103], d, x, y, L, ans;
char xx, yy;
int a[30][30], dp[103][103][30];
int main() {
  cin >> S >> K;
  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> xx >> yy >> d;
    a[xx - 'a' + 1][yy - 'a' + 1] = d;
  }
  L = S.size();
  for (i = 0; i < L; i++) s[i + 1] = int(S[i] - 'a' + 1);
  for (i = 1; i <= L; i++)
    for (j = 0; j <= K; j++)
      for (x = 1; x <= 26; x++) dp[i][j][x] = min_ans;
  dp[1][0][s[1]] = 0;
  for (x = 1; x <= 26; x++) dp[1][1][x] = 0;
  for (i = 1; i < L; i++)
    for (j = 0; j <= i; j++)
      for (x = 1; x <= 26; x++)
        for (y = 1; y <= 26; y++) {
          dp[i + 1][j + 1][y] = max(dp[i + 1][j + 1][y], dp[i][j][x] + a[x][y]);
          if (s[i + 1] == y)
            dp[i + 1][j][y] = max(dp[i + 1][j][y], dp[i][j][x] + a[x][y]);
        }
  ans = min_ans;
  for (j = 0; j <= K; j++)
    for (x = 1; x <= 26; x++) ans = max(ans, dp[L][j][x]);
  cout << ans << endl;
}
