from functools import reduce


MOD = 998_244_353


def madd(a, b, c):
    return (a * b + c) % MOD


def sub(a, b):
    return (a - b + MOD) % MOD


def diffs(xs):
    target = 1
    result = []
    for x in xs[::-1]:
        result.append(abs(target - x))
        if x == 2:
            target = 4 - target
    return result
    

def moves(diffs):
    return reduce(lambda r, d: madd(r, 3, d), diffs, 0)


def solve():
    n = int(input())
    xs = diffs(list(map(int, input().split())))
    ys = diffs(list(map(int, input().split())))
    d = sub(moves(ys), moves(xs)) 
    print(d if xs < ys else sub(MOD, d))


for _ in range(int(input())):
    solve()
