#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#include <random>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
clock_t __my_start_clock = clock();
#else
#define eprintf(...)
#endif

#define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)
#define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)

#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")\n"
#define dbv(a) cerr << #a << " = "; for (auto xxxx: a) cerr << xxxx << " "; cerr << endl


using namespace std;

#ifdef LOCAL
static struct __timestamper {
    string what;
    __timestamper(const char* what) : what(what){};
    __timestamper(const string& what) : what(what){};
    ~__timestamper(){
        TIMESTAMPf("%s", what.data());
    }
} __TIMESTAMPER("end");
#else
struct __timestamper {};
#endif

typedef long long ll;
typedef long double dbl;

const int MOD = 998244353;

void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
}

int mul(int x, int y) {
    return 1LL * x * y % MOD;
}


int solve(int n, vector<int> a, vector<int> b) {
    while (n && a.back() == b.back()) {
        n--;
        a.pop_back();
        b.pop_back();
    }
    if (n == 0) return 0;

    auto rem = [&](int x, int y) {
        return 0 ^ 1 ^ 2 ^ x ^ y;
    };

    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(3, vector<int>(3)));
    for (int i = 1; i <= n; i++) {
        for (int fr = 0; fr < 3; fr++) {
            for (int to = 0; to < 3; to++) {
                if (fr == to) continue;
                int oth = rem(fr, to);

                int &val = dp[i][fr][to];
                if (abs(fr - to) > 1) {
                    add(val, dp[i - 1][fr][to]);
                    add(val, 1);
                    add(val, dp[i - 1][to][fr]);
                    add(val, 1);
                    add(val, dp[i - 1][fr][to]);
                } else {
                    add(val, dp[i - 1][fr][oth]);
                    add(val, 1);
                    add(val, dp[i - 1][oth][to]);
                }
            }
        }
    }

    vector<vector<vector<int>>> dp2(2, vector<vector<int>>(n + 1, vector<int>(3, -1)));

    function<int(int, int, int)> rec = [&](int which, int len, int where) -> int {
        auto &val = dp2[which][len][where];
        if (val == -1) {
            val = 0;
            int fr = which == 0 ? a[len - 1] : b[len - 1];
            int to = where;
            if (len != 0 && fr != to) {
                int oth = rem(fr, to);

                if (abs(fr - to) > 1) {
                    add(val, rec(which, len - 1, to));
                    add(val, 1);
                    add(val, dp[len - 1][to][fr]);
                    add(val, 1);
                    add(val, dp[len - 1][fr][to]);
                } else {
                    add(val, rec(which, len - 1, oth));
                    add(val, 1);
                    add(val, dp[len - 1][oth][to]);
                }
            }
        }
        return val;
    };

    int ans = 0;

    int fr = a.back();
    int to = b.back();
    int oth = rem(fr, to);
    if (abs(fr - to) == 1) {
        add(ans, rec(0, n - 1, oth));
        add(ans, 1);
        add(ans, rec(1, n - 1, oth));
    } else {
        add(ans, rec(0, n - 1, to));
        add(ans, 1);
        add(ans, dp[n - 1][to][fr]);
        add(ans, 1);
        add(ans, rec(1, n - 1, fr));
    }

    return ans;
}

void stress() {
    for (int it = 0;; it++) {
        mt19937 rnd(it);
        db(it);

        int n = rnd() % 10 + 1;
        vector<int> a(n), b(n);
        for (auto &x : a) x = rnd() % 3;
        for (auto &x : b) x = rnd() % 3;

        solve(n, a, b);
    }
}

int main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
//    stress();
#endif

    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        vector<int> a(n), b(n);
        for (int &x : a) scanf("%d", &x), x--;
        for (int &x : b) scanf("%d", &x), x--;

        printf("%d\n", solve(n, a, b));
    }


    return 0;
}