#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 100;
const int maxm = 2000 + 100;
int g[maxn][maxn], pre[maxm][maxm], match[maxn], matched[maxn], a[maxm], b[maxm];
long long f[maxm][maxm];
char base[maxn], ta[maxm], tb[maxm], ans1[maxm * 2], ans2[maxm * 2];
map<char, int> ap;
int main() {
scanf("%s%s%s", base, ta, tb);
int l = strlen(base), la = strlen(ta), lb = strlen(tb);
for(int i = 0; i < l; ++i) {
ap[base[i]] = i;
}
for(int i = 0; i < la; ++i) {
a[i] = ap[ta[i]];
}
for(int i = 0; i < lb; ++i) {
b[i] = ap[tb[i]];
}
for(int i = 0; i < l; ++i) {
for(int j = 0; j < l; ++j) {
scanf("%d", &g[i][j]);
if(g[i][j] < g[i][match[i]]) {
match[i] = j;
}
if(g[i][j] < g[matched[j]][j]) {
matched[j] = i;
}
}
}
f[0][0] = 0;
pre[0][0] = -1;
for(int i = 0; i < la; ++i) {
f[i + 1][0] = f[i][0] + g[a[i]][match[a[i]]];
pre[i + 1][0] = 0;
}
for(int i = 0; i < lb; ++i) {
f[0][i + 1] = f[0][i] + g[matched[b[i]]][b[i]];
pre[0][i + 1] = 1;
}
for(int i = 0; i < la; ++i) {
for(int j = 0; j < lb; ++j) {
if(f[i][j + 1] + g[a[i]][match[a[i]]] < f[i + 1][j] + g[matched[b[j]]][b[j]]) {
f[i + 1][j + 1] = f[i][j + 1] + g[a[i]][match[a[i]]];
pre[i + 1][j + 1] = 0;
} else {
f[i + 1][j + 1] = f[i + 1][j] + g[matched[b[j]]][b[j]];
pre[i + 1][j + 1] = 1;
}
if(f[i][j] + g[a[i]][b[j]] < f[i + 1][j + 1]) {
f[i + 1][j + 1] = f[i][j] + g[a[i]][b[j]];
pre[i + 1][j + 1] = 2;
}
}
}
printf("%lld\n", f[la][lb]);
int lans;
for(lans = 0; pre[la][lb] != -1; ++lans) {
if(pre[la][lb] == 2) {
--la, --lb;
ans1[lans] = base[a[la]];
ans2[lans] = base[b[lb]];
} else if(pre[la][lb] == 0) {
--la;
ans1[lans] = base[a[la]];
ans2[lans] = base[match[a[la]]];
} else if(pre[la][lb] == 1) {
--lb;
ans1[lans] = base[matched[b[lb]]];
ans2[lans] = base[b[lb]];
}
}
--lans;
for(int i = lans; 0 <= i; --i) {
putchar(ans1[i]);
}
puts("");
for(int i = lans; 0 <= i; --i) {
putchar(ans2[i]);
}
puts("");
}