#include <bits/stdc++.h>
using namespace std;
int dis[27][27], ans;
int main() {
  for (int i = 0; i < 27; i++)
    for (int j = 0; j < 27; j++) dis[i][j] = (i != j) * 1e9;
  string s, t, p;
  cin >> s >> t;
  p = s;
  if (s.size() != t.size()) return cout << -1 << '\n', 0;
  int n, w;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char a, b;
    cin >> a >> b >> w;
    dis[a - 'a'][b - 'a'] = min(dis[a - 'a'][b - 'a'], w);
  }
  for (int k = 0; k < 27; k++)
    for (int i = 0; i < 27; i++)
      for (int j = 0; j < 27; j++)
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  if (s.length() != t.length()) return cout << -1 << '\n', 0;
  for (int i = 0; i < s.size(); i++) {
    int res = 1e9;
    for (int j = 0; j < 27; j++)
      if (res > dis[s[i] - 'a'][j] + dis[t[i] - 'a'][j])
        p[i] = j + 'a', res = dis[s[i] - 'a'][j] + dis[t[i] - 'a'][j];
    if (res >= 1e9) return cout << -1 << '\n', 0;
    ans += res;
  }
  cout << ans << '\n' << p;
  return 0;
}
