#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <stdio.h>
#include <iomanip>
#include <sstream>
using namespace std;

ifstream filein("DATA5.txt");
ofstream fileout("OUT5.txt");

/*void go(char c, int n) {
	if (check[(char)c - 97][n] == true)
		return;
	else {
		for (i = 0; i < strlen(c, n); i++) {
			istringstream sin(str(c, n), istringstream::in);
*/

bool check[5][6];
string a[6], b[6], c[6], d[6], e[6];
int va[6], vb[6], vc[6], vd[6], ve[6];
int split(string s, int a, int b);

int convint(string s) {
	int total=0, i;
	for(i=0;i<s.length();++i)
	{
		total*=10;
		total+=(char)s[i]-48;
	}
	return total;
}
int go(char q, int n) {
	if (q == 'A') {
		if (check[0][n] == true)
			return(va[n]);
		else
			return(split(a[n], 0, n));
	}
	if (q == 'B') {
		if (check[1][n] == true)
			return(vb[n]);
		else
			return(split(b[n], 1, n));
	}
	if (q == 'C') {
		if (check[2][n] == true)
			return(vc[n]);
		else
			return(split(c[n], 2, n));
	}
	if (q == 'D') {
		if (check[3][n] == true)
			return(vd[n]);
		else
			return(split(d[n], 3, n));
	}
	if (q == 'E') {
		if (check[4][n] == true)
			return(ve[n]);
		else
			return(split(e[n], 4, n));
	}
}

bool in(int c, int a, int b) {
	if (c >= a && c <= b) 
		return(true);
	else
		return(false);
}
	
int value(string s) {
	if (in((char)s[0], 65, 90) == true) {
		return(go(s[0], (char)(s[1]) - 48));
	}
	else {
		return(convint(s));
	}
}

int split(string s, int a, int b) {
	int j, k, i;
	string temp[100];
	istringstream sin(s, istringstream::in);
	j = 0;
	while (sin >> temp[j]) {
		j++;
	}
	if (temp[0][0] == '=') {
		temp[0].erase(0, 1);
	}
	i = value(temp[0]);
	for (k = 2; k < j; k += 2) {
		if (temp[k - 1] == "+") {
			i += value(temp[k]);
		}
		if (temp[k - 1] == "-") {
			i -= value(temp[k]);
		}
		if (temp[k - 1] == "*") {
			i *= value(temp[k]);
		}
	}
	check[a][b] = true;
	return(i);
}

int main() {

	int i, j, k;

		for (i = 0; i < 6; i++) {
			va[i] = 0;
			vb[i] = 0;
			vc[i] = 0;
			vd[i] = 0;
			ve[i] = 0;
		}
		for (i = 0; i < 5; i++) {
			for (j = 0; j < 6; j++) {
				check[i][j] = false;
			}
		}
		for (i = 1; i <= 5; i++) {
			getline(filein, a[i]);
		}
		for (i = 1; i <= 5; i++) {
			getline(filein, b[i]);
		}
		for (i = 1; i <= 5; i++) {
			getline(filein, c[i]);
		}
		for (i = 1; i <= 5; i++) {
			getline(filein, d[i]);
		}
		for (i = 1; i <= 5; i++) {
			getline(filein, e[i]);
		}
		for (i = 1; i <= 5; i++) {
			istringstream sin(a[i], istringstream::in);
			string temp[100];
			j = 0;
			while (sin >> temp[j]) {
				j++;
			}
			if (temp[0][0] == '=') {
				temp[0].erase(0, 1);
			}
			va[i] = value(temp[0]);
			for (k = 2; k < j; k += 2) {
				if (temp[k - 1] == "+") {
					va[i] += value(temp[k]);
				}
				if (temp[k - 1] == "-") {
					va[i] -= value(temp[k]);
				}
				if (temp[k - 1] == "*") {
					va[i] *= value(temp[k]);
				}
			}
			check[0][i] = true;
		}
		for (i = 1; i <= 5; i++) {
			istringstream sin(b[i], istringstream::in);
			string temp[100];
			j = 0;
			while (sin >> temp[j]) {
				j++;
			}
			if (temp[0][0] == '=') {
				temp[0].erase(0, 1);
			}
			vb[i] = value(temp[0]);
			for (k = 2; k < j; k += 2) {
				if (temp[k - 1] == "+") {
					vb[i] += value(temp[k]);
				}
				if (temp[k - 1] == "-") {
					vb[i] -= value(temp[k]);
				}
				if (temp[k - 1] == "*") {
					vb[i] *= value(temp[k]);
				}
			}
			check[1][i] = true;
		}
		for (i = 1; i <= 5; i++) {
			istringstream sin(c[i], istringstream::in);
			string temp[100];
			j = 0;
			while (sin >> temp[j]) {
				j++;
			}
			if (temp[0][0] == '=') {
				temp[0].erase(0, 1);
			}
			vc[i] = value(temp[0]);
			for (k = 2; k < j; k += 2) {
				if (temp[k - 1] == "+") {
					vc[i] += value(temp[k]);
				}
				if (temp[k - 1] == "-") {
					vc[i] -= value(temp[k]);
				}
				if (temp[k - 1] == "*") {
					vc[i] *= value(temp[k]);
				}
			}
			check[2][i] = true;
		}
		for (i = 1; i <= 5; i++) {
			istringstream sin(d[i], istringstream::in);
			string temp[100];
			j = 0;
			while (sin >> temp[j]) {
				j++;
			}
			if (temp[0][0] == '=') {
				temp[0].erase(0, 1);
			}
			vd[i] = value(temp[0]);
			for (k = 2; k < j; k += 2) {
				if (temp[k - 1] == "+") {
					vd[i] += value(temp[k]);
				}
				if (temp[k - 1] == "-") {
					vd[i] -= value(temp[k]);
				}
				if (temp[k - 1] == "*") {
					vd[i] *= value(temp[k]);
				}
			}
			check[3][i] = true;
		}
		for (i = 1; i <= 5; i++) {
			istringstream sin(e[i], istringstream::in);
			string temp[100];
			j = 0;
			while (sin >> temp[j]) {
				j++;
			}
			if (temp[0][0] == '=') {
				temp[0].erase(0, 1);
			}
			ve[i] = value(temp[0]);
			for (k = 2; k < j; k += 2) {
				if (temp[k - 1] == "+") {
					ve[i] += value(temp[k]);
				}
				if (temp[k - 1] == "-") {
					ve[i] -= value(temp[k]);
				}
				if (temp[k - 1] == "*") {
					ve[i] *= value(temp[k]);
				}
			}
			check[4][i] = true;
		}
		for (i = 1; i <= 5; i++) {
			fileout << va[i] << endl;
		}
		for (i = 1; i <= 5; i++) {
			fileout << vb[i] << endl;
		}
		for (i = 1; i <= 5; i++) {
			fileout << vc[i] << endl;
		}
		for (i = 1; i <= 5; i++) {
			fileout << vd[i] << endl;
		}
		for (i = 1; i <= 5; i++) {
			fileout << ve[i] << endl;
		}
	

	filein.close();
	fileout.close();
	return(0);
}
