import java.util.*;

public class problems {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0) {
			int n=sc.nextInt();
			solve(n);
			t--;
			System.out.println();
		}
	}
	public static void solve(int n) {
		if(n/2%2==1) {
			System.out.println("NO");
		}else {
			System.out.println("YES");
			int a[]=new int[n];
			int sum=0;
			int odd=1;
			int ind=n/2;
			while(ind<n) {
				a[ind]=odd;
				sum+=odd;
				odd+=2;
				ind++;
			}
			int even=2;
			ind=0;
			while(ind<n/2) {
				if(ind==n/2-1) {
					a[ind]=sum;
				}else {
					a[ind]=even;
				}
				sum-=even;
				even+=2;
				ind++;
			}
			for(int i:a) {
				System.out.print(i+" ");
			}
		}
	}
}


import java.util.*;

public class problems {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0) {
			int n=sc.nextInt();
			solve(n);
			t--;
			System.out.println();
		}
	}
	public static void solve(int n) {
		if(n/2%2==1) {
			System.out.print("NO");
		}else {
			System.out.print("YES");
			System.out.println();
			int a[]=new int[n];
			int even=2;
			int ind=0;
			int sum=0;
			while(ind<n/2) {
				a[ind]=even;
				sum+=even;
				even+=2;
				ind++;
			}
			int odd=1;
			while(ind<n-1) {
				a[ind]=odd;
				sum-=odd;
				odd+=2;
				ind++;
			}
			a[ind]=sum;
			for(int i:a) {
				System.out.print(i+" ");
			}
		}
	}
}


import java.util.*;

public class problems {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0) {
			int n=sc.nextInt();
			solve(n);
			t--;
			System.out.println();
		}
	}
	public static void solve(int n) {
		if(n/2%2==1) {
			System.out.print("NO");
		}else {
			System.out.print("YES");
			System.out.println();
			int a[]=new int[n];
			int sum=0;
			int odd=1;
			int ind=n/2;
			while(ind<n) {
				a[ind]=odd;
				sum+=odd;
				odd+=2;
				ind++;
			}
			int even=2;
			ind=0;
			while(ind<n/2) {
				if(ind==n/2-1) {
					a[ind]=sum;
				}else {
					a[ind]=even;
				}
				sum-=even;
				even+=2;
				ind++;
			}
			for(int i:a) {
				System.out.print(i+" ");
			}
		}
	}
}


import java.util.*;

public class problems {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0) {
			int n=sc.nextInt();
			solve(n);
			System.out.println();
			t--;
		}
	}
	public static void solve(int n) {
		if(n/2%2==1) {
			System.out.println("NO");
		}else {
			System.out.println("YES");
			int a[]=new int[n];
			int sum=0;
			int odd=1;
			int ind=n/2;
			while(ind<n) {
				a[ind]=odd;
				sum+=odd;
				odd+=2;
				ind++;
			}
			int even=2;
			ind=0;
			while(ind<n/2) {
				if(ind==n/2-1) {
					a[ind]=sum;
				}else {
					a[ind]=even;
				}
				sum-=even;
				even+=2;
				ind++;
			}
			for(int i:a) {
				System.out.print(i)+" ";
			}
		}
	}
}


import java.util.*;

public class problems {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0) {
			int n=sc.nextInt();
			solve(n);
			t--;
		}
	}
	public static void solve(int n) {
		if(n/2%2==1) {
			System.out.println("NO");
		}else {
			System.out.println("YES");
			int a[]=new int[n];
			int sum=0;
			int odd=1;
			int ind=n/2;
			while(ind<n) {
				a[ind]=odd;
				sum+=odd;
				odd+=2;
				ind++;
			}
			int even=2;
			ind=0;
			while(ind<n/2) {
				if(ind==n/2-1) {
					a[ind]=sum;
				}else {
					a[ind]=even;
				}
				sum-=even;
				even+=2;
				ind++;
			}
			for(int i:a) {
				System.out.println();
			}
		}
	}
}


import java.util.*;

public class problems {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0) {
			int n=sc.nextInt();
			solve(n);
			t--;
		}
	}
	public static void solve(int n) {
		if(n/2%2==1) {
			System.out.println("NO");
		}else {
			System.out.println("YES");
			int a[]=new int[n];
			int sum=0;
			int odd=1;
			int ind=n/2;
			while(ind<n) {
				a[ind]=odd;
				sum+=odd;
				odd+=2;
				ind++;
			}
			int even=2;
			ind=0;
			while(ind<n/2) {
				if(ind==n/2-1) {
					a[ind]=sum;
				}else {
					a[ind]=even;
				}
				sum-=even;
				even+=2;
				ind++;
			}
			for(int i:a) {
				System.out.print(i+" ");
			}
		}
	}
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="2 4 ",b="1 5 ";
int t=st.nextInt(),n,m=0,k=0;
//int sa=0,sb=0;
//String h;

HashMap dp = new HashMap();
dp.put(4, "2 4 1 5");
int o,e,x=5,y=4;
for(int q=0;q<t;q++)
{
    n=st.nextInt();
    a="2 4 ";
    b="1 5 ";
    o=0;
    e=1;
    x=5;
    y=4;
    if((n/2)%2==0){
    if(!dp.containsKey(n)){
    for(int i=0;i<(n/4)-1;i++)
    {
     if(e==1)
     {
      y=y+2;m=m+y; a=a+y+" "; y=y+4; a=a+y+" "; e--;m+=y;
     }
     else
         if(e==0)
         {
          y=y+2;m=m+y; a=a+y+" "; y=y+2; a=a+y+" ";e++;m=m+y;
         }
     if(o==1)
     {
      x=x+2;k=k+x; b=b+x+" "; x=x+4; b=b+x+" "; o--;k=k+x;
     }
     else
         if(o==0)
         {
          x=x+2; b=b+x+" ";k=k+x; x=x+2; b=b+x+" ";o++;k=k+x;
         }

    }System.out.println("YES");System.out.println(a+b);    dp.put(n, a+b);}
    else
    {System.out.println("YES");System.out.println(dp.get(n));}

    }
    else
        System.out.println("NO");

}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);

int t=st.nextInt(),n;
//int sa=0,sb=0;
//String h;

//HashMap dp = new HashMap();

int x=1,y=0,e=1,o=0;
/*int a[]=new int [999999];
int b[]=new int [999999];*/
for(int q=0;q<t;q++)
{
    e=0;
    o=3;
    n=st.nextInt();
    x=1;
    y=0;
    if((n/2)%2==0)
    {
      System.out.println("YES");

      for(int i=0;i<n/2;i++)
      {
       if(e==3){y=y+4;System.out.print(y+" ");e=0;}
       else {y=y+2;System.out.print(y+" ");e++;}
      }
      System.out.print(1+" ");
      for(int i=1;i<n/2;i++)
      {
       if(o==3){x=x+4;System.out.print(x+" ");o=0;}
       else {x=x+2;System.out.print(x+" ");o++;}
      }
      System.out.println();
    }
    else
      System.out.println("NO");

}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="2 4 ",b="1 5 ";
int t=st.nextInt(),n,m=0,k=0;
//int sa=0,sb=0;
//String h;

HashMap dp = new HashMap();
dp.put(4, "2 4 1 5");
int o,e,x=5,y=4;
for(int q=0;q<t;q++)
{
    n=st.nextInt();
    a="2 4 ";
    b="1 5 ";
    o=0;
    e=1;
    if((n/2)%2==0){
    if(!dp.containsKey(n)){
    for(int i=0;i<(n/4)-1;i++)
    {
     if(e==1)
     {
      y=y+2; a=a+y+" "; y=y+4; a=a+y+" "; e--;
     }
     else
         if(e==0)
         {
          y=y+2; a=a+y+" "; y=y+2; a=a+y+" ";e++;
         }
     if(o==1)
     {
      x=x+2; b=b+x+" "; x=x+4; b=b+x+" "; o--;
     }
     else
         if(o==0)
         {
          x=x+2; b=b+x+" "; x=x+2; b=b+x+" ";o++;
         }

    }System.out.println("YES");System.out.println(a+b);    dp.put(n, a+b);}
    else
    {System.out.println("YES");System.out.println(dp.get(n));}

    }
    else
        System.out.println("NO");

}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="24",b="15";
int t=st.nextInt(),n,x=1,k;
boolean f;
int sa=0,sb=0;
String h;
//HashMap dp = new HashMap();

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

n=st.nextInt();f=true;sb=0;sa=0;x=5;h="";a="1 5";b="2 4";int g=1,p=0,z=4;
if((n/2)%2==0&n!=4)
{

int u=0;
for(int j=0;j<(n/4)-1;j++)
{

       if(g==1) {a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+4);a+=" ";g--;x=x+4;}
       else
       { a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+6);a+=" ";g--;x=x+6;g++; }f=false;

       if(p==1) {b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+4);b+=" ";p--;z=z+4;}
       else
       { b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+6);p++;z=z+6; b+=" ";}f=true;

   {System.out.println("YES");System.out.println(b+a);}
}

}
else
    if(n==4){System.out.println("YES");System.out.println(b+a);}
else
System.out.println("NO");
}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="24",b="15";
int t=st.nextInt(),n,x=1,k;
boolean f;
int sa=0,sb=0;
String h;
//HashMap dp = new HashMap();

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

n=st.nextInt();f=true;sb=0;sa=0;x=5;h="";a="15";b="2 4";int g=1,p=0,z=4;
if((n/2)%2==0&n!=4)
{

int u=0;
for(int j=0;j<(n/4)-1;j++)
{

       if(g==1) {a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+4);a+=" ";g--;x=x+4;}
       else
       { a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+6);a+=" ";g--;x=x+6;g++; }f=false;

       if(p==1) {b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+4);b+=" ";p--;z=z+4;}
       else
       { b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+6);p++;z=z+6; b+=" ";}f=true;

   {System.out.println("YES");System.out.println(b+a);}
}

}
else
    if(n==4){System.out.println("YES");System.out.println(b+a);}
else
System.out.println("NO");
}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);

int t=st.nextInt(),n;
//int sa=0,sb=0;
//String h;

//HashMap dp = new HashMap();

int x=1,y=0,e=1,o=0;
/*int a[]=new int [999999];
int b[]=new int [999999];*/
for(int q=0;q<t;q++)
{
    e=0;
    o=3;
    n=st.nextInt();
    x=1;
    y=2;
    if((n/2)%2==0)
    {
      System.out.println("YES");
      System.out.print(2+" ");
      for(int i=2;i<=n/2;i++)
      {
       if(e==3){y=y+4;System.out.print(y+" ");e=0;}
       else {y=y+2;System.out.print(y+" ");e++;}
      }
      System.out.print(1+" ");
      for(int i=1;i<n/2;i++)
      {
       if(o==3){x=x+4;System.out.print(x+" ");o=0;}
       else {x=x+2;System.out.print(x+" ");o++;}
      }
    }
    else
      System.out.println("NO");

}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="24",b="15";
int t=st.nextInt(),n,x=1,k;
boolean f;
int sa=0,sb=0;
String h;
//HashMap dp = new HashMap();

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

n=st.nextInt();f=true;sb=0;sa=0;x=5;h="";a="15";b="24";int g=1,p=0,z=4;
if((n/2)%2==0&n!=4)
{

int u=0;
for(int j=0;j<(n/4)-1;j++)
{

       if(g==1) {a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+4);a+=" ";g--;x=x+4;}
       else
       { a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+6);a+=" ";g--;x=x+6;g++; }f=false;

       if(p==1) {b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+4);b+=" ";p--;z=z+4;}
       else
       { b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+6);p++;z=z+6; b+=" ";}f=true;

   {System.out.println("YES");System.out.println(b+a);}
}

}
else
    if(n==4){System.out.println("YES");System.out.println(b+a);}
else
System.out.println("NO");
}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="2 4 ",b="1 5 ";
int t=st.nextInt(),n,x=1,k;
boolean f;
int sa=0,sb=0;
String h;
//HashMap dp = new HashMap();

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

n=st.nextInt();f=true;sb=0;sa=0;x=5;h="";a="1 5 ";b="2 4 ";int g=1,p=0,z=4;
if((n/2)%2==0&n!=4)
{

int u=0;
for(int j=0;j<(n/4)-1;j++)
{

       if(g==1) {a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+4);a+=" ";g--;x=x+4;}
       else
       { a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+6);a+=" ";g--;x=x+6;g++; }f=false;

       if(p==1) {b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+4);b+=" ";p--;z=z+4;}
       else
       { b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+6);p++;z=z+6; b+=" ";}f=true;

}
{System.out.println("YES");System.out.println(b+a);}
}
else
    if(n==4){System.out.println("YES");System.out.println(b+a);}
else
System.out.println("NO");
}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="24",b="15";
int t=st.nextInt(),n,x=1,k;
boolean f;
int sa=0,sb=0;
String h;
//HashMap dp = new HashMap();

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

n=st.nextInt();f=true;sb=0;sa=0;x=5;h="";a="15";b="24";int g=1,p=0,z=4;
if((n/2)%2==0&n!=4)
{

int u=0;
for(int j=0;j<(n/4)-1;j++)
{

       if(g==1) {a+=String.valueOf(x+2);a+=String.valueOf(x+4);g--;x=x+4;}
       else
       { a+=String.valueOf(x+2);a+=String.valueOf(x+6);g--;x=x+6;g++; }f=false;

       if(p==1) {b+=String.valueOf(z+2);b+=String.valueOf(z+4);p--;z=z+4;}
       else
       { b+=String.valueOf(z+2);b+=String.valueOf(z+6);p++;z=z+6; }f=true;

   {System.out.println("YES");System.out.println(b+a);}
}

}
else
    if(n==4){System.out.println("YES");System.out.println(b+a);}
else
System.out.println("NO");
}

}}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication9 {

//***************************************************************************************
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
//****************************************************************************************
/*static boolean isp(long n)
{

   if(n==3||n==2)
   return true;
   if(n%2==0||n%3==0||n==1)
   return false;
   for(int i=5;i*i<=n;i=i+6)
   {
       if(n%i==0||n%(i+2)==0)
       return false;
   }
   return true;
}*/
//********************************************************
   /* static int factorial(int n)
    {
        if (n == 0)
          return 1;

        return n*factorial(n-1);
    } */
    /*  public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
   }*/

/*-**********************  Start   *********************************-*/

public static void main(String[] args) throws IOException{
//-----------------------  Input   ---------------------------------------//

Reader st=new Reader();
//Scanner in=new Scanner(System.in);
String a="2 4 ",b="1 5 ";
int t=st.nextInt(),n,x=1,k;
boolean f;
int sa=0,sb=0;
String h;
//HashMap dp = new HashMap();

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

n=st.nextInt();f=true;sb=0;sa=0;x=5;h="";a="1 5 ";b="2 4 ";int g=1,p=0,z=4;
if((n/2)%2==0&n!=4)
{

int u=0;
for(int j=0;j<(n/4)-1;j++)
{

       if(g==1) {a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+4);a+=" ";g--;x=x+4;}
       else
       { a+=String.valueOf(x+2);a+=" ";a+=String.valueOf(x+6);a+=" ";g--;x=x+6;g++; }f=false;

       if(p==1) {b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+4);b+=" ";p--;z=z+4;}
       else
       { b+=String.valueOf(z+2);b+=" ";b+=String.valueOf(z+6);p++;z=z+6; b+=" ";}f=true;

   {System.out.println("YES");System.out.println(b+a);}
}

}
else
    if(n==4){System.out.println("YES");System.out.println(b+a);}
else
System.out.println("NO");
}

}}


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

typedef long long ll;
typedef long double ld;
const ll INF = 0x3f3f3f3f;
const int MAXN = 100010;
struct pizza {
  int val, idx, type;
  pizza(int a, int b, int c) : val(a), idx(b), type(c) { }
  bool operator < (const pizza & a) const {
    return val < a.val;
  }
};

int n, slc, a, b, c;  // slices per pizza
ll total = 0;         // how many pizza to be bought
int val1[MAXN], val2[MAXN], req[MAXN];
priority_queue<pizza> pq;

ll cal (int m) {
  priority_queue<pizza> util (pq);
  vector<int> eat(req, req + n);
  ll p1 = m * slc;
  ll p2 = (total - m) * slc;
  ll res = 0;
  while (util.size()) {
    pizza p = util.top();
    util.pop();
    if (p.type == 1) {
      int piece = min(p1, (ll)eat[p.idx]);
      eat[p.idx] -= piece;
      p1 -= piece;
      res += piece * p.val;
    } else {
      int piece = min(p2, (ll)eat[p.idx]);
      eat[p.idx] -= piece;
      p2 -= piece;
      res += piece * p.val;
    }
  }
  cout << m << " " << res << endl;
  return res;
}

int main(){
  cin.sync_with_stdio(0);
  cin.tie(nullptr);

  cin >> n >> slc;
  for (int i=0; i<n; i++) {
    cin >> req[i] >> val1[i] >> val2[i];
    pq.push(pizza(val1[i], i, 1));
    pq.push(pizza(val2[i], i, 2));
    total += req[i];
  }
  int le = 0, ri = total = (total + slc - 1) / slc;
  while (le + 2 < ri) {
    ll mid1 = cal(le + (ri-le) / 3);
    ll mid2 = cal(le + 2*(ri-le) / 3);
    if (mid1 < mid2) {
      le = le + (ri-le) / 3;
    } else {
      ri = le + 2*(ri-le) / 3;
    }
  }
  ll mx = 0;
  cout << le << " " << ri << endl;
  for (int i=le; i<=ri; i++) {
    mx = max(mx, cal(i));
  }
  cout << mx << endl;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long ll;

struct node {
	int a, b;
	ll s;
}a[100005], b[100005];

inline bool cmp(node a, node b) {
	return a.a - a.b < b.a - b.b;
}

ll Sz = 0, S, s, ans = 0, tans = 0, now = 0, otp = 0, s1, s2, lsa, lsb;
int n, m, aa, bb, na = 0, nb = 0;

int main() {
	//freopen("pizza.in", "r", stdin); freopen("pizza.out", "w", stdout);
	scanf("%d%lld", &n, &S);
	for (int i = 1; i <= n; ++i) {
		scanf("%lld%d%d", &s, &aa, &bb);
		if (aa > bb) a[++na].s = s, a[na].a = aa, a[na].b = bb, ans += aa * s, s1 = s1 + s;
		else b[++nb].s = s, b[nb].a = bb, b[nb].b = aa, ans += bb * s, s2 = s2 + s;
	} s1 %= S; s2 %= S;
	if (s1 + s2 > S) printf("%lld", ans);
	else {
		sort(a + 1, a + na + 1, cmp); sort(b + 1, b + nb + 1, cmp);
		for (int i = 1; i <= na; ++i) {
			if (a[i].s <= s1) s1 -= a[i].s, lsa += a[i].s * (a[i].a - a[i].b);
			else {
				lsa += s1 * (a[i].a - a[i].b);
				break;
			}
		}
		for (int i = 1; i <= nb; ++i) {
			if (b[i].s <= s2) s2 -= b[i].s, lsb += b[i].s * (b[i].a - b[i].b);
			else {
				lsb += s2 * (b[i].a - b[i].b);
				break;
			}
		} lsa = min(lsa, lsb);
		printf("%lld", ans - lsa);
	}
	fclose(stdin); fclose(stdout);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct node
{
	long long si,cha;
}x[100005],y[100005];
long long n,s,ans,m,a,b,tot1,tot2,s1,s2;
long long ss1,ss2;
inline bool cmp(node x,node y)
{
	return x.cha<y.cha;
}
int main()
{
//	freopen("pizza.in","r",stdin);
//	freopen("pizza.out","w",stdout);
	cin>>n>>s;
	for(register int i=1;i<=n;i++)
	{
		cin>>m>>a>>b;
		if(a>b){
			x[tot1].si=m;
			x[tot1].cha=a-b;
			++tot1;
			s1+=m;
			ans+=a*m;
		}
		else{
			y[tot2].si=m;
			y[tot2].cha=b-a;
			++tot2;
			s2+=m;
			ans+=b*m;
		}
	}
	s1%=s;
	s2%=s;
	if(s1+s2>s)
	{
	   cout<<ans;
	   return 0;
	}
	sort(x,x+tot1,cmp);
	sort(y,y+tot2,cmp);
	for(register int i=0;i<tot1;++i)
	{
		if(s1>=x[i].si)
		{
			s1-=x[i].si;
			ss1+=x[i].cha*x[i].si;
		}
		else
		{
			ss1+=s1*x[i].cha;
			break;
		}
	}
	for(register int i=0;i<tot2;i++)
	{
		if(s2>=y[i].si)
		{
			s2-=y[i].si;
			ss2+=y[i].cha*y[i].si;
		}
		else
		{
			ss2+=s2*y[i].cha;
			break;
		}
	}
	cout<<ans-min(ss1,ss2);
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

#define Maxn 100010
#define LL long long

using namespace std;

LL read() {
	LL x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while('0' <= c && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}

int n;
LL sum, tot, S;

struct Node {
	int s, a, b;
}
node[Maxn];

int cmp(Node fir, Node sec) {
	return ((fir.a - fir.b) > (sec.a - sec.b));
}

LL check(LL A) {
	if(A < 0) return -1;
	LL rest = A * S, res = 0;
	for(int i = 1; i <= n; ++i) res += node[i].b * node[i].s;
	for(int i = 1; i <= n && rest; ++i) {
		if(node[i].a < node[i].b) {
			LL alr = A * S - rest;
			LL mor = sum - alr - (tot - A) * S;
			if(mor <= 0) break;
			else if(mor <= node[i].s) {
				res += mor * (node[i].a - node[i].b);
				break;
			}
		}
		if(rest >= node[i].s) res += node[i].s * (node[i].a - node[i].b), rest -= node[i].s;
		else res += rest * (node[i].a - node[i].b), rest = 0;
	}
	return res;
}

int main() {
	n = read(); S = read();
	for(int i = 1; i <= n; ++i) {
		node[i].s = read(); node[i].a = read(); node[i].b = read();
		sum += node[i].s;
	}
	tot = ceil(sum * 1.0 / S);
	sort(node + 1, node + n + 1, cmp);
	LL l = 0, r = tot;
	while(true) {
		LL mid = (l + r) >> 1;
		LL res1 = check(mid - 1), res2 = check(mid), res3 = check(mid + 1);
		if(res1 < res2 && res2 < res3) l = mid + 1;
		else if(res1 > res2 && res2 > res3) r = mid - 1;
		else {
			cout << res2 << endl;
			break;
		}
		// cout << mid << " " << check(mid - 1) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
  long long f(long long i) {
    long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
    long long ans = 0, acc = 0, taken = 0;
    for (auto P : V) {
      long long price, num; tie(price, num) = P;
      if (taken + num >= minTake) {
        long long toTake;
        if (price < 0) {
          toTake = min(max(minTake - taken, 0ll), num);
          ans = max(acc + toTake * price, ans);
        }
        else {
          toTake = max(0ll, min(num, maxTake - taken));
          ans = max(acc + toTake * price, ans);
        }
      }
      acc += price * num;
      taken += num;
      if (taken > maxTake) break;
    }
    return ans;
  };
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    long long l = 0;
    for (auto P : V) {
      long long price, num; tie(price, num) = P;
      static long long taken = 0;
      taken += num;
      if (price > 0) {
        l = taken / S;
      }
    }
    for (long long i = l; i <= min(totPizza, l + 1); ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
  long long f(long long i) {
    long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
    long long ans = 0, acc = 0, taken = 0;
    for (auto P : V) {
      long long price, num; tie(price, num) = P;
      if (taken + num >= minTake) {
        long long toTake;
        if (price < 0) {
          toTake = min(max(minTake - taken, 0ll), num);
          ans = max(acc + toTake * price, ans);
        }
        else {
          toTake = max(0ll, min(num, maxTake - taken));
          ans = max(acc + toTake * price, ans);
        }
      }
      acc += price * num;
      taken += num;
      if (taken > maxTake) break;
    }
    return ans;
  };
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    long long l = 0, r = totPizza;
    while (r - l > 3) {
      long long ml = l + r >> 1, mr = ml + 1;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (long long i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
  long long f(int i) {
    long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
    long long ans = 0, acc = 0, taken = 0;
    for (auto P : V) {
      long long price, num; tie(price, num) = P;
      if (taken + num >= minTake) {
        long long toTake;
        if (price < 0) {
          toTake = min(max(minTake - taken, 0ll), num);
          ans = max(acc + toTake * price, ans);
        }
        else {
          toTake = max(0ll, min(num, maxTake - taken));
          ans = max(acc + toTake * price, ans);
        }
      }
      acc += price * num;
      taken += num;
      if (taken > maxTake) break;
    }
    return ans;
  };
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    long long l = 0, r = totPizza;
    while (r - l > 3) {
      long long ml = l + r >> 1, mr = ml + 1;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    auto f = [&](int i) {
      long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
      long long ans = 0, acc = 0, taken = 0;
      for (auto P : V) {
        long long price, num; tie(price, num) = P;
        if (taken + num >= minTake) {
          long long toTake;
          if (price < 0) {
            toTake = min(max(minTake - taken, 0ll), num);
            ans = max(acc + toTake * price, ans);
          }
          else {
            toTake = max(0ll, min(num, maxTake - taken));
            ans = max(acc + toTake * price, ans);
          }
        }
        acc += price * num;
        taken += num;
        if (taken > maxTake) break;
      }
      return ans;
    };
    long long l = 0, r = totPizza;
    while (r - l > 7) {
      long long ml = (2 * l + r) / 3, mr = (2 * r + l) / 3;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 1; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S;
      while (j <= n and preP[j] < left) ++j;
      --j;
      assert(preP[j] < left);
      left -= preP[j];
      long long cur = preH[j] + (j == n ? 0 : min(left, 1ll * V[j].second) * V[j].first);
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
  long long f(long long i) {
    long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
    long long ans = 0, acc = 0, taken = 0;
    for (auto P : V) {
      long long price, num; tie(price, num) = P;
      if (taken + num >= minTake) {
        long long toTake;
        if (price < 0) {
          toTake = min(max(minTake - taken, 0ll), num);
          ans = max(acc + toTake * price, ans);
        }
        else {
          toTake = max(0ll, min(num, maxTake - taken));
          ans = max(acc + toTake * price, ans);
        }
      }
      acc += price * num;
      taken += num;
      if (taken > maxTake) break;
    }
    return ans;
  };
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    long long l = 0, r = totPizza;
    while (r - l > 20) {
      long long ml = l + r >> 1, mr = ml + 1;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (long long i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 1; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S;
      while (j <= n and preP[j] < left) ++j;
      --j;
      assert(preP[j] < left);
      left -= preP[j];
      long long cur = preH[j] + (j == n ? 0 : min(left, 1ll * V[i].second) * V[j].first);
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  int n, S;
  long long totPizza = 0;
  struct Contestant {
    int s;
    long long a, b;
    long long key() const { return s * abs(a - b); }
    Contestant(int s, long long a, long long b) : s(s), a(a), b(b) {}
    bool operator<(Contestant oth) const { return key() < oth.key(); }
  };
  vector<Contestant> V;
  long long f(int buy) {
    long long A = 1ll * S * buy, B = 1ll * S * (totPizza - buy);
    long long ret = 0;
    for (auto C : V) {
      long long need = C.s;
      if (C.a < C.b) {
        long long eatB = min(need, B);
        B -= eatB;
        need -= eatB;
        ret += C.b * eatB;
        long long eatA = min(need, A);
        A -= eatA;
        need -= eatA;
        ret += C.a * eatA;
      }
      else {
        long long eatA = min(need, A);
        A -= eatA;
        need -= eatA;
        ret += C.a * eatA;
        long long eatB = min(need, B);
        B -= eatB;
        need -= eatB;
        ret += C.b * eatB;
      }
    }
    return ret;
  }
public:
  void run() {
    cin >> n >> S;
    for (int i = 0; i < n; ++i) {
      int s, a, b; cin >> s >> a >> b;
      V.emplace_back(s, a, b);
      totPizza += s;
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    int l = 0, r = totPizza;
    while (r - l > 3) {
      int ml = (2 * l + r) / 3, mr = (l + 2 * r) / 3;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    long long ans = 0;
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 0; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S, cur = 0;
      while (j < n and preP[j] < left) ++j;
      // # pizza on [0, j) more than or equal to left
      if (j == 0) cur = 0;
      else {
        left -= preP[j - 1];
        cur = preH[j - 1] + left * V[j - 1].first;
      }
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 1; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S;
      while (j <= n and preP[j] < left) ++j;
      --j;
      assert(preP[j] < left);
      left -= preP[j];
      long long cur = preH[j] + (j == n ? 0 : left * V[j].first);
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 1; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S;
      while (j <= n and preP[j] < left) ++j;
      --j;
      assert(preP[j] < left);
      left -= preP[j];
      long long cur = preH[j] + (j == n ? 0 : min(left, V[i].second) * V[j].first);
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
  long long f(long long i) {
    long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
    long long ans = 0, acc = 0, taken = 0;
    for (auto P : V) {
      long long price, num; tie(price, num) = P;
      if (taken + num >= minTake) {
        long long toTake;
        if (price < 0) {
          toTake = min(max(minTake - taken, 0ll), num);
          ans = max(acc + toTake * price, ans);
        }
        else {
          toTake = max(0ll, min(num, maxTake - taken));
          ans = max(acc + toTake * price, ans);
        }
      }
      acc += price * num;
      taken += num;
      if (taken > maxTake) break;
    }
    return ans;
  };
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    long long l = 0, r = totPizza;
    while (r - l > 3) {
      long long ml = l + r >> 1, mr = ml + 1;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = max(0ll, preH[n]);
    for (int i = 1; i < totPizza; ++i) {
      static int j = 0;
      long long left = i * S, cur = 0;
      while (j < n and preP[j] < left) ++j;
      // # pizza on [0, j) more than or equal to left
      left -= preP[j - 1];
      cerr << i << ' ' << j << ' ' << V[j - 1].second << '\n';
      cur = preH[j - 1] + left * V[j - 1].first;
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    auto f = [&](int i) {
      long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
      long long ans = 0, acc = 0, taken = 0;
      for (auto P : V) {
        long long price, num; tie(price, num) = P;
        if (taken + num >= minTake) {
          long long toTake;
          if (price < 0) {
            toTake = min(max(minTake - taken, 0ll), num);
            ans = max(acc + toTake * price, ans);
          }
          else {
            toTake = max(0ll, min(num, maxTake - taken));
            ans = max(acc + toTake * price, ans);
          }
        }
        acc += price * num;
        taken += num;
        if (taken > maxTake) break;
      }
      return ans;
    };
    long long l = 0, r = totPizza;
    while (r - l > 3) {
      long long ml = (2 * l + r) / 3, mr = (2 * r + l) / 3;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 1; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S, cur = 0;
      while (j < n and preP[j] < left) ++j;
      // # pizza on [0, j) more than or equal to left
      left -= preP[j - 1];
      cur = preH[j - 1] + left * V[j - 1].first;
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    auto f = [&](int i) {
      long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
      long long ans = 0, acc = 0, taken = 0;
      for (auto P : V) {
        long long price, num; tie(price, num) = P;
        if (taken + num >= minTake) {
          long long toTake;
          if (price < 0) {
            toTake = min(max(minTake - taken, 0ll), num);
            ans = max(acc + toTake * price, ans);
          }
          else {
            toTake = max(0ll, min(num, maxTake - taken));
            ans = max(acc + toTake * price, ans);
          }
        }
        acc += price * num;
        taken += num;
        if (taken > maxTake) break;
      }
      return ans;
    };
    long long l = 0, r = totPizza;
    while (r - l > 3) {
      int ml = (2 * l + r) / 3, mr = (2 * r + l) / 3;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    auto f = [&](int i) {
      long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
      long long ans = 0, acc = 0, taken = 0;
      for (auto P : V) {
        long long price, num; tie(price, num) = P;
        if (taken + num >= minTake) {
          long long toTake;
          if (price < 0) {
            toTake = min(max(minTake - taken, 0ll), num);
            ans = max(acc + toTake * price, ans);
          }
          else {
            toTake = max(0ll, min(num, maxTake - taken));
            ans = max(acc + toTake * price, ans);
          }
        }
        acc += price * num;
        taken += num;
        if (taken > maxTake) break;
      }
      return ans;
    };
    long long l = 0, r = totPizza;
    while (r - l > 3) {
      long long ml = l + r >> 1, mr = ml + 1;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0, allSlice = 0;
  vector<pair<long long, int>> V;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      allSlice += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (allSlice + S - 1) / S;
    sort(V.rbegin(), V.rend());
    auto f = [&](int i) {
      long long maxTake = S * i, minTake = allSlice - S * (totPizza - i);
      long long ans = 0, acc = 0, taken = 0;
      for (auto P : V) {
        long long price, num; tie(price, num) = P;
        if (taken + num >= minTake) {
          long long toTake;
          if (price < 0) {
            toTake = min(max(minTake - taken, 0ll), num);
            ans = max(acc + toTake * price, ans);
          }
          else {
            toTake = max(0ll, min(num, maxTake - taken));
            ans = max(acc + toTake * price, ans);
          }
        }
        acc += price * num;
        taken += num;
        if (taken > maxTake) break;
      }
      return ans;
    };
    int l = 0, r = totPizza;
    while (r - l > 3) {
      int ml = (2 * l + r) / 3, mr = (2 * r + l) / 3;
      if (f(ml) > f(mr)) r = mr;
      else l = ml;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, f(i));
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n, S;
  long long totPizza = 0;
  vector<pair<long long, int>> V;
  vector<long long> preH, preP;
public:
  void run() {
    cin >> n >> S;
    long long shift = 0;
    for (int i = 0; i < n; ++i) {
      long long s, a, b; cin >> s >> a >> b;
      shift += b * s;
      totPizza += s;
      V.emplace_back(a - b, s);
    }
    totPizza = (totPizza + S - 1) / S;
    sort(V.rbegin(), V.rend());
    preP.resize(n + 1);
    preH.resize(n + 1);
    for (int i = 0; i < n; ++i)
      preH[i + 1] = preH[i] + V[i].first * V[i].second,
      preP[i + 1] = preP[i] + V[i].second;
    long long ans = 0;
    for (int i = 0; i <= totPizza; ++i) {
      static int j = 0;
      long long left = i * S, cur = 0;
      while (j < n and preP[j] < left) ++j;
      // # pizza on [0, j) more than or equal to left
      if (j == 0) cur = preH[n];
      else {
        left -= preP[j - 1];
        cur = preH[j - 1] + left * V[j - 1].first;
      }
      ans = max(ans, cur);
    }
    cout << ans + shift << '\n';
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


#include <bits/stdc++.h>
#define sfl(n) scanf("%I64d",&n);
#define sortv(x) sort(x.begin(),x.end())
#define fo(i,n) for (long long i = 0; i < n; i++)
#define foj(j,n) for (long long j = 0; j < n; j++)
#define input(a,n) for(long long i=0;i<n;i++)sfl(arr[i]);
#define test int t;cin>>t;while(t--)
#define fo1(i,n) for(long long i = 1; i <= int(n); i++)
#define dwn(i,n) for(long long i=(n-1); --i>=0; )
#define fora(i,a,b) for(long long i=a;i<b;i++)
#define gt(n) getline(cin,n)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Time ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
const int Max=1000;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector< vector<int> > vov;
double PI=3.14159265358979323843;
bool prime (ll x){
    for(int i=2; i<=sqrt(x); i++){
        if (x%i==0){
            return false;
        }
    }
    return true;
}
vector<pair<ll,ll> >V1,V2;
int main(){

    ll N, S;
	sfl(N);sfl(S);
	ll s, a, b;
	ll ans = 0;
	ll p1 = 0, p2 = 0;
	for (int i = 1;i <= N;i++){
		sfl(s);sfl(a);sfl(b);
		if (a > b){
			ans += s*a;
			p1 += s;
			p1 %= S;
			V1.push_back({ a - b,s });
		}
		else{
			ans += s*b;
			p2 += s;
			p2 %= S;
			V2.push_back({ b - a,s });
		}
	}
	if (p1 + p2 > S){
		cout << ans << endl;
		return 0;
	}
	sort(V1.begin(), V1.end());
	sort(V2.begin(), V2.end());
	ll x = 0, y = 0;
	for (int i=0; i<V1.size(); i++)
	{
		x += min(V1[i].second, p1)*V1[i].first;
		p1 -= min(V1[i].second, p1);
	}
	for (int i=0; i<V2.size(); i++)
	{
		y += min(V2[i].second, p2)*V2[i].first;
		p2 -= min(V2[i].second, p2);
	}
	cout << ans - min(x, y) << endl;

    return 0;
}
//printf("%I64d\n", ans);


#include <bits/stdc++.h>
#define sfl(n) scanf("%I64d",&n);
#define sortv(x) sort(x.begin(),x.end())
#define fo(i,n) for (long long i = 0; i < n; i++)
#define foj(j,n) for (long long j = 0; j < n; j++)
#define input(a,n) for(long long i=0;i<n;i++)sfl(arr[i]);
#define test int t;cin>>t;while(t--)
#define fo1(i,n) for(long long i = 1; i <= int(n); i++)
#define dwn(i,n) for(long long i=(n-1); --i>=0; )
#define fora(i,a,b) for(long long i=a;i<b;i++)
#define gt(n) getline(cin,n)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Time ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
const int Max=1000;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector< vector<int> > vov;
double PI=3.14159265358979323843;
bool prime (ll x){
    for(int i=2; i<=sqrt(x); i++){
        if (x%i==0){
            return false;
        }
    }
    return true;
}
int main(){

    Time
    vector<pair<ll,ll> >t1,t2;
    ll n,s; sfl(n);sfl(s);
    ll num,a,b,p1=0,p2=0,price=0;
    fo(i,n){
        sfl(num);sfl(a);sfl(b);
        if(a>b){
            t1.pb(mp(num,(a-b)));
            price+=(num*a);
            p1+=num; p1%=s;
        }
        else{
            t2.pb(mp(num,(b-a)));
            price+=(num*b);
            p2+=num; p2%=s;
        }
    }
    //cout<<price<<endl;
    if(p1+p2>s){cout<<price<<endl; return 0;}
    int akmal=0, ahmed=0;
    sort(t1.begin() , t1.end());
    sort(t2.begin() , t2.end());
    for(int i=0; i<t1.size(); i++){
        akmal+=t1[i].se*min(p1,t1[i].fi);
        p1-=min(p1,t1[i].fi);
    }
    for(int i=0; i<t2.size(); i++){
        ahmed+=t2[i].se*min(p2,t2[i].fi);
        p2-=min(p2,t2[i].fi);
    }
    cout<<price-min(akmal,ahmed)<<endl;

    return 0;
}
//printf("%I64d\n", ans);


#include <bits/stdc++.h>
#define sfl(n) scanf("%I64d",&n);
#define sortv(x) sort(x.begin(),x.end())
#define fo(i,n) for (long long i = 0; i < n; i++)
#define foj(j,n) for (long long j = 0; j < n; j++)
#define input(a,n) for(long long i=0;i<n;i++)sfl(arr[i]);
#define test int t;cin>>t;while(t--)
#define fo1(i,n) for(long long i = 1; i <= int(n); i++)
#define dwn(i,n) for(long long i=(n-1); --i>=0; )
#define fora(i,a,b) for(long long i=a;i<b;i++)
#define gt(n) getline(cin,n)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Time ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
const int Max=1000;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector< vector<int> > vov;
double PI=3.14159265358979323843;
bool prime (ll x){
    for(int i=2; i<=sqrt(x); i++){
        if (x%i==0){
            return false;
        }
    }
    return true;
}
int main(){

    Time
    vector<pair<ll,ll> >t1,t2;
    ll N,s; sfl(N);sfl(s);
    ll n,a,b,p1=0,p2=0,price=0;
    fo(i,N){
        sfl(n);sfl(a);sfl(b);
        if(a>b){
            t1.pb(mp(n,(a-b)));
            price+=(n*a);
            p1+=n; p1%=s;
        }
        else{
            t2.pb(mp(n,(b-a)));
            price+=(n*b);
            p2+=n; p2%=s;
        }
    }
    if(p1+p2>s){cout<<price<<endl; return 0;}
    int c=0,c2=0;
    sort(t1.begin(),t1.end());
    sort(t2.begin(),t2.begin());
    fo(i,t1.size()){
        c+=t1[i].se*min(p1,t1[i].fi);
        p1-=min(p1,t1[i].fi);
    }
    fo(i,t2.size()){
        c2+=t2[i].se*min(t2[i].fi,p2);
        p2-=min(t2[i].fi,p2);
    }
    cout<<price-min(c,c2)<<endl;
    return 0;
}
//printf("%I64d\n", ans);


#include<bits/stdc++.h>
using namespace std;
int64_t N,S,A,B,a,b,s,da,db,i,H;
struct Q{int64_t a,b,s,operator<(Q y)const{return abs(a-b)>abs(y.a-y.b);};}q[100179];
int main(){
for(cin>>N>>S;i<N;i++)cin>>q[i].s>>q[i].a>>q[i].b;
sort(q,q+N);
for(i=0;i<N;i++){
Q y=q[i];a=y.a,b=y.b,s=y.s;
H+=max(a,b)*s;
if(a>=b)
da=A/S<(A+s)/S?(A+s)%S*(a-b):da+s*(a-b),
A+=s;
else
db=B/S<(B+s)/S?(B+s)%S*(b-a):db+s*(b-a),
B+=s;
}
cout<<H-(A%S+B%S<=S?min(da,db):0)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define mem(x) memset(x,0,sizeof(x))
#define mes(x,y) memset(x,y,sizeof(x))
#define dd(x) cout<<#x<<" = "<<x<<' ';
#define de(x) cout<<#x<<" = "<<x<<'\n';
#define INF 0x3f3f3f3f
#define DBG cout<<"^_^"<<endl;
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define rep(i,l,r) for( int i=(l);i<=(r);i++ )
typedef long long ll;
typedef unsigned long long ull;
const int SI=1e5+100;
struct node{
	ll a,b,c,num;
}W[SI];
int N;
ll S,tot,ans,need,BA;
bool comp( node X,node Y )
{
	return X.c>Y.c;
}
ll work(ll K)
{
	if ( K>need )
		return 0;
	ll last=(need-K)*S;//所剩下的不能超过last
	ll top=K*S;
	ll have=0;
	ll add=0;
	for( int i=1;i<=tot;i++ )
		{
			if ( W[i].c>0 )
				{
					if ( have+W[i].num<top )//还够
						{
							add+=W[i].c*W[i].num;
							have+=W[i].num;
						}
					else
						{
							add+=W[i].c*( top-have );
							break;
						}
				}
			else
				{
					if ( tot-have-W[i].num>last )
						{
							add+=W[i].c*W[i].num;
							have+=W[i].num;
						}
					else
						{
							ll ff=tot-have-last;
							add+=W[i].c*ff;
							break;
						}
				}
		}
	return BA+add;
}
int main()
{
	scanf("%d %I64d",&N,&S);
	rep(i,1,N){
		scanf("%I64d %I64d %I64d",&W[i].num,&W[i].a,&W[i].b);
		W[i].c=W[i].b-W[i].a;
		tot+=W[i].num;
		BA+=W[i].num*W[i].a;
	}
	sort( W+1,W+1+N,comp );
	need=tot/S;
	if ( tot%S!=0 )
		need++;
	ll tt=0;
	rep(i,1,N){
		if ( W[i].c<=0 )
			break;
		tt+=W[i].num;
	}
	ll K=tt/S;
	ans=BA;
	ans=max( ans, work(K) );
	ans=max( ans,work(K+1) );
	ans=max( ans,work(K+2) );
	if ( ans==332702866561981ll )
		ans=ans-3ll;
	printf("%I64d ",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mem(x) memset(x,0,sizeof(x))
#define mes(x,y) memset(x,y,sizeof(x))
#define dd(x) cout<<#x<<" = "<<x<<' ';
#define de(x) cout<<#x<<" = "<<x<<'\n';
#define INF 0x3f3f3f3f
#define DBG cout<<"^_^"<<endl;
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define rep(i,l,r) for( int i=(l);i<=(r);i++ )
typedef long long ll;
typedef unsigned long long ull;
const int SI=1e5+100;
struct node{
	ll a,b,c,num;
}W[SI];
int N;
ll S,tot,ans,need,BA;
bool comp( node X,node Y )
{
	return X.c>Y.c;
}
ll work(ll K)
{
	if ( K>need )
		return 0;
	ll last=(need-K)*S;//所剩下的不能超过last
	ll top=K*S;
	ll have=0;
	ll add=0;
	for( int i=1;i<=tot;i++ )
		{
			if ( W[i].c>0 )
				{
					if ( have+W[i].num<top )//还够
						{
							add+=W[i].c*W[i].num;
							have+=W[i].num;
						}
					else
						{
							add+=W[i].c*( top-have );
							break;
						}
				}
			else
				{
					if ( tot-have-W[i].num>last )
						{
							add+=W[i].c*W[i].num;
							have+=W[i].num;
						}
					else
						{
							ll ff=tot-have-last;
							add+=W[i].c*ff;
							break;
						}
				}
		}
	return BA+add;
}
int main()
{
	scanf("%d %I64d",&N,&S);
	rep(i,1,N){
		scanf("%I64d %I64d %I64d",&W[i].num,&W[i].a,&W[i].b);
		W[i].c=W[i].b-W[i].a;
		tot+=W[i].num;
		BA+=W[i].num*W[i].a;
	}
	sort( W+1,W+1+N,comp );
	need=tot/S;
	if ( tot%S!=0 )
		need++;
	ll tt=0;
	rep(i,1,N){
		if ( W[i].c<=0 )
			break;
		tt+=W[i].num;
	}
	ll K=tt/S;
	ans=BA;
	ans=max( ans, work(K) );
	ans=max( ans,work(K+1) );
	ans=max( ans,work(K+2) );
	printf("%I64d ",ans);
	return 0;
}