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

int main(){
	int t;
	cin>>t;

	while(t--){
		int n,m;
		cin>>n>>m;

		int a[3][4];
		long long sum =0;

		int count =0;

		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>a[i][j];

				if(a[i][j]<0){
					count++;
				}
			}
		}

		if(count%2==0){
			for(int i=0;i<n;i++){
				for(int j=0;j<m;j++){
					if(a[i][j]<0){
						sum = sum + ((-1)*a[i][j]);
					}
					else{
						sum = sum +a[i][j];
					}

				}
			}
		}
		else{
			for(int i=0;i<n;i++){
				for(int j=0;j<m;j++){
					sum = sum + a[i][j];
				}
			}
		}

		if(sum <0){
			cout<<(-1)*sum<<endl;
		}
		else {
			cout<<sum<<endl;
		}

	}
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 40 , inf = 1e9;const ld pi = 3.14159265;
int n , t , m , a[maxn][maxn];
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        int answer = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j];
                answer += a[i][j];
            }
        }
        if (n == 1 && m == 1) {cout << answer << endl; continue;}
        cout << abs(answer) << endl;
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 40 , inf = 1e9;const ld pi = 3.14159265;
int n , t , m , a[maxn][maxn] , b[maxn][maxn];
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        for (int i = 0; i <= n + 1; i ++){
            for (int j = 0; j <= m + 1; j ++){
                a[i][j] = -inf;
            }
        }
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j]; b[i][j] = a[i][j];
            }
        }
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j += 2){
               // debug(i) ; debug(j) << endl;
                if (-a[i][j] + (-a[i][j + 1]) > a[i][j] + a[i][j + 1]) a[i][j] *= -1 , a[i][j + 1] *= -1;
            }
        }
        int answer = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                answer += a[i][j];
            }
        }
        for (int i = 1; i <= n; i += 2){
            for (int j = 1; j <= m; j ++){
                if (-b[i][j] + (-b[i + 1][j]) > b[i][j] + b[i + 1][j]) b[i][j] *= -1 , b[i + 1][j] *= -1;
            }
        }
        int answer2 = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                answer2 += b[i][j];
            }
        }
        cout << max(answer , answer2) << endl;
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 40 , inf = 1e9;const ld pi = 3.14159265;
ll n , t , m , a[maxn][maxn];
vector<ll> tmp;
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        ll mini = 0 , ans = 0;
        tmp.clear();
        for (int i = 1; i <= n; i ++){
            int cnt = 0;
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j]; cnt += a[i][j];
            }
            ans += abs(cnt);
        }
        cout << ans << endl;
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 1e6 + 10 , inf = 1e9;const ld pi = 3.14159265;
ll t , n , m , mini = 0 , cnt = 0;
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
      cin >> n >> m;
      mini = inf , cnt = 0;
      ll sum = 0;
      for (int i = 0; i < n; i ++){
        for (int j = 0; j < m; j ++){
          ll x; cin >> x; sum += abs(x);
          if (x <= 0) {cnt ++;
            mini = min (mini , abs(x));
          }
        }
      }
      ((cnt & 1) == 1)? cout << sum - (2 * mini) << endl : cout << sum << endl;
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 40 , inf = 1e9;const ld pi = 3.14159265;
ll n , t , m , a[maxn][maxn];
void prin(){
    for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
}
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        ll mini = 0 , ans = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j];
            }
        }
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                if (a[i][j] <= 0 && j <= m - 1) {
                    a[i][j + 1] *= -1 ; a[i][j] *= -1;
                }
                else if (a[i][j] <= 0 && i <= n - 1){
                    a[i][j] *= -1 ; a[i + 1][j] *= -1;
                }
            }
        }
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                ans += a[i][j];
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 40 , inf = 1e9;const ld pi = 3.14159265;
ll n , t , m , a[maxn][maxn];
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        ll mini = 0 , ans = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j];
                if (a[i][j] < 0) mini = min (mini , a[i][j]); ans += a[i][j];
            }
        }
        if ((n * m) & 1) cout << abs(ans) + mini;
        else {cout << abs(ans) << endl;}
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 1e6 + 10 , inf = 1e9;const ld pi = 3.14159265;
ll n , t , m , a[maxn][maxn];
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        ll mini = 0 , ans = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j];
                if (a[i][j] < 0) mini = min (mini , a[i][j]); ans += a[i][j];
            }
        }
        if ((n * m) & 1) cout << abs(ans) + mini;
        else {cout << abs(ans) << endl;}
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 40 , inf = 1e9;const ld pi = 3.14159265;
int n , t , m , a[maxn][maxn];
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> n >> m;
        int answer = 0;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                cin >> a[i][j]; answer += a[i][j];
            }
        }
        cout << abs(answer) << endl;
    }
    return 0;
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 1e6 + 10 , inf = 1e9;const ld pi = 3.14159265;
ll t , n , m , mini, cnt;
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
      cin >> n >> m;
      mini = inf , cnt = 0;
      ll sum = 0;
      for (int i = 0; i < n; i ++){
        for (int j = 0; j < m; j ++){
          ll x; cin >> x; sum += abs(x);
          mini = min (mini , abs(x));
          if (x <= 0) cnt ++;
        }
      }
      ((cnt & 1) == 1)? cout << sum - (2 * mini) << endl : cout << sum << endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int r, c;
        cin >> r >> c;
        int a[r][c];
        int w = 0, ne = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cin >> a[i][j];
                if (a[i][j] < 0) {
                    ne++;
                }
                w += abs(a[i][j]);
            }
        }
        int sum=0;
        if(ne%2!=0) {
            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {
                    sum += abs(a[i][j]);
                }
            }
            if(a[0][0]<0){
                cout<<sum-2*abs(a[0][0])<<"\n";
            }
            else {
                cout << sum - abs(a[0][0]) << "\n";
            }
        } else {
            cout << w << "\n";
        }
    }
}


#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int r, c;
        cin >> r >> c;
        int a[r][c];
        int flag = 0;
        int w = 0, ne = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cin >> a[i][j];
                if (a[i][j] == 0) {
                    flag = 1;
                }
                if (a[i][j] < 0) {
                    ne++;
                }
                w += abs(a[i][j]);
            }
        }
        int sum = 0;
        if (flag == 1) {
            cout << w << "\n";
        } else {
            if (ne % 2 != 0) {
                for (int i = 0; i < r; i++) {
                    for (int j = 0; j < c; j++) {
                        sum += abs(a[i][j]);
                    }
                }
                if (a[0][0] < 0) {
                    cout << sum - 2 * abs(a[0][0]) << "\n";
                } else {
                    cout << sum - abs(a[0][0]) << "\n";
                }
            } else {
                cout << w << "\n";
            }
        }
    }
}


#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int r, c;
        cin >> r >> c;
        int a[r][c];
        int flag = 0;
        int w = 0, ne = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cin >> a[i][j];
                if (a[i][j] == 0) {
                    flag = 1;
                }
                if (a[i][j] < 0) {
                    ne++;
                }
                w += abs(a[i][j]);
            }
        }
        int sm=abs(a[0][0]);
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                sm=min(abs(a[i][j]),sm);
            }
        }
        int sum = 0;
        if (flag == 1) {
            cout << w << "\n";
        } else {
            if (ne % 2 != 0) {
                for (int i = 0; i < r; i++) {
                    for (int j = 0; j < c; j++) {
                        sum += abs(a[i][j]);
                    }
                }
                cout << sum - 2 * abs(sm) << "\n";
            } else {
                cout << w << "\n";
            }
        }
    }
}


#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int r, c;
        cin >> r >> c;
        int a[r][c];
        int w = 0, b = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cin >> a[i][j];
                if ((i + j) % 2 == 0) {
                    w += a[i][j];
                } else {
                    b += a[i][j];
                }
            }
        }
        w = abs(-1 * (w));
        b = abs(-1 * (b));
        cout << w + b << "\n";
    }
}


#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int r, c;
        cin >> r >> c;
        int a[r][c];
        int w = 0, ne = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cin >> a[i][j];
                if (a[i][j] <= 0) {
                    ne++;
                }
                w += abs(a[i][j]);
            }
        }
        if (ne % 2 != 0) {
            cout << w + 2 * a[0][0] << "\n";
        } else {
            cout << w - a[0][0] << "\n";
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"

int main()
{   fast;
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,m;
       cin>>n>>m;
      ll a;
      bool x=false;

      ll cnt=0;
      ll minimum = 1e9;
      ll sum=0;
       for(int i =0;i<n;i++)
       {
           for(int j =0;j<m;j++)
           {
               cin>>a;

               if(a<0)
                {
                    cnt++;
                    a*=-1;
                    if(a<minimum)
                        minimum = a;

                }
                sum+=a;
               if(a==0)
                x=true;
                cout<<sum<<endl;
           }

       }
       if(x==true)
       cout<<sum;
       else{
        if(cnt%2==0)
            cout<<sum;
        else
            cout<<sum - 2*minimum;
       }
cout<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"

int main()
{   fast;
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,m;
       cin>>n>>m;
      ll a;
      bool x=false;

      ll cnt=0;
      ll minimum = 1e9;
      ll sum=0;
       for(int i =0;i<n;i++)
       {
           for(int j =0;j<m;j++)
           {
               cin>>a;

               if(a<0)
                {
                    cnt++;
                    a*=-1;
                    if(a<minimum)
                        minimum = a;

                }
                sum+=a;
               if(a==0)
                x=true;
           }

       }
       if(x==true)
       cout<<sum;
       else{
        if(cnt%2==0)
            cout<<sum;
        else
            cout<<sum - 2*minimum;
       }
cout<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"

int main()
{   fast;
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,m;
       cin>>n>>m;
      ll a;
      bool x=false;

      ll cnt=0;
      ll minimum = 1e9;
      ll sum=0;
       for(int i =0;i<n;i++)
       {
           for(int j =0;j<m;j++)
           {
               cin>>a;

               if(a<0)
                {
                    cnt++;
                    a*=-1;
                    if(a<minimum)
                        minimum = a;

                }
                sum+=a;
               if(a==0)
                x=true;
           }

       }
       if(cnt%2==0)
        cout<<sum<<endl;
       else {
        if(x==true)
            cout<<sum<<endl;
        else{
            cout<<sum-2*minimum<<endl;
        }
       }
   }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"

int main()
{   fast;
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,m;
       cin>>n>>m;
      ll a;
      bool x=false;

      ll cnt=0;
      ll minimum = 101;
      ll sum=0;
       for(int i =0;i<n;i++)
       {
           for(int j =0;j<m;j++)
           {
               cin>>a;

               if(a<0)
                {
                    cnt++;
                    a*=-1;
                    if(a<minimum)
                        minimum = a;

                }
                sum+=a;
               if(a==0)
                x=true;
           }

       }
       if(cnt%2==0)
        cout<<sum<<endl;
       else {
        if(x==true)
            cout<<sum<<endl;
        else{
            cout<<sum-2*minimum<<endl;
        }
       }
   }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"

int main()
{   fast;
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,m;
       cin>>n>>m;
      ll a;
      bool x=false;

      ll cnt=0;
      ll minimum = 1e9;
      ll sum=0;
       for(int i =0;i<n;i++)
       {
           for(int j =0;j<m;j++)
           {
               cin>>a;

               if(a<0)
                {
                    cnt++;
                    a*=-1;

                }
                if(a<minimum)
                        minimum = a;
                sum+=a;
               if(a==0)
                x=true;
           }
       }
       if(x==true)
       cout<<sum;
       else{
        if(cnt%2==0)
            cout<<sum;
        else
            cout<<sum - 2*minimum;
       }
cout<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"

int main()
{   fast;
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,m;
       cin>>n>>m;
      ll a;
      bool x;
      x=false;

      ll cnt=0;
      ll minimum = 1e9;
      ll sum=0;
       for(int i =0;i<n;i++)
       {
           for(int j =0;j<m;j++)
           {
               cin>>a;

               if(a<0)
                {
                    cnt++;
                    a*=-1;
                    if(a<minimum)
                        minimum = a;

                }
                sum+=a;
               if(a==0)
                x=true;
           }

       }
       if(x==true)
       cout<<sum;
       else{
        if(cnt%2==0)
            cout<<sum;
        else
            cout<<sum - 2*cnt;
       }
cout<<endl;
   }
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t)
	{
		ll n, m, arr[102][102];
		ll c0 = 0, c = 0, s = 0;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				if (arr[i][j] < 0)
				{
					c++;
					arr[i][j] = abs(arr[i][j]);
					s += arr[i][j];

				}
				else
				{
					s += arr[i][j];
				}

			}
		}
		ll min = arr[0][0];
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				if (arr[i][i] < min)
				{
					min = arr[i][j];
				}
			}
		}
		if (c % 2 == 0)
		{
			cout << s << endl;
		}
		else
		{
			cout << s - 2 * min << endl;

		}

	}
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t)
	{
		ll n, m, arr[102][102];
		ll c0 = 0, c = 0, min = -101;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				if (arr[i][j] < 0)
				{
					c++;
					if (arr[i][j] > min)
					{

						min = arr[i][j];
					}
				}
				if (arr[i][j] == 0)
				{
					c0++;
				}

			}
		}
		if (c0 >= 1)
		{
			ll s = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					s = s + abs(arr[i][j]);

				}
			}
			cout << s << endl;
		}
		else if (c % 2 != 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			ans = ans - 2 * min;
			cout << ans << endl;

		}
		else if (c % 2 == 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			cout << ans << endl;

		}

	}
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t)
	{
		ll n, m, arr[102][102];
		ll c0 = 0, c = 0, min = -101;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				if (arr[i][j] < 0)
				{
					c++;
					if (arr[i][j] > min)
					{

						min = arr[i][j];
					}
				}
				if (arr[i][j] == 0)
				{
					c0++;
				}

			}
		}
		if (c0 >= 1)
		{
			ll s = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					s = s + abs(arr[i][j]);

				}
			}
			cout << s << endl;
		}
		else if (c % 2 != 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			// cout << min << endl;
			ans = ans + 2 * min;
			cout << ans << endl;

		}
		else if (c % 2 == 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			cout << ans << endl;

		}

	}
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t)
	{
		ll n, m, arr[102][102];
		ll c0 = 0, c = 0, min = -101;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				if (arr[i][j] < 0)
				{
					c++;
					if (arr[i][j] > min)
					{

						min = arr[i][j];
					}
				}
				if (arr[i][j] == 0)
				{
					c0++;
				}

			}
		}
		if (c0 >= 1)
		{
			ll s = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					s = s + abs(arr[i][j]);

				}
			}
			cout << s << endl;
		}
		else if (c % 2 != 0 && c > 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			// cout << min << endl;
			ans = ans + 2 * min;
			cout << ans << endl;

		}
		else if (c % 2 == 0 )
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			cout << ans << endl;

		}

	}
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// 	c_p_c();
	w(t)
	{
		ll n, m, arr[10000][10000];
		ll c0 = 0, c = 0, min = -101;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				if (arr[i][j] < 0)
				{
					c++;
					if (arr[i][j] > min)
					{

						min = arr[i][j];
					}
				}
				if (arr[i][j] == 0)
				{
					c0++;
				}

			}
		}
		if (c0 >= 1)
		{
			ll s = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					s = s + abs(arr[i][j]);

				}
			}
			cout << s << endl;
		}
		else if (c % 2 != 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			ans = ans - 2 * min;
			cout << ans << endl;

		}
		else if (c % 2 == 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			cout << ans << endl;

		}

	}
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t)
	{
		ll n, m, arr[102][102];
		ll c0 = 0, c = 0, min = -101;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				if (arr[i][j] < 0)
				{
					c++;
					if (arr[i][j] > min)
					{

						min = arr[i][j];
					}
				}
				if (arr[i][j] == 0)
				{
					c0++;
				}

			}
		}
		if (c0 >= 1)
		{
			ll s = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					s = s + abs(arr[i][j]);

				}
			}
			cout << s << endl;
		}
		else if (c % 2 != 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			cout << min << endl;
			ans = ans + 2 * min;
			cout << ans << endl;

		}
		else if (c % 2 == 0)
		{
			ll ans = 0;
			for (ll i = 0; i < n; i++)
			{
				for (ll j = 0; j < m; j++)
				{
					ans = ans + abs(arr[i][j]);

				}
			}
			cout << ans << endl;

		}

	}
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace std;

#define ff              first
#define ss              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t)
	{
		ll n, m, arr[102][102], mi = 111;
		ll c0 = 0, c = 0, s = 0;
		cin >> n >> m;
		for (ll i = 0; i < n; i++)
		{
			for (ll j = 0; j < m; j++)
			{
				cin >> arr[i][j];
				mi = min(mi, abs(arr[i][j]));

				if (arr[i][j] < 0)
				{
					c++;
					arr[i][j] = abs(arr[i][j]);
					s += arr[i][j];

				}
				else
				{
					s += arr[i][j];
				}

			}
		}

		if (c % 2 == 0)
		{
			cout << s << endl;
		}
		else
		{
			cout << s - 2 * mi << endl;

		}

	}
}


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

void solve(){
    int m,n;
    int mn = 0;
    int sum = 0;
    int c = 0;
    cin >> m >> n;
    int ar[m][n];
    for(int i = 0; i<n;i++)
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,ar[i][j]);
            if(ar[i][j] <= 0 )
                c++;

        }
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);

    if(c%2 == 0)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';
}

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


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

void solve(){
    int m,n;
    int mn = -1;
    int sum = 0;
    int z = 0;
    cin >> n >> m;
    int ar[n][m];
    for(int i = 0; i<n;i++){
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,abs(ar[i][j]));
            if(ar[i][j] == 0)
                z = 1;

        }
    }

    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);

    if(z == 1)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';

}

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


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

void solve(){
    int m,n,c = 0;
    int mn = INT_MAX;
    int sum = 0;
    int z = 0;
    cin >> n >> m;
    int ar[n][m];
    for(int i = 0; i<n;i++){
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,abs(ar[i][j]));
            if(ar[i][j] < 0)
                c++;
            if(ar[i][j] == 0)
                z = 1;

        }
    }

    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);
    // cout << sum << ' ' << mn<< '\n';
    if(z == 1 || c%2 == 0)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';

 // 347
}

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


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

void solve(){
    int m,n;
    int mn = 0;
    int sum = 0;
    int c = 0;
    cin >> n >> m;
    int ar[n][m];
    for(int i = 0; i<n;i++){
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,ar[i][j]);
            if(ar[i][j] <= 0 )
                c++;
        }
    }
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);

    if(c%2 == 0)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';
}

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


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

void solve(){
    int m,n;
    int mn = 0;
    int sum = 0;
    int z = 0;
    cin >> n >> m;
    int ar[n][m];
    for(int i = 0; i<n;i++){
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,ar[i][j]);
            if(ar[i][j] == 0)
                z = 1;

        }
    }
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);

    if(z == 1)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';

}

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


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

void solve(){
    int m,n,c = 0;
    int mn = -1;
    int sum = 0;
    int z = 0;
    cin >> n >> m;
    int ar[n][m];
    for(int i = 0; i<n;i++){
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,abs(ar[i][j]));
            if(ar[i][j] < 0)
                c++;
            if(ar[i][j] == 0)
                z = 1;

        }
    }

    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);

    if(z == 1 || c%2 == 0)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';

}

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


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

void solve(){
    int m,n;
    int mn = 0;
    int sum = 0;
    int c = 0;
    cin >> m >> n;
    int ar[m][n];
    for(int i = 0; i<n;i++){
        for(int j = 0;j<m;j++){
            cin >> ar[i][j];
            mn = min(mn,ar[i][j]);
            if(ar[i][j] <= 0 )
                c++;
        }
    }
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            sum = sum + abs(ar[i][j]);

    if(c%2 == 0)
        cout << sum << '\n';
    else
        cout << sum - 2*abs(mn) << '\n';
}

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


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const int mod=1e9+7;

int takemod(int a,int m = mod)
{
  return ((a%m)+m)%m;
}
int power(int  b, int  p)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=power(b,p/2);
      return ((q)*(q));
  }
  else return ((b)*(power(b,p-1)));
}
int powerm(int  b, int  p,int mod = mod)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=powerm(b,p/2)%mod;
      return ((q%mod)*(q%mod))%mod;
  }
  else return ((b%mod)*(powerm(b,p-1)%mod))%mod;
}

bool isprime(int n)
{
  if(n==1)return false;
  if(n==2 or n==3)return true;
  int  lim=sqrt(n);
  int  i=2;
  while(i<=lim)
  {
      if(n%i==0)return false;
      i++;
  }
  return true;
}
vector< int > factors( int  n)
{
  vector< int > a;
  a.pb(1);
  int  lim=sqrt(n);
  int i=2;
  while(i<=lim)
  {
      if(n%i==0)
      {
          a.pb(i);
          if(i!=n/i)a.pb(n/i);
      }
      i++;
  }

  if(n!=1)a.pb(n);
  //sort(a.begin(),a.end());
  return a;
}
vector<pii> pf_with_count(int  b)
{
  pii p;
  vector<pii>pf;
  int  count=0;
  while(b%2==0)
  {
    b/=2;
    count++;
  }
  if(count>0)
  {
    p.f=2;
    p.s=count;
    pf.pb(p);
  }
  int i=3;
  while(i<=sqrt(b))
  {
        count=0;
        while(b%i==0)
        {
            b/=i;
            count++;
        }
        if(count>0)
        {
            p.f=i;
            p.s=count;
            pf.pb(p);
        }
        i+=2;
  }
  if(b>2)
  {
      p.f=b;
      p.s=1;
      pf.pb(p);
  }
  sort(pf.begin(),pf.end());
  return pf;
}
int  bs_t_i(string s)
{
  int  l=strtoull(s.c_str(),NULL,2);
  return l;
}
string  i_to_bs(int  n)
{
  string s = bitset< 30 >( n ).to_string();
  return s;
}
int s_t_i(string s)
{
  stringstream obj(s);
  int x = 0;
  obj >> x;
  return x;
}
 //sons of  anarchy, top boy , end of the fucking world, the crown, ..

void sol()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    vector<pair<pii,pii > > v;
    int i=0;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        cin>>a[i][j];
        if(i-1>=0)v.pb({{i,j},{i-1,j}});
        if(j-1>=0)v.pb({{i,j},{i,j-1}});
        if(i+1<n)v.pb({{i,j},{i+1,j}});
        if(j+1<m)v.pb({{i,j},{i,j+1}});
        j++;
      }
      i++;
    }
    while(1)
    {
     pair<pii,pii> p;
     int foo=0;
     for(auto x:v)
     {
       int pro=0;
       if(a[x.f.f][x.f.s]<0 or a[x.s.f][x.s.s]<0)
       {
          int proo=0;
          if(a[x.f.f][x.f.s]<0)proo+=-a[x.f.f][x.f.s];
          if(a[x.s.f][x.s.s]<0)proo+=-a[x.s.f][x.s.s];
          if(a[x.f.f][x.f.s]>0)proo-=a[x.f.f][x.f.s];
          if(a[x.s.f][x.s.s]>0)proo-=a[x.s.f][x.s.s];
          if(proo>pro)
          {
            pro=proo;
            p={{x.f.f,x.f.s},{x.s.f,x.s.s}};
            foo=1;
          }
       }

     }
      if(foo==0)break;
      a[p.f.f][p.f.s]*=-1;
      a[p.s.f][p.s.s]*=-1;
    }
    int ans=0;
    i=0;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        ans+=a[i][j];
        j++;
      }
      i++;
    }
    cout<<ans<<endl;

}

signed main()
  {
         ios;
         int t = 1;
         cin >> t;
         while(t--)sol();
  }


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const int mod=1e9+7;

int takemod(int a,int m = mod)
{
  return ((a%m)+m)%m;
}
int power(int  b, int  p)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=power(b,p/2);
      return ((q)*(q));
  }
  else return ((b)*(power(b,p-1)));
}
int powerm(int  b, int  p,int mod = mod)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=powerm(b,p/2)%mod;
      return ((q%mod)*(q%mod))%mod;
  }
  else return ((b%mod)*(powerm(b,p-1)%mod))%mod;
}

bool isprime(int n)
{
  if(n==1)return false;
  if(n==2 or n==3)return true;
  int  lim=sqrt(n);
  int  i=2;
  while(i<=lim)
  {
      if(n%i==0)return false;
      i++;
  }
  return true;
}
vector< int > factors( int  n)
{
  vector< int > a;
  a.pb(1);
  int  lim=sqrt(n);
  int i=2;
  while(i<=lim)
  {
      if(n%i==0)
      {
          a.pb(i);
          if(i!=n/i)a.pb(n/i);
      }
      i++;
  }

  if(n!=1)a.pb(n);
  //sort(a.begin(),a.end());
  return a;
}
vector<pii> pf_with_count(int  b)
{
  pii p;
  vector<pii>pf;
  int  count=0;
  while(b%2==0)
  {
    b/=2;
    count++;
  }
  if(count>0)
  {
    p.f=2;
    p.s=count;
    pf.pb(p);
  }
  int i=3;
  while(i<=sqrt(b))
  {
        count=0;
        while(b%i==0)
        {
            b/=i;
            count++;
        }
        if(count>0)
        {
            p.f=i;
            p.s=count;
            pf.pb(p);
        }
        i+=2;
  }
  if(b>2)
  {
      p.f=b;
      p.s=1;
      pf.pb(p);
  }
  sort(pf.begin(),pf.end());
  return pf;
}
int  bs_t_i(string s)
{
  int  l=strtoull(s.c_str(),NULL,2);
  return l;
}
string  i_to_bs(int  n)
{
  string s = bitset< 30 >( n ).to_string();
  return s;
}
int s_t_i(string s)
{
  stringstream obj(s);
  int x = 0;
  obj >> x;
  return x;
}
 //sons of  anarchy, top boy , end of the fucking world, the crown, ..

void sol()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    vector<pair<pii,pii > > v;
    int i=0;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        cin>>a[i][j];
        if(i-1>=0)v.pb({{i,j},{i-1,j}});
        if(j-1>=0)v.pb({{i,j},{i,j-1}});
        //if(i+1<n)v.pb({{i,j},{i+1,j}});
        //if(j+1<m)v.pb({{i,j},{i,j+1}});
        j++;
      }
      i++;
    }
    while(1)
    {
     pair<pii,pii> p;
     int foo=0;
     for(auto x:v)
     {
       int pro=0;
       if(a[x.f.f][x.f.s]<0 or a[x.s.f][x.s.s]<0)
       {
          int proo=0;
          if(a[x.f.f][x.f.s]<0)proo+=-a[x.f.f][x.f.s];
          if(a[x.s.f][x.s.s]<0)proo+=-a[x.s.f][x.s.s];
          if(a[x.f.f][x.f.s]>0)proo-=a[x.f.f][x.f.s];
          if(a[x.s.f][x.s.s]>0)proo-=a[x.s.f][x.s.s];
          if(proo>pro)
          {
            pro=proo;
            p={{x.f.f,x.f.s},{x.s.f,x.s.s}};
            foo=1;
          }
       }
     }
      if(foo==0)break;
      a[p.f.f][p.f.s]*=-1;
      a[p.s.f][p.s.s]*=-1;
    }
    int ans=0;
    i=0;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        ans+=a[i][j];
        j++;
      }
      i++;
    }
    cout<<ans<<endl;

}

signed main()
  {
         ios;
         int t = 1;
         cin >> t;
         while(t--)sol();
  }


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const int mod=1e9+7;

int takemod(int a,int m = mod)
{
  return ((a%m)+m)%m;
}
int power(int  b, int  p)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=power(b,p/2);
      return ((q)*(q));
  }
  else return ((b)*(power(b,p-1)));
}
int powerm(int  b, int  p,int mod = mod)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=powerm(b,p/2)%mod;
      return ((q%mod)*(q%mod))%mod;
  }
  else return ((b%mod)*(powerm(b,p-1)%mod))%mod;
}

bool isprime(int n)
{
  if(n==1)return false;
  if(n==2 or n==3)return true;
  int  lim=sqrt(n);
  int  i=2;
  while(i<=lim)
  {
      if(n%i==0)return false;
      i++;
  }
  return true;
}
vector< int > factors( int  n)
{
  vector< int > a;
  a.pb(1);
  int  lim=sqrt(n);
  int i=2;
  while(i<=lim)
  {
      if(n%i==0)
      {
          a.pb(i);
          if(i!=n/i)a.pb(n/i);
      }
      i++;
  }

  if(n!=1)a.pb(n);
  //sort(a.begin(),a.end());
  return a;
}
vector<pii> pf_with_count(int  b)
{
  pii p;
  vector<pii>pf;
  int  count=0;
  while(b%2==0)
  {
    b/=2;
    count++;
  }
  if(count>0)
  {
    p.f=2;
    p.s=count;
    pf.pb(p);
  }
  int i=3;
  while(i<=sqrt(b))
  {
        count=0;
        while(b%i==0)
        {
            b/=i;
            count++;
        }
        if(count>0)
        {
            p.f=i;
            p.s=count;
            pf.pb(p);
        }
        i+=2;
  }
  if(b>2)
  {
      p.f=b;
      p.s=1;
      pf.pb(p);
  }
  sort(pf.begin(),pf.end());
  return pf;
}
int  bs_t_i(string s)
{
  int  l=strtoull(s.c_str(),NULL,2);
  return l;
}
string  i_to_bs(int  n)
{
  string s = bitset< 30 >( n ).to_string();
  return s;
}
int s_t_i(string s)
{
  stringstream obj(s);
  int x = 0;
  obj >> x;
  return x;
}
 //sons of  anarchy, top boy , end of the fucking world, the crown, ..

void sol()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    vector<pair<pii,pii > > v;
    int cnt=0;
    int sum=0;
    int cnt0=0;
    int i=0;
    int ma=-mod;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        cin>>a[i][j];
        sum+=abs(a[i][j]);
        if(a[i][j]==0)cnt0++;
        if(a[i][j]<=0)
          {
            cnt++;
            ma=max(ma,a[i][j]);
          }
        j++;
      }
      i++;
    }
  //  cout<<ma<<endl;
    if(cnt%2==0 or cnt0>0)cout<<sum<<endl;
    else cout<<sum+2*ma<<endl;

}

signed main()
  {
         ios;
         int t = 1;
         cin >> t;
         while(t--)sol();
  }


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const int mod=1e9+7;

int takemod(int a,int m = mod)
{
  return ((a%m)+m)%m;
}
int power(int  b, int  p)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=power(b,p/2);
      return ((q)*(q));
  }
  else return ((b)*(power(b,p-1)));
}
int powerm(int  b, int  p,int mod = mod)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=powerm(b,p/2)%mod;
      return ((q%mod)*(q%mod))%mod;
  }
  else return ((b%mod)*(powerm(b,p-1)%mod))%mod;
}

bool isprime(int n)
{
  if(n==1)return false;
  if(n==2 or n==3)return true;
  int  lim=sqrt(n);
  int  i=2;
  while(i<=lim)
  {
      if(n%i==0)return false;
      i++;
  }
  return true;
}
vector< int > factors( int  n)
{
  vector< int > a;
  a.pb(1);
  int  lim=sqrt(n);
  int i=2;
  while(i<=lim)
  {
      if(n%i==0)
      {
          a.pb(i);
          if(i!=n/i)a.pb(n/i);
      }
      i++;
  }

  if(n!=1)a.pb(n);
  //sort(a.begin(),a.end());
  return a;
}
vector<pii> pf_with_count(int  b)
{
  pii p;
  vector<pii>pf;
  int  count=0;
  while(b%2==0)
  {
    b/=2;
    count++;
  }
  if(count>0)
  {
    p.f=2;
    p.s=count;
    pf.pb(p);
  }
  int i=3;
  while(i<=sqrt(b))
  {
        count=0;
        while(b%i==0)
        {
            b/=i;
            count++;
        }
        if(count>0)
        {
            p.f=i;
            p.s=count;
            pf.pb(p);
        }
        i+=2;
  }
  if(b>2)
  {
      p.f=b;
      p.s=1;
      pf.pb(p);
  }
  sort(pf.begin(),pf.end());
  return pf;
}
int  bs_t_i(string s)
{
  int  l=strtoull(s.c_str(),NULL,2);
  return l;
}
string  i_to_bs(int  n)
{
  string s = bitset< 30 >( n ).to_string();
  return s;
}
int s_t_i(string s)
{
  stringstream obj(s);
  int x = 0;
  obj >> x;
  return x;
}
 //sons of  anarchy, top boy , end of the fucking world, the crown, ..

void sol()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    vector<pair<pii,pii > > v;
    int cnt=0;
    int sum=0;
    int cnt0=0;
    int i=0;
    int ma=mod;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        cin>>a[i][j];
        ma=min(ma,abs(a[i][j]));
        sum+=abs(a[i][j]);
        if(a[i][j]==0)cnt0++;
        if(a[i][j]<0)cnt++;
        j++;
      }
      i++;
    }
    if(cnt%2==0)cout<<sum<<endl;
    else cout<<sum-2*ma<<endl;

}

signed main()
  {
         ios;
         int t = 1;
         cin >> t;
         while(t--)sol();
  }


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const int mod=1e9+7;

int takemod(int a,int m = mod)
{
  return ((a%m)+m)%m;
}
int power(int  b, int  p)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=power(b,p/2);
      return ((q)*(q));
  }
  else return ((b)*(power(b,p-1)));
}
int powerm(int  b, int  p,int mod = mod)
{
  if(p==0)return 1;
  else if(p%2==0)
  {
      int q=powerm(b,p/2)%mod;
      return ((q%mod)*(q%mod))%mod;
  }
  else return ((b%mod)*(powerm(b,p-1)%mod))%mod;
}

bool isprime(int n)
{
  if(n==1)return false;
  if(n==2 or n==3)return true;
  int  lim=sqrt(n);
  int  i=2;
  while(i<=lim)
  {
      if(n%i==0)return false;
      i++;
  }
  return true;
}
vector< int > factors( int  n)
{
  vector< int > a;
  a.pb(1);
  int  lim=sqrt(n);
  int i=2;
  while(i<=lim)
  {
      if(n%i==0)
      {
          a.pb(i);
          if(i!=n/i)a.pb(n/i);
      }
      i++;
  }

  if(n!=1)a.pb(n);
  //sort(a.begin(),a.end());
  return a;
}
vector<pii> pf_with_count(int  b)
{
  pii p;
  vector<pii>pf;
  int  count=0;
  while(b%2==0)
  {
    b/=2;
    count++;
  }
  if(count>0)
  {
    p.f=2;
    p.s=count;
    pf.pb(p);
  }
  int i=3;
  while(i<=sqrt(b))
  {
        count=0;
        while(b%i==0)
        {
            b/=i;
            count++;
        }
        if(count>0)
        {
            p.f=i;
            p.s=count;
            pf.pb(p);
        }
        i+=2;
  }
  if(b>2)
  {
      p.f=b;
      p.s=1;
      pf.pb(p);
  }
  sort(pf.begin(),pf.end());
  return pf;
}
int  bs_t_i(string s)
{
  int  l=strtoull(s.c_str(),NULL,2);
  return l;
}
string  i_to_bs(int  n)
{
  string s = bitset< 30 >( n ).to_string();
  return s;
}
int s_t_i(string s)
{
  stringstream obj(s);
  int x = 0;
  obj >> x;
  return x;
}
 //sons of  anarchy, top boy , end of the fucking world, the crown, ..

void sol()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    vector<pair<pii,pii > > v;
    int cnt=0;
    int sum=0;
    int cnt0=0;
    int i=0;
    int ma=-mod;
    while(i<n)
    {
      int j=0;
      while(j<m)
      {
        cin>>a[i][j];
        sum+=abs(a[i][j]);
        if(a[i][j]==0)cnt0++;
        if(a[i][j]<0)
          {
            cnt++;
            ma=max(ma,a[i][j]);
          }
        j++;
      }
      i++;
    }
  //  cout<<ma<<endl;
    if(cnt%2==0 or cnt0>0)cout<<sum<<endl;
    else cout<<sum+2*ma<<endl;

}

signed main()
  {
         ios;
         int t = 1;
         cin >> t;
         while(t--)sol();
  }


#include<bits/stdc++.h>
using namespace std;
typedef long long           ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...);
}

ll max(ll a, ll b) {return (a > b ? a : b);}
ll min(ll a, ll b) {return (a < b ? a : b);}
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x) cout << "YES\n"; else cout << "NO\n";
}
void solve_test() {
  ll ans = 0;
  ll n, m;
  cin >> n >> m;
  ll a[n][m];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  bool zero = 0, neg = 0;
  ll neg_count = 0, pos_count = 0;
  ll mx = INT_MIN, mx_pos = 1;
  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) zero = 1;
      if (a[i][j] < 0) {
        neg = 1;
        neg_count++;
        mx = max(mx, a[i][j]);
      }
      else {
        mx_pos = max(mx_pos, a[i][j]);
        pos_count++;
      }
    }

  }

  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      ans += abs(a[i][j]);

    }

  }
//  trace(mx, ans);
  if (zero == 0) {
    if (neg_count & 1) {
      if (pos_count == 0) mx_pos = 1;
      ans += (2 * max(mx, -mx_pos));
    }

  }

  cout << ans << "\n";
}
int main()
{
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t; while (t--)
    solve_test();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long           ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...);
}

ll max(ll a, ll b) {return (a > b ? a : b);}
ll min(ll a, ll b) {return (a < b ? a : b);}
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x) cout << "YES\n"; else cout << "NO\n";
}
void solve_test() {
  ll ans = 0;
  ll n, m;
  cin >> n >> m;
  ll a[n][m];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  bool zero = 0, neg = 0;
  ll neg_count = 0, pos_count = 0;
  ll mx = INT_MIN, mx_pos = INT_MAX;
  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) zero = 1;
      if (a[i][j] < 0) {
        neg = 1;
        neg_count++;
        mx = max(mx, a[i][j]);
      }
      else {
        mx_pos = min(mx_pos, a[i][j]);
        pos_count++;
      }
    }

  }

  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      ans += abs(a[i][j]);

    }

  }
//  trace(mx, ans);
  if (zero == 0) {
    if (neg_count & 1) {
      ll temp;
      temp = mx;
      if (pos_count > 0) {
        temp = max(temp, -mx_pos);

      }
      ans += (2 * temp);
    }

  }

  cout << ans << "\n";
}
int main()
{
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t; while (t--)
    solve_test();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long           ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...);
}

ll max(ll a, ll b) {return (a > b ? a : b);}
ll min(ll a, ll b) {return (a < b ? a : b);}
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x) cout << "YES\n"; else cout << "NO\n";
}
void solve_test() {
  ll ans = 0;
  ll n, m;
  cin >> n >> m;
  ll a[n][m];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  bool zero = 0, neg = 0;
  ll neg_count = 0;
  ll mx = INT_MIN;
  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) zero = 1;
      if (a[i][j] < 0) {
        neg = 1;
        neg_count++;
        mx = max(mx, a[i][j]);
      }
    }

  }

  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      ans += abs(a[i][j]);

    }

  }
//  trace(mx, ans);
  if (zero == 0) {
    if (neg_count & 1)
      ans += (2 * mx);

  }

  cout << ans << "\n";
}
int main()
{
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t; while (t--)
    solve_test();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long           ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...);
}

ll max(ll a, ll b) {return (a > b ? a : b);}
ll min(ll a, ll b) {return (a < b ? a : b);}
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x) cout << "YES\n"; else cout << "NO\n";
}
void solve_test() {
  ll ans = 0;
  ll n, m;
  cin >> n >> m;
  ll a[n][m];

  for (int i = 0; i < n; i++)

    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  bool zero = 0, neg = 0;

  ll mx = INT_MIN;
  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) zero = 1;
      if (a[i][j] < 0) {
        neg = 1;
        mx = max(mx, a[i][j]);
      }
    }

  }

  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      ans += abs(a[i][j]);

    }

  }
//  trace(mx, ans);
  if (zero == 0) {
    if (neg)
      ans += (2 * mx);
  }
  cout << ans << "\n";
}
int main()
{
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t; while (t--)
    solve_test();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long           ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...);
}

ll max(ll a, ll b) {return (a > b ? a : b);}
ll min(ll a, ll b) {return (a < b ? a : b);}
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x) cout << "YES\n"; else cout << "NO\n";
}
void solve_test() {
  ll ans = 0;
  ll n, m;
  cin >> n >> m;
  ll a[n][m];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  bool zero = 0, neg = 0;
  ll neg_count = 0, pos_count = 0;
  ll mx = INT_MIN, mx_pos = -1;
  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) zero = 1;
      if (a[i][j] < 0) {
        neg = 1;
        neg_count++;
        mx = max(mx, a[i][j]);
      }
      else {
        mx_pos = max(mx_pos, a[i][j]);
        pos_count++;
      }
    }

  }

  for (int i = 0; i < n; i++)
  {

    for (int j = 0; j < m; j++) {
      ans += abs(a[i][j]);

    }

  }
//  trace(mx, ans);
  if (zero == 0) {
    if (neg_count & 1) {
      ll temp;
      temp = mx;
      if (pos_count > 0) {
        temp = max(temp, -mx_pos);

      }
      ans += (2 * temp);
    }

  }

  cout << ans << "\n";
}
int main()
{
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t; while (t--)
    solve_test();
  return 0;
}


// Problem : B. Numbers Box
// Contest : Codeforces - Codeforces Round #683 (Div. 2, by Meet IT)
// URL : https://codeforces.com/contest/1447/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
int main()
{
	fast;
	ll t = 1;
	cin >> t;
	while (t--)
	{
		ll n,m, cnt = 0, res = 0,z = 0;
		cin >> n >> m;
		ll num = n*m;
		ll a[num];
		for(ll i=0;i<num;i++)
		{
			cin>>a[i];
			if(a[i] == 0)
				z = 1;
			if(a[i] < 0)
			{
				cnt++;
				res += -a[i];
			}
			else
				res += a[i];
		}
		if(z == 1)
			cout<<res<<"\n";
		else if((cnt%2) == 0)
			cout<<res<<"\n";
		else
		{
			ll mn = INT_MAX,nn = 0;
			for(ll i=0;i<num;i++)
			{
				if(a[i] < 0)
				{
					nn = -a[i];
					mn = min(mn,nn);
				}
			}
			res -= mn;
			res -= mn;
			cout<<res<<"\n";
		}
	}
	return 0;
}


// Problem : B. Numbers Box
// Contest : Codeforces - Codeforces Round #683 (Div. 2, by Meet IT)
// URL : https://codeforces.com/contest/1447/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
int main()
{
	fast;
	ll t = 1;
	cin >> t;
	while (t--)
	{
		ll n,m, cnt = 0, res = 0;
		cin >> n >> m;
		ll num = n*m;
		ll a[num];
		for(ll i=0;i<num;i++)
		{
			cin>>a[i];
			if(a[i] < 0)
			{
				cnt++;
				res += -a[i];
			}
			else
				res += a[i];
		}
		sort(a,a+num);
		if(cnt != 1)
			cout<<res<<"\n";
		else
		{
			ll mn = INT_MAX,nn;
			for(ll i=0;i<num;i++)
			{
				if(a[i] < 0)
				{
					nn = -a[i];
					mn = min(mn,nn);
				}
			}
			res -= nn;
			res -= nn;
			cout<<res<<"\n";
		}
	}
	return 0;
}


// Problem : B. Numbers Box
// Contest : Codeforces - Codeforces Round #683 (Div. 2, by Meet IT)
// URL : https://codeforces.com/contest/1447/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
int main()
{
	fast;
	ll t = 1;
	cin >> t;
	while (t--)
	{
		ll n,m, cnt = 0, res = 0,z = 0;
		cin >> n >> m;
		ll num = n*m;
		ll a[num];
		for(ll i=0;i<num;i++)
		{
			cin>>a[i];
			if(a[i] == 0)
				z = 1;
			if(a[i] < 0)
			{
				cnt++;
				res += -a[i];
			}
			else
				res += a[i];
		}
		sort(a,a+num);
		if(z == 1)
			cout<<res<<"\n";
		else if(cnt%2 == 0)
			cout<<res<<"\n";
		else
		{
			ll mn = INT_MAX,nn;
			for(ll i=0;i<num;i++)
			{
				if(a[i] < 0)
				{
					nn = -a[i];
					mn = min(mn,nn);
				}
			}
			res -= nn;
			res -= nn;
			cout<<res<<"\n";
		}
	}
	return 0;
}


// Problem : B. Numbers Box
// Contest : Codeforces - Codeforces Round #683 (Div. 2, by Meet IT)
// URL : https://codeforces.com/contest/1447/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
int main()
{
	fast;
	ll t = 1;
	cin >> t;
	while (t--)
	{
		ll n,m, cnt = 0, res = 0,z = 0;
		cin >> n >> m;
		ll num = n*m;
		ll a[num];
		for(ll i=0;i<num;i++)
		{
			cin>>a[i];
			if(a[i] == 0)
				z = 1;
			if(a[i] < 0)
			{
				cnt++;
				res += -a[i];
				a[i] = -a[i];
			}
			else
				res += a[i];
		}
		sort(a,a+num);
		if(z == 1)
			cout<<res<<"\n";
		else if((cnt%2) == 0)
			cout<<res<<"\n";
		else
		{
			ll mn = INT_MAX,nn = 0;
			for(ll i=0;i<num;i++)
			{
				if(a[i] < 0)
				{
					nn = -a[i];
					mn = min(mn,nn);
				}
			}
			res -= (2*a[0]);
			cout<<res<<"\n";
		}
	}
	return 0;
}


// Problem : B. Numbers Box
// Contest : Codeforces - Codeforces Round #683 (Div. 2, by Meet IT)
// URL : https://codeforces.com/contest/1447/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
int main()
{
	fast;
	ll t = 1;
	cin >> t;
	while (t--)
	{
		ll n,m, cnt = 0, res = 0,z = 0;
		cin >> n >> m;
		ll num = n*m;
		ll a[num];
		for(ll i=0;i<num;i++)
		{
			cin>>a[i];
			if(a[i] == 0)
				z = 1;
			if(a[i] < 0)
			{
				cnt++;
				res += -a[i];
			}
			else
				res += a[i];
		}
		sort(a,a+num);
		if(cnt != 1)
			cout<<res<<"\n";
		else if(z == 1)
			cout<<res<<"\n";
		else
		{
			ll mn = INT_MAX,nn;
			for(ll i=0;i<num;i++)
			{
				if(a[i] < 0)
				{
					nn = -a[i];
					mn = min(mn,nn);
				}
			}
			res -= nn;
			res -= nn;
			cout<<res<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
#define T int t = 0; cin >> t; while(t--)
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef  long double lld;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const ll mx = 1e4;

void input(){
    int n ; cin >> n;
    vector<int>a(n);
    f(i, n ) cin >> a[i];
    vector<unordered_map<int, int> > dp(n);
    int ans = 1;
    for(int i = n - 1; i >= 0; --i){
            dp[i][0] = 1;
        for(int j= i + 1; j < n; ++j){
            int diff = a[i] - a[j];
             dp[i][diff] = max(dp[i][diff], 1 + max(1, dp[j][-diff]));
             ans = max(ans, dp[i][diff]);
        }
    }
    cout << ans << endl;
}
int main(){
    input();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
#define T int t = 0; cin >> t; while(t--)
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef  long double lld;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const ll mx = 1e4;
vector<unordered_map<int, int> > dp(4001);
void compress(vector<int>&a, int n){
    set<int>s;
    for(int x : a)s.insert(x);
    map<int, int>m;
    int i =0;
    for(auto &it : s)m[it] = i++;
    f(i, n){
        a[i] = m[a[i]];
    }
}
void input(){
    int n ; cin >> n;
    vector<int>a(n);
    f(i, n ) cin >> a[i];
    compress(a, n);

    int ans = 1;
    for(int i = n - 1; i >= 0; --i){
            dp[i][0] = 1;
        for(int j= i + 1; j < n; ++j){
            int diff = a[i] - a[j];
             dp[i][diff] = max(dp[i][diff], 1 + max(1, dp[j][-diff]));
             ans = max(ans, dp[i][diff]);
        }
    }
    cout << ans << endl;
}
int main(){
    input();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
#define T int t = 0; cin >> t; while(t--)
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef  long double lld;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const ll mx = 1e4;
vector<unordered_map<int, int> > dp(4001);

void input(){
    int n ; cin >> n;
    vector<int>a(n);
    f(i, n ) cin >> a[i];

    int ans = 1;
    for(int i = n - 1; i >= 0; --i){
            dp[i][0] = 1;
        for(int j= i + 1; j < n; ++j){
            int diff = a[i] - a[j];
             dp[i][diff] = max(dp[i][diff], 1 + max(1, dp[j][-diff]));
             ans = max(ans, dp[i][diff]);
        }
    }
    cout << ans << endl;
}
int main(){
    input();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
#define T int t = 0; cin >> t; while(t--)
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef  long double lld;
const int mx = 1 << 12;
int dp[mx][mx];
map<int, int>m;
set<int>s;
int z, n;
int a[mx];
void compress(){
    f(i, n)s.insert(a[i]);
    int i =0;
    for(auto &it : s)m[it] = i++;
    f(i, n){
        a[i] = m[a[i]];
    }
    s.clear();
    m.clear();
}
void input(){
    scanf("%d", &n);
    f(i, n ) {
        scanf("%d", &a[i]);
    }
    compress();
    int ans = 1;
    for(int i = n - 1; i >= 0; --i){
            dp[a[i]][a[i]] = 1;
        for(int j= i + 1; j < n; ++j){
                dp[a[i]][a[j]] = max(dp[a[i]][a[j]], 1 + max(1, dp[a[j]][a[i]]));
                ans = max(ans, dp[a[i]][a[j]]);
        }
    }
    cout << ans << endl;
}
int main(){
    input();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
#define T int t = 0; cin >> t; while(t--)
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef  long double lld;
const int mx = 4000;
int dp[mx][mx];
map<int, int>m;
set<int>s;
int z, n;
int a[mx];
void compress(){
    f(i, n)s.insert(a[i]);
    int i =0;
    for(auto &it : s)m[it] = i++;
    f(i, n){
        a[i] = m[a[i]];
    }
    s.clear();
    m.clear();
}
void input(){
    scanf("%d", &n);
    f(i, n ) {
        cin >> a[i];
    }
    compress();
    int ans = 1;
    for(int i = n - 1; i >= 0; --i){
            dp[a[i]][a[i]] = 1;
        for(int j= i + 1; j < n; ++j){
                dp[a[i]][a[j]] = max(dp[a[i]][a[j]], 1 + max(1, dp[a[j]][a[i]]));
                ans = max(ans, dp[a[i]][a[j]]);
        }
    }
    cout << ans << endl;
}
int main(){
    input();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
#define T int t = 0; cin >> t; while(t--)
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef  long double lld;
const int mx = 1e4;
int dp[mx][mx];
map<int, int>m;
set<int>s;
int z;
int a[mx];
void compress(int n){
    f(i, n)s.insert(a[i]);
    int i =0;
    for(auto &it : s)m[it] = i++;
    f(i, n){
        a[i] = m[a[i]];
    }
    s.clear();
    m.clear();
}
void input(){
    int n ; cin >> n;
    f(i, n ) {
        cin >> a[i];
    }
    compress(n);
    int ans = 1;
    for(int i = n - 1; i >= 0; --i){
            dp[a[i]][a[i]] = 1;
        for(int j= i + 1; j < n; ++j){
                dp[a[i]][a[j]] = max(dp[a[i]][a[j]], 1 + max(1, dp[a[j]][a[i]]));
                ans = max(ans, dp[a[i]][a[j]]);
        }
    }
    cout << ans << endl;
}
int main(){
    input();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;
vvi ind(1e6 + 1);

int solve(int idx, int q, int one) {
	int nxt = num[idx] + one * q;
	auto it = upper_bound(ind[nxt].begin(), ind[nxt].end(), idx);
	if (it == ind[nxt].end())
		return 1;
	return solve(*it, q, -one) + 1;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i], ind[num[i]].push_back(i);

	int mx = 0;
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			mx = max(mx, solve(j, num[i] - num[j], 1));
		}
	}

	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;
vvi ind(1e6 + 1);
short mem[4000][14000][2];
int solve(int idx, int q, int one) {
	if (q + 7000 < 14000 && q + 7000 >= 0 && mem[idx][q][one > 0] != -1) return mem[idx][q][one > 0];
	int nxt = num[idx] + one * q;
	auto it = upper_bound(ind[nxt].begin(), ind[nxt].end(), idx);
	if (it == ind[nxt].end())
		return 1;
	int ret = solve(*it, q, -one) + 1;
	return q + 7000 < 14000 && q + 7000 >= 0 ? mem[idx][q][one > 0] = ret : ret;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	memset(mem, -1, sizeof mem);
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i], ind[num[i]].push_back(i);

	int mx = 0;
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			mx = max(mx, solve(j, num[i] - num[j], 1));
		}
	}

	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;

int mem[4000][7000][2];
int solve(int idx, int q, int one) {
	if (idx == n) return 0;
	if (q < 7000 & mem[idx][q][one>0] != -1) return mem[idx][q][one>0];

	int ret = 0;
	for (int i = idx + 1; i < n; i++)
		if (num[i] == num[idx] + one * q) {
			ret = max(ret, solve(i, q, -one));
		}
	return q < 7000 ? mem[idx][q][one>0] = ret + 1 : ret + 1;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	memset(mem, -1, sizeof mem);
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i];
	int mx = 0;
	for (int i = 0; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
			mx = max(mx, solve(j, num[i] - num[j], 1));
	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;

short mem[4000][14000][2];
int solve(int idx, int q, int one) {
	if (idx == n) return 0;
	if (q + 7000 < 14000 && q + 7000 >= 0 && mem[idx][q][one > 0] != -1) return mem[idx][q][one > 0];

	int ret = 0;
	for (int i = idx + 1; i < n; i++)
		if (num[i] == num[idx] + one * q) {
			ret = max(ret, solve(i, q, -one));
		}
	return q + 7000 < 14000 && q + 7000 >= 0 ? mem[idx][q][one > 0] = ret + 1 : ret + 1;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	memset(mem, -1, sizeof mem);
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i];
	int mx = 0;
	for (int i = 0; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
			mx = max(mx, solve(j, num[i] - num[j], 1));
	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;
vvi ind(1e6 + 1);
short mem[4000][4000][2];
int solve(int idx, int prvidx, int one) {
	if (mem[idx][prvidx][one > 0] != -1) return mem[idx][prvidx][one > 0];
	int nxt = num[idx] + one * (num[idx] - num[prvidx]);
	auto it = upper_bound(ind[nxt].begin(), ind[nxt].end(), idx);
	if (it == ind[nxt].end())
		return 1;
	int ret = solve(*it, idx, -one) + 1;
	return mem[idx][prvidx][one > 0] = ret;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	memset(mem, -1, sizeof mem);
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i], ind[num[i]].push_back(i);

	int mx = 0;
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			mx = max(mx, solve(j, i, -1));
		}
	}

	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;
vvi ind(1e6 + 1);
short mem[4000][4000];
int solve(int idx, int prvidx) {
	if (mem[idx][prvidx] != -1) return mem[idx][prvidx];

	int nxt = num[idx] + -(num[idx] - num[prvidx]);
	auto it = upper_bound(ind[nxt].begin(), ind[nxt].end(), idx);
	if (it == ind[nxt].end())
		return 1;
	int ret = solve(*it, idx) + 1;

	return mem[idx][prvidx] = ret;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	memset(mem, -1, sizeof mem);
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i], ind[num[i]].push_back(i);

	int mx = 0;
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			mx = max(mx, solve(j, i));
		}
	}

	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<string> vs;
const int di[] = { -1,0,1,0 };
const int dj[] = { 0,1,0,-1 };
const int MOD = 1e9 + 7;
const int INF = 1e9;
#define mp make_pair

int n;
vi num;

int mem[4000][7000][2];
int solve(int idx, int q, int one) {
	if (idx == n) return 0;
	if (q < 7000 & mem[idx][q][one] != -1) return mem[idx][q][one];

	int ret = 0;
	for (int i = idx + 1; i < n; i++)
		if (num[i] == num[idx] + one * q) {
			ret = max(ret, solve(i, q, -one));
		}
	return q < 7000 ? mem[idx][q][one] = ret + 1 : ret + 1;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	//ofstream cout("output.txt"); for (int i = 1; i <= 4000; i++) cout << i * 100 << endl;
	memset(mem, -1, sizeof mem);
	cin >> n;
	num.resize(n);
	for (int i = 0; i < n; i++)
		cin >> num[i];
	int mx = 0;
	for (int i = 0; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
			mx = max(mx, solve(j, num[i] - num[j], 1));
	cout << mx + 1 << endl;
	cin.ignore(), cin.get();
}


#include <iostream>
#include <vector>
using namespace std;
int main() {// TLE
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) cin >> v[i] ;
    int mx = -1 ;
    for ( int i = 1 ; i <= n ; i++ ){
        for ( int j = i+1 ; j <= n ; j++ ){
            int q =  v[j] - v[i] ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std ;
#define ll long long
int main ()
{
	int n; cin >> n;
	vector<int>v(n);
	map<int,int>mask,freq;
	int counter = 0;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&v[i]);
		if( mask[ v[i] ] == 0 )
			mask[ v[i] ] = ++counter;
		v[i] = mask[ v[i] ];
		freq[v[i]]++;
	}
	vector<int> dp[4001] ,prev[4001];
	for(int i=1;i<=4000;++i)
		dp[i].resize(4001) , prev[i].resize(4001);
	for(int i=0;i<n;i++)
	{
		for(int j=1;j<=4000;++j)
		{
			if ( j == v[i] )continue;
			if( dp[ v[i] ][j] == 0 )
			{
				dp[v[i]][j] = 1;
				dp[j][v[i]] = 1;
				prev[v[i]][j] = 1;
				prev[j][v[i]] = 2;
			}else
			if( prev[ v[i] ][j] == 2 )
			{
				dp[ v[i] ][j] ++ ;
				dp[j][ v[i] ] ++ ;
				prev[ v[i] ][ j ] = 1;
				prev[j][ v[i] ] = 2;
			}
		}

	}
	int mx = -1;
	for(int i=1;i<=4000;i++)
	{
		mx = max( mx , freq[i] );// q == 0
		for(int j=1;j<=4000;j++)
			mx = max( mx , dp[i][j] );
	}
	cout << mx ;

}


#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) cin >> v[i] ;
    int mx = 1 ;
    for ( int i = 1 ; i <= n ; i++ ){
        for ( int j = i+1 ; j <= n ; j++ ){
            int q =  v[j] - v[i] ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int N = 1e6 + 1 ;
int dp[N][N] ={};
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) cin >> v[i] ;
    int mx = 1 ;
    for ( int i = 1 ; i <= n ; i++ ){ bool flag = 1 ;
        for ( int j = i+1 ; j <= n ; j++ ){
            if ( ( dp [ v[i] ][ v[j] ] == 1 || dp [ v[j] ][ v[i] ] == 1 ) && flag ){
               continue ;
            }flag = 0 ;
            dp [ v[i] ][ v[j] ] = 1 , dp [ v[j] ][ v[i] ] = 1 ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std ;
#define ll long long
int main ()
{
	int n; cin >> n;
	vector<int>v(n);
	map<int,int>mask;
	int counter = 0;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&v[i]);
		if( mask[ v[i] ] == 0 )
			mask[ v[i] ] = ++counter;
		v[i] = mask[ v[i] ];
	}
	vector<int> dp[4001] ,prev[4001];
	for(int i=1;i<=4000;++i)
		dp[i].resize(4001) , prev[i].resize(4001);
	for(int i=0;i<n;i++)
	{
		for(int j=1;j<=2;++j)
		{
			if ( j == v[i] )continue;
			if( dp[ v[i] ][j] == 0 )
			{
				dp[v[i]][j] = 1;
				dp[j][v[i]] = 1;
				prev[v[i]][j] = 1;
				prev[j][v[i]] = 2;
			}else
			if( prev[ v[i] ][j] == 2 )
			{
				dp[ v[i] ][j] ++ ;
				dp[j][ v[i] ] ++ ;
				prev[ v[i] ][ j ] = 1;
				prev[j][ v[i] ] = 2;
			}
		}

	}
	int mx = -1;
	for(int i=1;i<=4000;i++)
	{
		for(int j=1;j<=4000;j++)
			mx = max( mx , dp[i][j] );
	}
	cout << mx ;

}


#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) cin >> v[i] ;
    int mx = 1 ;
    vector < map<int,int > > dp(n+1) ;
    for ( int i = 1 ; i <= n ; i++ ){
        for ( int j = i+1 ; j <= n ; j++ ){
            if ( dp[i][j] == 1 ) continue ;
            int q =  v[j] - v[i] ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    dp[j][k] = 1 ;
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
#include <stdio.h>
using namespace std;
const int N = 1e6 + 1 ;
map<int,int> dp ;
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) scanf("%d",v[i]) ;
    int mx = 1 ;
    for ( int i = 1 ; i <= n ; i++ ){ bool flag = 1 ;
        for ( int j = i+1 ; j <= n ; j++ ){
            if ( ( dp [ v[i] ] == v[j] || dp [ v[j] ] == v[i] ) && flag ){
               continue ;
            }flag = 0 ;
            dp [ v[i] ] = v[j] , dp [ v[j] ] = v[i] ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
using namespace std;
int dp[4001][4001] ={};
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) cin >> v[i] ;
    int mx = 1 ;
    for ( int i = 1 ; i <= n ; i++ ){ bool flag = 1 ;
        for ( int j = i+1 ; j <= n ; j++ ){
            if ( ( dp [ v[i] ][ v[j] ] == 1 || dp [ v[j] ][ v[i] ] == 1 ) && flag ){
               continue ;
            }flag = 0 ;
            dp [ v[i] ][ v[j] ] = 1 , dp [ v[j] ][ v[i] ] = 1 ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std ;
#define ll long long
int main ()
{
	int n; cin >> n;
	vector<int>v(n);
	map<int,int>mask;
	int counter = 0;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&v[i]);
		if( mask[ v[i] ] == 0 )
			mask[ v[i] ] = ++counter;
		v[i] = mask[ v[i] ];
	}
	vector<int> dp[4001] ,prev[4001];
	for(int i=1;i<=4000;++i)
		dp[i].resize(4001) , prev[i].resize(4001);
	for(int i=0;i<n;i++)
	{
		for(int j=1;j<=4000;++j)
		{
			if ( j == v[i] )continue;
			if( dp[ v[i] ][j] == 0 )
			{
				dp[v[i]][j] = 1;//fuk
				dp[j][v[i]] = 1;
				prev[v[i]][j] = 1;
				prev[j][v[i]] = 2;
			}else
			if( prev[ v[i] ][j] == 2 )
			{
				dp[ v[i] ][j] ++ ;
				dp[j][ v[i] ] ++ ;
				prev[ v[i] ][ j ] = 1;
				prev[j][ v[i] ] = 2;
			}
		}

	}
	int mx = -1;
	for(int i=1;i<=4000;i++)
	{
		for(int j=1;j<=4000;j++)
			mx = max( mx , dp[i][j] );
	}
	cout << mx ;

}


#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int N = 1e6 + 1 ;
map<int,int> dp ;
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) cin >> v[i] ;
    int mx = 1 ;
    for ( int i = 1 ; i <= n ; i++ ){ bool flag = 1 ;
        for ( int j = i+1 ; j <= n ; j++ ){
            if ( ( dp [ v[i] ] == v[j] || dp [ v[j] ] == v[i] ) && flag ){
               continue ;
            }flag = 0 ;
            dp [ v[i] ] = v[j] , dp [ v[j] ] = v[i] ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
#include <stdio.h>
using namespace std;
const int N = 1e6 + 1 ;
map<int,int> dp ;
int main() {
    int n ; cin >> n ;
    vector <int > v(n+1);
    for (int i=1;i<=n;i++) scanf("%d",&v[i]) ;
    int mx = 1 ;
    for ( int i = 1 ; i <= n ; i++ ){ bool flag = 1 ;
        for ( int j = i+1 ; j <= n ; j++ ){
            if ( ( dp [ v[i] ] == v[j] || dp [ v[j] ] == v[i] ) && flag ){
               continue ;
            }flag = 0 ;
            dp [ v[i] ] = v[j] , dp [ v[j] ] = v[i] ;
            int current = v[i] , vessile = v[j] ;
            int counter = 0 ;
            for ( int k = i ; k <=n ; k++ ){
                if ( v[k] == current ){
                    counter ++ ; swap( current , vessile ) ;
                }
            }
            mx = max ( mx , counter ) ;
        }
    }
    cout << mx ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std ;
#define ll long long
int main ()
{
	int n; cin >> n;
	vector<int>v(n);
	map<int,int>mask;
	int counter = 0;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&v[i]);
		if( mask[ v[i] ] == 0 )
			mask[ v[i] ] = ++counter;
		v[i] = mask[ v[i] ];
	}
	vector<int> dp[4001] ,prev[4001];
	for(int i=1;i<=4000;++i)
		dp[i].resize(4001) , prev[i].resize(4001);
	for(int i=0;i<n;i++)
	{
		for(int j=1;j<=n;++j)
		{
			if ( j == v[i] )continue;
			if( dp[ v[i] ][j] == 0 )
			{
				dp[v[i]][j] = 1;
				dp[j][v[i]] = 1;
				prev[v[i]][j] = 1;
				prev[j][v[i]] = 2;
			}else
			if( prev[ v[i] ][j] == 2 )
			{
				dp[ v[i] ][j] ++ ;
				dp[j][ v[i] ] ++ ;
				prev[ v[i] ][ j ] = 1;
				prev[j][ v[i] ] = 2;
			}
		}

	}
	int mx = -1;
	for(int i=1;i<=4000;i++)
	{
		for(int j=1;j<=4000;j++)
			mx = max( mx , dp[i][j] );
	}
	cout << mx ;

}


#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std ;
#define ll long long
int main ()
{
	int n; cin >> n;
	vector<int>v(n);
	map<int,int>mask,visited;
	int counter = 0;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&v[i]);
		if( mask[ v[i] ] == 0 )
			mask[ v[i] ] = ++counter;
		visited[ mask[ v[i] ] ] = 1;
		v[i] = mask[ v[i] ];
	}
	vector<int> dp[4001] ,prev[4001];
	for(int i=1;i<=4000;++i)
		dp[i].resize(4001) , prev[i].resize(4001);
	for(int i=0;i<n;i++)
	{
		for(int j=1;j<=4000;++j)
		{
			if ( j == v[i] || visited[j] == 0 )continue;
			if( dp[ v[i] ][j] == 0 )
			{
				dp[v[i]][j] = 1;
				dp[j][v[i]] = 1;
				prev[v[i]][j] = 1;
				prev[j][v[i]] = 2;
			}else
			if( prev[ v[i] ][j] == 2 )
			{
				dp[ v[i] ][j] ++ ;
				dp[j][ v[i] ] ++ ;
				prev[ v[i] ][ j ] = 1;
				prev[j][ v[i] ] = 2;
			}
		}

	}
	int mx = -1;
	for(int i=1;i<=4000;i++)
	{
		for(int j=1;j<=4000;j++)
			mx = max( mx , dp[i][j] );
	}
	cout << mx ;

}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const long long N = 4010;
vector<long long>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<long long>s;
    for(long long i = 1;i<=n;i++)
    {
        long long a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    long long ans = 1;

    for(auto i:s)
    {
        for(auto j:s)
        {
            long long id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                auto it2 = upper_bound(g[i].begin(), g[i].end(), id);
                if(it2 == g[i].end())
                    break;
                id = *it2;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 4010;
vector<int>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<int>s;
    for(int i = 1;i<=n;i++)
    {
        int a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    int ans = 1;

    for(auto i:s)
    {
        for(auto j:s)
        {
            int id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                auto it2 = upper_bound(g[i].begin(), g[i].end(), id);
                if(it2 == g[i].end())
                    break;
                id = *it2;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 4010;
vector<long long>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<long long>s;
    for(int i = 1;i<=n;i++)
    {
        long long a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    long long ans = 0;

    for(auto i:s)
    {
        for(auto j:s)
        {
            long long id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                auto it2 = upper_bound(g[i].begin(), g[i].end(), id);
                if(it2 == g[i].end())
                    break;
                id = *it2;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 4010;
vector<long long>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<long long>s;
    for(int i = 1;i<=n;i++)
    {
        long long a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    long long ans = 0;

    for(auto i:s)
    {
        for(auto j:s)
        {
            long long id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                it = upper_bound(g[i].begin(), g[i].end(), id);
                if(it == g[i].end())
                    break;
                id = *it;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 4010;
vector<long long>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<long long>s;
    for(long long i = 1;i<=n;i++)
    {
        long long a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    long long ans = 0;

    for(auto i:s)
    {
        for(auto j:s)
        {
            long long id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                auto it2 = upper_bound(g[i].begin(), g[i].end(), id);
                if(it2 == g[i].end())
                    break;
                id = *it2;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 1e6+5;
vector<int>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<int>s;
    for(int i = 1;i<=n;i++)
    {
        int a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    int ans = 1;

    for(auto i:s)
    {
        for(auto j:s)
        {
            int id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                auto it2 = upper_bound(g[i].begin(), g[i].end(), id);
                if(it2 == g[i].end())
                    break;
                id = *it2;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}