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

int fun(vector<int> &x, vector<int> &y)
{
    int cnt = 0;
    int n = x.size();
    
    for(int i = 0; i < n; i++)
    {
        bool flag = true;
        
        for(int j = 0; j < n; j++)
        {
            if(x[(i + j) % n] >= y[j])
                flag = false;
        }
        
        if(flag == true)
            cnt ++;
    }
    return cnt;
}

void solve()
{
    int n;
    cin >> n;
    
    vector<int> a(n), b(n), c(n);
    
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < n; i++)
        cin >> b[i];
    for(int i = 0; i < n; i++)
        cin >> c[i];
        
    cout << 1LL * n * fun(a, b) * fun(b, c) << endl;
}

int main() {
    // your code goes here
    int t;
    cin >> t;
    
    while(t--)
        solve();
}