Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
Input
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
Output
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
Examples
Input
ross rachel
4
ross joey
rachel phoebe
phoebe monica
monica chandler
Output
ross rachel
joey rachel
joey phoebe
joey monica
joey chandler
Input
icm codeforces
1
codeforces technex
Output
icm codeforces
icm technex
Note
In first example, the killer starts with ross and rachel.
After day 1, ross is killed and joey appears.
After day 2, rachel is killed and phoebe appears.
After day 3, phoebe is killed and monica appears.
After day 4, monica is killed and chandler appears.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to simulate the process: Initially we have two names. Then for each day, we get a murdered person (who is one of the two current potential victims) and a replacement name. So we replace the murdered name with the new name, and the other one remains. So after each day, we have a new pair of two names (the other surviving person and the replacement). The output requires to output the state before each murder? Let's read carefully:
"Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected."
Interpretation: At the beginning, before any murder, the killer has two potential victims (the initial two). That is the pair from which he selects for the 1st murder. So first line: initial pair. Then after the 1st murder, the pair becomes updated (the surviving one and the replacement). That new pair is the one from which the killer selects for the 2nd murder. So we need to output the pair before each day (i.e., the state at the start of that day). Then after n days, we output the pair that exists after the last murder (which would be the pair for the next murder). So we need to output n+1 lines: initial state, and after each day's replacement we output the new state (which is the pair before the next murder). Actually careful: "i-th line should contain the two persons from which the killer selects for the i-th murder." So line 1: pair before day1. line2: pair before day2? But day2 selection uses the pair after day1's murder. So line2 is after day1's murder and before day2. Similarly, line n: pair before day n? Actually the i-th murder corresponds to day i. So for i=1, pair before day1. For i=2, pair before day2, which is after day1's changes. For i=3, pair before day3, after day2. So lines 1..n correspond to the pair at start of day i. Then line n+1 corresponds to the pair after day n, i.e., for next murder. That matches examples:
Example1:
Initial: ross rachel -> line1: ross rachel
After day1: ross killed, replaced by joey -> new pair: joey rachel -> line2: joey rachel
After day2: rachel killed, replaced by phoebe -> new pair: joey phoebe -> line3: joey phoebe
After day3: phoebe killed, replaced by monica -> new pair: joey monica -> line4: joey monica
After day4: monica killed, replaced by chandler -> new pair: joey chandler -> line5: joey chandler
Matches output.
Thus simulation straightforward: maintain two names (say a and b). For each day, read murdered (m) and replacement (r). We need to output current a b before processing? Actually we already output the initial state. Then for each day, we output the current pair before updating? Wait, in example: output lines: initial then after each day they show the pair after the murder (i.e., the state for the next day). But they call line i the pair for the i-th murder. So line1 for first murder: initial. Then after processing first murder, we get the pair for second murder. So algorithm: print current pair (a,b). Then for each event: read m, r. If m equals a, then a = r; else if m equals b, then b = r. Then print the updated (a,b). After loop we would have printed n+1 lines: initial and after each update. That yields exactly output.
But careful: After each event we should print the updated pair, which will be the pair for next murder. At the end after processing n events, we print the final pair (which is after nth event) which is pair for next murder (n+1-th line). That's consistent.
Thus solution: read a,b initial. Print them. Then loop i=1..n: read m and r. If m==a: a=r; else (must be b): b=r. Print a,b. Order doesn't matter, so we can print as is or sort? In examples they printed in order of replacement first? Actually output: after day1 they printed joey rachel (joey is the replacement, rachel is the survivor). They didn't sort alphabetically; just the natural order after replacement? In initial they printed ross rachel (as given). Then after update they printed joey rachel (so the new person first? They could just print the two names in any order. We can just print a and b as they are (maybe in the order we maintain). But careful: if we maintain a and b as the two names, after replacement we set the murdered one to the replacement. So order may change: For example, initial a=ross, b=rachel. On day1 murder ross: then we set a=joey, b=rachel unchanged. So printing a b yields joey rachel. That matches. On day2 murder rachel: currently a=joey, b=rachel. Since m==b, set b=phoebe. Then print a b yields joey phoebe. Works. So fine.
Thus problem is trivial.
Constraints: names length <=10, n <= 1000. Straightforward.
Edge cases: If murder victim is both? No, guaranteed to be one of the two. All names distinct initially and later? They say all names guaranteed to be distinct and consists of lowercase letters. At each step, the replacement is a new person? They said "all names are guaranteed to be distinct", likely meaning globally distinct: i.e., no name repeats in the entire process? Actually the input: "Also, all the names are guaranteed to be distinct and consists of lowercase English letters." That likely means that all names that appear (initial two, and all murder victims and replacements) are distinct from each other? But victims are from the pair, and replacement names are new and distinct from all others. So the pair always contains two distinct names. So we can rely on that.
Implementation: Use strings.
Thus answer: read a,b. print them. read n. for i in range(n): read m,r. if m==a: a=r else: b=r. print a,b.
That's it.
But perhaps they want the output for each day including after the last? Yes.
Thus problem is solved.
But let's double-check example2: initial: icm codeforces, output line1: icm codeforces. Then n=1: murder codeforces, replacement technex. So if a=icm, b=codeforces. m==b -> b=technex. Print a b => icm technex. Matches output.
All good.
Now we need to output the answer in the required format.
We must be careful about input format: the first line contains two names. Next line contains integer n. Then n lines each with two names. So use input().
We need to handle maybe trailing spaces? Not needed.
Thus final solution in Python:
def main():
import sys
data = sys.stdin.read().strip().split()
if not data: return
# first two are initial
a, b = data[0], data[1]
idx = 2
n = int(data[idx]); idx+=1
out = [f"{a} {b}"]
for i in range(n):
m = data[idx]; r = data[idx+1]; idx+=2
if m == a:
a = r
else:
b = r
out.append(f"{a} {b}")
sys.stdout.write("\n".join(out))
if name=="main": main()
Alternatively, use input() line by line.
Complexity: O(n).
Thus final answer.
But the user asked: "You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern." So we provide solution.
We need to output in any order: but we can maintain order as we like.
All set.
Now produce final answer with explanation and code. The problem describes a simple simulation. Initially, we have two names. For each day, one of the two names is murdered and replaced by a new name. The task is to output the pair of potential victims before each murder (i.e., the initial pair and the state after each replacement) and also the final pair after the last murder.
We can simulate this directly:
Store the two current names as a and b.
Print the initial a and b.
For each of the n days:
Read the murdered name m and the replacement r.
If m == a, set a = r; otherwise (since m is guaranteed to be one of the two) set b = r.
Print the updated a and b.
The order of the two names in the output can be arbitrary; the simulation naturally produces one valid order.
Time complexity: O(n)
Space complexity: O(1) apart from input/output.
Below is an implementation in Python:
python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
a, b = data[0], data[1] # initial pair
idx = 2
n = int(data[idx]); idx += 1
out_lines = [f"{a} {b}"]
for _ in range(n):
m, r = data[idx], data[idx + 1]
idx += 2
if m == a:
a = r
else:
b = r
out_lines.append(f"{a} {b}")
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()