Getting WA in Uva 335 due to wierd reasons

When I submit Code1 on 335 - Processing MX Records https://onlinejudge.org/external/3/335.pdf I am getting WA but I am getting AC if I submit Code2. I cant figure out why. They are completely identical except the if else chain for command.
Code1

#include <bits/stdc++.h>
using namespace std;

bool EndsWith(const string& a, const string& b) {
	if (b.size() > a.size()) return false;
	return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin());
}

struct redirect {
public:
	string start, end;
	int precedence;
	bool matches_start(const string& a) const {
		if (a == start) {
			return true;
		}
		if (start[0] == '*' && EndsWith(a, start.substr(1))) {
			return true;
		}
		return false;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	cin.ignore(1, '\n');
	vector<redirect> mx_record(n);
	map <string, bool> machine_state;
	string input;
	for (int i = 0; i < n; i++) {
		getline(cin, input);
		stringstream input_stream(input);
		if (input[0] == ' ') {
			mx_record[i].start = mx_record[i - 1].start;
			input_stream >> mx_record[i].precedence >> mx_record[i].end;
		}
		else {
			input_stream >> mx_record[i].start >> mx_record[i].precedence >> mx_record[i].end;
		}
		machine_state[mx_record[i].end] = true;
	}
	sort(mx_record.begin(), mx_record.end(), [](const redirect& a, const redirect& b) {return a.precedence < b.precedence; });
	char command;
	while (cin >> command) {
		if (command == 'X') {
			return 0;
		}
		cin >> input;
		if (command == 'D') {
			machine_state[input] = false;
		}
		else if (command == 'U') {
			machine_state[input] = true;
		}
		else {
			cout << input << " =>";
			for (int i = 0; i < n; i++) {
				if (machine_state[mx_record[i].end] == true && mx_record[i].matches_start(input)) {
					cout << " " << mx_record[i].end;
					break;
				}
			}
			cout << '\n';
		}
	}
}

Code2

#include <bits/stdc++.h>
using namespace std;

bool EndsWith(const string& a, const string& b) {
	if (b.size() > a.size()) return false;
	return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin());
}

struct redirect {
public:
	string start, end;
	int precedence;
	bool matches_start(const string& a) const {
		if (a == start) {
			return true;
		}
		if (start[0] == '*' && EndsWith(a, start.substr(1))) {
			return true;
		}
		return false;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	cin.ignore(1, '\n');
	vector<redirect> mx_record(n);
	map <string, bool> machine_state;
	string input;
	for (int i = 0; i < n; i++) {
		getline(cin, input);
		stringstream input_stream(input);
		if (input[0] == ' ') {
			mx_record[i].start = mx_record[i - 1].start;
			input_stream >> mx_record[i].precedence >> mx_record[i].end;
		}
		else {
			input_stream >> mx_record[i].start >> mx_record[i].precedence >> mx_record[i].end;
		}
		machine_state[mx_record[i].end] = true;
	}
	sort(mx_record.begin(), mx_record.end(), [](const redirect& a, const redirect& b) {return a.precedence < b.precedence; });
	char command;
	while (cin >> command) {
		if (command == 'X') {
			return 0;
		}
		cin >> input;
		if (command == 'A') {
			cout << input << " =>";
			for (int i = 0; i < n; i++) {
				if (machine_state[mx_record[i].end] == true && mx_record[i].matches_start(input)) {
					cout << " " << mx_record[i].end;
					break;
				}
			}
			cout << '\n';
		}
		else if (command == 'D') {
			machine_state[input] = false;
		}
		else {
			machine_state[input] = true;
		}
	}
}