A JK flip-flop

Simulate a JK flip-flop. At each rising clock edge: J = 0 and K = 0 hold Q, J = 0 and K = 1 reset Q to 0, J = 1 and K = 0 set Q to 1, and J = 1 and K = 1 toggle Q.

Input

A line with the starting Q (0 or 1) and the number of clocks n (1 ≤ n ≤ 100). Then n lines, each with J and K for that clock.

Output

Q after each clock, separated by spaces.

Examples

Example 1 · Input
0 5
1 0
0 0
1 1
1 1
0 1
Expected output
1 1 0 1 0
Example 2 · Input
1 2
0 0
0 1
Expected output
1 0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep Q, starting from the given value.

Hint 2

At each clock: J=0 K=0 hold, J=0 K=1 reset to 0, J=1 K=0 set to 1, J=1 K=1 toggle.

Hint 3

Collect Q after every clock and print them separated by spaces.

Solution

Show one correct solution (try it yourself first)
q, n = map(int, input().split())
out = []
for _ in range(n):
    j, k = map(int, input().split())
    if j and k:
        q = 1 - q
    elif j:
        q = 1
    elif k:
        q = 0
    out.append(q)
print(*out)

Lesson: Latches and flip-flops →