#!/usr/bin/perl -w

use v5.36;
use strict;

my %expr;
my %val;
while (<>) {
	chomp;
	my ($name, $rest) = split /: /;
	my @ops = split /\s+/, $rest;
	if (@ops > 1) {
		$expr{$name} = \@ops;
	} else {
		$val{$name} = $ops[0];
	}
}

sub walk($var) {
	return $val{$var} if defined $val{$var};
	my ($arg1, $op, $arg2) = @{ $expr{$var} };
	if ($op eq '+') {
		return $val{$var} = walk($arg1) + walk($arg2);
	} elsif ($op eq '-') {
		return $val{$var} = walk($arg1) - walk($arg2);
	} elsif ($op eq '*') {
		return $val{$var} = walk($arg1) * walk($arg2);
	} elsif ($op eq '/') {
		return $val{$var} = walk($arg1) / walk($arg2);
	}
}

say walk('root');
