Candidate names are the arguments (case-insensitive). Each stdin line is a ballot listing all candidates in preference order. A ballot is valid iff its tokens are exactly the candidate set, each once; for every invalid ballot print Invalid ballot: <line> and discard it.
Then run instant-runoff on the valid ballots: each round, tally every ballot's first non-eliminated preference; if a candidate has strictly more than half of the valid ballots print Winner: <c>. If one candidate remains it wins; if two remain with equal tallies print Tie; otherwise eliminate the lowest-tally candidate (ties → eliminate the lexicographically-greatest name) and repeat.
Precise IRR rules defined here (exam source underspecified).
POSIX shell only (#!/bin/dash); standard UNIX filters. From COMP2041/9044 — adapted for deterministic autotest.