#include <bits/stdc++.h>
using namespace std;
int c[26][26];
char s[121];
int f[121][121][29];
bool check[121][121][29];
int m;
int com(int i, int k, int p) {
  if (i == m) return 0;
  if (check[i][k][p]) return f[i][k][p];
  int r = c[p][s[i] - 'a'] + com(i + 1, k, s[i] - 'a');
  int j;
  if (k)
    for (j = 0; j < 26; j++)
      if (j != (s[i] - 'a')) {
        r = max(r, c[p][j] + com(i + 1, k - 1, j));
      }
  f[i][k][p] = r;
  check[i][k][p] = 1;
  return r;
}
int main() {
  int i, j, n, k, l, t;
  char x, y;
  for (i = 0; i < 29; i++)
    for (j = 0; j < 29; j++) c[i][j] = 0;
  cin >> s >> k >> n;
  m = strlen(s);
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    cin >> c[x - 'a'][y - 'a'];
  }
  cout << com(0, k, 27);
  return 0;
}
