You are given three sequences: a_1, a_2, …, a_n; b_1, b_2, …, b_n; c_1, c_2, …, c_n.
For each i, a_i ≠ b_i, a_i ≠ c_i, b_i ≠ c_i.
Find a sequence p_1, p_2, …, p_n, that satisfy the following conditions:
p_i ∈ \{a_i, b_i, c_i}
p_i ≠ p_{(i mod n) + 1}.
In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements i,i+1 adjacent for i<n and also elements 1 and n) will have equal value.
It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence.
Input
The first line of input contains one integer t (1 ≤ t ≤ 100): the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 100): the number of elements in the given sequences.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100).
The third line contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 100).
The fourth line contains n integers c_1, c_2, …, c_n (1 ≤ c_i ≤ 100).
It is guaranteed that a_i ≠ b_i, a_i ≠ c_i, b_i ≠ c_i for all i.
Output
For each test case, print n integers: p_1, p_2, …, p_n (p_i ∈ \{a_i, b_i, c_i}, p_i ≠ p_{i mod n + 1}).
If there are several solutions, you can print any.
All possible correct answers to this test case are: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
In the second test case p = [1, 2, 1, 2].
In this sequence p_1 = a_1, p_2 = a_2, p_3 = a_3, p_4 = a_4. Also we can see, that no two adjacent elements of the sequence are equal.
In the third test case p = [1, 3, 4, 3, 2, 4, 2].
In this sequence p_1 = a_1, p_2 = a_2, p_3 = b_3, p_4 = b_4, p_5 = b_5, p_6 = c_6, p_7 = c_7. Also we can see, that no two adjacent elements of the sequence are equal.
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 have a problem: Given three sequences a_i, b_i, c_i, each of length n, and for each i, the three values are distinct. We need to pick for each i a value p_i from {a_i, b_i, c_i} such that p_i != p_{i+1} (cyclic: i mod n + 1). The constraints are small: n up to 100, t up to 100. It's guaranteed that a solution exists.
We need to output any valid p sequence.
Given the constraints, we could treat it as a constraint satisfaction problem with each position having three choices and a simple adjacency condition. Since n is small, we could even try greedy construction, or DP, or simply do a backtracking. But we need to guarantee that we find a solution quickly; given that existence is guaranteed, we need to design a simple method.
We must handle the cycle: p_n != p_1.
Observation: For each position i, the three values are all different. So each position has three distinct choices.
We need to pick a value for each i such that adjacent values differ.
This resembles a proper coloring of a cycle graph where each vertex has a list of three colors (all different per vertex), and it's always possible because of cycle length? But not exactly, because each vertex's list is independent. But maybe we can use the fact that all lists have size 3, and the graph is a cycle. It's known that a cycle is 2-colorable if even length, but with list coloring? There's a known theorem: A cycle is list 2-choosable if and only if all lists have size at least 2? Actually, the list coloring problem: For cycles, it's known that if all lists have size 2, it's not always colorable (e.g., odd cycle with same two colors). But if lists have size 3, then it's always colorable? Possibly because of Brooks' theorem or something? But there is a stronger result: any cycle is 2-choosable if it is bipartite? Actually, an even cycle is 2-choosable, an odd cycle is not 2-choosable (needs 3 colors). Since each vertex has 3 colors, it's always colorable because you can greedily assign? Not exactly: for odd cycle with each list = {1,2}, it's not colorable. But with lists of size 3, you can always choose a color? Could there be a counterexample? Let's think: For a cycle of length 3, each vertex has three distinct numbers, but they are not necessarily same across vertices. But are they arbitrary? Each vertex i has its own set S_i = {a_i, b_i, c_i} with all three distinct. Could there be a configuration where no proper coloring exists? They guarantee solution exists, so not.
But we need to construct one.
One simple approach: For i from 1 to n-1, choose p_i such that p_i != p_{i-1} (if i>1) and also ensure we don't paint ourselves into a corner at the end when closing the cycle. Since n is only 100, we can try backtracking/DFS. But we need to guarantee O(t * something). Since existence is guaranteed, maybe we can do a greedy algorithm that always works.
Let's attempt a constructive method.
Idea: Since each position has three colors, we can always pick p_i that is not equal to p_{i-1} (for i>1). The only difficulty is the last step: ensuring p_n != p_{n-1} and also p_n != p_1. But p_1 is already chosen, so we need to choose p_n from its three values such that it differs from both p_{n-1} and p_1. Since each position has three distinct values, there is at least one value that is not equal to p_{n-1} and not equal to p_1, as long as p_1 and p_{n-1} are not both the same and also occupy both of the two "forbidden" values? Wait: The set of forbidden values for p_n: {p_{n-1}, p_1}. If p_{n-1} != p_1, then there are at most two forbidden values. Since p_n has three choices, there is at least one available. If p_{n-1} == p_1, then there is only one forbidden value? Actually, if p_{n-1} = p_1, then the forbidden set = {p_{n-1}} (since p_1 equals that). But is that correct? p_n must not equal p_{n-1} and also must not equal p_1. If p_{n-1} == p_1, then both constraints forbid the same value, so only one forbidden value. So there are two other values allowed (since p_n has three distinct values). So in both cases, as long as we can choose p_1 such that we don't create an impossible situation? But the greedy forward pass for i=2..n-1 always picks a value != previous. That is trivial: for each i, from the three values, at least two are not equal to p_{i-1} (since p_{i-1} might be one of them, but not necessarily, but if p_{i-1} is not in the set? Actually p_{i-1} is some value from its own set, not necessarily among {a_i,b_i,c_i}. So p_{i-1} might be none of the available options at position i. In that case all three are allowed for p_i, but we just need to avoid conflict with previous. Since p_{i-1} could equal a_i, b_i, or c_i? It might be equal because the values across positions are not necessarily distinct; they could be same or different. The condition only says that within each i, the three are distinct. So it's possible that p_{i-1} equals one of a_i, b_i, c_i. So when choosing p_i, we must avoid p_{i-1}. So if p_{i-1} is one of the three options, then only two are allowed. If p_{i-1} is not in the three, then all three allowed. So we can always pick a p_i (since there are at least two allowed if p_{i-1} is in the set, else three). So we can greedily assign for i from 2 to n-1 easily.
Now the issue: At step i=n, we need to pick p_n from its three values such that p_n != p_{n-1} and p_n != p_1. However, it might happen that the three values at position n are such that two of them are exactly p_{n-1} and p_1, and the third is maybe also something? Actually there are three distinct values. If both p_{n-1} and p_1 are distinct and both belong to the set {a_n, b_n, c_n}, then the only remaining value is the third, which is allowed. So that's fine. If one of them is in the set, and the other is not, then at most one forbidden is in the set, leaving at least two allowed. If both are not in the set, all three allowed. So again there will always be an allowed choice? Wait, the forbidden values are p_{n-1} and p_1, regardless of whether they are in the set; p_n must not equal those values. But if p_{n-1} is not in the set, then p_n cannot equal it because p_n can only be one of its three values, and p_{n-1} is not among them, so that condition is automatically satisfied because p_n cannot be that value anyway. Similarly for p_1. So the actual restriction reduces to: p_n must be chosen from its set, but it cannot be equal to p_{n-1} if p_{n-1} equals one of its values; same for p_1. So the effective forbidden among its set are those values that happen to equal p_{n-1} or p_1. Since the set has three distinct values, we need at least one value that is not equal to either. As argued, if both p_{n-1} and p_1 are distinct and both in the set, that uses up two of the three values, leaving one allowed. If only one is in the set, then two allowed. If none, three allowed. So always at least one allowed. So it seems we can always complete if we do the greedy forward assignment? Is there any hidden catch? The catch is that the greedy forward assignment may affect the possibilities for p_n. But we argued that no matter what p_1 and p_{n-1} are, there will always be an allowed p_n. So if we can choose p_1 arbitrarily (as we need to start the process), we can just pick any p_1, then for i=2..n-1 pick any allowed value (like the first that works), and then for i=n pick an allowed value. This would always work? Let's test with some tricky cases.
Consider n=3. Position 1: choose any, say a1. Position 2: we need to pick p2 != p1. Among {a2,b2,c2}, at least two are not equal to p1, because if p1 equals one of them, then two others are not. So we can choose p2. Now p3: need != p2 and != p1. The set {a3,b3,c3} has three distinct values. Could it be that p1 and p2 are exactly the two values that are in the set, leaving the third as the only possible? That's fine as long as they are distinct. But what if p1 and p2 are equal? They cannot be equal because we enforced p2 != p1. So they are distinct. So the only potential problem is if {p1,p2} equals exactly two of the three values in S3, leaving third available; that's fine. If they are both not in S3, then any works. So indeed always possible. So for n=3 it works.
But what about n>3? The same reasoning. However, we must check the possibility that for some intermediate i, we might have only one allowed choice that later forces a dead end at the end? But our analysis for the final step seems independent of earlier choices except for p1 and p_{n-1}. Since we always can pick p_n as long as p_{n-1} and p1 are not both in the set and covering all three? Actually the condition is: there exists x in S_n such that x != p_{n-1} and x != p1. That's equivalent to: S_n not subset of {p_{n-1}, p1}. Since S_n size 3, if {p_{n-1}, p1} is a set of size 1, then S_n cannot be subset of it because S_n has three distinct elements. If {p_{n-1}, p1} has size 2, then S_n could possibly be exactly that set plus some other element? Actually subset would require that every element of S_n is either p_{n-1} or p1. But S_n has three elements. If the set {p_{n-1}, p1} has size 2, then S_n could be {p_{n-1}, p1, some third}, which is not a subset of {p_{n-1}, p1} because the third is not in the set. So S_n cannot be a subset of a 2-element set because it has three elements. So the only way S_n would be subset of {p_{n-1}, p1} is if {p_{n-1}, p1} has at least 3 distinct elements? But it can have at most 2 distinct because p_{n-1} and p1 are only two values. So S_n cannot be subset of {p_{n-1}, p1} because S_n has cardinality 3 > cardinality 2. Therefore, there always exists at least one element in S_n not equal to p_{n-1} or p1. Wait, is that always true? Consider the case where p_{n-1} and p1 are both in S_n, but S_n has three elements, so there is a third element. That third element is not equal to p_{n-1} nor p1. So yes, there is always at least one element different from both. But what if p_{n-1} and p1 are the same? Then {p_{n-1}, p1} is a set of size 1. S_n has three elements; it could possibly include that value, but then there are two other elements, so there exists at least one not equal to that value. So again there is an allowed value. So indeed, regardless of p1 and p_{n-1}, there always exists a choice for p_n. This is because |S_n| = 3, and the forbidden set has at most 2 distinct values. So there will always be at least one element not forbidden. Therefore, the greedy approach should always work.
But wait, there is a nuance: The choices at positions 2..n-1 must be made such that p_i is in S_i and p_i != p_{i-1}. That's always possible because S_i has three values, and p_{i-1} could be one of them; then two remain. So it's fine. So we can just greedily assign for i=2..n-1 arbitrarily (e.g., pick the first value from the list that is not equal to previous). Then assign p_n by picking any value that is not equal to p_{n-1} nor p1. That always works.
But is there any scenario where at some intermediate step i, there is no allowed value because p_{i-1} might equal all three? That's impossible because p_{i-1} is a single value, and S_i has three distinct values, they can't all be equal to p_{i-1} unless they are all equal to that value, but they must be distinct, so they can't all be same. So at most one of them can equal p_{i-1}. So there are at least 2 allowed values. So yes.
Thus the problem is trivial: Just start with any p_1 = a_1 (or any). Then for i from 2 to n-1: choose p_i as any element from {a_i,b_i,c_i} that is not equal to p_{i-1}. Finally for i = n: choose p_n as any element from {a_n,b_n,c_n} that is not equal to p_{n-1} and not equal to p_1.
That will always yield a valid sequence.
But is there any twist? Let's test with the provided examples.
Example 1:
n=3, a=[1,1,1], b=[2,2,2], c=[3,3,3].
Pick p1 = a1 = 1.
i=2: choose not equal to 1: options: b2=2 or c2=3. Pick first not 1: say b2=2.
i=3: need not equal p2=2 and not equal p1=1. Options: a3=1 (invalid), b3=2(invalid), c3=3 (valid). So p3=3. Output: 1 2 3 correct.
Example 2:
n=4, a=[1,2,1,2], b=[2,1,2,1], c=[3,4,3,4].
p1 = a1=1.
i=2: need !=1, options: a2=2? a2=2 !=1, so choose a2=2.
i=3: need !=2, options: a3=1 (allowed, 1!=2), so pick a3=1.
i=4: need != p3=1 and != p1=1? Actually p1=1, p3=1, both are 1, so forbidden set = {1}. Options: a4=2 (2!=1), b4=1 (invalid), c4=4 (4!=1). So pick a4=2. Output: 1 2 1 2. Valid.
Example 3:
n=7, given sequences. We'll test quickly: p1 = a1=1.
i=2: !=1. a2=3, b2=4, c2=2. All not 1? Actually 3,4,2 all !=1, pick a2=3.
i=3: !=3. S3: a3=3, b3=4, c3=2. Allowed: b3=4, c3=2 (a3=3 invalid). Pick b3=4.
i=4: !=4. S4: a4=1, b4=3, c4=2. Allowed: all (1,3,2) !=4? 1,3,2 all !=4. Pick a4=1.
i=5: !=1. S5: a5=1, b5=2, c5=4. Allowed: b5=2, c5=4. Pick b5=2.
i=6: !=2. S6: a6=1, b6=2, c6=4. Allowed: a6=1, c6=4. Pick c6=4 (first not 2).
i=7: need != p6=4 and != p1=1. S7: a7=1, b7=4, c7=2. Forbidden: 4 and 1. Allowed: c7=2. So output: 1,3,4,1,2,4,2. Wait expected output given: 1 3 4 3 2 4 2. In my assignment, p4=1, but expected shows 3? Let's check: In example output, p4=3. Did I make a mistake? The problem's output for test case 3: "1 3 4 3 2 4 2". So they have p4=3, p5=2, p6=4, p7=2. My assignment gave p4=1, p5=2, p6=4, p7=2. But does my assignment satisfy constraints? Check adjacency: p3=4, p4=1: 4!=1 ok. p4=1, p5=2: ok. p5=2, p6=4: ok. p6=4, p7=2: ok. p7=2, p1=1: ok. So mine is also valid. The problem says any solution is acceptable. So that's fine. So greedy works.
Example 4:
n=3, a=[1,2,1], b=[2,3,3], c=[3,1,2]
p1=a1=1.
i=2: need !=1, S2: a2=2, b2=3, c2=1. Allowed: a2=2, b2=3. Choose a2=2.
i=3: need !=2 and !=1. S3: a3=1, b3=3, c3=2. Forbidden: 2 and 1, allowed: b3=3. So output 1,2,3. Example output: 1 3 2 (they have different). Both are valid.
Example 5:
n=10, given sequences. Let's test quickly to ensure no hidden pitfall.
p1 = a1 = 1.
i=2: need !=1, S2: a2=1, b2=2, c2=3? Actually given:
a:1 1 1 2 2 2 3 3 3 1
b:2 2 2 3 3 3 1 1 1 2
c:3 3 3 1 1 1 2 2 2 3
So S2: a2=1, b2=2, c2=3. Need !=1: allowed: 2,3. Choose b2=2.
i=3: need !=2, S3: a3=1,b3=2,c3=3. Allowed:1,3. Choose a3=1.
i=4: need !=1, S4: a4=2,b4=3,c4=1. Allowed:2,3. Choose a4=2.
i=5: need !=2, S5: a5=2,b5=3,c5=1. Allowed:3,1. Choose b5=3.
i=6: need !=3, S6: a6=2,b6=3,c6=1. Allowed:2,1. Choose a6=2.
i=7: need !=2, S7: a7=3,b7=1,c7=2. Allowed:3,1. Choose a7=3.
i=8: need !=3, S8: a8=3,b8=1,c8=2. Allowed:1,2. Choose b8=1.
i=9: need !=1, S9: a9=3,b9=1,c9=2. Allowed:3,2. Choose a9=3.
i=10: need != p9=3 and != p1=1. S10: a10=1,b10=2,c10=3. Forbidden:3 and 1, allowed: b10=2. So output: 1,2,1,2,3,2,3,1,3,2? Wait let's list: p1=1, p2=2, p3=1, p4=2, p5=3, p6=2, p7=3, p8=1, p9=3, p10=2. Check adjacency: p9=3, p10=2 ok, p10=2, p1=1 ok. So valid. Example output: 1 2 3 1 2 3 1 2 3 2 (different). So fine.
Thus the greedy method seems robust.
But is there any case where this fails? Let's think deeply. The crucial point: At step i (2 to n-1), we need p_i != p_{i-1}. Since p_{i-1} is fixed, S_i contains three distinct values. Could it be that all three values equal p_{i-1}? No, because they are distinct, they can't all equal the same value. So at most one equals p_{i-1}. So there are at least two values not equal to p_{i-1}. So we can always pick one. So no problem.
At final step n: Need p_n != p_{n-1} and p_n != p_1. There are at most two forbidden values (distinct or same). S_n has three distinct values. Could it be that all three values are exactly the two forbidden values? That would require that S_n is a subset of {p_{n-1}, p_1} and |S_n| = 3. But {p_{n-1}, p_1} has at most two distinct elements. So S_n cannot be a subset, because a set of size 3 cannot be a subset of a set of size ≤2. However, consider the possibility that p_{n-1} and p_1 are the same and the set {p_{n-1}} = {x}. Then S_n could be {x, y, z} where y and z are different. That's still not a subset of {x} unless y and z also equal x, which they cannot because distinct. So S_n cannot be wholly forbidden. There will always be at least one element that is not equal to either p_{n-1} or p_1. So final step also always possible.
Thus the greedy algorithm is correct.
So we can implement simply.
But wait, there's a nuance: When we pick p_i for i between 2 and n-1, we have a choice. Could our choice lead to p_{n-1} being something that makes the final step impossible? Our final step analysis shows it's always possible irrespective of p_{n-1} and p_1. So no.
Hence solution: For each test case, read n, arrays a, b, c. Initialize p[0] = a[0] (or any, e.g., a1). For i from 1 to n-2 (0-indexed: i=1..n-2), pick p[i] among a[i], b[i], c[i] that is != p[i-1]; if multiple, pick e.g., the first that works. Finally, for i=n-1 (last index), pick p[n-1] among a[n-1], b[n-1], c[n-1] that is != p[n-2] and != p[0].
Edge Cases: n=3. Then the loop for i=1..n-2 would be i=1..1 (since n-2=1). So we assign p[1] from a[1],b[1],c[1] with condition p[1] != p[0]. Then we assign p[2] from a[2],b[2],c[2] with condition p[2] != p[1] and p[2] != p[0]. Works.
Check: n=4, loop i=1..2 (i=1,2). So assign p[1], p[2] based only on previous. Then p[3] based on p[2] and p[0]. Good.
Implementation details: For each position i, we have three candidates: a_i, b_i, c_i. Since they are distinct, we can test each.
We need to be careful about 1-indexing vs 0-indexing.
Complexity: O(t * n).
Thus solution is trivial.
But wait, the problem statement says "It can be proved that in the given constraints solution always exists." So indeed they want to assure us that a solution exists, but perhaps they intend a more elaborate reasoning, but the greedy method we described is valid. However, we should double-check if there is any hidden condition: The sequences a_i, b_i, c_i are given, but we can choose p_i from them. The condition p_i != p_{i+1} is required. The greedy algorithm uses only local decisions and the final global check. As argued, it always works. So we can implement.
But I recall that there might be a known Codeforces problem: "Three Sequences" maybe? I think I've seen this problem before, and indeed the solution is greedy: pick p1 = a1, then for i=2..n-1 choose any value not equal to previous, and for i=n choose any value not equal to previous and first. So that's likely the intended solution.
But wait: There is a subtle point: For i from 2 to n-1, we must ensure that we don't inadvertently cause a situation where the only possible value for p_n is forced but maybe conflict with p_1? As argued, it's fine. However, let's test a tricky constructed case to see if there is any scenario where after choosing greedy, the only available value for p_n is ... but we have at least one. But could it be that the only available value for p_n is something that also violates the condition p_n != p_{n-1}? But we are picking from the set of values that satisfy both conditions, so we choose that. So if exactly one value is allowed, we choose it.
Thus no issue.
But is there any case where at i from 2 to n-1, we might have to choose a value that later forces p_{n-1} to be equal to p_1? That wouldn't be a problem; final step still works.
So greedy works.
Let's attempt a formal proof: By construction, p_1 is chosen arbitrarily (say a_1). For i=2 to n-1: Since the set S_i has three elements, and at most one of them equals p_{i-1} (since they're distinct), there exists at least one element x in S_i with x != p_{i-1}; choose any such x as p_i. For i=n: We need to choose p_n from S_n such that p_n != p_{n-1} and p_n != p_1. The set of forbidden values F = {p_{n-1}, p_1} has size at most 2. Since |S_n| = 3, S_n \ F is non-empty. Thus we can choose p_n accordingly. Therefore the constructed sequence satisfies all constraints.
Thus solution exists and is found in linear time.
Implementation: For each test case, read n, then three lists a, b, c. We'll store them in arrays of size n. Then:
p = [0]*n
p[0] = a[0] # any works
for i in range(1, n-1):
# choose p[i] from a[i], b[i], c[i] such that != p[i-1]
if a[i] != p[i-1]:
p[i] = a[i]
elif b[i] != p[i-1]:
p[i] = b[i]
else: # c[i] must be != p[i-1] because at most one equals, and if a and b both equal? They can't both equal because a_i,b_i,c_i are all distinct, so if a[i]==p[i-1] and b[i]==p[i-1], that would mean a[i]==b[i] which contradicts distinctness. So indeed at most one can equal p[i-1]. So the else ensures c[i] != p[i-1].
p[i] = c[i]
Now for i = n-1:
for candidate in [a[n-1], b[n-1], c[n-1]]:
if candidate != p[n-2] and candidate != p[0]:
p[n-1] = candidate
break
Note: At the final step, we should ensure we break after assigning. Since it's guaranteed, we can just check.
Alternatively, we can combine the condition: if a[n-1] not in {p[n-2], p[0]} then choose that, else if b[n-1] not in that set, else c[n-1] (which must work).
But careful: It is possible that a[n-1] and b[n-1] both are forbidden? Let's consider: Forbidden set has two values. Could it be that a and b are exactly those two? Then c must be different from both, so c is allowed. So the else case will assign c.
So final step similar structure.
Thus we can output p.
Edge Cases: n=3, we have i=1 only (since n-2=1). So p[1] assigned by loop. Then final step i=n-1 (2) assign with both conditions.
If n=3 and we assign p[1] by loop, we need to ensure that p[2] assignment works. It will.
Double-check for n=3 when p[1] assignment might accidentally pick a value that causes p[2] to have no allowed value? But as argued, it always has at least one. Since p[2] can be chosen from three values not equal to both p[1] and p[0], that is always possible because set size 3 > max forbiddens 2.
Thus good.
So code is straightforward.
Now consider if p[0] was chosen as a[0]; but what if a[0] equals something that later leads to? Could that cause any issue? No, because all steps are flexible.
However, I want to consider a pathological case: Suppose n=3, a1=1, b1=2, c1=3; a2=1, b2=2, c2=3; a3=1, b3=2, c3=3. Our method: p1=1. For i=2: choose !=1 => choose a2=1? cannot because equals, b2=2 is not 1, so p2=2. Then p3: need !=2 and !=1, choose c3=3. Works.
Another case: p1=1. Suppose at i=2: we choose p2=3 (if we pick the first, but we might pick a2=1? we would skip that if it's equal to p1, so we pick b2=2 or c2=3. In our code, we check a[i] first, then b, then c. So if a[i] != p[i-1] we pick it, else if b[i] != p[i-1] we pick b, else c. That's deterministic. It's fine.
Now think about n=4. Suppose we have a situation where p1 = 1, and at i=2 we pick p2=2. i=3 we pick p3 =? Must !=2. Suppose S3 = {2,1,3}. Then a3=2 equals p2 so we skip, b3=1 !=2, so we pick b3=1. So p3=1. Then final: p4: need !=1 and !=1 (both p3 and p1 are 1) so forbiddens {1}. S4 = {2,1,?} maybe {2,1,3}. So choose a4=2 if not 1, etc. So works.
Thus solution correct.
But wait: There is a nuance: In the loop for i from 1 to n-2, we only enforce p_i != p_{i-1}. However, what about the condition that p_i should also not equal p_{i+1}? That will be enforced when we choose p_{i+1}. So it's fine.
Thus solution is straightforward.
Potential pitfalls: The input reading: t up to 100, each n up to 100, values up to 100. So no issues.
I will now craft the final answer: Provide explanation and code in Python.
But before finalizing, we should ensure that the algorithm indeed always works under the constraints. Let's attempt to construct a case that might break it: e.g., n=4, S1: {1,2,3}, choose p1=1. S2: {1,4,5}, then p2 must !=1 so choose 4 or 5. Suppose we choose p2=4 (first not 1). S3: {4,5,6}, p3 must !=4, choose 5 or 6; suppose we choose p3=5 (first not 4). S4: {1,5,6}? Now p4 must !=p3=5 and !=p1=1, allowed: 6 only. So output works.
What if S4: {1,4,5}? Then p4 must !=5 and !=1, so cannot be 5, cannot be 1, but can be 4? Check: 4 !=5 and 4 !=1, so allowed. So works.
What if S4: {1,5,?} actually need three distinct: e.g., {1,5,5} not allowed. So ok.
Now consider scenario where the greedy choice at i=2 could cause p3 to have only one choice that leads to p4 being impossible? But as argued, p4 always possible regardless of p3. So no.
Thus the solution is robust.
One more subtlety: The condition p_i != p_{i+1} is for all i, including i=n (with n+1 = 1). We satisfied that with final condition p_n != p_1. Yes.
Thus implementation is done.
Now code:
def solve():
import sys
data = sys.stdin.read().strip().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
a = [int(next(it)) for __ in range(n)]
b = [int(next(it)) for __ in range(n)]
c = [int(next(it)) for __ in range(n)]
p = [0]*n
p[0] = a[0] # choose first arbitrarily
for i in range(1, n-1):
# choose p[i] != p[i-1]
if a[i] != p[i-1]:
p[i] = a[i]
elif b[i] != p[i-1]:
p[i] = b[i]
else:
p[i] = c[i]
# for last
i = n-1
if a[i] != p[i-1] and a[i] != p[0]:
p[i] = a[i]
elif b[i] != p[i-1] and b[i] != p[0]:
p[i] = b[i]
else:
p[i] = c[i] # guaranteed
out_lines.append(' '.join(map(str, p)))
sys.stdout.write('\n'.join(out_lines))
Now test with given examples.
Let's simulate quickly in mind: Example 1 gives [1,2,3] (since p1=1; i=1 (index1): a1? Actually indices: a[1] is second element? careful: In zero-indexed: a[0]=1, a[1]=1, a[2]=1; b[0]=2, b[1]=2, b[2]=2; c[0]=3, c[1]=3, c[2]=3. p[0]=a[0]=1. Loop i=1 (i from 1 to n-2 = 1): i=1< n-1? n-1=2, so i goes up to 1 inclusive? Actually range(1, n-1) in Python is from 1 to n-2 inclusive, because range stop is exclusive. For n=3, range(1,2) yields i=1. So i=1: check a[1]=1 != p[0]=1? false; then check b[1]=2 !=1 true, so p[1]=2. Then for last i=2: a[2]=1 != p[1]=2? true, but also need != p[0]=1? a[2]=1 equals p[0]=1, so condition fails. So we go to elif: b[2]=2 != p[1]=2? false (2==2), check also != p[0]=1? b[2]=2 !=1 true but first condition fails (b[2] != p[1] is false, so we skip to else? Actually our code: if a[i] != p[i-1] and a[i] != p[0]: ... elif b[i] != p[i-1] and b[i] != p[0]: ... else: c[i]. So for a[2]=1: condition a[2] != p[1] (2) is true? 1 != 2, so first part true; but we also require a[2] != p[0] (1) which is false (1 !=1 false). So overall false. Then b[2]=2: check b[2] != p[1] (2) false, so skip. else: c[2]=3, assign p[2]=3. Good.
Example 3: as earlier gave [1,3,4,1,2,4,2] but we need to test with code: a = [1,3,3,1,1,1,1]? Wait from input: line: "1 3 3 1 1 1 1" is a, b: "2 4 4 3 2 2 4", c: "4 2 2 2 4 4 2". So a: [1,3,3,1,1,1,1]; b: [2,4,4,3,2,2,4]; c: [4,2,2,2,4,4,2]. p[0]=1. i=1: a[1]=3 !=1 -> p[1]=3. i=2: a[2]=3 != p[1]=3? false; b[2]=4 !=3? true -> p[2]=4. i=3: range goes to i=3? n=7, n-2=5, so i from 1 to 5 inclusive: i=1,2,3,4,5. Continue: i=3: a[3]=1 != p[2]=4 -> p[3]=1. i=4: a[4]=1 != p[3]=1? false; b[4]=2 !=1 -> p[4]=2. i=5: a[5]=1 != p[4]=2 -> p[5]=1. Now last i=6: check a[6]=1 != p[5]=1? false (first condition fails), b[6]=4 !=1? true and b[6]!=p[0]=1? 4 !=1 true -> p[6]=4. So we got p = [1,3,4,1,2,1,4]. Wait we have p[5]=1? Actually check: i=5 is index 5 (6th element? Let's index 0..6: i=0 first, i=1 second, i=2 third, i=3 fourth, i=4 fifth, i=5 sixth, i=6 seventh. In my manual above: i=4 gave p[4]=2 (fifth element), i=5 gave p[5]=1 (sixth). Then final i=6 gave p[6]=4 (seventh). So sequence: p0=1, p1=3, p2=4, p3=1, p4=2, p5=1, p6=4. Check adjacency: p5=1, p6=4 ok; p6=4, p0=1 ok; p4=2, p5=1 ok; p3=1, p4=2 ok; p2=4, p3=1 ok; p1=3, p2=4 ok; p0=1, p1=3 ok. So valid. So any solution is okay.
Thus code works.
Potential nuance: If we had chosen p[5] as something else? Actually our greedy method for i=5 (which is n-2? For n=7, n-2=5, so i=5 is within the loop. We assigned p[5] based only on p[4] (not equal). p[4]=2, a[5]=1 !=2, so we set p[5]=1. That's fine.
But check final step: p[6] must != p[5]=1 and != p[0]=1. Forbidden set {1}. b[6]=4 allowed, so we pick b[6]=4. Works.
Thus all good.
Now confirm with example 5 using code mental: Already tested earlier similar but not exactly same as code. But it will produce some valid sequence.
Thus solution is correct.
Now some might ask: Could the choice of p1 be such that the final step has a candidate? We argued that any p1 works. However, if we pick p1 as something that later cause the loop to get stuck? Not possible. So p1 = a1 is fine. Alternatively, we could pick p1 arbitrarily from the three, but a1 is simplest.
But what if a1 is such that at i=2 there is no choice? But there always is, as argued. So fine.
Thus we can output solution.
Now, sometimes Codeforces problems with this structure have additional twist: The three values are distinct, but they may not be all distinct across positions? That's fine.
Thus we can finalize.
Now to produce the answer: Provide a clear explanation of the greedy algorithm, proof of correctness, and the code.
Let's structure the answer accordingly.
Will also mention that we can simply pick p1 = a1, then for i=2..n-1 pick any value not equal to previous, and for i=n pick any value not equal to previous and first.
Proof: At each step, the available set has size 3, and the forbidden set has size at most 1 (for steps 2..n-1) or at most 2 (for last step), so there is always a valid choice.
Edge case: n=3, the step 2 is also the last? But we treat separately.
We'll implement.
We'll also note that we can avoid using extra condition for n=3, because loop range(1, n-1) works fine (range(1,2) gives i=1) and then final step.
Thus final answer.
Now check if there is any issue with reading input: The example input has multiple test cases, each with three lines after n. So reading as described works.
Time complexity: O(t * n). Memory: O(n).
We'll produce Python code.
But maybe we can also implement in other languages, but we're here to produce final answer in text.
Now, to be thorough, we should consider if there is any scenario where the greedy approach fails for some reason like the loop for i from 1 to n-2 might produce p_i that is not allowed for some later step? As argued, final step always works, but could there be a scenario where the loop itself fails at some i? The only requirement is p_i != p_{i-1}. We guarantee that by choosing a value not equal to p_{i-1}. As argued, at most one of the three equals p_{i-1}, so there are at least two options, so we can always choose. So no fail.
But wait: The loop for i from 1 to n-2: At i=1, p_{i-1}=p0. That's fine. At i=2, p_{i-1}=p1, we have at least two options, choose one. So no issue.
Thus algorithm is correct.
Now we can write the final answer.
One more subtlety: The problem statement says: "p_i ∈ {a_i, b_i, c_i}". It does not say that the three numbers are distinct, but it says "For each i, a_i ≠ b_i, a_i ≠ c_i, b_i ≠ c_i." So yes, they are distinct per index. So the sets S_i have size exactly 3 distinct elements.
Thus solution holds.
Now let's produce final answer with explanation and code.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
c = [int(next(it)) for _ in range(n)]
code
p = [0] * n
# first element: any of the three works, e.g. a1
p[0] = a[0]
# positions 2 .. n-1 (0‑based indices 1 .. n-2)
for i in range(1, n - 1):
# choose a value different from the previous one
if a[i] != p[i-1]:
p[i] = a[i]
elif b[i] != p[i-1]:
p[i] = b[i]
else:
# c[i] must be different because all three are distinct
p[i] = c[i]
# last position n (0‑based index n-1)
i = n - 1
if a[i] != p[i-1] and a[i] != p[0]:
p[i] = a[i]
elif b[i] != p[i-1] and b[i] != p[0]:
p[i] = b[i]
else:
p[i] = c[i] # guaranteed to satisfy both conditions
out_lines.append(' '.join(map(str, p)))
sys.stdout.write('\n'.join(out_lines))