#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m;
    b /= 2;
    a = ((a % m) * (a % m)) % m;
  }
  return ans % m;
}
int t;
string s;
long long g[30][30];
long long dp[105][30][105];
bool vis[105][30][105];
long long solve(int id, int prev, int k) {
  if (id == t) return 0;
  long long ans = -(long long)1e18 + 5LL;
  if (vis[id][prev][k]) return dp[id][prev][k];
  vis[id][prev][k] = 1;
  if (id == 0) {
    for (int i = 0; i < 26; i++) {
      if (s[id] - 'a' == i)
        ans = max(ans, solve(id + 1, i, k));
      else if (k > 0)
        ans = max(ans, solve(id + 1, i, k - 1));
    }
  } else {
    for (int i = 0; i < 26; i++) {
      if (s[id] - 'a' == i)
        ans = max(ans, g[prev][i] + solve(id + 1, i, k));
      else if (k > 0)
        ans = max(ans, g[prev][i] + solve(id + 1, i, k - 1));
    }
  }
  return dp[id][prev][k] = ans;
}
int main() {
  int k;
  cin >> s >> k;
  t = s.length();
  int n;
  cin >> n;
  char u, v;
  long long w;
  for (int i = 0; i < n; i++) {
    cin >> u >> v >> w;
    g[u - 'a'][v - 'a'] = w;
  }
  cout << solve(0, 0, k);
}
