#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int ans = 0, n;
  string s, t, p;
  cin >> s >> t >> n;
  vector<vector<int>> dist(26, vector<int>(26, INF));
  vector<vector<pair<int, int>>> gr(26);
  p = s;
  for (int i = 0; i < n; ++i) {
    char a, b;
    int w;
    cin >> a >> b >> w;
    gr[a - 'a'].push_back({b - 'a', w});
  }
  if (s.size() != t.size()) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < 26; ++i) {
    vector<int> d(26, INF);
    d[i] = 0;
    queue<int> q;
    q.push(i);
    while (q.size() > 0) {
      int f = q.front();
      q.pop();
      for (auto g : gr[f]) {
        if (d[g.first] > d[f] + g.second) {
          d[g.first] = d[f] + g.second;
          q.push(g.first);
        }
      }
    }
    dist[i] = d;
  }
  for (int i = 0; i < s.size(); ++i) {
    int r = INF;
    for (int j = 0; j < 26; ++j)
      if (r > dist[s[i] - 'a'][j] + dist[t[i] - 'a'][j]) {
        p[i] = j + 'a';
        r = dist[s[i] - 'a'][j] + dist[t[i] - 'a'][j];
      }
    if (r >= INF) {
      cout << -1;
      return 0;
    }
    ans += r;
  }
  cout << ans << endl;
  cout << p;
}
