Implement an in-memory key–value store that supports nested transactions with the operations: SET key value, RETURN key, BEGIN, APPLY (commit current transaction into its parent), and DISCARD (rollback current transaction). Reads and writes should reflect the most recent value visible in the current transactional context. Transactions can be arbitrarily nested. Example sequence: SET X=20; BEGIN; BEGIN; SET X=10; RETURN X; APPLY; DISCARD; RETURN X. The first RETURN should yield 10 and the second 20. Describe the data structures, APIs, and the time/space complexity of each operation, and consider edge cases (e.g., RETURN on unset keys, APPLY/DISCARD with no active transaction).