	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define mod2 998244353
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define sz(v) ((ll)(v).size())
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define pb push_back
	#define f first
	#define s second
	#define dbg(a) cout<<#a<<" --> "<<(a)<<"\n";
	#define forn(i,n) for (ll i=0; i<(n); ++i)
	#define fornd(i,n) for (ll i=(n)-1; i>=0; --i)
	#define forab(i,a,b) for (ll i=(a); i<=(b); ++i)
	#define read(a) ll a; cin >> a;
	#define reads(s) string s; cin >> s;
	#define readb(a, b) ll a, b; cin >> a >> b;
	#define readc(a, b, c) ll a, b, c; cin >> a >> b >> c;
	#define readarr(a, n) int a[(n) + 1] = {}; forab(i, 0, (n-1)) {cin >> a[i];}
	#define readmat(a, n, m) int a[n + 1][m + 1] = {}; forab(i, 1, n) {forab(j, 1, m) cin >> a[i][j];}
	#define readv(A,n) vll A(n,0); forn(i,n) cin>>A[i];
	#define print(a) cout << a << endl;
	#define printarr(a, n) forab (i, 1, n) cout << a[i] << " "; cout << endl;
	#define printv(v) for (int i: v) cout << i << " "; cout << endl;
	#define printmat(a, n, m) forab(i, 1, n) {forab (j, 1, m) cout << a[i][j] << " "; cout << endl;}

	#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
	using namespace std;
	bool isPowerOfTwo(int x) {
    return x && (!(x & (x - 1)));
	}
	int takemod(int x) {
    return (x % mod + mod) % mod;
	}

	int fpow(int base, int power, int MOD) {
    int result = 1;
    base = base % MOD;
    while (power) {
        if (power & 1)
            result = (result * base) % MOD;
        base = (base * base) % MOD;
        power = power >> 1;
    }
    return result;
	}

	int modinv(int x) {return fpow(x, mod - 2, mod);}
	void solve()
	{
		ll n;
		cin>>n;
		vll A(n);
		forn(i,n)
		{
			cin>>A[i];
			A[i]%=3;
		}
		sort(all(A));
		ll ans=0,a=0,b=0,c=0;
		forn(i,n)
		{
			if(A[i]==0)a++;
			else if(A[i]==1)b++;
			else c++;
		}
		ans+=min(b,c);
		ans+=a;
		b-=min(b,c);c-=min(b,c);
		ans+=(b/3);ans+=(c/3);
		cout<<ans<<endl;

	}
	int main()
	{
		#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
		#endif
		fast_io;
		int T=1;
		cin>>T;
		// cin.ignore(); must be there when using getline(cin, s)
		while(T--)
		{
			solve();
		}
		return 0;
	}


#include<bits/stdc++.h>

using namespace std;

int t, n;
int cnt[3];

int main(){
    cin >> t;
    for(int tc = 0; tc < t; ++tc){
        memset(cnt, 0, sizeof cnt);
        cin >> n;
        for(int i = 0; i < n; ++i){
            int x;
            cin >> x;
            ++cnt[x % 3];
        }

    	int res = cnt[0];
        int mn = min(cnt[1], cnt[2]);
        res += mn;
        cnt[1] -= mn, cnt[2] -= mn;
        res += (cnt[1] + cnt[2]) / 3;
        cout << res << endl;
    }
    return 0;
}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define mod2 998244353
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define sz(v) ((ll)(v).size())
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define pb push_back
	#define f first
	#define s second
	#define dbg(a) cout<<#a<<" --> "<<(a)<<"\n";
	#define forn(i,n) for (ll i=0; i<(n); ++i)
	#define fornd(i,n) for (ll i=(n)-1; i>=0; --i)
	#define forab(i,a,b) for (ll i=(a); i<=(b); ++i)
	#define read(a) ll a; cin >> a;
	#define reads(s) string s; cin >> s;
	#define readb(a, b) ll a, b; cin >> a >> b;
	#define readc(a, b, c) ll a, b, c; cin >> a >> b >> c;
	#define readarr(a, n) int a[(n) + 1] = {}; forab(i, 0, (n-1)) {cin >> a[i];}
	#define readmat(a, n, m) int a[n + 1][m + 1] = {}; forab(i, 1, n) {forab(j, 1, m) cin >> a[i][j];}
	#define readv(A,n) vll A(n,0); forn(i,n) cin>>A[i];
	#define print(a) cout << a << endl;
	#define printarr(a, n) forab (i, 1, n) cout << a[i] << " "; cout << endl;
	#define printv(v) for (int i: v) cout << i << " "; cout << endl;
	#define printmat(a, n, m) forab(i, 1, n) {forab (j, 1, m) cout << a[i][j] << " "; cout << endl;}

	#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
	using namespace std;
	bool isPowerOfTwo(int x) {
    return x && (!(x & (x - 1)));
	}
	int takemod(int x) {
    return (x % mod + mod) % mod;
	}

	int fpow(int base, int power, int MOD) {
    int result = 1;
    base = base % MOD;
    while (power) {
        if (power & 1)
            result = (result * base) % MOD;
        base = (base * base) % MOD;
        power = power >> 1;
    }
    return result;
	}

	int modinv(int x) {return fpow(x, mod - 2, mod);}
	void solve()
	{
		int n;
		cin>>n;
		vi arr(n);
		int ans=0,a=0,b=0,c=0;
		forn(i,n)
		{

			int x;
			if((x%3) ==0) a++;
			else if((x%3) ==1) b++;
			else c++;

		}

		ans=a;
		ans+=min(b,c);
		b-=min(b,c);
		c-=min(b,c);
		ans+=((b+c)/3);
		cout<<ans<<endl;

	}
	int main()
	{
		#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
		#endif
		fast_io;
		int T=1;
		cin>>T;
		// cin.ignore(); must be there when using getline(cin, s)
		while(T--)
		{
			solve();
		}
		return 0;
	}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define mod2 998244353
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define sz(v) ((ll)(v).size())
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define pb push_back
	#define f first
	#define s second
	#define dbg(a) cout<<#a<<" --> "<<(a)<<"\n";
	#define forn(i,n) for (ll i=0; i<(n); ++i)
	#define fornd(i,n) for (ll i=(n)-1; i>=0; --i)
	#define forab(i,a,b) for (ll i=(a); i<=(b); ++i)
	#define read(a) ll a; cin >> a;
	#define reads(s) string s; cin >> s;
	#define readb(a, b) ll a, b; cin >> a >> b;
	#define readc(a, b, c) ll a, b, c; cin >> a >> b >> c;
	#define readarr(a, n) int a[(n) + 1] = {}; forab(i, 0, (n-1)) {cin >> a[i];}
	#define readmat(a, n, m) int a[n + 1][m + 1] = {}; forab(i, 1, n) {forab(j, 1, m) cin >> a[i][j];}
	#define readv(A,n) vll A(n,0); forn(i,n) cin>>A[i];
	#define print(a) cout << a << endl;
	#define printarr(a, n) forab (i, 1, n) cout << a[i] << " "; cout << endl;
	#define printv(v) for (int i: v) cout << i << " "; cout << endl;
	#define printmat(a, n, m) forab(i, 1, n) {forab (j, 1, m) cout << a[i][j] << " "; cout << endl;}

	#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
	using namespace std;
	bool isPowerOfTwo(int x) {
    return x && (!(x & (x - 1)));
	}
	int takemod(int x) {
    return (x % mod + mod) % mod;
	}

	int fpow(int base, int power, int MOD) {
    int result = 1;
    base = base % MOD;
    while (power) {
        if (power & 1)
            result = (result * base) % MOD;
        base = (base * base) % MOD;
        power = power >> 1;
    }
    return result;
	}

	int modinv(int x) {return fpow(x, mod - 2, mod);}
	void solve()
	{
		int n;
		cin>>n;
		vi arr(n);
		int ans=0,a=0,b=0,c=0;
		forn(i,n)
		{

			int x;
			cin>>x;

			if((x%3) ==0) a++;
			else if((x%3) ==1) b++;
			else c++;

		}

		ans=a;
		ans+=min(b,c);
		b-=min(b,c);
		c-=min(b,c);
		ans+=((b+c)/3);
		cout<<ans<<endl;

	}
	int main()
	{
		#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
		#endif
		fast_io;
		int T=1;
		cin>>T;
		// cin.ignore(); must be there when using getline(cin, s)
		while(T--)
		{
			solve();
		}
		return 0;
	}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define mod2 998244353
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define sz(v) ((ll)(v).size())
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define pb push_back
	#define f first
	#define s second
	#define dbg(a) cout<<#a<<" --> "<<(a)<<"\n";
	#define forn(i,n) for (ll i=0; i<(n); ++i)
	#define fornd(i,n) for (ll i=(n)-1; i>=0; --i)
	#define forab(i,a,b) for (ll i=(a); i<=(b); ++i)
	#define read(a) ll a; cin >> a;
	#define reads(s) string s; cin >> s;
	#define readb(a, b) ll a, b; cin >> a >> b;
	#define readc(a, b, c) ll a, b, c; cin >> a >> b >> c;
	#define readarr(a, n) int a[(n) + 1] = {}; forab(i, 0, (n-1)) {cin >> a[i];}
	#define readmat(a, n, m) int a[n + 1][m + 1] = {}; forab(i, 1, n) {forab(j, 1, m) cin >> a[i][j];}
	#define readv(A,n) vll A(n,0); forn(i,n) cin>>A[i];
	#define print(a) cout << a << endl;
	#define printarr(a, n) forab (i, 1, n) cout << a[i] << " "; cout << endl;
	#define printv(v) for (int i: v) cout << i << " "; cout << endl;
	#define printmat(a, n, m) forab(i, 1, n) {forab (j, 1, m) cout << a[i][j] << " "; cout << endl;}

	#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
	using namespace std;
	bool isPowerOfTwo(int x) {
    return x && (!(x & (x - 1)));
	}
	int takemod(int x) {
    return (x % mod + mod) % mod;
	}

	int fpow(int base, int power, int MOD) {
    int result = 1;
    base = base % MOD;
    while (power) {
        if (power & 1)
            result = (result * base) % MOD;
        base = (base * base) % MOD;
        power = power >> 1;
    }
    return result;
	}

	int modinv(int x) {return fpow(x, mod - 2, mod);}
	void solve()
	{
		int n;
		cin>>n;
		vi arr(n);
		int ans=0,a=0,b=0,c=0;
		forn(i,n)
		{
			cin>>arr[i];

			if((arr[i]%3) ==0) a++;
			else if((arr[i]%3) ==1) b++;
			else c++;

		}

		ans=a;
		ans+=min(b,c);
		b-=min(b,c);
		c-=min(b,c);
		ans+=((b+c)/3);
		cout<<ans<<endl;

	}
	int main()
	{
		#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
		#endif
		fast_io;
		int T=1;
		cin>>T;
		// cin.ignore(); must be there when using getline(cin, s)
		while(T--)
		{
			solve();
		}
		return 0;
	}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vLL vetor(n);
        map<LL, int> mapa;
        for(int i=0; i<n; i++){
            cin >> vetor[i];
            mapa[vetor[i]]++;
        }
        sort(vetor.begin(), vetor.end());
        for(int i=n-1; i>=0 ; i--){
            if(vetor[i]%3==0){
                marked++;
                cont++;
                continue;
            }
            if(vetor[i]%3!=vetor[i] and mapa[vetor[i]%3]){
                mapa[vetor[i]%3]--;
                cont++;
                continue;
            }
            for(int j=0; j<i; j++){
                if(vetor[j]==-1) continue;
                if(mapa[vetor[j]]){
                    mapa[vetor[j]]--;
                    vetor[i]+=vetor[j];
                    vetor[j]=-1;
                }else vetor[j] = -1;
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    break;
                }
                if(vetor[i]%3!=vetor[i] and mapa[vetor[i]%3]){
                    mapa[vetor[i]%3]--;
                    cont++;
                    continue;
                }
            }
        }
        /*

        1 2 2 2 4

        */
        cout << cont << endl;
    }
    /*

    1 1 1 1 2 2 2 2
    2 1
    2 1
    2 1
    2 1
    */
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0;
        cin >> n;
        map<int, int> mapa;
        vi vetor(n);
        for(int i=0; i<n; i++)
            cin >> vetor[i];
        sort(vetor.begin(), vetor.end());
        for(int i=vetor.size()-1; i>=0; i--){
            if(vetor[i]%3==0){
                cont++;
                vetor[i] = -1;
            }
            if(vetor[i]==-1) continue;
            for(int j=i-1; j>=0; j--){
                if(vetor[j]==-1) continue;

                if((vetor[i]+vetor[j])%3==0){
                    cont++;
                    vetor[i] = -1;
                    vetor[j] = -1;
                    break;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        //cout << endl;
        int s = 0;
        for(int i=vetor.size()-1; i>=0; i--){
            if(vetor[i]!=-1) s+=vetor[i];
            if(s%3==0 and s>0){
                s = 0;
                cont++;
            }

        }
        cout << cont << endl;
    }
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vLL vetor(n);
        for(int i=0; i<n; i++)
            cin >> vetor[i];
        //sort(vetor.begin(), vetor.end());
        for(int i=vetor.size()-1; i>=0; i--){
            if(vetor[i]%3==0){
                cont++;
                vetor[i] = -1;
                marked++;
            }
            if(vetor[i]==-1) continue;
            for(int j=i-1; j>=0; j--){
                if(vetor[j]==-1) continue;

                if((vetor[i]+vetor[j])%3==0){
                    cont++;
                    vetor[i] = -1;
                    vetor[j] = -1;
                    marked+=2;
                    break;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        //cout << endl;
        while(marked<vetor.size()-1){
            for(int i=vetor.size()-1; i>=0 and marked<vetor.size()-1; i--){
                if(vetor[i]%3==0){
                    cont++;
                    vetor[i] = -1;
                    marked++;
                }
                if(vetor[i]==-1) continue;
                for(int j=i-1; j>=0; j--){
                    if(vetor[j]==-1) continue;
                    vetor[i]+= vetor[j];
                    vetor[j] = -1;
                    marked++;
                    break;
                }
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    vetor[i] = -1;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        cout << cont << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {

    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<int> vetor(n);
        set<int> arr;

        vector<int> count(3, 0);

        for(int i=0; i<n; i++) {
            cin >> vetor[i];
            count[vetor[i]%3]++;
        }
        int ans = count[0];

        int mini = min(count[1], count[2]);

        ans+=((count[1]+count[2]-(2*mini))/3) + mini;
        cout << ans << endl;
    }

    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vi vetor(n);
        for(int i=0; i<n; i++)
            cin >> vetor[i];
        sort(vetor.begin(), vetor.end());
        for(int i=vetor.size()-1; i>=0; i--){
            if(vetor[i]%3==0){
                cont++;
                vetor[i] = -1;
                marked++;
            }
            if(vetor[i]==-1) continue;
            for(int j=i-1; j>=0; j--){
                if(vetor[j]==-1) continue;

                if((vetor[i]+vetor[j])%3==0){
                    cont++;
                    vetor[i] = -1;
                    vetor[j] = -1;
                    marked+=2;
                    break;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        //cout << endl;
        while(marked<vetor.size()-1){
            for(int i=vetor.size()-1; i>=0 and marked<vetor.size()-1; i--){
                if(vetor[i]%3==0){
                    cont++;
                    vetor[i] = -1;
                    marked++;
                }
                if(vetor[i]==-1) continue;
                for(int j=i-1; j>=0; j--){
                    if(vetor[j]==-1) continue;
                    vetor[i]+= vetor[j];
                    vetor[j] = -1;
                    marked++;
                    break;
                }
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    vetor[i] = -1;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        cout << cont << endl;
    }
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vLL vetor(n);
        for(int i=0; i<n; i++)
            cin >> vetor[i];
        sort(vetor.begin(), vetor.end());
        for(int i=n-1; i>=0 and marked<vetor.size()-1; i--){
            if(vetor[i]%3==0){
                marked++;
                cont++;
                continue;
            }
            for(int j=0; j<i; j++){
                if(vetor[j]==-1) continue;
                vetor[i]+=vetor[j];
                vetor[j] = -1;
                marked++;
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    break;
                }
            }
        }
        /*
        1 1 1 1 1 2 2
        1 2
        1 2

        */
        cout << cont << endl;
    }
    /*

    1 1 1 1 2 2 2 2
    2 1
    2 1
    2 1
    2 1
    */
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vLL vetor(n);
        for(int i=0; i<n; i++)
            cin >> vetor[i];
        sort(vetor.begin(), vetor.end());
        for(int i=n-1; i>=0 ; i--){
            if(vetor[i]%3==0){
                marked++;
                cont++;
                continue;
            }
            for(int j=0; j<i; j++){
                if(vetor[j]==-1) continue;
                vetor[i]+=vetor[j];
                vetor[j] = -1;
                marked++;
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    break;
                }
            }
        }
        /*
        1 1 1 1 1 2 2
        1 2
        1 2

        */
        cout << cont << endl;
    }
    /*

    1 1 1 1 2 2 2 2
    2 1
    2 1
    2 1
    2 1
    */
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vLL vetor(n);
        for(int i=0; i<n; i++)
            cin >> vetor[i];
        sort(vetor.begin(), vetor.end());
        for(int i=vetor.size()-1; i>=0; i--){
            if(vetor[i]%3==0){
                cont++;
                vetor[i] = -1;
                marked++;
            }
            if(vetor[i]==-1) continue;
            for(int j=i-1; j>=0; j--){
                if(vetor[j]==-1) continue;

                if((vetor[i]+vetor[j])%3==0){
                    cont++;
                    vetor[i] = -1;
                    vetor[j] = -1;
                    marked+=2;
                    break;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        //cout << endl;
        while(marked<vetor.size()-1){
            for(int i=vetor.size()-1; i>=0 and marked<vetor.size()-1; i--){
                if(vetor[i]%3==0){
                    cont++;
                    vetor[i] = -1;
                    marked++;
                }
                if(vetor[i]==-1) continue;
                for(int j=i-1; j>=0; j--){
                    if(vetor[j]==-1) continue;
                    vetor[i]+= vetor[j];
                    vetor[j] = -1;
                    marked++;
                    break;
                }
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    vetor[i] = -1;
                }
            }
        }
        //for(int i=0; i<vetor.size(); i++) cout << vetor[i] << " ";
        cout << cont << endl;
    }
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);
    #endif
    int t;
    cin >> t;
    while(t--){
        int n, cont = 0, marked = 0;
        cin >> n;
        vLL vetor(n);
        map<LL, int> mapa;
        for(int i=0; i<n; i++){
            cin >> vetor[i];
            mapa[vetor[i]]++;
        }
        sort(vetor.begin(), vetor.end());
        for(int i=n-1; i>=0 ; i--){
            if(vetor[i]%3==0){
                marked++;
                cont++;
                continue;
            }
            if(vetor[i]%3!=vetor[i] and mapa[vetor[i]%3]){
                mapa[vetor[i]%3]--;
                cont++;
                continue;
            }
            for(int j=0; j<i; j++){
                if(vetor[j]==-1) continue;
                if(mapa[vetor[j]]){
                    mapa[vetor[j]]--;
                    vetor[i]+=vetor[j];
                    vetor[j]=-1;
                }else vetor[j] = -1;
                if(vetor[i]%3==0){
                    cont++;
                    marked++;
                    break;
                }
            }
        }
        /*

        1 2 2 2 4

        */
        cout << cont << endl;
    }
    /*

    1 1 1 1 2 2 2 2
    2 1
    2 1
    2 1
    2 1
    */
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else if(a%3==0)
           z++;

         //A.push_back(a%3);
     }
     ans=z;
     ans+=min(x,y);
     x-=min(x,y);
     y-=min(x,y);

      ans+=(x+y)/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else
           z++;

         //A.push_back(a%3);
     }
     ans+=z;
     ans+=min(x,y);
     x-=min(x,y);
     y-=min(x,y);

          ans+=y/3;
      ans+=x/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else if(a%3==0)
           z++;

         //A.push_back(a%3);
     }
     ans=z;
     k=min(x,y);
     ans+=k;
     x-=k;//min(x,y);
     y-=k;//min(x,y);

      ans+=(x+y)/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else
           z++;

         //A.push_back(a%3);
     }
     ans+=z;
     ans+=min(x,y);
     x-=min(x,y);
     y-=min(x,y);

      ans+=(x+y)/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else
           z++;

         //A.push_back(a%3);
     }
     ans+=z;

          ans+=y/3;
      ans+=x/3;
      y%=3;
      x%=3;
      ans+=min(x,y);
     x-=min(x,y);
     y-=min(x,y);

          ans+=y/3;
      ans+=x/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else
           z++;

         //A.push_back(a%3);
     }
     ans+=z;
     ans+=min(x,y);
     x-=min(x,y);
     if(x<=0)
      {
          y-=min(x,y);

          ans+=y/3;
          cout<<ans<<endl;
          return ;
      }
      //cout<<"x "<<x<<endl;
      ans+=x/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
const ll MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;

void vok()
{
    FAST
    FO
    FI
 }
bool isprime(ll n)
 {
     if(n<=1)
        return false;
     if(n<=3)
       return true;
     if(n==5 or n==7)
       return true;
     if(n%2==0 or n%5==0 or n%3==0)
       return false;
     for(int i=5;i*i<=n;i+=6)
      {
          if(n%i==0 or n%(i+2)==0)
               return false;

      }
      return true;

 }
ll gcd(ll a,ll b)
 {
     if(b==0)
       return a;
     return gcd(b,a%b);
 }
ll lcm(ll a,ll b)
 {
     return ((a*b)/gcd(a,b));
    }

 vector<ll>pp;
void gen()
 {
    vector<bool>prime(33000,true);
    prime[0]=prime[1]=false;
    for(int p=0;p*p<=33000;p++)
     {
         if(prime[p]){
         for(int i=p*p;i<=33000;i+=p)
            prime[i]=false;
         }
     }
    for(int i=0;i<33000;i++)
      if(prime[i])
         pp.push_back(i);
      //  cout<<i<<" ";

 }
ll find(vector<int>&A,int n)
{

}
void solve()
 {
    ll n,m,x,y,a,b,c,d,k,z;
    ll mx=-1;
    ll mi=INT_MAX;

    a=b=x=y=k=c=z=0;
    ll ans=0,res=0;
    cin>>n;
    string str;

    vector<int>A;
    for(int i=0;i<n;i++)
     {
         cin>>a;
         if(a%3==1)
           x++;
         else if(a%3==2)
           y++;
         else
           z++;

         //A.push_back(a%3);
     }
     ans+=z;
     ans+=min(x,y);
     x-=min(x,y);
     if(x<=0)
      {
          cout<<ans<<endl;
          return ;
      }
      //cout<<"x "<<x<<endl;
      ans+=x/3;
      cout<<ans<<endl;

     //cout<<find(A,n)<<endl;

}
int main()
{
  vok();

   int t = 1; cin >> t;
    while(t--)
  {
      solve();

  }

   return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

map<int,int> ma;

int main()
{
    int t,n;
    int a[110];
    int b[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        int top = 0;
        ma.clear();
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]%3 == 0)
            {
                ans++;
                continue;
            }
            if(!ma[a[i]]) b[top++] = a[i];
            ma[a[i]]++;
        }
        for(int i = 0; i < top; i++)
        {
            for(int j = i+1; j < top; j++)
            {
                if(ma[b[i]] && ma[b[j]])
                {
                    int now = min(ma[b[i]], ma[b[j]]);
                    ans += now;
                    ma[b[i]] -= now;
                    ma[b[j]] -= now;
                }
            }
        }
        for(int i = 0; i < top; i++)
        {
            if(ma[b[i]]/3)
            {
                ans += ma[b[i]]/3;
                ma[b[i]] %= 3;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

map<int,int> ma;

int main()
{
    int t,n;
    int a[110];
    int b[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        int top = 0;
        ma.clear();
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]%3 == 0)
            {
                ans++;
                continue;
            }
            if(!ma[a[i]]) b[top++] = a[i];
            ma[a[i]]++;
        }
        for(int i = 0; i < top; i++)
        {
            for(int j = i+1; j < top; j++)
            {
                if(ma[b[i]] && ma[b[j]] && (b[i]+b[j])%3 == 0)
                {
                    int now = min(ma[b[i]], ma[b[j]]);
                    ans += now;
                    ma[b[i]] -= now;
                    ma[b[j]] -= now;
                }
            }
        }
        for(int i = 0; i < top; i++)
        {
            if(ma[b[i]]/3)
            {
                ans += ma[b[i]]/3;
                ma[b[i]] %= 3;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

map<int,int> ma;

int main()
{
    int t,n;
    int a[110];
    int b[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        int top = 0;
        ma.clear();
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]%3 == 0)
            {
                ans++;
                continue;
            }
            if(!ma[a[i]]) b[top++] = a[i];
            ma[a[i]]++;
        }
        for(int i = 0; i < top; i++)
        {
            for(int j = i+1; j < top; j++)
            {
                if(ma[b[i]] && ma[b[j]] && (b[i]+b[i])%3 == 0)
                {
                    int now = min(ma[b[i]], ma[b[j]]);
                    ans += now;
                    ma[b[i]] -= now;
                    ma[b[j]] -= now;
                }
            }
        }
        for(int i = 0; i < top; i++)
        {
            if(ma[b[i]]/3)
            {
                ans += ma[b[i]]/3;
                ma[b[i]] %= 3;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

int vis[5];

int main()
{
    int t,n;
    int a[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        memset(vis, 0, sizeof(vis));
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            vis[a[i]%3]++;
        }
        ans = vis[0]+min(vis[1], vis[2])+abs(vis[1]-vis[2])/3;
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

int main()
{
    int t,n;
    int a[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        ll sum = 0;
        int tmp = 0;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
            tmp += a[i];
            if(sum%3 == 0)
            {
                ans += tmp/3;
                tmp = 0;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

int main()
{
    int t,n;
    int a[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        ll sum = 0;
        ll tmp = 0;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
            tmp += a[i];
            if(sum%3 == 0)
            {
                ans += tmp/3;
                tmp = 0;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

map<int,int> ma;

int main()
{
    int t,n;
    int a[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        ma.clear();
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]%3 == 0)
            {
                ans++;
            }
            ma[a[i]]++;
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(ma[a[i]] && ma[a[j]] && (a[i]+a[j])%3 == 0 && a[i]%3 && a[j]%3)
                {
                    int now = min(ma[a[i]], ma[a[j]]);
                    ans += now;
                    ma[a[i]] -= now;
                    ma[a[j]] -= now;
                }
            }
        }
        for(int i = 0; i < n; i++)
        {
            if(ma[a[i]]/3)
            {
                ans += ma[a[i]]/3;
                ma[a[i]] %= 3;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

int vis[5];

int main()
{
    int t,n;
    int a[110];
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        memset(vis, 0, sizeof(vis));
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            vis[a[i]%3]++;
        }
        cout << vis[0] << ' ' << vis[1] << ' ' << vis[2] << endl;
        ans = vis[0]+min(vis[1], vis[2])+abs(vis[1]-vis[2])/3;
        printf("%d\n",ans);
    }
    return 0;
}


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

int main()
{
    int t;
     map<int,int>cnt;
    cin>>t;
    while(t--){
        int n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]%3]++;
        }
        int ans=0,on=0,to=0;
        for(auto &i:cnt){
           if(i.first==0)ans+=i.second;
           else if(i.first==1)on+=i.second;
           else if(i.first==2)to+=i.second;

        }
        int te=min(on,to);
        ans+=te;
        on-=te; to-=te;

        cout<<ans+(on/3)+(to/3)<<endl;
        ans=0; on=0; to=0; cnt.clear();
    }

      //0 2 1 0 2
    return 0;
}


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

int main()
{
    int t;
     map<int,int>cnt;
    cin>>t;
    while(t--){
        int n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]%3]++;
        }
        int ans=0;
        ans+=cnt[0];
        ans+=min(cnt[1],cnt[2]);
        cnt[1]-=min(cnt[1],cnt[2]); cnt[2]-=min(cnt[1],cnt[2]);

        ans+=cnt[1]/3;
        ans+=cnt[2]/3;

        cout<<ans<<endl;
        ans=0;  cnt.clear();
    }

      //0 2 1 0 2
    return 0;
}


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

int main()
{
    int t;
     map<int,int>cnt;
    cin>>t;
    while(t--){
        int n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]%3]++;
        //cout<<a[i]%3<<endl;
        }
        int ans=0,on=0,to=0;
        for(auto &i:cnt){
           if(i.first==0)ans+=i.second;
           else if(i.first==1)on+=i.second;
           else if(i.first==2)to+=i.second;

        }
        ans+=min(on,to);
        on-=min(on,to); to-=min(on,to);
        cout<<ans+(on/3)<<endl;
        ans=0; on=0; to=0; cnt.clear();
    }

      //0 2 1 0 2
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    map<int,int>cnt;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n;
        for(int i=0;i<n;i++){
        cin>>x;
        cnt[x%3]++;
        }
        int ans=0;
        ans+=cnt[0];
        ans+=min(cnt[1],cnt[2]);
        cnt[1]-=min(cnt[1],cnt[2]); cnt[2]-=min(cnt[1],cnt[2]);

        ans+=cnt[1]/3;
        ans+=cnt[2]/3;

        cout<<ans<<endl;
        ans=0;  cnt.clear();
    }

      //0 2 1 0 2
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    map<long long int,long long int>cnt;
    cin>>t;
    while(t--){
            long long int n,x,ans=0;
            cnt.clear();
            cin>>n;
            while(n--){
                    cin>>x;
                    cnt[x%3]++;
            }

            ans+=cnt[0];
            ans+=min(cnt[1],cnt[2]);
            cnt[1]-=min(cnt[1],cnt[2]);
            cnt[2]-=min(cnt[1],cnt[2]);
            ans+=(cnt[1]+cnt[2])/3;
            cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    map<int,int>cnt;
    cin>>t;
    while(t--){
            int n,x,ans=0;
            cnt.clear();
            cin>>n;
            while(n--){
                    cin>>x;
                    cnt[x%3]++;
            }

            ans+=cnt[0];
            ans+=min(cnt[1],cnt[2]);
            cnt[1]-=min(cnt[1],cnt[2]);
            cnt[2]-=min(cnt[1],cnt[2]);
            ans+=(cnt[1]+cnt[2])/3;
            cout<<ans<<endl;
    }
    return 0;
}


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

int main()
{
    int t;
     map<int,int>cnt;
    cin>>t;
    while(t--){
        int n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]%3]++;
        }
        int ans=0,on=0,to=0;
        for(auto &i:cnt){
           if(i.first==0)ans+=i.second;
           else if(i.first==1)on+=i.second;
           else if(i.first==2)to+=i.second;

        }
        ans+=min(on,to);
        on-=min(on,to); to-=min(on,to);

        cout<<ans+(on/3)+(to/3)<<endl;
        ans=0; on=0; to=0; cnt.clear();
    }

      //0 2 1 0 2
    return 0;
}


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

int main()
{
    int t;
     map<int,int>cnt;
    cin>>t;
    while(t--){
        int n,a[200000];
        cin>>n;
        for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]%3]++;
        //cout<<a[i]%3<<endl;
        }
        int ans=0,on=0,to=0;
        for(auto &i:cnt){
           if(i.first==0)ans+=i.second;
           else if(i.first==1)on+=i.second;
           else if(i.first==2)to+=i.second;

        }
        ans+=min(on,to);
        on-=min(on,to); to-=min(on,to);
        cout<<ans+on/3<<endl;
        ans=0; on=0; to=0; cnt.clear();
    }

      //0 2 1 0 2
    return 0;
}


#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <string.h>
#include <vector>
#include <math.h>
#include <string>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <deque>
#include <queue>
#include <stack>
#include<stdio.h>
#define lld long long int
#define forn(i,n) for(int i = 0; i < n;i++)
// #define INT_MAX 2148483647
// #define INT_MIN -2147483648

using namespace std;

const int N = 100001;
const int M =  998244353;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif

	lld t;
	cin>>t;
	while(t--){
		lld n;
		cin>>n;
		vector<lld> a(n);
		forn(i,n) cin >> a[i];
		lld m[3] = {0,0,0};
		for(int i = 0; i < n;i++){
			m[a[i]%3]++;
		}
		lld ans = m[0];
		while(m[1] > 0 && m[2] > 0){
			ans++;
			m[1]--;
			m[2]--;
		}
		cout << ans + m[1]/3 << '\n';
	}

}


#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <string.h>
#include <vector>
#include <math.h>
#include <string>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <deque>
#include <queue>
#include <stack>
#include<stdio.h>
#define lld long long int
#define forn(i,n) for(int i = 0; i < n;i++)
// #define INT_MAX 2148483647
// #define INT_MIN -2147483648

using namespace std;

const int N = 100001;
const int M =  998244353;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif

	lld t;
	cin>>t;
	while(t--){
		lld n;
		cin>>n;
		vector<int> a(n);
		forn(i,n) cin >> a[i];
		int m[3] = {0,0,0};
		for(int i = 0; i < n;i++){
			m[a[i]%3]++;
		}
		cout << m[0] + min(m[1],m[2]) + max(0,m[1] - m[2])/3 <<'\n';
	}

}


#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <string.h>
#include <vector>
#include <math.h>
#include <string>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <deque>
#include <queue>
#include <stack>
#include<stdio.h>
#define lld long long int
#define forn(i,n) for(int i = 0; i < n;i++)
// #define INT_MAX 2148483647
// #define INT_MIN -2147483648

using namespace std;

const int N = 100001;
const int M =  998244353;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif

	lld t;
	cin>>t;
	while(t--){
		lld n;
		cin>>n;
		vector<int> a(n);
		forn(i,n) cin >> a[i];
		int m[3] = {0,0,0};
		for(int i = 0; i < n;i++){
			m[a[i]%3]++;
		}
		cout << m[0] + min(m[0],m[1]) + max(0,m[1] - m[2])/3 <<'\n';
	}

}


#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <string.h>
#include <vector>
#include <math.h>
#include <string>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <deque>
#include <queue>
#include <stack>
#include<stdio.h>
#define lld long long int
#define forn(i,n) for(int i = 0; i < n;i++)
// #define INT_MAX 2148483647
// #define INT_MIN -2147483648

using namespace std;

const int N = 100001;
const int M =  998244353;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif

	lld t;
	cin>>t;
	while(t--){
		lld n;
		cin>>n;
		vector<int> a(n);
		forn(i,n) cin >> a[i];
		int m[3] = {0,0,0};
		for(int i = 0; i < n;i++){
			m[a[i]%3]++;
		}
		cout << m[2] + min(m[0],m[1]) + max(0,m[1] - m[2])/3 <<'\n';
	}

}


#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <string.h>
#include <vector>
#include <math.h>
#include <string>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <deque>
#include <queue>
#include <stack>
#include<stdio.h>
#define lld long long int
#define forn(i,n) for(int i = 0; i < n;i++)
// #define INT_MAX 2148483647
// #define INT_MIN -2147483648

using namespace std;

const int N = 100001;
const int M =  998244353;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif

	lld t;
	cin>>t;
	while(t--){
		lld n;
		cin>>n;
		vector<int> a(n);
		forn(i,n) cin >> a[i];
		int m[3] = {0,0,0};
		for(int i = 0; i < n;i++){
			m[a[i]%3]++;
		}
		lld ans = m[0];
		while(m[1] > 0 && m[2] > 0){
			ans++;
			m[1]--;
			m[2]--;
		}
		cout << ans + m[1]/3 << '\n';
	}

}


#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <string.h>
#include <vector>
#include <math.h>
#include <string>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <deque>
#include <queue>
#include <stack>
#include<stdio.h>
#define lld long long int
#define forn(i,n) for(int i = 0; i < n;i++)
// #define INT_MAX 2148483647
// #define INT_MIN -2147483648

using namespace std;

const int N = 100001;
const int M =  998244353;

int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif

	lld t;
	cin>>t;
	while(t--){
		lld n;
		cin>>n;
		vector<int> a(n);
		forn(i,n) cin >> a[i];
		int m[3] = {0,0,0};
		for(int i = 0; i < n;i++){
			m[a[i]%3]++;
		}
		cout << m[0] + min(m[1],m[2]) + max(0,m[1] - m[2])/3+max(0,m[2]-m[1])/3 <<'\n';
	}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            m[a[i]%3]++;
        }
        int u=m[1];
        int v=m[2];
        if(m1[1]>m[2])
        m[1]-=m[2];
        else
            m[1]=0;
        cout<<m[0]+min(u,v)+(m[1]/3)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            m[a[i]%3]++;
        }
        int u=m[1];
        int v=m[2];
        int h=min(u,v);
        m[1]-=h;
        m[2]-=h;
        cout<<m[0]+h+(m[1]/3)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            m[a[i]%3]++;
        }
        int u=m[1];
        int v=m[2];
        m[1]-=m[2];
        cout<<m[0]+min(u,v)+(m[1]/3)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            m[a[i]%3]++;
        }
        int u=m[1];
        int v=m[2];
        if(m[1]>m[2])
        m[1]-=m[2];
        else
            m[1]=0;
        cout<<m[0]+min(u,v)+(m[1]/3)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            m[a[i]%3]++;
        }
        int u=m[1];
        int v=m[2];
        m[1]-=m[2];
        cout<<m[0]+min(u,v)+(m[1]/3)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            m[a[i]%3]++;
        }
        int u=m[1];
        int v=m[2];
        int h=min(u,v);
        m[1]-=h;
        m[2]-=h;
        cout<<m[0]+h+(m[1]/3)+m[2]/3<<endl;
    }
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        long long arr[num];
        for( int j = 0; j < num; j++ ) cin >> arr[j];
        sort(arr, arr + num);
        int cont = 0, abajo = 0, arriba = num-1;
        while( abajo <= arriba )
        {
            if( arr[arriba] == 3 ){
                cont++;
                arriba--;
            } else if( arr[arriba] + arr[abajo] == 3 ){
                cont++;
                arr[arriba] = arr[abajo] = 0;
                arriba--;
                abajo++;
            } else if( arr[arriba] + arr[abajo] > 3 ){
                arriba--;
            } else if( arr[arriba] + arr[abajo] < 3 ){
                arr[abajo] = arr[abajo] + arr[arriba];
                arr[arriba] = 0;
                arriba--;
            }
        }
        cout << cont << endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        long long arr[num];
        int uno = 0, dos = 0, cont = 0;
        for( int j = 0; j < num; j++ )
        {
            cin >> arr[j];
            if( arr[j] == 1 )
                uno++;
            if( arr[j] == 2 )
                dos++;
            if( arr[j] == 3 )
                cont++;
        }

        cont += min(uno, dos);
        uno = uno - min(uno, dos);
        dos = dos - min(uno, dos);
        cont += (uno+dos)/3;
        cout << cont << endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        long long arr[num];
        int uno = 0, dos = 0, cont = 0;
        for( int j = 0; j < num; j++ )
        {
            cin >> arr[j];
            if( arr[j] == 1 )
                uno++;
            if( arr[j] == 2 )
                dos++;
            if( arr[j] == 3 )
                cont++;
        }

        int uno1 = uno, dos1 = dos, cont1 = cont;

        while( uno > 0 && dos > 0 )
        {
            uno--;
            dos--;
            cont++;
        }
        cont += uno/3;

        cont1 += uno1/3;
        uno1 = uno1%3;

        while( uno1 > 0 && dos1 > 0 )
        {
            uno1--;
            dos1--;
            cont1++;
        }

        if( cont1 > cont )
            cout << cont1 << endl;
        else
            cout << cont << endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        long long arr[num];
        int cont = 0, abajo = 0, arriba = num-1;
        for( int j = 0; j < num; j++ )
        {
            cin >> arr[j];
            if( arr[j] % 3 == 0 ){
                cont++;
                arr[j] = 0;
            }
        }
        sort(arr, arr + num);
        while( abajo <= arriba )
        {
            if( arr[arriba] % 3 == 0 && arr[arriba] != 0 ){
                cont++;
                arriba--;
            } else if( arr[abajo] % 3 == 0 && arr[abajo] != 0 ){
                cont++;
                abajo++;
            } else if( arr[abajo] + arr[arriba] % 3 == 0 ){
                cont++;
                abajo++;
                arriba--;
            } else {
                arr[abajo] = arr[abajo] + arr[arriba];
                arriba--;
            }

        }
        cout << cont << endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        long long arr[num];
        int uno = 0, dos = 0, cont = 0;
        for( int j = 0; j < num; j++ )
        {
            cin >> arr[j];
            if( arr[j] == 1 )
                uno++;
            if( arr[j] == 2 )
                dos++;
            if( arr[j] == 3 )
                cont++;
        }

        while( uno > 0 && dos > 0 )
        {
            uno--;
            dos--;
            cont++;
        }
        cont += (uno+dos)/3;
        cout << cont << endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        long long arr[num];
        for( int j = 0; j < num; j++ ) cin >> arr[j];
        sort(arr, arr + num);
        int cont = 0, abajo = 0, arriba = num-1;
        while( abajo <= arriba )
        {
            if( arr[arriba] % 3 == 0 ){
                cont++;
                arriba--;
            } else if( arr[abajo] % 3 == 0 ){
                cont++;
                abajo++;
            } else if( arr[abajo] + arr[arriba] % 3 == 0 ){
                cont++;
                abajo++;
                arriba--;
            } else {
                arr[abajo] = arr[abajo] + arr[arriba];
                arriba--;
            }

        }
        cout << cont << endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
    {
        int num;
        cin >> num;
        int uno = 0, dos = 0, cont = 0;
        for( int j = 0; j < num; j++ )
        {
            int t;
            cin >> t;
            if( t % 3 == 1 )
                uno++;
            if( t % 3 ==  2 )
                dos++;
            if( t % 3 == 0 )
                cont++;
        }

        cont += min(uno, dos);
        uno = uno - min(uno, dos);
        dos = dos - min(uno, dos);

        cont += (uno+dos)/3;

        cout << cont << endl;

    }
    return 0;
}


import java.util.*;

public class C {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            int m = sc.nextInt();

            int mat[][] = new int[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    mat[i][j] = sc.nextInt();
                }
            }

            long ans = 0;

            for (int i = 0; i < n / 2; i++) {
                for (int j = 0; j < m / 2; j++) {
                    int a = mat[i][j];
                    int b = mat[i][m - j - 1];
                    int c = mat[n - i - 1][j];
                    int d = mat[n - i - 1][m - j - 1];

                    LinkedList<Integer> ll = new LinkedList<>();
                    ll.add(a);
                    ll.add(b);
                    ll.add(c);
                    ll.add(d);

                    Collections.sort(ll);
                    long mid = ll.get(1);
                    ans += Math.abs(a - mid) + Math.abs(b - mid) + Math.abs(c - mid) + Math.abs(d - mid);
                }
            }

            if (n % 2 != 0) {
                int arr[] = mat[n / 2];
                for (int i = 0; i < m / 2; i++) {

                    long a = arr[i];
                    long b = arr[m - i - 1];
                    long poss = 0;
                    long mid = (long) Math.ceil(((double) a + b) / 2);

                    poss = Math.abs(mid - a) + Math.abs(mid - b);

                    mid = (long) Math.floor(((double) a + b) / 2);
                    poss = Math.min(poss, Math.abs(mid - a) + Math.abs(mid - b));

                    ans += poss;
                }
            }

            if (m % 2 != 0) {
                for (int i = 0; i < n / 2; i++) {

                    long a = mat[i][m / 2];
                    long b = mat[n - i - 1][m / 2];

                    long mid = (long) Math.ceil(((double) a + b) / 2);
                    long poss = 0;
                    poss = Math.abs(mid - a) + Math.abs(mid - b);

                    mid = (long) Math.floor(((double) a + b) / 2);
                    poss = Math.min(poss, Math.abs(mid - a) + Math.abs(mid - b));

                    ans += poss;

                }
            }

            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
}


import java.util.*;

public class B {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            int l = sc.nextInt();

            Deque<Integer> set = new LinkedList<>();
            for (int i = 0; i < n; i++) {
                int cur = sc.nextInt();
                set.add(cur);
            }

            int left = 0;
            int right = l;
            double time = 0;

            int left_inc = 1;
            int right_inc = 1;

            while (left <= right) {
                if (set.contains(left)) {
                    left_inc++;
                }
                if (set.contains(right)) {
                    right_inc++;
                }

                if (left + left_inc >= right) {
                    break;
                }

                if (right - right_inc <= left) {
                    break;
                }

                left = left + left_inc;
                right = right - right_inc;

                time++;
            }

            // Time taken upto now is clear
            // For every 10^6 of time they will increase by increment
            double div = 1000000;
            double left_inc_double = (double) left_inc / div;
            double right_inc_double = (double) right_inc / div;

            double dleft = left;
            double dright = right;

            while (dleft < dright) {
                dleft = dleft + left_inc_double;
                dright = dright - right_inc_double;

                time += 0.000001;
            }

            sb.append(time).append("\n");

        }

        System.out.print(sb);

    }
}


import java.util.*;

public class C {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            int m = sc.nextInt();

            int mat[][] = new int[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    mat[i][j] = sc.nextInt();
                }
            }

            long ans = 0;

            for (int i = 0; i < n / 2; i++) {
                for (int j = 0; j < m / 2; j++) {
                    long a = mat[i][j];
                    long b = mat[i][m - j - 1];
                    long c = mat[n - i - 1][j];
                    long d = mat[n - i - 1][m - j - 1];

                    long min = Math.min(a, Math.min(b, Math.min(c, d)));
                    long max = Math.max(a, Math.max(b, Math.max(c, d)));

                    long mid = (min + max) / 2;

                    ans += Math.abs(a - mid) + Math.abs(b - mid) + Math.abs(c - mid) + Math.abs(d - mid);
                }
            }

            if (n % 2 != 0) {
                int arr[] = mat[n / 2];
                for (int i = 0; i < m / 2; i++) {

                    long a = arr[i];
                    long b = arr[m - i - 1];

                    long mid = (a + b) / 2;

                    ans += Math.abs(mid - a) + Math.abs(mid - b);

                }
            }

            if (m % 2 != 0) {
                for (int i = 0; i < n / 2; i++) {

                    long a = mat[i][m / 2];
                    long b = mat[n - i - 1][m / 2];

                    long mid = (a + b) / 2;

                    ans += Math.abs(mid - a) + Math.abs(mid - b);

                }
            }

            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
}


import java.util.*;

public class C {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            int m = sc.nextInt();

            int mat[][] = new int[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    mat[i][j] = sc.nextInt();
                }
            }

            long ans = 0;

            for (int i = 0; i < n / 2; i++) {
                for (int j = 0; j < m / 2; j++) {
                    int a = mat[i][j];
                    int b = mat[i][m - j - 1];
                    int c = mat[n - i - 1][j];
                    int d = mat[n - i - 1][m - j - 1];

                    int min = Math.min(a, Math.min(b, Math.min(c, d)));
                    int max = Math.max(a, Math.max(b, Math.max(c, d)));

                    int poss = Integer.MAX_VALUE;
                    for (int val = min; val <= max; val++) {
                        poss = Math.min(poss,
                                Math.abs(a - val) + Math.abs(b - val) + Math.abs(c - val) + Math.abs(d - val));
                    }

                    ans += poss;
                }
            }

            if (n % 2 != 0) {
                int arr[] = mat[n / 2];
                for (int i = 0; i < m / 2; i++) {

                    int a = arr[i];
                    int b = arr[m - i - 1];

                    long mid = ((long) a + b) / 2;

                    ans += Math.abs(mid - a) + Math.abs(mid - b);

                }
            }

            if (m % 2 != 0) {
                for (int i = 0; i < n / 2; i++) {

                    int a = mat[m / 2][i];
                    int b = mat[m / 2][n - i - 1];

                    long mid = ((long) a + b) / 2;

                    ans += Math.abs(mid - a) + Math.abs(mid - b);

                }
            }

            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
}


import java.util.*;

public class C {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            int m = sc.nextInt();

            int mat[][] = new int[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    mat[i][j] = sc.nextInt();
                }
            }

            long ans = 0;

            for (int i = 0; i < n / 2; i++) {
                for (int j = 0; j < m / 2; j++) {
                    int a = mat[i][j];
                    int b = mat[i][m - j - 1];
                    int c = mat[n - i - 1][j];
                    int d = mat[n - i - 1][m - j - 1];

                    int min = Math.min(a, Math.min(b, Math.min(c, d)));
                    int max = Math.max(a, Math.max(b, Math.max(c, d)));

                    long mid = ((long) min + max) / 2;

                    ans += Math.abs(a - mid) + Math.abs(b - mid) + Math.abs(c - mid) + Math.abs(d - mid);
                }
            }

            if (n % 2 != 0) {
                int arr[] = mat[n / 2];
                for (int i = 0; i < m / 2; i++) {

                    int a = arr[i];
                    int b = arr[m - i - 1];

                    long mid = ((long) a + b) / 2;

                    ans += Math.abs(mid - a) + Math.abs(mid - b);

                }
            }

            if (m % 2 != 0) {
                for (int i = 0; i < n / 2; i++) {

                    int a = mat[m / 2][i];
                    int b = mat[m / 2][n - i - 1];

                    long mid = ((long) a + b) / 2;

                    ans += Math.abs(mid - a) + Math.abs(mid - b);

                }
            }

            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
}


import java.util.*;

public class C {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            int m = sc.nextInt();

            int mat[][] = new int[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    mat[i][j] = sc.nextInt();
                }
            }

            long ans = 0;

            for (int i = 0; i < n / 2; i++) {
                for (int j = 0; j < m / 2; j++) {
                    long a = mat[i][j];
                    long b = mat[i][m - j - 1];
                    long c = mat[n - i - 1][j];
                    long d = mat[n - i - 1][m - j - 1];

                    long min = Math.min(a, Math.min(b, Math.min(c, d)));
                    long max = Math.max(a, Math.max(b, Math.max(c, d)));

                    long poss = 0;

                    long mid = (long) Math.ceil(((double) min + max) / 2);

                    poss = Math.abs(a - mid) + Math.abs(b - mid) + Math.abs(c - mid) + Math.abs(d - mid);

                    mid = (long) Math.floor(((double) min + max) / 2);

                    poss = Math.min(poss,
                            Math.abs(a - mid) + Math.abs(b - mid) + Math.abs(c - mid) + Math.abs(d - mid));

                    ans += poss;
                }
            }

            if (n % 2 != 0) {
                int arr[] = mat[n / 2];
                for (int i = 0; i < m / 2; i++) {

                    long a = arr[i];
                    long b = arr[m - i - 1];
                    long poss = 0;
                    long mid = (long) Math.ceil(((double) a + b) / 2);

                    poss = Math.abs(mid - a) + Math.abs(mid - b);

                    mid = (long) Math.floor(((double) a + b) / 2);
                    poss = Math.min(poss, Math.abs(mid - a) + Math.abs(mid - b));

                    ans += poss;
                }
            }

            if (m % 2 != 0) {
                for (int i = 0; i < n / 2; i++) {

                    long a = mat[i][m / 2];
                    long b = mat[n - i - 1][m / 2];

                    long mid = (long) Math.ceil(((double) a + b) / 2);
                    long poss = 0;
                    poss = Math.abs(mid - a) + Math.abs(mid - b);

                    mid = (long) Math.floor(((double) a + b) / 2);
                    poss = Math.min(poss, Math.abs(mid - a) + Math.abs(mid - b));

                    ans += poss;

                }
            }

            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
}


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.*;

    public class d1 extends PrintWriter {

        static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        // static Scanner s=new Scanner(System.in);
        d1 () { super(System.out); }
        public static void main(String[] args) throws IOException{
            d1 d1=new d1();d1.main();d1.flush();
        }
        void main() throws IOException {
            BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
            StringBuffer sb = new StringBuffer();
            StringBuffer sb1 = new StringBuffer();
            PrintWriter out = new PrintWriter(System.out);
           String[] s1 = s();
           int t=i(s1[0]);
          while(t-->0){
              String[] s2 = s();
               int n=i(s2[0]);
               int m=i(s2[1]);
               long[] [] a=new long[n][m];
               for(int i=0;i<n;i++){
                   String[] s3=s();
                   for(int j=0;j<m;j++){
                       a[i][j]=i(s3[j]);
                   }
               }long ans=0;
               for(int i=0;i<n/2;i++){
                   for(int j=0;j<m/2;j++){
                       long x=(a[i][j]+(a[n-1-i][j]+(a[i][m-1-j]+a[n-1-i][m-1-j])))/4;
                       long[] arr={a[i][j],a[n-1-i][j],a[i][m-1-j],a[n-1-i][m-1-j]};
                       Arrays.sort(arr);
                       x=arr[1];
                       long y=x+1;
//                       System.out.println(i+" "+j+" "+x);
                       long z=(Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-a[n-1-i][m-1-j])+Math.abs(x-a[i][m-1-j]));
                         x=arr[2];
                       long z1=(Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-a[n-1-i][m-1-j])+Math.abs(x-a[i][m-1-j]));
                       ans+=Math.min(z,z1);
                   }
               }
               if(n%2!=0){
                   int i=n/2;
                   for(int j=0;j<m/2;j++){
                       long x=(a[i][j]+((a[i][m-1-j])))/2;
                       x=a[i][j];
                       long y=a[i][m-1-j];
//                       System.out.println(i+" "+j+" "+x);
                       ans+=Math.min(Math.abs(x-a[i][j])+Math.abs(x-x)+Math.abs(x-x)+Math.abs(x-a[i][m-1-j]),Math.abs(y-a[i][j])+Math.abs(x-x)+Math.abs(x-x)+Math.abs(y-a[i][m-1-j]));
                   }
               }
              if(m%2!=0){
                  int j=m/2;
                  for(int i=0;i<n/2;i++){
                      long x=(a[i][j]+((a[n-1-i][j])))/2;
                      long y=x+1;
                      x=a[i][j];y=a[n-1-i][j];
//                      System.out.println(i+" "+j+" "+x);
                      ans+=Math.min(Math.abs(x-x)+Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-x),Math.abs(y-a[i][j])+Math.abs(y-a[n-1-i][j]));
                  }
              }
              System.out.println(ans);
               //int[] a=new int[n];arr(a,n);
//              System.out.println(Math.min(a,Math.min(b,c)));
          }

            System.out.println(sb);
        }
        static long powerwithmod(long  x, long y, long p) {
            long  res = 1;
            x = x % p;
            while (y > 0) {
                if ((y & 1) == 1)
                    res = (res * x) % p;
                y = y >> 1;
                x = (x * x) % p;
            }
            return res;
        }
        public void arr(int[] a,int n) throws IOException{
    //        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
            String[] s2=s();
            for(int i=0;i<n;i++){
                a[i]=i(s2[i]);
            }
        }
        public void sort(int[] a,int l,int h){
            if(l==h) return;
            int mid=(l+h)/2;

            sort(a,l,mid);
            sort(a,mid+1,h);
            merge(a,l,(l+h)/2,h);
        }
        void merge(int arr[], int l, int m, int r)
        {
            int n1 = m - l + 1;
            int n2 = r - m;
            int L[] = new int[n1];
            int R[] = new int[n2];
            for (int i = 0; i < n1; ++i)
                L[i] = arr[l + i];
            for (int j = 0; j < n2; ++j)
                R[j] = arr[m + 1 + j];
            int i = 0, j = 0;
            int k = l;
            while (i < n1 && j < n2) {
                if (L[i] <= R[j]) {
                    arr[k] = L[i];
                    i++;
                }
                else {
                    arr[k] = R[j];
                    j++;
                }
                k++;
            }

            while (i < n1) {
                arr[k] = L[i];
                i++;
                k++;
            }
            while (j < n2) {
                arr[k] = R[j];
                j++;
                k++;
            }
        }

        static long gcd(long a, long b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }
        static String[] s() throws IOException {
            return s.readLine().trim().split("\\s+");
        }

        static int i(String ss) {
            return Integer.parseInt(ss);
        }
        static long l(String ss) {
            return Long.parseLong(ss);
        }
    }
    class Student {
        long  a;int b;int c;
        public Student(int a,int b) {
            this.a=a;this.c=c;this.b=b;
        }
    }
    class Pair {
        int  a,b,c;
        public Pair(int  a,int b,int c){
            this.a=a;this.b=b;this.c=c;}
    }
    class Sortbyroll implements Comparator<Student> {

        public int compare(Student a, Student b){
          if(a.b==b.b) return (int)b.a-(int)a.a;
            return   a.b-b.b;}
    }


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.*;

    public class d1 extends PrintWriter {

        static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        // static Scanner s=new Scanner(System.in);
        d1 () { super(System.out); }
        public static void main(String[] args) throws IOException{
            d1 d1=new d1();d1.main();d1.flush();
        }
        void main() throws IOException {
            BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
            StringBuffer sb = new StringBuffer();
            StringBuffer sb1 = new StringBuffer();
            PrintWriter out = new PrintWriter(System.out);
           String[] s1 = s();
           int t=i(s1[0]);
          while(t-->0){
              String[] s2 = s();
               int n=i(s2[0]);
               int m=i(s2[1]);
               long[] [] a=new long[n][m];
               for(int i=0;i<n;i++){
                   String[] s3=s();
                   for(int j=0;j<m;j++){
                       a[i][j]=i(s3[j]);
                   }
               }long ans=0;
               for(int i=0;i<n/2;i++){
                   for(int j=0;j<m/2;j++){
                       long x=(a[i][j]+(a[n-1-i][j]+(a[i][m-1-j]+a[n-1-i][m-1-j])))/4;
                       long y=x+1;
//                       System.out.println(i+" "+j+" "+x);
                       long z=(Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-a[n-1-i][m-1-j])+Math.abs(x-a[i][m-1-j]));
x+=1;
                       long z1=(Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-a[n-1-i][m-1-j])+Math.abs(x-a[i][m-1-j]));
                       ans+=Math.min(z,z1);
                   }
               }
               if(n%2!=0){
                   int i=n/2;
                   for(int j=0;j<m/2;j++){
                       long x=(a[i][j]+((a[i][m-1-j])))/2;
                       long y=x+1;
//                       System.out.println(i+" "+j+" "+x);
                       ans+=Math.min(Math.abs(x-a[i][j])+Math.abs(x-x)+Math.abs(x-x)+Math.abs(x-a[i][m-1-j]),Math.abs(y-a[i][j])+Math.abs(x-x)+Math.abs(x-x)+Math.abs(y-a[i][m-1-j]));
                   }
               }
              if(m%2!=0){
                  int j=m/2;
                  for(int i=0;i<n/2;i++){
                      long x=(a[i][j]+((a[n-1-i][j])))/2;
                      long y=x+1;
//                      System.out.println(i+" "+j+" "+x);
                      ans+=Math.min(Math.abs(x-x)+Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-x),Math.abs(y-a[i][j])+Math.abs(y-a[n-1-i][j]));
                  }
              }
              System.out.println(ans);
               //int[] a=new int[n];arr(a,n);
//              System.out.println(Math.min(a,Math.min(b,c)));
          }

            System.out.println(sb);
        }
        static long powerwithmod(long  x, long y, long p) {
            long  res = 1;
            x = x % p;
            while (y > 0) {
                if ((y & 1) == 1)
                    res = (res * x) % p;
                y = y >> 1;
                x = (x * x) % p;
            }
            return res;
        }
        public void arr(int[] a,int n) throws IOException{
    //        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
            String[] s2=s();
            for(int i=0;i<n;i++){
                a[i]=i(s2[i]);
            }
        }
        public void sort(int[] a,int l,int h){
            if(l==h) return;
            int mid=(l+h)/2;

            sort(a,l,mid);
            sort(a,mid+1,h);
            merge(a,l,(l+h)/2,h);
        }
        void merge(int arr[], int l, int m, int r)
        {
            int n1 = m - l + 1;
            int n2 = r - m;
            int L[] = new int[n1];
            int R[] = new int[n2];
            for (int i = 0; i < n1; ++i)
                L[i] = arr[l + i];
            for (int j = 0; j < n2; ++j)
                R[j] = arr[m + 1 + j];
            int i = 0, j = 0;
            int k = l;
            while (i < n1 && j < n2) {
                if (L[i] <= R[j]) {
                    arr[k] = L[i];
                    i++;
                }
                else {
                    arr[k] = R[j];
                    j++;
                }
                k++;
            }

            while (i < n1) {
                arr[k] = L[i];
                i++;
                k++;
            }
            while (j < n2) {
                arr[k] = R[j];
                j++;
                k++;
            }
        }

        static long gcd(long a, long b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }
        static String[] s() throws IOException {
            return s.readLine().trim().split("\\s+");
        }

        static int i(String ss) {
            return Integer.parseInt(ss);
        }
        static long l(String ss) {
            return Long.parseLong(ss);
        }
    }
    class Student {
        long  a;int b;int c;
        public Student(int a,int b) {
            this.a=a;this.c=c;this.b=b;
        }
    }
    class Pair {
        int  a,b,c;
        public Pair(int  a,int b,int c){
            this.a=a;this.b=b;this.c=c;}
    }
    class Sortbyroll implements Comparator<Student> {

        public int compare(Student a, Student b){
          if(a.b==b.b) return (int)b.a-(int)a.a;
            return   a.b-b.b;}
    }