# Long arith
from functools import reduce


MOD = 998_244_353


def moves(xs):
    target = 1
    diffs = []
    for x in xs[::-1]:
        diffs.append(abs(target - x))
        if x == 2:
            target = 4 - target
    return reduce(lambda r, d: r * 3 + d, diffs, 0)
    

def solve():
    n = int(input())
    xs = list(map(int, input().split()))
    ys = list(map(int, input().split()))
    print(abs(moves(ys) - moves(xs)) % MOD)


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