#include <bits/stdc++.h>
using namespace std;
int s[101], a[27][27], dp[101][101][27];
int n, m;
const int INF = 0x3f3f3f3f;
string t;
int main() {
  cin >> t;
  for (int(i) = 0; (i) < (int)(t.size()); (i)++) s[i] = t[i] - 'a';
  cin >> n >> m;
  for (int(i) = 0; (i) < (int)(m); (i)++) {
    char c1, c2;
    int c;
    cin >> c1 >> c2 >> c;
    a[c1 - 'a'][c2 - 'a'] = c;
  }
  memset(dp, 0xc0, sizeof dp);
  dp[0][0][26] = 0;
  for (int(i) = 0; (i) < (int)(t.size()); (i)++) {
    for (int(j) = 0; (j) < (int)(n + 1); (j)++) {
      for (int(k) = 0; (k) < (int)(26); (k)++) {
        for (int(l) = 0; (l) < (int)(27); (l)++) {
          int j2 = (s[i] != k);
          if (j - j2 >= 0)
            dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j - j2][l] + a[l][k]);
        }
      }
    }
  }
  int ans = -INF;
  for (int(j) = 0; (j) < (int)(n + 1); (j)++)
    for (int(k) = 0; (k) < (int)(26); (k)++) {
      if (ans < dp[t.size()][j][k]) {
        ans = dp[t.size()][j][k];
      }
    }
  cout << ans << endl;
  return 0;
}
