#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000")
using namespace std;
long long min(long long a, long long b) {
  if (a < b) return a;
  return b;
}
vector<vector<pair<int, int> > > g;
int d[26][26], m, used[26];
string s[2], anss;
int main() {
  cin >> s[0] >> s[1];
  if (s[0].size() != s[1].size()) {
    cout << -1;
    return 0;
  }
  anss = s[0];
  cin >> m;
  g.resize(26);
  for (int i = 0; i < m; i++) {
    char a, b;
    int w;
    cin >> a >> b >> w;
    g[a - 'a'].push_back(make_pair(b - 'a', w));
  }
  queue<int> q;
  for (int i = 0; i < 26; i++) {
    q.push(i);
    for (int j = 0; j < 26; j++) {
      d[i][j] = (1e9 + 7);
      used[j] = 0;
    }
    d[i][i] = 0;
    while (!q.empty()) {
      int v = q.front();
      used[v] = 0;
      q.pop();
      for (int j = 0; j < g[v].size(); j++) {
        int to = g[v][j].first, cost = g[v][j].second;
        if (cost + d[i][v] < d[i][to]) {
          d[i][to] = cost + d[i][v];
          if (!used[to]) {
            used[to] = 1;
            q.push(to);
          }
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < s[0].size(); i++) {
    if (s[0][i] != s[1][i]) {
      int a = s[0][i] - 'a', b = s[1][i] - 'a', best = -1, cst = (1e9 + 7);
      for (int j = 0; j < 26; j++) {
        if (d[a][j] + d[b][j] < cst) {
          cst = d[a][j] + d[b][j];
          best = j;
        }
      }
      if (cst == (1e9 + 7)) {
        cout << -1;
        return 0;
      }
      ans += cst;
      anss[i] = char(best + 'a');
    }
  }
  cout << ans << '\n' << anss << '\n';
  return 0;
}
