#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > dis(26, vector<int>(26, 27 * 100));
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char x, y;
    int z;
    cin >> x >> y >> z;
    dis[x - 'a'][y - 'a'] = min(dis[x - 'a'][y - 'a'], z);
  }
  if (s.length() != t.length()) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < 26; i++) {
    dis[i][i] = 0;
  }
  for (int k = 0; k < 26; k++) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  int tot = 0;
  string pt;
  for (int i = 0; i < s.length(); i++) {
    int ans = dis[s[i] - 'a'][t[i] - 'a'];
    char z = t[i];
    for (int j = 0; j < 26; j++) {
      if (ans > (dis[s[i] - 'a'][j] + dis[t[i] - 'a'][j])) {
        ans = (dis[s[i] - 'a'][j] + dis[t[i] - 'a'][j]);
        z = char('a' + j);
      }
    }
    if (ans == 2700) {
      cout << -1;
      return 0;
    }
    tot += ans;
    pt += z;
  }
  cout << tot << endl;
  cout << pt << endl;
  return 0;
}
