#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& os, const vector<int>& v) {
  for (auto const& i : v) {
    os << i << " ";
  }
  os << endl;
  return os;
}
const long long int mod = ((1LL << 31) - 1);
const int N = 1e4 + 1;
int k;
int n;
string s;
int bonus[124][124];
int memo[102][102][124];
int dp(int c, int t, int l) {
  if (c > k) return -1e8;
  if (t >= n) return 0;
  int& ans = memo[c][t][l];
  if (ans != -1) return ans;
  ans = -1e8;
  for (int i = 97; i < 123; i++) {
    ans = max(ans, dp(c + (i != s[t]), t + 1, i) + bonus[l][i]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> s;
  cin >> k;
  char f, m;
  int lines;
  cin >> lines;
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < lines; i++) {
    cin >> f;
    cin >> m;
    cin >> bonus[f][m];
  }
  n = s.size();
  cout << dp(0, 0, 'A') << endl;
  return 0;
}
