#include <bits/stdc++.h>
int INF = 99999;
int main() {
  std::string a, b;
  int n;
  std::cin >> a >> b >> n;
  if (a.size() != b.size()) {
    std::cout << -1 << std::endl;
    return 0;
  }
  std::vector<std::vector<int> > g(26, std::vector<int>(26, INF));
  for (int i = 0; i < n; ++i) {
    char x, y;
    int w;
    std::cin >> x >> y >> w;
    g[x - 'a'][y - 'a'] = std::min(g[x - 'a'][y - 'a'], w);
  }
  for (int i = 0; i < 26; ++i) {
    g[i][i] = 0;
  }
  for (int k = 0; k < 26; ++k) {
    for (int i = 0; i < 26; ++i) {
      for (int j = 0; j < 26; ++j) {
        int ncost = g[i][k] + g[k][j];
        g[i][j] = std::min(g[i][j], ncost);
      }
    }
  }
  int res = 0;
  std::string c;
  for (int i = 0; i < (int)a.size(); ++i) {
    int best = INF;
    int bestidx;
    for (int j = 0; j < 26; ++j) {
      int cost = g[a[i] - 'a'][j] + g[b[i] - 'a'][j];
      if (best > cost) {
        best = cost;
        bestidx = j;
      }
    }
    if (best == INF) {
      res = -1;
      break;
    } else {
      res += best;
      c += bestidx + 'a';
    }
  }
  std::cout << res << std::endl;
  if (res != -1) {
    std::cout << c << std::endl;
  }
}
