#include<bits/stdc++.h>
using namespace std;
set<long long>s;
long long mo=998244353;
long long k,n,a[1000000],b[1000000],pos[1000000];
int main(){
	long long t;
	cin>>t;
	for(long long o=1;o<=t;o++){
		cin>>n>>k;
		long long ans=1;
		for(long long i=1;i<=n;i++)cin>>a[i],pos[a[i]]=i;
		for(long long i=1;i<=k;i++)cin>>b[i],s.insert(pos[b[i]]);
		for(long long i=1;i<=k;i++){
			long long cnt=2;
			if(s.count(pos[b[i]]+1)||pos[b[i]]+1>n)cnt--;
			if(s.count(pos[b[i]]-1)||pos[b[i]]-1<=0)cnt--;
			ans=(ans*cnt)%mo;
			s.erase(pos[b[i]]);
			//cout<<pos[b[i]]<<' '<<cnt<<endl;
		}
		cout<<ans<<endl;
		s.clear();
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e5 + 10, mod = 998244353;
int a[N], b[N];
bool st[N];
int pos[N];

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
			pos[a[i]] = i;
		}
		for (int j = 0; j < m; j++) {
			scanf("%d", &b[j]);
			st[b[j]] = true;
		}
		ll ans = 1;
		for (int i = 0; i < m; i++) {
			st[b[i]] = false;
			ll t = 0;
			if (pos[b[i]] - 1 >= 0 && !st[a[pos[b[i]] - 1]]) t++;
			if (pos[b[i]] + 1 < n && !st[a[pos[b[i]] + 1]]) t++;
			ans = ans * t % mod;
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e5 + 10, mod = 998244535;
int a[N], b[N];
bool st[N];
int pos[N];

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
			pos[a[i]] = i;
		}
		for (int j = 0; j < m; j++) {
			scanf("%d", &b[j]);
			st[b[j]] = true;
		}
		ll ans = 1;
		for (int i = 0; i < m; i++) {
			st[b[i]] = false;
			ll t = 0;
			if (pos[b[i]] - 1 >= 0 && !st[a[pos[b[i]] - 1]]) t++;
			if (pos[b[i]] + 1 < n && !st[a[pos[b[i]] + 1]]) t++;
			ans = ans * t % mod;
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
const int mod=998244353;
typedef long long LL;
int f[N];
int a[N],b[N];
LL quickpow(LL x,LL k){
	LL res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1;
		x=x*x%mod;
	}
	return res;
}
int inv2=quickpow(2,mod-2);
int main()
{
	ios::sync_with_stdio(0);
	int T; cin>>T;
	while(T--){
		int n,k; cin>>n>>k;
		int cnt=0;
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<k;i++) cin>>b[i],f[b[i]]=i+1;

		int last=0,ff=1;
		LL res=1;
		for(int i=0;i<n;i++){
			if(!ff) break;
			if(f[a[i]]){
				if(!last)
					last=i;
				if(i==1){
					if(f[a[i-1]] && f[a[i]]>f[a[i-1]])
						ff=0;
				}
				else if(i==n-1 &&i-1>=0){
					if(f[a[i-1]] && f[a[i]]<f[a[i-1]])
						ff=0;
				}
				else if(i && f[a[i-1]] && f[a[i+1]]){
					if(f[a[i]]<f[a[i-1]] && f[a[i]]<f[a[i+1]])
						ff=0;
				}
			}
			else{
				if(last){
					last=0;
					cnt++;
					res=res*2%mod;
				}
			}
		}
//		if(f[a[0]])
//			cnt--,res=res*inv2%mod;
		if(ff)
			cout<<res<<endl;
		else
			cout<<0<<endl;

		for(int i=0;i<k;i++) f[b[i]]=0;
	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int inf=2e5+5;
const int N=2e5+5;
int t;
bool vis[N];
int a[N];
int b[N];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        a[0]=a[n+1]=0;
        b[0]=inf;
        vis[0]=1;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            //pos[a[i]]=i;
            vis[i]=0;
        }
        for(int i=1;i<=k;++i)
        {
            int x;
            scanf("%d",&x);
            b[x]=i;
            vis[x]=1;
        }
        bool flag=0;
        for(int i=1;i<=n;++i)
        {
            if(vis[a[i]]&&vis[a[i-1]]&&vis[a[i+1]]&&b[a[i]]<b[a[i-1]]&&b[a[i]]<b[a[i+1]])
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            puts("0");
            continue;
        }
        ll ans=1;
        int len=0;
        int pos=1;
        if(vis[a[1]])
        for(int i=1;i<=n;++i)
        {
            if(!vis[a[i]])
            {
                len=0;
                pos=i;
                break;
            }
            else
            len++;
        }
        for(int i=pos;i<=n;++i)
        {
            if(!vis[a[i]]&&len)
            {
                len=0;
                ans<<=1;
                ans%=mod;
            }
            else
            if(vis[a[i]])
            {
                len++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
const int mod=998244353;
typedef long long LL;
int f[N];
int a[N],b[N];
LL quickpow(LL x,LL k){
	LL res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1;
		x=x*x%mod;
	}
	return res;
}
int main()
{
	ios::sync_with_stdio(0);
	int T; cin>>T;
	while(T--){
		int n,k; cin>>n>>k;
		int cnt=0;
		for(int i=0;i<n;i++){
            cin>>a[i];
		}
		for(int i=0;i<k;i++) cin>>b[i],f[b[i]]=i+1;

		int last=0,ff=1;
		for(int i=0;i<n;i++){
			if(!ff) break;
			if(f[a[i]]){
				if(!last)
					last=i;
				if(i==1){
					if(f[a[i-1]] && f[a[i]]>f[a[i-1]])
						ff=0;
				}
				else if(i==n-1 &&i-1>=0){
					if(f[a[i-1]] && f[a[i]]<f[a[i-1]])
						ff=0;
				}
				else if(i && f[a[i-1]] && f[a[i+1]]){
					if(f[a[i]]<f[a[i-1]] && f[a[i]]<f[a[i+1]])
						ff=0;
				}
			}
			else{
				if(last){
					last=0;
					cnt++;
				}
			}
		}
		if(f[a[0]])
			cnt--;
		if(ff)
		cout<<quickpow(2,cnt)<<endl;
		else
			cout<<0<<endl;

		for(int i=0;i<k;i++) f[b[i]]=0;
	}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
const int mod=998244353;
typedef long long LL;
int f[N];
int a[N],b[N];
LL quickpow(LL x,LL k){
	LL res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1;
		x=x*x%mod;
	}
	return res;
}
int inv2=quickpow(2,mod-2);
int main()
{
	ios::sync_with_stdio(0);
	int T; cin>>T;
	while(T--){
		int n,k; cin>>n>>k;
		int cnt=0;
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<k;i++) cin>>b[i],f[b[i]]=i+1;

		int last=0,ff=1;
		LL res=1;
		for(int i=0;i<n;i++){
			if(!ff) break;
			if(f[a[i]]){
				if(!last)
					last=i;
				if(i==1){
					if(f[a[i-1]] && f[a[i]]>f[a[i-1]])
						ff=0;
				}
				else if(i==n-1 &&i-1>=0){
					if(f[a[i-1]] && f[a[i]]<f[a[i-1]])
						ff=0;
				}
				else if(i && f[a[i-1]] && f[a[i+1]]){
					if(f[a[i]]<f[a[i-1]] && f[a[i]]<f[a[i+1]])
						ff=0;
				}
			}
			else{
				if(last){
					last=0;
					cnt++;
					res=res*2%mod;
				}
			}
		}
		if(f[a[0]])
			cnt--,res=res*inv2%mod;
		if(ff)
			cout<<res<<endl;
		else
			cout<<0<<endl;

		for(int i=0;i<k;i++) f[b[i]]=0;
	}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
const int mod=998244353;
typedef long long LL;
int f[N];
int a[N],b[N];
LL quickpow(LL x,LL k){
	LL res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1;
		x=x*x%mod;
	}
	return res;
}
int inv2=quickpow(2,mod-2);
int main()
{
	ios::sync_with_stdio(0);
	int T; cin>>T;
	while(T--){
		int n,k; cin>>n>>k;
		int cnt=0;
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<k;i++) cin>>b[i],f[b[i]]=i+1;

		int last=0,ff=1;
		LL res=1;
		for(int i=0;i<n;i++){
			if(!ff) break;
			if(f[a[i]]){
				if(!last)
					last=i;
				if(i==1){
					if(f[a[i-1]] && f[a[i]]>f[a[i-1]])
						ff=0;
				}
				else if(i==n-1 &&i-1>=0){
					if(f[a[i-1]] && f[a[i]]<f[a[i-1]])
						ff=0;
				}
				else if(i && f[a[i-1]] && f[a[i+1]]){
					if(f[a[i]]<f[a[i-1]] && f[a[i]]<f[a[i+1]])
						ff=0;
				}
			}
			else{
				if(last){
					last=0;
					cnt++;
					res=res*2%mod;
				}
			}
		}
		if(f[a[0]] && cnt)
			cnt--,res=res*inv2%mod;
		if(ff)
			cout<<res<<endl;
		else
			cout<<0<<endl;

		for(int i=0;i<k;i++) f[b[i]]=0;
	}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
const int mod=998244353;
typedef long long LL;
int f[N];
int a[N],b[N];
LL quickpow(LL x,LL k){
	LL res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1;
		x=x*x%mod;
	}
	return res;
}
int main()
{
	int T; cin>>T;
	while(T--){
		int n,k; cin>>n>>k;
		int cnt=0;
		for(int i=0;i<n;i++){
            cin>>a[i];
		}
		for(int i=0;i<k;i++) cin>>b[i],f[b[i]]=i+1;

		int last=0,ff=1;
		for(int i=0;i<n;i++){
			if(!ff) break;
			if(f[a[i]]){
				if(!last)
					last=i;
				if(i==1){
					if(f[a[i-1]] && f[a[i]]>f[a[i-1]])
						ff=0;
				}
				else if(i==n-1 &&i-1>=0){
					if(f[a[i-1]] && f[a[i]]<f[a[i-1]])
						ff=0;
				}
				else if(i && f[a[i-1]] && f[a[i+1]]){
					if(f[a[i]]<f[a[i-1]] && f[a[i]]<f[a[i+1]])
						ff=0;
				}
			}
			else{
				if(last){
					last=0;
					cnt++;
				}
			}
		}
		if(f[a[0]])
			cnt--;
		if(ff)
		cout<<quickpow(2,cnt)<<endl;
		else
			cout<<0<<endl;

		for(int i=0;i<k;i++) f[b[i]]=0;
	}

    return 0;
}


#include <bits/stdc++.h>

#define fo(i, x, y) for (int i = x; i <= (y); ++i)
#define fd(i, x, y) for (int i = x; i >= (y); --i)
using namespace std;

typedef long long ll;

const int maxn = 2e5 + 5;
const ll mod = 998244353;

int n, k;
int a[maxn], b[maxn], s[maxn], pos[maxn], nxt[maxn], pre[maxn];
bool flag[maxn];

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0', ch = getchar();
	return res * p;
}

inline int sum(int l, int r)
{
	if (l - 1 <= 0) return s[r];
	assert(r >= 0);
	return s[r] - s[l - 1];
}

void work()
{
	n = getint(); k = getint();
	fo(i, 0, n + 1) s[i] = 0;
	fo(i, 1, n) a[i] = getint(), pos[a[i]] = i, flag[i] = 1;
	fo(i, 1, k)
	{
		b[i] = getint();
		flag[pos[b[i]]] = 0;
	}
	flag[0] = flag[n + 1] = 0;
	fo(i, 1, n + 1) s[i] = s[i - 1] + flag[i];
	int lst = -1;
	fo(i, 0, n + 1) nxt[i] = pre[i] = 0;
	fo(i, 0, n + 1)
		if (!sum(i, i))
		{
			if (~lst)
			{
				nxt[lst] = i;
				pre[i] = lst;
			}
			lst = i;
		}
	ll ans = 1;
	fo(i, 1, k)
	{
		(ans *= (sum(pre[pos[b[i]]] + 1, pos[b[i]] - 1) > 0) + (sum(pos[b[i]] + 1, nxt[pos[b[i]]] - 1) > 0)) %= mod;
		if (!ans) break;
		nxt[pre[pos[b[i]]]] = nxt[pos[b[i]]];
		pre[nxt[pos[b[i]]]] = pre[pos[b[i]]];
	}
	printf("%lld\n", ans);
}

int main()
{
	int T;
	T = getint();
	while (T--) work();
	return 0;
}


#include <bits/stdc++.h>

#define fo(i, x, y) for (int i = x; i <= (y); ++i)
#define fd(i, x, y) for (int i = x; i >= (y); --i)
using namespace std;

typedef long long ll;

const int maxn = 2e5 + 5;
const ll mod = 998244353;

int n, k;
int a[maxn], b[maxn], s[maxn], pos[maxn], nxt[maxn], pre[maxn];
bool flag[maxn];

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0', ch = getchar();
	return res * p;
}

inline int sum(int l, int r)
{
	if (l - 1 <= 0) return s[r];
	assert(r >= 0 && l - 1 >= 0);
	return s[r] - s[l - 1];
}

void work()
{
	n = getint(); k = getint();
	fo(i, 0, n + 1) s[i] = 0;
	fo(i, 1, n) a[i] = getint(), pos[a[i]] = i, flag[i] = 1;
	fo(i, 1, k)
	{
		b[i] = getint();
		flag[pos[b[i]]] = 0;
	}
	fo(i, 1, n + 1) s[i] = s[i - 1] + flag[i];
	int lst = -1;
	fo(i, 0, n + 1) nxt[i] = pre[i] = 0;
	fo(i, 0, n + 1)
		if (!sum(i, i))
		{
			if (~lst)
			{
				nxt[lst] = i;
				pre[i] = lst;
			}
			lst = i;
		}
	ll ans = 1;
	fo(i, 1, k)
	{
		(ans *= (sum(pre[pos[b[i]]] + 1, pos[b[i]] - 1) > 0) + (sum(pos[b[i]] + 1, nxt[pos[b[i]]] - 1) > 0)) %= mod;
		if (!ans) break;
		nxt[pre[pos[b[i]]]] = nxt[pos[b[i]]];
		pre[nxt[pos[b[i]]]] = pre[pos[b[i]]];
	}
	printf("%lld\n", ans);
}

int main()
{
	int T;
	T = getint();
	while (T--) work();
	return 0;
}


#include <bits/stdc++.h>

#define fo(i, x, y) for (int i = x; i <= (y); ++i)
#define fd(i, x, y) for (int i = x; i >= (y); --i)
using namespace std;

typedef long long ll;

const int maxn = 2e5 + 5;
const ll mod = 998244353;

int n, k;
int a[maxn], b[maxn], s[maxn], pos[maxn], nxt[maxn], pre[maxn];
bool flag[maxn];

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0', ch = getchar();
	return res * p;
}

inline int sum(int l, int r)
{
	return s[r] - s[l - 1];
}

void work()
{
	n = getint(); k = getint();
	fo(i, 0, n + 1) s[i] = 0;
	fo(i, 1, n) a[i] = getint(), pos[a[i]] = i, flag[i] = 1;
	fo(i, 1, k)
	{
		b[i] = getint();
		flag[pos[b[i]]] = 0;
	}
	fo(i, 1, n + 1) s[i] = s[i - 1] + flag[i];
	int lst = -1;
	fo(i, 0, n + 1) nxt[i] = pre[i] = 0;
	fo(i, 0, n + 1)
		if (!sum(i, i))
		{
			if (~lst)
			{
				nxt[lst] = i;
				pre[i] = lst;
			}
			lst = i;
		}
	ll ans = 1;
	fo(i, 1, k)
	{
		(ans *= (sum(pre[pos[b[i]]] + 1, pos[b[i]] - 1) > 0) + (sum(pos[b[i]] + 1, nxt[pos[b[i]]] - 1) > 0)) %= mod;
		if (!ans) break;
		nxt[pre[pos[b[i]]]] = nxt[pos[b[i]]];
		pre[nxt[pos[b[i]]]] = pre[pos[b[i]]];
	}
	printf("%lld\n", ans);
}

int main()
{
	int T;
	T = getint();
	while (T--) work();
	return 0;
}


#include <bits/stdc++.h>

#define fo(i, x, y) for (int i = x; i <= (y); ++i)
#define fd(i, x, y) for (int i = x; i >= (y); --i)
using namespace std;

typedef long long ll;

const int maxn = 2e5 + 5;
const ll mod = 998244353;

int n, k;
int a[maxn], b[maxn], s[maxn], pos[maxn], nxt[maxn], pre[maxn];
bool flag[maxn];

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0', ch = getchar();
	return res * p;
}

inline int sum(int l, int r)
{
	if (l - 1 <= 0) return s[r];
	return s[r] - s[l - 1];
}

void work()
{
	n = getint(); k = getint();
	fo(i, 0, n + 1) s[i] = 0;
	fo(i, 1, n) a[i] = getint(), pos[a[i]] = i, flag[i] = 1;
	fo(i, 1, k)
	{
		b[i] = getint();
		flag[pos[b[i]]] = 0;
	}
	fo(i, 1, n + 1) s[i] = s[i - 1] + flag[i];
	int lst = -1;
	fo(i, 0, n + 1) nxt[i] = pre[i] = 0;
	fo(i, 0, n + 1)
		if (!sum(i, i))
		{
			if (~lst)
			{
				nxt[lst] = i;
				pre[i] = lst;
			}
			lst = i;
		}
	ll ans = 1;
	fo(i, 1, k)
	{
		(ans *= (sum(pre[pos[b[i]]] + 1, pos[b[i]] - 1) > 0) + (sum(pos[b[i]] + 1, nxt[pos[b[i]]] - 1) > 0)) %= mod;
		if (!ans) break;
		nxt[pre[pos[b[i]]]] = nxt[pos[b[i]]];
		pre[nxt[pos[b[i]]]] = pre[pos[b[i]]];
	}
	printf("%lld\n", ans);
}

int main()
{
	int T;
	T = getint();
	while (T--) work();
	return 0;
}


#include <bits/stdc++.h>

#define fo(i, x, y) for (int i = x; i <= (y); ++i)
#define fd(i, x, y) for (int i = x; i >= (y); --i)
using namespace std;

typedef long long ll;

const int maxn = 2e5 + 5;
const ll mod = 998244353;

int n, k;
int a[maxn], b[maxn], s[maxn], pos[maxn], nxt[maxn], pre[maxn];
bool flag[maxn];

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0', ch = getchar();
	return res * p;
}

inline int sum(int l, int r)
{
	if (l - 1 <= 0) return s[r];
	assert(r >= 0);
	return s[r] - s[l - 1];
}

void work()
{
	n = getint(); k = getint();
	fo(i, 0, n + 1) s[i] = 0;
	fo(i, 1, n) a[i] = getint(), pos[a[i]] = i, flag[i] = 1;
	fo(i, 1, k)
	{
		b[i] = getint();
		flag[pos[b[i]]] = 0;
	}
	fo(i, 1, n + 1) s[i] = s[i - 1] + flag[i];
	int lst = -1;
	fo(i, 0, n + 1) nxt[i] = pre[i] = 0;
	fo(i, 0, n + 1)
		if (!sum(i, i))
		{
			if (~lst)
			{
				nxt[lst] = i;
				pre[i] = lst;
			}
			lst = i;
		}
	ll ans = 1;
	fo(i, 1, k)
	{
		(ans *= (sum(pre[pos[b[i]]] + 1, pos[b[i]] - 1) > 0) + (sum(pos[b[i]] + 1, nxt[pos[b[i]]] - 1) > 0)) %= mod;
		if (!ans) break;
		nxt[pre[pos[b[i]]]] = nxt[pos[b[i]]];
		pre[nxt[pos[b[i]]]] = pre[pos[b[i]]];
	}
	printf("%lld\n", ans);
}

int main()
{
	int T;
	T = getint();
	while (T--) work();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(0);
	int test;
	cin >> test;
	while (test--) {
		int n, k;
		cin >> n >> k;
		int a[n], b[k];
		unordered_map<int, int> m;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			m[a[i]] = i;
		}
		unordered_set<int> s;
		for (int i = 0; i < k; i++) {
			cin >> b[i];
			s.insert(b[i]);
		}
		int ans = 0;
		for (int i = 0; i < k; i++) {
			s.erase(b[i]);
			int cur = 0;
			// cout << a[m[b[i]]-1] << '\n';
			if (m[b[i]] && !s.count(a[m[b[i]]-1])) {
				cur++;
				// cout << a[m[b[i]]-1] << '\n';
			}
			// cout << a[m[b[i]+1]] << '\n';
			if (m[b[i]] < n-1 && !s.count(a[m[b[i]]+1]))
				cur++;
			// cout << "cur = " << cur << '\n';
			if (cur == 0) {
				ans = 0;
				break;
			}
			// cout << cur << '\n';
			if (i == 0)
				ans = cur;
			else
				ans *= cur;
		}
		cout << ans << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 998244353;

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(0);
	int test;
	cin >> test;
	while (test--) {
		int n, k;
		cin >> n >> k;
		int a[n], b[k];
		unordered_map<int, int> m;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			m[a[i]] = i;
		}
		unordered_set<int> s;
		for (int i = 0; i < k; i++) {
			cin >> b[i];
			s.insert(b[i]);
		}
		ll ans = 0;
		for (int i = 0; i < k; i++) {
			s.erase(b[i]);
			int cur = 0;
			// cout << a[m[b[i]]-1] << '\n';
			if (m[b[i]] && !s.count(a[m[b[i]]-1])) {
				cur++;
				// cout << a[m[b[i]]-1] << '\n';
			}
			// cout << a[m[b[i]+1]] << '\n';
			if (m[b[i]] < n-1 && !s.count(a[m[b[i]]+1]))
				cur++;
			// cout << "cur = " << cur << '\n';
			if (cur == 0) {
				ans = 0;
				break;
			}
			// cout << cur << '\n';
			if (i == 0)
				ans = cur;
			else
				ans = ans * cur % mod;
		}
		cout << ans << '\n';
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 200000 + 20;
const int MOD = 998244353;

int n, k;
int A[N], B[N];
int pos[N], vis[N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T -- )
    {
        int res = 1;
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; ++ i) vis[i] = 0;
        for(int i = 1; i <= n; ++ i) scanf("%d", &A[i]), pos[A[i]] = i;
        for(int i = 1; i <= k; ++ i) scanf("%d", &B[i]), vis[B[i]] = 1;
        for(int i = 1; i <= k; ++ i)
        {
            int cnt = 0;
            if(pos[B[i]] - 1 >= 1 && !vis[A[pos[B[i]] - 1]]) cnt ++;
            if(pos[B[i]] + 1 <= n && !vis[A[pos[B[i]] + 1]]) cnt ++;
            res = res * cnt % MOD;
            vis[B[i]] = 0;
        }
        printf("%d\n", res);
    }
    //system("pause");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2E5+10;
int read()
{
    int a=0;char b=1,c;
    do if((c=getchar())==45)b=-1;while(!(c&16));
    do a=(a<<3)+(a<<1)+(c&15);while((c=getchar())&16);
    return a*b;
}
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    char a[10],s=0;
    do a[s++]=x%10|48;while(x/=10);
    do putchar(a[--s]);while(s);
}
int a[N],b[N];
int main()
{
	for(int x=read(),p=998244353;x--;)
	{
		int n=read(),m=read(),ans=1;
		for(int i=1;i<=n;++i)a[read()]=i;
		for(int i=1;i<=m;++i)b[i]=a[read()];
		memset(a,0,sizeof a),a[0]=a[n+1]=1;
		for(int i=1;i<=m;++i)a[b[i]]=1;
		for(int i=1;i<=m;++i)
		{
			ans=ans*1ll*(2-a[b[i]-1]-a[b[i]+1])%p;
			a[b[i]]=0;
		}
		write(ans),putchar('\n');
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=2E5+10;
int a[N],b[N];
int main()
{
	int x,p=998244353;
	cin>>x;
	while(x--)
	{
		int n,m,ans=1;
		cin>>n>>m;
		for(int i=1;i<=n;++i)
		{
			int t;
			cin>>t;
			a[t]=i;
		}
		for(int i=1;i<=m;++i)
		{
			int t;
			cin>>t;
			b[i]=a[t];
		}
		memset(a,0,sizeof a),a[0]=a[n+1]=1;
		for(int i=1;i<=m;++i)a[b[i]]=1;
		for(int i=1;i<=m;++i)
		{
			ans=ans*1ll*(2-a[b[i]-1]-a[b[i]+1])%p;
			a[b[i]]=0;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=2E5+10;
int read()
{
    int a=0;char b=1,c;
    do if((c=getchar())==45)b=-1;while(!(c&16));
    do a=(a<<3)+(a<<1)+(c&15);while((c=getchar())&16);
    return a*b;
}
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    char a[10],s=0;
    do a[s++]=x%10|48;while(x/=10);
    do putchar(a[--s]);while(s);
}
int a[N],b[N];
int main()
{
	for(int x=read();x--;)
	{
		int n=read(),m=read(),ans=1;
		for(int i=1;i<=n;++i)a[read()]=i;
		for(int i=1;i<=m;++i)b[i]=a[read()];
		memset(a,0,sizeof a),a[0]=a[n+1]=1;
		for(int i=1;i<=m;++i)a[b[i]]=1;
		for(int i=1;i<=m;++i)
		{
			ans=ans*1ll*(2-a[b[i]-1]-a[b[i]+1])%p;
			a[b[i]]=0;
		}
		write(ans),putchar('\n');
	}
}


#include"bits/stdc++.h"
using namespace std;
#define ri register int
#define N 200010
#define ll long long
#define mod 998244353
inline int rd(){
    int x=0,flg=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') flg=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
    return x*flg;
}
struct node{
    int l,r;
}s[N];
int n,m,t,l,r,x,tag[N],vis[N],a[N],b[N];
int main(){
    t=rd();
    while(t--){
        int ans=1;
        n=rd(),m=rd();
        for(ri i=1;i<=n;++i) a[i]=rd(),vis[a[i]]=i;
        for(ri i=1;i<=m;++i) b[i]=rd(),tag[vis[b[i]]]=1;
        for(ri i=1;i<=n;++i) s[i].l=i-1,s[i].r=i+1;
        for(ri i=1;i<=m;++i){
            int pos=vis[b[i]];int cnt=0;
            //cout<<pos<<" "<<s[pos].l<<" "<<s[pos].r<<endl;
            if(s[pos].r!=n+1&&!tag[s[pos].r]) cnt+=1;
            if(s[pos].l!=0&&!tag[s[pos].l]) cnt+=10;
            //cout<<i<<" "<<cnt<<endl;
            if(cnt==0) {ans=0;break;}
            else if(cnt<11){
                tag[pos]=0;
                if(cnt==1) s[s[s[pos].r].r].l=pos,s[pos].r=s[s[pos].r].r;
                else s[s[s[pos].l].l].r=pos,s[pos].l=s[s[pos].l].l;
            }
            else{
                tag[pos]=0;
                s[s[s[pos].r].r].l=pos,s[pos].r=s[s[pos].r].r;
                ans+=ans;if(ans>=mod) ans-=mod;
            }
        }
        cout<<ans<<endl;
        for(ri i=1;i<=n;++i) s[i].l=s[i].r=tag[i]=vis[i]=0;
    }
    return 0;
}


#include"bits/stdc++.h"
using namespace std;
#define ri register int
#define N 200010
#define ll long long
#define mod 998244353
inline int rd(){
    int x=0,flg=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') flg=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
    return x*flg;
}
struct node{
    int l,r;
}s[N];
int n,m,t,l,r,x,tag[N],vis[N],a[N],b[N];
int main(){
    t=rd();
    while(t--){
        int ans=1;
        n=rd(),m=rd();
        for(ri i=1;i<=n;++i) a[i]=rd(),vis[a[i]]=i;
        for(ri i=1;i<=m;++i) b[i]=rd(),tag[vis[b[i]]]=1;
        for(ri i=1;i<=n;++i) s[i].l=i-1,s[i].r=i+1;
        for(ri i=1;i<=m;++i){
            int pos=vis[b[i]];int cnt=0;
            //cout<<pos<<" "<<s[pos].l<<" "<<s[pos].r<<endl;
            if(s[pos].r!=n+1&&!tag[s[pos].r]) cnt+=1;
            if(s[pos].l!=0&&!tag[s[pos].l]) cnt+=10;
            //cout<<i<<" "<<cnt<<endl;
            if(cnt==0) {ans=0;break;}
            else if(cnt<11){
                tag[pos]=0;
                if(cnt==1) s[s[s[pos].r].r].l=pos,s[pos].r=s[s[pos].r].r;
                else s[s[s[pos].l].l].r=pos,s[pos].l=s[s[pos].l].l;
            }
            else{
                tag[pos]=0;
                s[s[s[pos].r].r].l=pos,s[pos].r=s[s[pos].r].r;
                ans+=ans;if(ans>=mod) ans-=mod;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int t;
int a[200100];
int b[200100];
int use[200100];
int to[200100];
int n,k;
int last[200100],ne[200100];
int disl[200100],disr[200100];
const int mod=998244353;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(int i=0;i<=n;i++)  use[i]=0,disl[i]=disr[i]=0;
        for(int i=1;i<=n;i++)  cin>>a[i],to[a[i]]=i;
        for(int i=1;i<=k;i++)  cin>>b[i],use[b[i]]=1;
        int la=0;
        for(int i=1;i<=n;i++)
        {
            if(use[a[i]])
            {
                ne[la]=i;disr[la]=i-la-1;
                last[i]=la;disl[i]=i-la-1;
                la=i;
            }
        }
        ne[la]=n+1;disr[la]=n-la;

        int sig=0;
        long long ans=1;
        for(int i=1;i<=k;i++)
        {
            int x=b[i];
            int j=to[x];
            sig=0;
            if(disl[j]>0)  sig++;
            if(disr[j]>0)  sig++;

            int l=last[j],r=ne[j];
            disl[r]=disr[l]=disl[j]+disr[j];
            ans=ans*sig%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
const int N=200010;
const LL M=998244353;
int a[N],b[N],c[N],d[N],s[N],n,k;
LL ans=0;
void f1(){
	int tag=1;
	ans=1;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){c[i]=0;d[i]=0;}
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),c[a[i]]=i;
	for(int i=1;i<=k;i++){scanf("%d",&b[i]);d[c[b[i]]]=i;}
	for(int j=1;j<=k;j++){
		int i=c[b[j]];
		if(i==1){
			if(d[i+1]){tag=0;break;}
		}
		else if(i==n){
			if(d[i-1]){tag=0;break;}
		}
		else{
			if(d[i-1]&&d[i+1]){tag=0;break;}
			else if(d[i-1]&&!d[i+1]||!d[i-1]&&d[i+1]){}
			else{
				ans=ans*2LL%M;
			}
		}
		d[i]=0;
	}
	if(!tag){printf("0\n");return ;}
	printf("%lld\n",ans);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--)f1();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
const int N=200010;
const LL M=998244353;
int a[N],b[N],c[N],d[N],s[N],n,k;
LL ans=0;
void f1(){
	int tag=1;
	ans=1;
	scanf("%d%d",&n,&k);
	memset(c,0,sizeof(c));
	memset(d,0,sizeof(d));

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),c[a[i]]=i;
	for(int i=1;i<=k;i++){scanf("%d",&b[i]);d[c[b[i]]]=i;}
	for(int j=1;j<=k;j++){
		int i=c[b[j]];
		if(i==1){
			if(d[i+1]){tag=0;break;}
		}
		else if(i==n){
			if(d[i-1]){tag=0;break;}
		}
		else{
			if(d[i-1]&&d[i+1]){tag=0;break;}
			else if(d[i-1]&&!d[i+1]||!d[i-1]&&d[i+1]){}
			else{
				ans=ans*2LL%M;
			}
		}
		d[i]=0;
	}
	if(!tag){printf("0\n");return ;}
	printf("%lld\n",ans);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--)f1();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>b;
        for(int i=1;i<=n;i++) {
            cin>>vala[i];
        }
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }
        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){
                ans=ans*2%mod;
                // r[lval]=r[valb[i]];
                // l[rval]=l[valb[i]];
                l[valb[i]]=l[lval];
                r[l[lval]]=valb[i];
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){
                ans=0;
            }
            else b.erase(b.find(valb[i]));
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>b;
        for(int i=1;i<=n;i++) {
            cin>>vala[i];
        }
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }
        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){
                ans=ans*2%mod;
                //delete l[val]
                if(lval!=0){
                    l[valb[i]]=l[lval];
                    r[l[lval]]=r[lval];
                }
                else{
                    l[valb[i]]=0;
                }
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){
                ans=0;
            }
            else b.erase(b.find(valb[i]));
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>b;
        for(int i=1;i<=n;i++) {
            cin>>vala[i];
        }
        for(int i=n-1;i<=n+3;i++) l[i]=r[i]=0;
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }
        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){
                ans=ans*2%mod;
                //delete l[val]
                if(lval!=0){
                    l[valb[i]]=l[lval];
                    r[l[lval]]=r[lval];
                }
                else{
                    l[valb[i]]=0;
                }
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){
                ans=0;
            }
            else
            {
                bool flag=1;//lval is in set b.
                if(b.count(rval)) flag=0;
                if(flag){ // delete rval
                    if(rval!=0){
                        r[valb[i]]=r[rval];
                        l[r[rval]]=l[rval];
                    }
                    else r[valb[i]]=0;
                }
                else{ //delete lval
                    if(lval!=0){
                        l[valb[i]]=l[lval];
                        r[l[lval]]=r[lval];
                    }
                    else{
                        l[valb[i]]=0;
                    }
                }
                b.erase(b.find(valb[i]));
            }
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>b;
        for(int i=n-1;i<=n+3;i++) vala[i]=l[i]=r[i]=0;
        for(int i=1;i<=n;i++) {
            cin>>vala[i];
        }
      //  for(int i=n-1;i<=n+3;i++) vala[i]=l[i]=r[i]=0;
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }

        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){//neither in b
                ans=ans*2%mod;
                //delete lval
                if(lval!=0){
                    l[valb[i]]=l[lval];
                    r[l[lval]]=r[lval];
                }
                else l[valb[i]]=0;
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){//both in b
                ans=0;
            }
            else
            {
                bool flag=1;//lval is in set b.
                if(b.count(rval)) flag=0;
                if(flag){ // delete rval
                    if(rval!=0){
                        r[valb[i]]=r[rval];
                        l[r[rval]]=l[rval];
                    }
                    else r[valb[i]]=0;
                }
                else{ //delete lval
                    if(lval!=0){
                        l[valb[i]]=l[lval];
                        r[l[lval]]=r[lval];
                    }
                    else l[valb[i]]=0;
                }
                b.erase(b.find(valb[i]));
            }
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>b;
        for(int i=1;i<=n;i++) {
            cin>>vala[i];
        }
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }
        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){
                ans=ans*2%mod;
                // r[lval]=r[valb[i]];
                // l[rval]=l[valb[i]];
                l[valb[i]]=l[lval];
                r[l[lval]]=r[lval];
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){
                ans=0;
            }
            else b.erase(b.find(valb[i]));
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>a,b;
        for(int i=1;i<=n;i++) {
            int rs;
            cin>>rs;
            vala[i]=rs;
            a.insert(rs);
        }
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }
        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){
                ans=ans*2%mod;
                r[lval]=r[valb[i]];
                l[rval]=l[valb[i]];
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){
                ans=0;
            }
            else b.erase(b.find(valb[i]));
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
bool ispow(ll n){return (n&(n-1))==0;}
#define lowbit(x) (x&(-x))
const ll mod=998244353;
ll vala[200005],valb[200005];
ll l[200005],r[200005];
int main()
{
	fast;
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        set<int>b;
        for(int i=1;i<=n;i++) {
            cin>>vala[i];
        }
        for(int i=1;i<=n;i++){
            l[vala[i]]=vala[i-1];
            r[vala[i]]=vala[i+1];
        }
        for(int i=1;i<=k;i++){
            int rs;
            cin>>rs;
            valb[i]=rs;
            b.insert(rs);
        }
        b.insert(0);
      //  for(int i=1;i<=n;i++) cout<<l[i]<<' '<<r[i]<<'\n';
        ll ans=1;
        for(int i=1;i<=k;i++) {
            int lval=l[valb[i]];
            int rval=r[valb[i]];
            if(!b.count(lval)&&!b.count(rval)){
                ans=ans*2%mod;
                //delete l[val]
                if(lval!=0){
                    l[valb[i]]=l[lval];
                    r[l[lval]]=r[lval];
                }
                else{
                    l[valb[i]]=0;
                }
                b.erase(b.find(valb[i]));
            }
            else if(b.count(lval)&&b.count(rval)){
                ans=0;
            }
            else
            {
                bool flag=1;//lval is in set b.
                if(b.count(rval)) flag=0;
                if(flag){ // delete rval
                    if(rval!=0){
                        r[valb[i]]=r[rval];
                        l[r[rval]]=l[rval];
                    }
                    else r[valb[i]]=0;
                }
                else{ //delete lval
                    if(lval!=0){
                        l[valb[i]]=l[lval];
                        r[l[lval]]=r[lval];
                    }
                    else{
                        l[valb[i]]=0;
                    }
                }
                b.erase(b.find(valb[i]));
            }
          //  cout<<ans<<"\n";
        }
        cout<<ans<<'\n';
    }
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int M=4e5+10;
const int Mod=998244353;
int n,m,T,pos[M],a[M],b[M],vis[M];

int main()
{

	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		memset(pos,0,sizeof(pos));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			pos[a[i]]=i;
		}
		for(int i=1;i<=m;i++){
			scanf("%d",&b[i]);
			vis[pos[b[i]]]=1;
		}
		int ans=1;
		for(int i=1;i<=m;i++)
		{
			int x=pos[b[i]];
			ans=ans*((x>1&&!vis[x-1])+(x<n&&!vis[x+1]))%Mod;
			vis[x]=0;
		}
		printf("%d\n",ans);
	}

	return 0;

}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int M=4e5+10;
const int Mod=998244353;
int n,m,T,pos[M],a[M],b[M],vis[M];

int main()
{

	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			vis[i]=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			pos[a[i]]=i;
		}
		for(int i=1;i<=m;i++){
			scanf("%d",&b[i]);
			vis[pos[b[i]]]=1;
		}
		int ans=1;
		for(int i=1;i<=m;i++)
		{
			int x=pos[b[i]];
			ans=ans*((x>1&&!vis[x-1])+(x<n&&!vis[x+1]))%Mod;
			vis[x]=0;
		}
		printf("%d\n",ans);
	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define ll long long
const int N=2e5+10;
int a[N],b[N],pos[N],cnt[N];
int n,m;
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        map<int,int>mp;
        for(int i=0;i<n;i++)scanf("%d",&a[i]),pos[a[i]]=i;
        for(int i=0;i<m;i++)scanf("%d",&b[i]),cnt[i]=pos[b[i]],mp[pos[b[i]]]++;
        ll ans=1;
        for(int i=0;i<m;i++){
            if(cnt[i]==0){
                if(mp[1]){ans=0;}
                mp[0]--;
            }
            else if(cnt[i]==n-1){
                if(mp[n-2]){ans=0;}
                mp[n-1]--;
            }
            else{
                ll k=2;
                if(mp[cnt[i]-1])k--;
                if(mp[cnt[i]+1])k--;
                ans*=k;ans%=mod;
                mp[cnt[i]]--;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 2e5+5;
typedef long long int ll;
int a[maxn],pos[maxn],jdg[maxn]; // jdg[i]=1说明不能删
queue<int> q,eq;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        q=eq;
        memset(jdg,0,sizeof(jdg));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[tmp]=1;
            q.push(tmp);
        }
        jdg[0]=jdg[n+1]=1;
        while(k--)
        {
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            ans=ans*(2-jdg[a[tpos-1]]-jdg[a[tpos+1]])%mod;
            jdg[tmp]=0;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 2e5+5;
typedef long long int ll;
int a[maxn],pos[maxn],jdg[maxn]; // jdg[i]=1说明不能删
queue<int> q,eq;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        q=eq;
        memset(jdg,0,sizeof(jdg));
        for(int i=0;i<n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[tmp]=1;
            q.push(tmp);
        }
        while(k--)
        {
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            if(!tpos){
                if(!jdg[a[1]])jdg[tmp]=0;
                else{
                    ans=0;break;
                }
            }
            else if(tpos==n-1){
                if(!jdg[a[n-2]])jdg[tmp]=0;
                else{
                    ans=0;break;
                }
            }
            else{
                if(jdg[a[tpos-1]]&&jdg[a[tpos+1]]){
                    ans=0;break;
                }
                else if(!jdg[a[tpos-1]]&&!jdg[a[tpos+1]]){
                    ans=ans*2%mod;
                }
                jdg[tmp]=0;
            }
           // cout<<ans<<endl;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

const ll mod = 998244353;
const int maxn = 2e5+5;
int a[maxn],pos[maxn];
bool jdg[maxn]; // jdg[i]=1说明a[i]不能删
queue<int> q;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        memset(jdg,0,sizeof(jdg));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[pos[tmp]]=1;
            q.push(tmp);
        }
        jdg[0]=jdg[n+1]=1;
        for(int i=0;i<k;i++){
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            ans=ans*(2LL-jdg[tpos-1]-jdg[tpos+1])%mod;
            jdg[tpos]=0;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

const ll mod = 998244353;
const int maxn = 2e5+5;
int a[maxn],pos[maxn],jdg[maxn]; // jdg[i]=1说明a[i]不能删
queue<int> q;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        memset(jdg,0,sizeof(jdg));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[pos[tmp]]=1;
            q.push(tmp);
        }
        jdg[0]=jdg[n+1]=1;
        for(int i=0;i<k;i++){
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            ans=ans*(2-jdg[tpos-1]-jdg[tpos+1])%mod;
            jdg[tpos]=0;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 2e5+5;
typedef long long int ll;
int a[maxn],pos[maxn],jdg[maxn]; // jdg[i]=1说明不能删
queue<int> q,eq;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        q=eq;
        memset(jdg,0,sizeof(jdg));
        for(int i=0;i<n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[tmp]=1;
            q.push(tmp);
        }
        while(k--)
        {
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            if(!tpos){
                if(!jdg[a[1]])jdg[tmp]=0;
                else{
                    ans=0;break;
                }
            }
            else if(tpos==n-1){
                if(!jdg[a[n-2]])jdg[tmp]=0;
                else{
                    ans=0;break;
                }
            }
            else{
                ans=ans*(2-jdg[a[tpos-1]]-jdg[a[tpos+1]])%mod;
                jdg[tmp]=0;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

const ll mod = 998244353;
const int maxn = 2e5+5;
int a[maxn],pos[maxn];
bool jdg[maxn]; // jdg[i]=1说明a[i]不能删
queue<int> q;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        memset(jdg,0,sizeof(jdg));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[pos[tmp]]=1;
            q.push(tmp);
        }
        jdg[0]=jdg[n+1]=1;
        for(int i=0;i<k;i++){
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            ans=ans*(2-jdg[tpos-1]-jdg[tpos+1])%mod;
            jdg[tpos]=0;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 2e5+5;
typedef long long int ll;
int a[maxn],pos[maxn],jdg[maxn]; // jdg[i]=1说明a[i]不能删
queue<int> q;
int t,n,k,tmp,tpos;
ll ans;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ans=1;
        memset(jdg,0,sizeof(jdg));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=0;i<k;i++){
            cin>>tmp;
            jdg[pos[tmp]]=1;
            q.push(tmp);
        }
        jdg[0]=jdg[n+1]=1;
        while(k--){
            tmp=q.front();
            q.pop();
            tpos=pos[tmp];
            ans=ans*(2-jdg[tpos-1]-jdg[tpos+1])%mod;
            jdg[tpos]=0;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
using namespace std;

const int MAX = 2e5+10;
const LL MOD = 998244353;
int a[MAX],b[MAX],p[MAX];
bool vis[MAX];

int main()
{
    //freopen("in.txt","r",stdin);
    int Cas = 1;
    scanf("%d",&Cas);
    while(Cas--)
    {
        memset(vis,0,sizeof(vis));
        int n,k;
        scanf("%d%d",&n,&k);
        vis[0] = vis[n+1] = 1;
        for (int i = 1;i <= n;i++)
        {
            scanf("%d",a+i);
            p[a[i]] = i;
        }
        for (int i = 1;i <= k;i++)
        {
            scanf("%d",b+i);
            vis[b[i]] = 1;
        }
        LL ans = 1;
        for (int i = 1;i <= k;i++)
        {
            int pos = p[b[i]];
            vis[b[i]] = 0;
            if (!vis[a[pos-1]] && !vis[a[pos+1]])
                ans = ans*2%MOD;
            else if (vis[a[pos-1]] && vis[a[pos+1]])
            {
                ans=0;
                break;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
using namespace std;

const int MAX = 2e5+10;
const LL MOD = 998244353;
map<int,int> mp;
int a[MAX],b[MAX];
bool vis[MAX];

int main()
{
    //freopen("in.txt","r",stdin);
    int Cas = 1;
    scanf("%d",&Cas);
    while(Cas--)
    {
        memset(vis,0,sizeof(vis));
        vis[0] = 1;
        mp.clear();
        int n,k;
        scanf("%d%d",&n,&k);
        for (int i = 1;i <= n;i++)
        {
            scanf("%d",a+i);
            mp[a[i]] = i;
        }
        for (int i = 1;i <= k;i++)
        {
            scanf("%d",b+i);
            vis[b[i]] = 1;
        }
        LL ans = 1;
        for (int i = 1;i <= k;i++)
        {
            int p = mp[b[i]];
            if (!vis[a[p-1]] && !vis[a[p+1]])
            {
                vis[a[p]] = 0;
                ans = ans*2%MOD;
            }
            else if (vis[a[p-1]] && vis[a[p+1]])
            {
                ans=0;
                break;
            }
            else if (vis[a[p-1]])
                vis[a[p-1]]=1,vis[a[p]]=0;
            else
                vis[a[p+1]]=1,vis[a[p]]=0;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
using namespace std;

const int MAX = 2e5+10;
const LL MOD = 998244353;
int a[MAX],b[MAX],p[MAX];
bool vis[MAX];

int main()
{
    //freopen("in.txt","r",stdin);
    int Cas = 1;
    scanf("%d",&Cas);
    while(Cas--)
    {
        memset(vis,0,sizeof(vis));
        int n,k;
        scanf("%d%d",&n,&k);
        vis[0] = vis[k] = 1;
        for (int i = 1;i <= n;i++)
        {
            scanf("%d",a+i);
            p[a[i]] = i;
        }
        for (int i = 1;i <= k;i++)
        {
            scanf("%d",b+i);
            vis[b[i]] = 1;
        }
        LL ans = 1;
        for (int i = 1;i <= k;i++)
        {
            int pos = p[b[i]];
            vis[b[i]] = 0;
            if (!vis[a[pos-1]] && !vis[a[pos+1]])
                ans = ans*2%MOD;
            else if (vis[a[pos-1]] && vis[a[pos+1]])
            {
                ans=0;
                break;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
using namespace std;

const int MAX = 2e5+10;
const LL MOD = 998244353;
int a[MAX],b[MAX],p[MAX];
bool vis[MAX];

int main()
{
    //freopen("in.txt","r",stdin);
    int Cas = 1;
    scanf("%d",&Cas);
    while(Cas--)
    {
        memset(vis,0,sizeof(vis));
        int n,k;
        scanf("%d%d",&n,&k);
        vis[0] = vis[n+1] = 1;
        for (int i = 1;i <= n;i++)
        {
            scanf("%d",a+i);
            p[a[i]] = i;
        }
        for (int i = 1;i <= k;i++)
        {
            scanf("%d",b+i);
            vis[b[i]] = 1;
        }
        LL ans = 1;
        for (int i = 1;i <= k;i++)
        {
            int pos = p[b[i]];
            vis[b[i]] = 0;
            int res = 0;
            if (pos-1 > 0 && !vis[a[pos-1]])
                res++;
            if (pos+1 <= n && !vis[a[pos+1]])
                res++;
            ans = ans*res%MOD;
            if (!ans)
                break;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

void solve() {
	long long mod = 998244353;
	long long n, k;
	cin>>n>>k;
	long long a[n];
	long long pos[n];
	for(int i = 0; i < n; i++) {
		cin>>a[i];
		pos[a[i]] = i;
	}
	long long b[k];
	set<long long> sig;
	for(int i = 0; i < k; i++) {
		cin>>b[i];
		sig.insert(b[i]);
	}
	long long sol = 1;
	long long cero = 0;
	for(int i = 0; i < k; i++) {
		//cerr<<"NUMERO "<<b[i]<<endl;
		long long p = pos[b[i]];
		long long mult = 2;
		if(p == 0 or p == n-1) {
			mult--;
		}
		if(p-1 >= 0 and sig.count(a[p-1]) != 0) {
		//cerr<<"ANTERIOR "<<a[p-1]<<endl;
			mult--;
		}
		if(p+1 < n and sig.count(a[p+1]) != 0) {
		//cerr<<"POSTERIOR "<<a[p+1]<<endl;
			mult--;
		}

		//cerr<<"MULT "<<mult<<endl;

		mult = max(mult, cero);
		sol *= mult;
		sol %= mod;

		sig.erase(b[i]);
		//cerr<<"SIG";
		//for(auto s: sig) {
			//cerr<<' '<<s;
		//}
		//cerr<<endl;
	}

	cout<<sol<<endl;
}

int main() {
	long long t;
	cin>>t;
	while(t--) {
		solve();
	}
}


#include<bits/stdc++.h>

using namespace std;

void solve() {
	long long mod = 998244353;
	long long n, k;
	cin>>n>>k;
	long long a[n];
	long long pos[n];
	for(int i = 0; i < n; i++) {
		cin>>a[i];
		pos[a[i]] = i;
	}
	long long b[k];
	set<long long> sig;
	for(int i = 0; i < k; i++) {
		cin>>b[i];
		sig.insert(b[i]);
	}
	long long sol = 1;
	long long cero = 0;
	for(int i = 0; i < k; i++) {
		//cerr<<"NUMERO "<<b[i]<<endl;
		long long p = pos[b[i]];
		long long mult = 2;
		if(p == 0 or p == n-1) {
			mult--;
		}
		if(p-1 >= 0 and sig.count(a[p-1]) != 0) {
		//cerr<<"ANTERIOR "<<a[p-1]<<endl;
			mult--;
		}
		if(p+1 < n and sig.count(a[p+1]) != 0) {
		//cerr<<"POSTERIOR "<<a[p+1]<<endl;
			mult--;
		}

		//cerr<<"MULT "<<mult<<endl;

		mult = max(mult, cero);
		sol *= mult;
		sol %= mod;

		sig.erase(b[i]);
		//cerr<<"SIG";
		for(auto s: sig) {
			//cerr<<' '<<s;
		}
		//cerr<<endl;
	}

	cout<<sol<<endl;
}

int main() {
	long long t;
	cin>>t;
	while(t--) {
		solve();
	}
}


/*
 * Author: MoliH97
 * Time: 2020-11-03 00:13:20
 */

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
const ll MOD = 998244353;
int a[N], b[N], l[N], r[N], mp1[N], mp2[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T; cin >> T;
    while(T--) {
        int n, k; cin >> n >> k;
        for(int i = 1; i <= n; i++) {cin >> a[i]; mp1[a[i]] = i;}
        for(int i = 1; i <= k; i++) {cin >> b[i]; mp2[b[i]] = i;}
        for(int i = 1; i <= n; i++) {
            a[i] = mp2[a[i]];
            l[i] = i - 1;
            r[i] = i + 1;
        }
        ll ans = 1;
        for(int i = 1; i <= k; i++) {
            int pos = mp1[b[i]];
            int cnt = 0;
            if(l[pos] >= 1 && !a[l[pos]]) cnt++;
            if(r[pos] <= n && !a[r[pos]]) cnt++;
            if(cnt == 0) {ans = 0; break;}
            for(int k = 1; k < cnt; k++) ans = (2LL * ans) % MOD;
            l[r[pos]] = l[pos];
            r[l[pos]] = r[pos];
        }
        cout << ans << endl;
        for(int i = 0; i <= n; i++) mp1[i] = mp2[i] = 0;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
typedef long long ll;
#define REG register
#define rep(i,a,b) for(REG int i=a;i<=b;i++)
const ll N=2e5+5,mod=998244353;
int n,m,A[N],B[N],p[N]; ll ans; bool vis[N];
int main(){
    REG int t; scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        memset(p,0,sizeof(p));
        memset(vis,0,sizeof(vis));
        rep(i,1,n) scanf("%d",A+i),p[A[i]]=i;
        rep(i,1,m) scanf("%d",B+i),vis[B[i]]=1;
        ans=1;
        rep(i,1,m){
            REG int pos=p[B[i]],c=0;
            if(pos-1>=1&&!vis[A[pos-1]]) c++;
            if(pos+1<=n&&!vis[A[pos+1]]) c++;
            ans=ans*c%mod; vis[B[i]]=0;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
typedef long long ll;
#define REG register
#define rep(i,a,b) for(REG int i=a;i<=b;i++)
const ll N=2e5+5,mod=998244353;
int n,m,A[N],B[N],p[N]; ll ans; bool vis[N];
int main(){
    REG int t; scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        //memset(p,0,sizeof(p));
        //memset(vis,0,sizeof(vis));
        rep(i,1,n) p[i]=vis[i]=0;
        rep(i,1,n) scanf("%d",A+i),p[A[i]]=i;
        rep(i,1,m) scanf("%d",B+i),vis[B[i]]=1;
        ans=1;
        rep(i,1,m){
            REG int pos=p[B[i]],c=0;
            if(pos-1>=1&&!vis[A[pos-1]]) c++;
            if(pos+1<=n&&!vis[A[pos+1]]) c++;
            ans=ans*c%mod; vis[B[i]]=0;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define DIM 200010
#define MOD 998244353
using namespace std;

int t,n,m,i;
int a[DIM],b[DIM],f[DIM],poz[DIM];
int main (){

    //ifstream cin ("date.in");
    //ofstream cout ("date.out");

    cin>>t;
    for (;t--;){

        cin>>n>>m;

        for (i=1;i<=n;i++)
            cin>>a[i];

        memset (f,0,sizeof f);
        memset (poz,0,sizeof poz);
        for (i=1;i<=m;i++){
            cin>>b[i];
            f[b[i]] = 1;
        }

        for (i=1;i<=n;i++){
            if (f[a[i]])
                poz[a[i]] = i;
        }

        long long sol = 1;
        for (i=1;i<=m;i++){
            int cnt = 0;
            if (poz[b[i]] > 1 && !poz[ a[poz[b[i]] - 1] ])
                cnt++;
            if (poz[b[i]] < n && !poz[ a[poz[b[i]] + 1] ])
                cnt++;

            sol = sol * cnt % MOD;
            poz[b[i]] = 0;
        }
        cout<<sol<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define DIM 200010
#define MOD 998244353
using namespace std;

int t,n,m,i;
int a[DIM],b[DIM],f[DIM],poz[DIM];
int main (){

    FILE *fin = stdin;
    FILE *fout = stdout;

    fscanf (fin,"%d",&t);
    for (;t--;){

        fscanf (fin,"%d%d",&n,&m);

        for (i=1;i<=n;i++){
            fscanf (fin,"%d",&a[i]);
            f[i] = poz[i] = 0;
        }

        //memset (f,0,sizeof f);
        //memset (poz,0,sizeof poz);
        for (i=1;i<=m;i++){
            fscanf (fin,"%d",&b[i]);
            f[b[i]] = 1;
        }

        for (i=1;i<=n;i++){
            if (f[a[i]])
                poz[a[i]] = i;
        }

        long long sol = 1;
        for (i=1;i<=m;i++){
            int cnt = 0;
            if (poz[b[i]] > 1 && !poz[ a[poz[b[i]] - 1] ])
                cnt++;
            if (poz[b[i]] < n && !poz[ a[poz[b[i]] + 1] ])
                cnt++;

            sol = sol * cnt % MOD;
            poz[b[i]] = 0;
        }

        fprintf(fout,"%d\n",sol);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
void nhap()
{
    //freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        if (a==0&&b==0&&k==1) printf("-1");
        if (a>k||b>k) d=-1;
        else if (abs(a-b)%2==1) d=k-1;
        else
        {
            if (k%2==0) d=k;
            if (k%2==1) d=k-2;
        }
        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
void nhap()
{
   // freopen("BTA.inp","r",stdin);
    //freopen("BTA.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld",&a,&b,&k);
        s=max(a,b);
        if (s>k) printf("-1");
        else if (s==k&&s%2==0) d=k;
        else if(s==k&&s%2==1) d=k-1;
        else
        {
            if (a==b)
            {
                if ((k-a)%2==0) d=k;
                else d=k-2;
            }
            else
            {
                long long k1=k;d=min(a,b);k-=d;
                int t=abs(b-a);
                if (t%2==0)
                {
                    if (k%2==0) d=k1;
                    else d=k1-2;
                }
                else
                {
                    d=k1-1;
                }
            }
        }
        printf("%lld\n",d);
    }

}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
int mx(long long x,long long y)
{
    if (y>x) return y;
    else return x;
}
int mn(long long a,long long b)
{
    if (b>a) return a;
    else return b;
}
void nhap()
{
   // freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        s=mx(a,b);
        if (s>k) d=-1;
        else if (s==k&&s%2==0) d=k;
        else if(s==k&&s%2==1) d=k-1;
        else
        {
            if (a==b)
            {
                if ((k-a)%2==0) d=k;
                else d=k-2;
            }
            else
            {
                long long k1=k;d=mn(a,b);k-=d;
                int t=abs(b-a);
                if (t%2==0)
                {
                    if (k%2==0) d=k1;
                    else d=k1-2;
                }
                else
                {
                    d=k1-1;
                }
            }
        }
        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
int mx(long long x,long long y)
{
    if (y>x) return y;
    else return x;
}
int mn(long long a,long long b)
{
    if (b>a) return a;
    else return b;
}
void nhap()
{
   // freopen("BTA.inp","r",stdin);
    //freopen("BTA.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld",&a,&b,&k);
        s=mx(a,b);
        if (s>k) printf("-1");
        else if (s==k&&s%2==0) d=k;
        else if(s==k&&s%2==1) d=k-1;
        else
        {
            if (a==b)
            {
                if ((k-a)%2==0) d=k;
                else d=k-2;
            }
            else
            {
                long long k1=k;d=mn(a,b);k-=d;
                int t=abs(b-a);
                if (t%2==0)
                {
                    if (k%2==0) d=k1;
                    else d=k1-2;
                }
                else
                {
                    d=k1-1;
                }
            }
        }
        printf("%lld\n",d);
    }

}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
void nhap()
{
    //freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        if (a>k||b>k) d=-1;
        else if (abs(a-b)%2==1) d=k-1;
        else
        {
            long long k1=k;k=k-min(a,b);
            if (k%2==0) d=k1;
            if (k%2==1) d=k1-2;
        }
        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
int mx(long long x,long long y)
{
    if (y>x) return y;
    else return x;
}
void nhap()
{
    //freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        s=mx(a,b);
        if (s>k) d=-1;
        else if ((a-b)%2==1) d=k-1;
        else
        {
            if (k%2==0) d=k;
            if (k%2==1) d=k-2;
        }

        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
int mx(long long x,long long y)
{
    if (y>x) return y;
    else return x;
}
void nhap()
{
    freopen("BTB.inp","r",stdin);
    freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        s=mx(a,b);
        if (s>k) d=-1;
        else if (abs((a-b))%2==1) d=k-1;
        else
        {
            if (k%2==0) d=k;
            if (k%2==1) d=k-2;
        }

        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
void nhap()
{
    //freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        if (a>k||b>k) d=-1;
        else if (abs(a-b)%2==1) d=k-1;
        else
        {
            k1=k;k-=abs(a-b);
            if (k%2==0) d=k1;
            if (k%2==1) d=k1-2;
        }
        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
void nhap()
{
    //freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        if (a>k||b>k) d=-1;
        else if (abs(a-b)%2==1) d=k-1;
        else
        {
            if (k%2==0) d=k;
            if (k%2==1) d=k-2;
        }

        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#include<bits/stdc++.h>
using namespace std;
long long n;
long long s,i,a,b,k,d;
int mx(long long x,long long y)
{
    if (y>x) return y;
    else return x;
}
int mn(long long a,long long b)
{
    if (b>a) return a;
    else return b;
}
void nhap()
{
    //freopen("BTB.inp","r",stdin);
    //freopen("BTB.out","w",stdout);
    scanf("%lld\n",&n);
}
void xuli()
{
    for(i=1;i<=n;i++)
    {
        scanf("%lld %lld %lld\n",&a,&b,&k);
        s=mx(a,b);
        if (s>k) d=-1;
        else if ((a-b)%2==1) d=k-1;
        else if (k%2==0) d=k;
        else if (k%2==1) d=k-2;

        printf("%lld\n",d);
    }
}
int main()
{
    nhap();
    xuli();
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			ll temp = max(n,m) - min(n,m);
 			k -= temp;
 			ll ans = 0;
 			ans += k;
 			temp /= 2;
 			ans += temp * 2;
 			cout << ans << endl;
 		}
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			ll temp = max(n,m) - min(n,m);
 			k -= temp;
 			ll ans = 0;
 			ll a = min(n,m);
 			if (a % 2 == 0) {
 				if (k % 2 == 0) {
	 				ans = a + (k - a);
 				}
 				else {
 					ans = a + 1;
 					k -= (a + 1);
 					ans += k;
 				}
 			}
 			else {
 				if (k % 2 == 0) {
 					ans = a + 1;
 					k -= (a + 1);
	 				ans += k;
 				}
 				else {
 					ans = a + (k - a);
 				}
 			}
 			cout << ans << endl;
 		}
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q, n, m, k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll ans = 0;
 		ll a = max(n, m) - min(n, m);
 		ll b = min(n, m);
 		ll rem = k - (a + b);
 		if (rem < 0) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (a % 2 == 0) {
 			if (rem % 2 == 0)
 				cout << k << endl;
 			else
 				cout << k - 2 << endl;
 		}
 		else
			cout << k - 1 << endl;
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			if (p % 2 == k % 2)
 				cout << k << endl;
 			else
 				cout << k - 1 << endl;
 		}
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			p = n + m;
 			if (p % 2 == k % 2) {
 				if (k % 2 != 0) k--;
 				cout << k << endl;
 			}
 			else
 				cout << k - 1 << endl;
 		}
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			ll temp = max(n,m) - min(n,m);
 			k -= temp;
 			ll ans = 0;
 			cout << k << endl;
 		}
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			if (p % 2 == k % 2) {
 				if (k % 2 != 0) k--;
 				cout << k << endl;
 			}
 			else
 				cout << k - 1 << endl;
 		}
 	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define ordered_set tree <ll, null_type,less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//K-th smallest
//cout << k << "kth smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll q,n,m,k;
 	cin >> q;
 	while (q--) {
 		cin >> n >> m >> k;
 		ll req = min(n, m);
 		ll p = max(n,m);
 		if (p > k) {
 			cout << -1 << endl;
 			continue;
 		}
 		if (n == m) {
 			ll temp = n - 1;
 			k -= temp;
 			if (k % 2 == 0)
 				temp += k / 2, temp--;
 			else
 				temp += k / 2, temp++;
 			cout << temp << endl;
 		}
 		else {
 			ll temp = max(n,m) - min(n,m);
 			k -= temp;
 			ll ans = 0;
 			ans += k;
 			ans += temp / 2;
 			cout << ans << endl;
 		}
 	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        ll mx = max(x,y);
        ll mn = min(x,y);
        ll diff = mx-mn;

        if(mx > k) cout<<-1<<endl;
        else if(mx==k) cout<<mn<<endl;
        else
        {
            if(k%2==0 && diff%2==0) cout<<k<<endl;
            else if((k&1)&&diff%2==0) cout<<(k-2)<<endl;
            else  cout<<(k-1)<<endl;
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        ll mx = max(x,y);
        ll mn = min(x,y);
        ll diff = mx-mn;

        if(mx > k) cout<<-1<<endl;
        else
        {
            k-=mn;
            if(k%2==0 && diff%2==0) cout<<(k+mn)<<endl;
            else if((k&1)&&diff%2==0) cout<<(k+mn-2)<<endl;
            else  cout<<(k+mn-1)<<endl;
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        if(x==y)
        {
            if(k==x) cout<<x<<endl;
            else if(k<x) cout<<-1<<endl;
            else
            {
                if((k-x)&1) cout<<(k-2)<<endl;
                else cout<<k<<endl;
            }
        }

        else
        {
            ll mx = max(x,y);
            ll mn = min(x,y);
            ll diff = mx-mn;

            if(mx > k) cout<<-1<<endl;
            else if(mx==k) cout<<mn<<endl;
            else
            {
                ll ans = mn;
                ans+=(k-diff-mn);
                cout<<ans<<endl;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        ll mx = max(x,y);
        ll mn = min(x,y);
        ll diff = mx-mn;

        if(mx > k) cout<<-1<<endl;
        else if(mx==k) cout<<mx<<endl;
        else
        {
            k-=mn;
            if(k%2==0 && diff%2==0) cout<<(k+mn)<<endl;
            else if((k&1)&&diff%2==0) cout<<(k+mn-2)<<endl;
            else  cout<<(k+mn-1)<<endl;
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        if(x==y)
        {
            if(k==x) cout<<x<<endl;
            else if(k<x) cout<<-1<<endl;
            else
            {
                if((k-x)&1) cout<<(k-2)<<endl;
                else cout<<k<<endl;
            }
        }

        else
        {
            ll mx = max(x,y);
            ll mn = min(x,y);
            ll diff = mx-mn;

            if(mx > k) cout<<-1<<endl;
            else if(mx==k) cout<<mn<<endl;
            else
            {
                if(k%2==0 && diff%2==0) cout<<k<<endl;
                else if((k&1)&&diff%2==0) cout<<(k-2)<<endl;
                else  cout<<(k-1)<<endl;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        if(x==y)
        {
            if(k==x) cout<<x<<endl;
            else if(k<x) cout<<-1<<endl;
            else cout<<(k-2)<<endl;
        }

        else
        {
            ll mx = max(x,y);
            ll mn = min(x,y);
            ll diff = mx-mn;

            if(mx > k) cout<<-1<<endl;
            else if(mx==k) cout<<mn<<endl;
            else cout<<(mx - mn + k -2)<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        bool flag=true;
        ll x,y,k;
        cin>>x>>y>>k;
        x = abs(x);
        y = abs(y);

        ll mx = max(x,y);
        ll mn = min(x,y);
        ll diff = mx-mn;

        if(mx > k) cout<<-1<<endl;
        else if(mx==k) cout<<mn<<endl;
        else
        {
            k-=mn;
            if(k%2==0 && diff%2==0) cout<<(k+mn)<<endl;
            else if((k&1)&&diff%2==0) cout<<(k+mn-2)<<endl;
            else  cout<<(k+mn-1)<<endl;
        }

    }
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;

		if(x>y)
		{
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if((k+y)%2==x%2)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-2<<endl;
		  }
		  else
		  {
            cout<<k+y-1<<endl;
		  }

		}
		}
		else if(x<y)
		{
			swap(x,y);
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if((k+y)%2==x%2)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-2<<endl;
		  }
		  else
		  {
            cout<<k+y-1<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
		}
		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	// your code goes here
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;

		if(x>y)
		{
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-2<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y<<endl;
		  }

		}
		}
		else if(x<y)
		{
			swap(x,y);
			if(k<x)
		 	cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-2<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y-1<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
		}
		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	// your code goes here
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;

		if(x!=y)
		{
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
			cout<<"@";
		}

		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	// your code goes here
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;

		if(x>y)
		{
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==x%2)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-2<<endl;
		  }
		  else
		  {
            cout<<k+y-1<<endl;
		  }

		}
		}
		else if(x<y)
		{
			swap(x,y);
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==x%2)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-2<<endl;
		  }
		  else
		  {
            cout<<k+y-1<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
		}
		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	// your code goes here
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;
		if(x>y)
		{
		if(x!=y)
		{
		if(k<=x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
		}

		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
		                                            }
	else
	{
				if(x!=y)
		{
		if(k<y)
		cout<<"-1"<<endl;
		else
		{
			k=k-x;
			d+=x;
		  if((y-x)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+x<<endl;
		  	else
		  	cout<<k+x-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+x<<endl;
		  	}
		  	else
		  	cout<<k+x<<endl;
		  }

		}
		}
      else
      {
      		if(k<y)
		{
			cout<<"-1"<<endl;

		}

		else
		{
			k=k-x+1;
			d+=x-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	}
	// your code goes here
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;
		if(x>y)
		{
		if(x!=y)
		{
		if(k<=x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
		}

		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
		                                            }
	else
	{
				if(x!=y)
		{
		if(k<=y)
		cout<<"-1"<<endl;
		else
		{
			k=k-x;
			d+=x;
		  if((y-x)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+x<<endl;
		  	else
		  	cout<<k+x-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+x-1<<endl;
		  	}
		  	else
		  	cout<<k+x<<endl;
		  }

		}
		}
      else
      {
      		if(k<y)
		{
			cout<<"-1"<<endl;

		}

		else
		{
			k=k-x+1;
			d+=x-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	}
	// your code goes here
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long t,k,x,y,d=0;
	cin>>t;
	while(t--)
	{
		d=0;
		cin>>x>>y>>k;

		if(x>y)
		{
		if(k<x)
		cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y<<endl;
		  }

		}
		}
		else if(x<y)
		{
			swap(x,y);
			if(k<x)
		 	cout<<"-1"<<endl;
		else
		{
			k=k-y;
			d+=y;
		  if((x-y)%2==0)
		  {
		  	if(k%2==0)
		  	cout<<k+y<<endl;
		  	else
		  	cout<<k+y-1<<endl;
		  }
		  else
		  {
		  	if(k%2==0)
		  	{
		  		cout<<k+y-1<<endl;
		  	}
		  	else
		  	cout<<k+y-1<<endl;
		  }

		}
		}
      else
      {
      		if(k<x)
		{
			cout<<"-1"<<endl;
		}
		else
		{
			k=k-y+1;
			d+=y-1;
			if(k%2==0)
			{
				cout<<k-2+d<<endl;
			}
			else
			cout<<d+k<<endl;
		}
      }
	}
	// your code goes here
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    while(q--){
        cin >> n >> m >> k;
        if(k<n || k<m)cout << -1 << endl;
        else if(n==m){
            if((k-n)%2==0)cout << k;
            else cout << k-2;
            cout << endl;
        }else if((n+m)%2==0){
            if((k-n)%2==0)cout << k << endl;
            else cout << k-2 << endl;
        }else cout << k-1 <<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    for(int i=0;i<q;i++){
        cin >> n >> m >> k;
        if(n==m && m==k){
            cout << k << endl;
        }
        else if(k<n || k<m)cout << -1 << endl;
        else{
            if(abs(n-m)%2==0){
                if(k%2==0){
                    cout << k << endl;
                }else{
                    cout << k-2 << endl;
                }
            }else{
                cout << k-1 << endl;
            }
        }

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    while(q--){
        cin >> n >> m >> k;
        if(k<n || k<m)cout << -1 << endl;
        else if(n==m){
            if((k-n)%2)cout << k;
            else cout << k-2;
            cout << endl;
        }else if((n+m)%2==0){
            if(k%2==0)cout << k << endl;
            else cout << k-2 << endl;
        }else cout << k-1 <<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    while(q--){
        cin >> n >> m >> k;
        if(k<n || k<m)cout << -1 << endl;
        else if(n==m){
            if((k-n)%2==0)cout << k;
            else cout << k-2;
            cout << endl;
        }else if((n+m)%2==0){
            if(k%2==0)cout << k << endl;
            else cout << k-2 << endl;
        }else cout << k-1 <<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    for(int i=0;i<q;i++){
        cin >> n >> m >> k;

        if(k<n || k<m)cout << -1 << endl;
        else if(k==2){
            if(n==1 && m==1)cout << 0 << endl;
            else if((n+m)%2)cout << 1 << endl;
            else cout << k << endl;
        }
        else if(k==1){
            if((n+m)%2)cout << 0 << endl;
            else cout << 1 << endl;
        }
        else{
            if(abs(n-m)%2==0){
                if(k%2==0){
                    cout << k << endl;
                }else{
                    cout << k-2 << endl;
                }
            }else{
                cout << k-1 << endl;
            }
        }

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    for(int i=0;i<q;i++){
        cin >> n >> m >> k;
        if(k<n || k<m)cout << -1 << endl;
        else if(n==m){
            if((k-n)%2==0)
            cout << k << endl;
            else cout << k-2 << endl;
        }

        else{
            if(abs(n-m)%2==0){
                if(k%2==0){
                    cout << k << endl;
                }else{
                    cout << k-2 << endl;
                }
            }else{
                cout << k-1 << endl;
            }
        }

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    for(int i=0;i<q;i++){
        cin >> n >> m >> k;
        if(k<n || k<m)cout << -1 << endl;
        else if(k==2){
            if(n==1 && m==1)cout << 0 << endl;
            else if((n+m)%2)cout << 1 << endl;
            else cout << k << endl;
        }
        else if(k==1){
            if((n+m)%2)cout << 0 << endl;
            else cout << 1 << endl;
        }
        else if((n+m)%2==0){
            if(k%2==0)
            cout << k << endl;
            else cout << k-2 << endl;
        }

        else{
            cout << k-1 << endl;
        }

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int q;
    cin >> q;
    ll n,m,k;
    while(q--){
        cin >> n >> m >> k;
        if(k<n || k<m)cout << -1 << endl;
        else if(n==m){
            if(k%2)cout << k;
            else cout << k-2;
            cout << endl;
        }else if((n+m)%2==0){
            if(k%2==0)cout << k << endl;
            else cout << k-2 << endl;
        }else cout << k-1 <<endl;
    }

}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll left_move = k - y;
        if (left_move == 1) {
            cout << x - 1 << endl;
        } else {
            cout << x + left_move << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        if (y % 2 == k % 2) {
            cout << k << endl;
        } else {
            cout << k - 2 << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll lk = k - y;
        ll ly = y - x;
        if (lk == 1) {
            if (ly % 2 == 1) {
                cout << k - 1 << endl;
            } else {
                cout << k - 2 << endl;
            }
        } else {
            if (ly % 2 == 1) {
                cout << k - 1 << endl;
            } else {
                cout << k << endl;
            }
        }
    }

    return 0;
}