Latch or flip-flop?

A D latch follows D for as long as the clock is high. An edge-triggered D flip-flop looks at D only at the moment the clock rises. The same input signals give different outputs.

What to build

Simulate both devices on the same signals and print what each one stores.

Requirements

  • Both start at 0. The clock before the first sample is 0, so a first sample with clock 1 is a rising edge.
  • Line 1: the latch's Q after each sample. Line 2: the flip-flop's Q after each sample.
  • Write the values as one string of 0 and 1 with no spaces.

Input

The first line has the number of samples s. Then s lines, each with the clock and D, as two bits.

Output

Two lines of s bits each, as described.

Constraints

1 ≤ s ≤ 30.

Example

Input:

6
0 1
1 1
1 0
1 1
0 0
1 0

Output:

010110
011110

Why it works

A latch is level-sensitive: all the time the clock is 1, any change of D goes straight to Q. A flip-flop is edge-triggered: it ignores D except at the instant of the rising edge, so a change of D in the middle of a high clock does not pass. That is why flip-flops, and not latches, are used to build registers and counters.

Examples

Example 1 · Input
6
0 1
1 1
1 0
1 1
0 0
1 0
Expected output
010110
011110
Example 2 · Input
3
1 1
0 0
1 1
Expected output
111
111

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

Hints

Hint 1

Both start with Q = 0. A time step is one sample of the clock and D.

Hint 2

The D latch is transparent while the clock is 1: when clk is 1, Q takes D; when clk is 0, Q holds.

Hint 3

The edge-triggered D flip-flop captures D only when the clock goes from 0 to 1, so compare with the clock of the step before (the clock before the first step is 0).

Solution

Show one correct solution (try it yourself first)
s = int(input())
latch = ff = 0
prev = 0
latch_out = ff_out = ""
for _ in range(s):
    clk, d = map(int, input().split())
    if clk == 1:
        latch = d
    if prev == 0 and clk == 1:
        ff = d
    prev = clk
    latch_out += str(latch)
    ff_out += str(ff)
print(latch_out)
print(ff_out)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Latches and flip-flops →