#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, p;
  cin >> s >> p;
  vector<vector<long long> > adj(26, (vector<long long>(26, INT_MAX)));
  long long n, num;
  char ch1, ch2;
  cin >> n;
  for (int i = 0; i < 26; ++i) adj[i][i] = 0;
  for (int i = 0; i < n; ++i) {
    cin >> ch1 >> ch2 >> num;
    adj[ch1 - 'a'][ch2 - 'a'] = min(adj[ch1 - 'a'][ch2 - 'a'], num);
  }
  int len = s.size(), len1 = p.size();
  if (len != len1) {
    cout << -1 << endl;
    return 0;
  }
  for (int tr = 0; tr < 26; ++tr)
    for (int src = 0; src < 26; ++src) {
      for (int to = 0; to < 26; ++to) {
        { adj[src][to] = min(adj[src][to], adj[src][tr] + adj[tr][to]); }
      }
    }
  long long ans = 0, temp;
  int ind;
  string ret = "";
  for (int i = 0; i < len; ++i) {
    {
      ind = -1, temp = INT_MAX;
      for (int j = 0; j < 26; ++j) {
        long long tot = adj[s[i] - 'a'][j] + adj[p[i] - 'a'][j];
        if (tot < temp) {
          temp = tot;
          ind = j;
        }
      }
      if (temp == INT_MAX || ind == -1) {
        cout << -1 << endl;
        return 0;
      }
      ret.insert(ret.end(), 'a' + ind);
      ans += temp;
    }
  }
  cout << ans << endl;
  cout << ret << endl;
  return 0;
}
