#include <bits/stdc++.h>
using namespace std;
int n, q, f[700][700][107], a[1001][1001];
string s;
char x, y;
int z;
int dp(int u, int x, int k) {
  if (f[u][x][k] != -1) return f[u][x][k];
  if (u == 1) return 0;
  int res = dp(u - 1, s[u - 1] - 'a', k) + a[s[u - 1] - 'a'][x];
  if (!k) return f[u][x][k] = res;
  for (int i = 0; i < 26; i++) {
    if (i != s[u - 1] - 'a') res = max(res, dp(u - 1, i, k - 1) + a[i][x]);
  }
  return f[u][x][k] = res;
}
int main() {
  ios_base::sync_with_stdio(0);
  if (fopen("test.inp", "r")) freopen("test.inp", "r", stdin);
  cin >> s >> n >> q;
  s = ' ' + s + char('z' + 1);
  for (int i = 1; i <= q; i++) {
    cin >> x >> y >> z;
    a[x - 'a'][y - 'a'] = z;
  }
  memset(f, -1, sizeof(f));
  cout << dp(s.size() - 1, 27, n);
}
