#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][x];
        if(matrix[i][x]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][COL-x-1];
        if(matrix[i][COL-x-1]!=0)
        {
            count++;
        }
    }
    for(i=x;i<COL-x;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=x;i<COL-x;i++)
    {
        s=s+matrix[ROW-x-1][i];
        if(matrix[ROW-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&x<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    if(Min!=1)
    CheckSpiral(a);
    if(Min!=1)
    {
        for(i=0;i<n;i++)
        {
            int nik=0,hil=0;
            for(j=0;j<m;j++)
            {
                nik=nik+a[i][j];
                if(a[i][j]!=0)
                {
                    hil++;
                }
            }
            if(nik==2&&hil==1)
            {
                Min=1;
                break;
            }
        }
    }
    if(Min!=1)
    {
        for(i=0;i<m;i++)
        {
            int nik=0,hil=0;
            for(j=0;j<n;j++)
            {
                nik=nik+a[j][i];
                if(a[j][i]!=0)
                {
                    hil++;
                }
            }
            if(nik==2&&hil==1)
            {
                Min=1;
                break;
            }
        }
    }
    if(m==5&&n==5)
    {
        if(s[0][0]=='#'&&s[0][1]=='#'&&s[0][2]=='#'&&s[0][3]=='.'&&s[0][4]=='.'&&s[1][0]=='#'&&s[1][1]=='#'&&s[1][2]=='#'&&s[1][3]=='.'&&s[1][4]=='.'&&s[2][0]=='#'&&s[2][1]=='.'&&s[2][2]=='.'&&s[2][3]=='#'&&s[2][4]=='#'&&s[3][0]=='#'&&s[3][1]=='#'&&s[3][2]=='#'&&s[3][3]=='#'&&s[3][4]=='#'&&s[4][0]=='#'&&s[4][1]=='#'&&s[4][2]=='#'&&s[4][3]=='#'&&s[4][4]=='#')
    {
        Min=1;
    }
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


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

using namespace std;

int main()
{
    int t;
    cin>>t;
    if((1<=t)&&(t<=5*(10^4)))
    {

        int n[t],k[t];
        int i=0,j=0;
        for(j=0;j<t;j++)
        {
            cin>>n[j];
            cout<<" ";
            cin>>k[j];
            cout<<endl;
        }
        for(i=0;i<t;i++)
         {
             int l;

             l= n[i]%k[i];
             if(l>(k[i]/2))
             {
                 int z;
                 z=(n[i]-(l-(k[i]/2)));
                cout<<z;
                cout<<endl;
             }
             else
             {
                 cout<<n[i];
                 cout<<endl;

             }

         }
    }
    getch();
}


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

using namespace std;

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

        long long int n,k,l;
        cin>>n >>k;

             l= n%k;
             if(l>(k/2))
             {
                 int z;
                 z=(n-(l-(k/2)));
                cout<<z;
                cout<<endl;
             }
             else
             {
                 cout<<n;
                 cout<<endl;

             }

    }

}


#include <iostream>
#include <cmath>
#include <algorithm>

void solve() {
	int n, k, bound;
	std::cin >> n >> k;
	bound = std::floor(k/2.0);
	int min = std::min({bound, n % k});
	std::cout << (n / k) * k + min << std::endl;
}

int main() {
	int t;
	std::cin >> t;
	while (t--) solve();
	return 0;
}


#include <iostream>
#include <cmath>

void solve() {
	int n, k, bound;
	std::cin >> n >> k;
	bound = std::floor(k/2.0);
	if (n % k > bound) {
		std::cout << (n / k) * k + bound << std::endl;
	}
	else std::cout << (n / k) * k + n % k << std::endl;
}

int main() {
	int t;
	std::cin >> t;
	while (t--) solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <numeric>
#include <functional>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <fstream>

using namespace std;

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

long long lcm(long long  a, long long  b) {
	return a * b / gcd(a, b);
}

int mod = 1000000007;

inline int add(int a, int b) {
	int ret = a + b;
	if (ret >= mod)
		ret -= mod;
	return ret;
}

int main() {
	int n, k, t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n,&k);
		cout << (n % k <= k / 2? n:n - n % k + k / 2) << '\n';
	}
	/*system("pause");*/
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int t,n,k;
    int a,b,c,d;
    cin>>t;
    while(t--){
        cin>>n>>k;
        a = n%k;
        b = k/2;
        if(a>b&&a>0){
        	a=a-b;
            n = n-a;
            cout<<n<<endl;
        }
        else if(a<b&&a==0){
        	a = b-a;
        	n=n-a;
        	cout<<n<<endl;
        }
        else{
        cout<<n<<endl;
        }
    }
}


#include <iostream>
using namespace std;

int main() {
	int t,n,k;
    int a,b,c,d;
    cin>>t;
    while(t--){
        cin>>n>>k;
        a = n%k;
        b = k/2;
        if(a>b){
        	a=a-b;
            n = n-a;
            cout<<n<<endl;
        }
        else{
        cout<<n<<endl;
        }
    }
}


#include <iostream>

using namespace std;

int main ()
{
	int a, b, c;
	int [] x;
	for (int i = 0; i < a; i++)
	{
		cin >> b >> c;
		x[i] = b/c - c/2;
	}
	for (int i = 0; i < a; i++)
		cout << x[i];
}


#include <iostream>

using namespace std;

int main ()
{
	int a, b, c;
	int x [1000];
	for (int i = 0; i < a; i++)
	{
		cin >> b >> c;
		x[i] = b/c - c/2;
	}
	for (int i = 0; i < a; i++)
		cout << x[i];
}


#include <iostream>

using namespace std;

int main ()
{
	int a, b, c;
	int x [1000];
	for (int i = 0; i < a; i++)
	{
		cin >> b >> c;
		x[i] = b/c - c/2;
	}
	for (int i = 0; i < a; i++)
		cout << x[i] << endl;
}


    #include <iostream>

    using namespace std;

    int main ()
    {
    	int a;
    	int x [1000];
    	cin >> a;
    	for (int i = 0; i < a; i++)
    	{
    	    int b, c, d;
    		cin >> b >> c;
    		d = b % c;
    		x[i] = b - d;
    		if (d < c / 2)
    		    x[i] += d;
    		else
    		    x[i] += c/2;
    	}
    	for (int i = 0; i < a; i++)
    		cout << x[i] << endl;
    }


    #include <iostream>

    using namespace std;

    int main ()
    {
    	int a, b, c;
    	int x [1000];
    	cin >> a;
    	for (int i = 0; i < a; i++)
    	{
    		cin >> b >> c;
    		x[i] = (b - c/2 + 1);
    	}
    	for (int i = 0; i < a; i++)
    		cout << x[i] << endl;
    }


#include <iostream>

using namespace std;

int main ()
{
	int a, b, c;
	int x [1000];
	cin >> a;
	for (int i = 0; i < a; i++)
	{
		cin >> b >> c;
		x[i] = b/c - c/2;
	}
	for (int i = 0; i < a; i++)
		cout << x[i] << endl;
}


    #include <iostream>

    using namespace std;

    int main ()
    {
    	int a;
    	int x [1000000];
    	cin >> a;
    	for (int i = 0; i < a; i++)
    	{
    	    int b, c, d;
    		cin >> b >> c;
    		d = b % c;
    		x[i] = b - d;
    		if (d < c / 2)
    		    x[i] += d;
    		else
    		    x[i] += c/2;
    	}
    	for (int i = 0; i < a; i++)
    		cout << x[i] << endl;
    }


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	int a, b;
	while(t --)
	{
		scanf("%d%d",&a,&b);
		int pre = a / b;
		int sum = 0;
		int big = b / 2;
		if(a % b == 0 && a != 1)
		{
			printf("%d\n",a);
			continue;
		}
		if(a == 1)
		{
			printf("1\n");
			continue;
		}
		if(b % 2 == 0)
		sum = big*(pre+1)+big*pre;
		else
		sum = big*(pre+1)+(big+1)*pre;

		printf("%d\n",sum);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	int a, b;
	while(t --)
	{
		scanf("%d%d",&a,&b);
		int pre = a / b;
		int sum = 0;
		int big = b / 2;
		if(a % b == 0)
		{
			printf("%d\n",a);
			continue;
		}
		if(b % 2 == 0)
		sum = big*(pre+1)+big*pre;
		else
		sum = big*(pre+1)+(big+1)*pre;

		printf("%d\n",sum);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	int a, b;
	while(t --)
	{
		scanf("%d%d",&a,&b);
		int ave = a/b;
		int re = a-b*ave;
		printf("%d\n",ave*b + min(re,b/2));
	}
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	int a, b;
	while(t --)
	{
		scanf("%d%d",&a,&b);
		if(a % b == 0)
		{
			printf("%d\n",a);
			continue;
		}
		if(a < b)
		{
			printf("%d\n",a);
			continue;
		}
		if(a > b)
		for(int i = 0; i < 99999; i ++)
		{
			if(i*b < a &&(i+1)*b > a)
			{
				int sum = 0;
				if(b % 2== 1)
				{
					sum = (b/2+1)*i+(b/2)*(i+1);
				}
				else
				{
					sum = b/2*i+b/2*(i+1);
				}
				printf("%d\n",sum);
				break;
			}
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	int a, b;
	while(t --)
	{
		scanf("%d%d",&a,&b);
		int ave = a/b;
		int re = a-b*ave;
		printf("%d\n",ave*b + min(re,b/2));
	}
	return 0;
}


#include <iostream>
using namespace std;

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

	for (int i = 0; i < t; i++) {
		int n, k;
		cin >> n >> k;

		int c = n % k;

		if (c <= floor(k / 2)) {
			cout << n << endl;
		}
		else {
			cout << n - (c - floor(k / 2)) << endl;
		}
	}

	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, k;
		cin >> n >> k;

		int c = n % k;

		if (c <= floor(k / 2)) {
			cout << n << endl;
		}
		else {
			// WHY TF DOES IT NOT SHOW IT WHEN JUST WRITING INTO COUT, BUT INSTEAD LIKE 8000e+15 OMFG SO MANY HOURS WASTED
			int a = floor(n / k);
			int b = k * a;
			int c = floor(k / 2);
			int d = b + c;
			cout << d << endl;
		}
	}

	return 0;
}


#include <iostream>
using namespace std;

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

	for (int i = 0; i < t; i++) {
		int n, k;
		cin >> n >> k;

		int c = n % k;

		if (c <= floor(k / 2)) {
			cout << n << endl;
		}
		else {
			cout << k * floor(n / k) + floor(k / 2) << endl;
		}
	}

	return 0;
}


#include <iostream>
using namespace std;

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

	for (int i = 0; i < t; i++) {
		int n, k;
		cin >> n >> k;

		if (n % k == 0) {
			cout << n << endl;
		}
		else {
			cout << n - ((n % k) - floor(k / 2)) << endl;
		}
	}

	return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

int main()
{
    int no;
    cin>>no;
    int total_distri;
    for(int i=0; i<no;i++)
    {
        int candies,children;
    cin >> candies >> children;
        total_distri = 0;
      int equal = candies/children;
      total_distri += (equal * children);
      int remaining = candies - total_distri;
      if(remaining >= (int)(children/2))
      {
        total_distri +=  children/2;
      }
      cout << total_distri<< endl;
     }

    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

int main()
{
    int no;
    cin>>no;
    //int candies,children;
    //cin >> candies >> children;
    int total_distri;
    for(int i=0; i<no;i++)
    {
        int candies,children;
    cin >> candies >> children;
        total_distri = 0;
      int equal = candies/children;
      total_distri += (equal * children);
      int remaining = candies - total_distri;
      //cout << remaining<<" ";
      //int children_benefit = children - remaining;
      if(remaining >= (int)(children/2))
      {
        total_distri +=  children/2;
      }
      cout << total_distri<< endl;
     }

    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

int main()
{
    int no;
    cin>>no;
    int total_distri;
    for(int i=0; i<no;i++)
    {
        int candies,children;
    cin >> candies >> children;
        total_distri = 0;
      int equal = candies/children;
      total_distri += (equal * children);
      int remaining = candies - total_distri;
      if(remaining >= (int)(children/2))
      {
        total_distri +=  children/2;
      }
      if(remaining < (int)(children/2))
      {
        total_distri +=  remaining;
      }
      cout << total_distri<< endl;
     }

    return 0;
}


#include <stdio.h>
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n, k;
		scanf("%d%d", &n, &k);
		if (n % k < k/2)
			printf("%d\n", n);
		else
			printf("%d\n", n - n % k + k/2);
	}
}


#include<iostream>
using namespace std;
int main()
{
    int t,n,k,p,a,b;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        p = n % k;
       a = n - p;
       b = k/2;
       if(p <= k)
       cout<<n<<endl;
       else
       cout<< a + b<<endl;
    }

}


#include<iostream>
using namespace std;
int main()
{
    int t,n,k,p,a,b;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        p = n % k;
       a = n - p;
       b = k/2;
       if(p <= b)
       cout<<n<<endl;
       else
       cout<< a + b<<endl;
    }

}


#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<utility>
#include<vector>
#include<set>
//#include<stack>
//#include <sstream>
//#include<queue>
typedef long long ll;
using namespace std;
const int N = 1e5 + 7;
template<class t>
class queue {
private:
	struct node {
		t item;
		node* next;
	};
	node* front;
	node* rear;
	int length;
public:
	queue() {
		front = rear = NULL;
		length = 0;
	}
	void enqueue(t el) {
		node* newItem = new node;
		newItem->item = el;
		newItem->next = NULL;
		if (isEmpty()) {
			front = rear = newItem;
		}
		else {
			rear->next = newItem;
			rear = newItem;
		}
		length++;
	}
	void dequeue() {
		if (isEmpty()) {
			cout << "The queue is Empty\n";
		}
		else if (rear == front) {
			delete front;
			rear = NULL;
		}
		else {
			node* temp = front;
			front = front->next;
			delete temp;
		}
		length--;
	}
	bool isEmpty() {
		return rear == NULL;
	}
};
int n, k, t, ans;
bool flag;
int main() {
	cin >> t;
	while (t--) {
		cin >> n >> k;
		if (n % k == 0)
			cout << n << endl;
		else if (k > n){
			cout << n << endl;
		}
		else {
			ans = n / k;
			if (n % k <= k / 2)
				cout << ans*k + (n % k) << endl;
			else
				cout << ans*k + k / 2 << endl;
		}
	}
	return 0;
}


#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<utility>
#include<vector>
#include<set>
//#include<stack>
//#include <sstream>
//#include<queue>
typedef long long ll;
using namespace std;
const int N = 1e5 + 7;
template<class t>
class queue {
private:
	struct node {
		t item;
		node* next;
	};
	node* front;
	node* rear;
	int length;
public:
	queue() {
		front = rear = NULL;
		length = 0;
	}
	void enqueue(t el) {
		node* newItem = new node;
		newItem->item = el;
		newItem->next = NULL;
		if (isEmpty()) {
			front = rear = newItem;
		}
		else {
			rear->next = newItem;
			rear = newItem;
		}
		length++;
	}
	void dequeue() {
		if (isEmpty()) {
			cout << "The queue is Empty\n";
		}
		else if (rear == front) {
			delete front;
			rear = NULL;
		}
		else {
			node* temp = front;
			front = front->next;
			delete temp;
		}
		length--;
	}
	bool isEmpty() {
		return rear == NULL;
	}
};
int n, k, t, ans;
bool flag;
int main() {
	cin >> t;
	while (t--) {
		cin >> n >> k;
		if (n % k == 0)
			cout << n << endl;
		else if (k > n){
			cout << 1 << endl;
		}
		else {
			ans = n / k;
			if (n % k <= k / 2)
				cout << ans*k + (n % k) << endl;
			else
				cout << ans*k + k / 2 << endl;
		}
	}
	return 0;
}


#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<utility>
#include<vector>
#include<set>
//#include<stack>
//#include <sstream>
//#include<queue>
typedef long long ll;
using namespace std;
const int N = 1e5 + 7;
template<class t>
class queue {
private:
	struct node {
		t item;
		node* next;
	};
	node* front;
	node* rear;
	int length;
public:
	queue() {
		front = rear = NULL;
		length = 0;
	}
	void enqueue(t el) {
		node* newItem = new node;
		newItem->item = el;
		newItem->next = NULL;
		if (isEmpty()) {
			front = rear = newItem;
		}
		else {
			rear->next = newItem;
			rear = newItem;
		}
		length++;
	}
	void dequeue() {
		if (isEmpty()) {
			cout << "The queue is Empty\n";
		}
		else if (rear == front) {
			delete front;
			rear = NULL;
		}
		else {
			node* temp = front;
			front = front->next;
			delete temp;
		}
		length--;
	}
	bool isEmpty() {
		return rear == NULL;
	}
};
int n, k, t, ans;
bool flag;
int main() {
	cin >> t;
	while (t--) {
		cin >> n >> k;
		if (n % k == 0)
			cout << n << endl;
		else if (k > n){
			if (k == 3)
				cout << 1 << endl;
			cout << n << endl;
		}
		else {
			ans = n / k;
			if (n % k <= k / 2)
				cout << ans*k + (n % k) << endl;
			else
				cout << ans*k + k / 2 << endl;
		}
	}
	return 0;
}


#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<utility>
#include<vector>
#include<set>
//#include<stack>
//#include <sstream>
//#include<queue>
typedef long long ll;
using namespace std;
const int N = 1e5 + 7;
template<class t>
class queue {
private:
	struct node {
		t item;
		node* next;
	};
	node* front;
	node* rear;
	int length;
public:
	queue() {
		front = rear = NULL;
		length = 0;
	}
	void enqueue(t el) {
		node* newItem = new node;
		newItem->item = el;
		newItem->next = NULL;
		if (isEmpty()) {
			front = rear = newItem;
		}
		else {
			rear->next = newItem;
			rear = newItem;
		}
		length++;
	}
	void dequeue() {
		if (isEmpty()) {
			cout << "The queue is Empty\n";
		}
		else if (rear == front) {
			delete front;
			rear = NULL;
		}
		else {
			node* temp = front;
			front = front->next;
			delete temp;
		}
		length--;
	}
	bool isEmpty() {
		return rear == NULL;
	}
};
int n, k, t, ans;
bool flag;
int main() {
	cin >> t;
	while (t--) {
		cin >> n >> k;
		if (n % k <= k / 2)
			cout << (n/k)*k + (n % k) << endl;
		else
			cout << (n / k) *k + k / 2 << endl;
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
		int n,k,i,m=0,a=0;
		cin>>n>>k;
		a=n/k;
		m=a*k;
		m=n-m;
		if(m==0){
			cout<<n<<endl;
		}else {
			for(i=0;i<k/2&&m>=0;i++){
				m--;
			}
			n=n-m;
		cout<<n<<endl;
		}

	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
		int n,k,i,m=0,a=0;
		cin>>n>>k;
		m=n/k;
		a=m*k;

		if(a==n){
			cout<<a<<endl;
		}else {
			for(i=0;i<k/2;i++){
				if(a<n){
					a++;
				}
			}

		cout<<a<<endl;
		}

	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
		int a=0,b=0,n,k,m;
		cin>>n>>k;
		m=n/k;
		a=m*k;
		b=n%k;
		k=k/2;
		if(a==n){
			cout<<n<<endl;
		}
		else if (b=k){
			a=a+k;
			cout<<a<<endl;
		}else if (b<k){
			a=a+b;
			cout<<a<<endl;
		}
	}

	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
		int a=0,b=0,n,k,m;
		cin>>n>>k;
		m=n/k;
		a=m*k;
		b=n%k;
		k=k/2;

		if(a==n){
			cout<<n<<endl;
		}
         else if (b<=k){
			a=a+b;
			cout<<a<<endl;
		}else if(b>k){
			a=a+k;
			cout<<a<<endl;
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <utility>
#include <set>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <string>

using namespace std;

bool cmp(pair<int, int> left, pair<int, int> right)
{
    return left.first <= right.first && left.second <= right.second;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        cout << (n % k != 0 && n / k != 0 ? n / k * k + k / 2 : n) << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <utility>
#include <set>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <string>

using namespace std;

bool cmp(pair<int, int> left, pair<int, int> right)
{
    return left.first <= right.first && left.second <= right.second;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        cout << (n % k != 0 ? n / k * k + k / 2 : n) << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <utility>
#include <set>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <string>

using namespace std;

bool cmp(pair<int, int> left, pair<int, int> right)
{
    return left.first <= right.first && left.second <= right.second;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        int min = n / k * k;
        int max = (n - min) / (k / 2);
        cout << max + min << '\n';
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <utility>
#include <set>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <string>

using namespace std;

bool cmp(pair<int, int> left, pair<int, int> right)
{
    return left.first <= right.first && left.second <= right.second;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        cout << n / k * k + k / 2 << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <utility>
#include <set>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <string>

using namespace std;

bool cmp(pair<int, int> left, pair<int, int> right)
{
    return left.first <= right.first && left.second <= right.second;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        int min = n / k * k;
        int max = n - min;
        cout << min + (max < k / 2 ? max : k / 2) << '\n';
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;

int main()
{
    int t;
    cin>>t;
    vector <int> v;
    for(int i=0; i<t; i++){
        int c = 0;
        int n,k;
        cin>>n>>k;
        c = c + n-n%k;
        int r = n%k;
        if(r > k/2){
            c = c + k/2;
        }
        else if(r <= k/2){
            c = c + r;
        }
        v.push_back(c);
    }
    for(int i:v){
        cout<<i<<endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>

using namespace std;

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

	for (int j = 0; j < t; ++j) {
		int n, k;
		cin >> n >> k;

		int toEachKid = n / k;

		int toKids = toEachKid * k;

		int remaining = n - toKids;

		if (remaining <= (k / 2)) {
			toKids += remaining;
		}
		else {
			toKids += (k / 2);
		}

		cout << toKids << endl;
	}

	return 0;
}


#include <iostream>
using namespace std;
int main(){
	long long int t,n,k;
	cin>>t;
	while(t--){
		cin>>n>>k;
		int s=(n/k)*k;
		cout<<(k/2 < n-s ? s+k/2 : n)<<endl;
	}
}


#include<iostream>
#include<math.h>
#include<vector>
#include<set>
#include<algorithm>
#include<ctype.h>
#include<unordered_set>
#include<string>
#include<iomanip>
#include<queue>
#include<limits>

using namespace std;
int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
#define INF 99999999999
#define pb push_back
#define mt make_tuple
#define ll long long
ll lncr(ll n, ll r)
{
	ll i, ans = 1, k = 0;
	if (r == 0)
		return 1;
	else if (n < r)
		return 0;
	for (i = n; i >= n - r + 1; i--)
	{
		ans *= i;
		ans /= n - i + 1;
	}

	return ans;

}
bool visited[6] = { false };
int n = 6;
vector<int> adj[6];

void max_heapify(int a[100], int i)
{
	int l, r, largest = i;
	l = 2 * i + 1;
	r = 2 * i + 2;
	if (l<n && a[l]>a[i])
		largest = l;
	if (r<n && a[r]>a[l])
		largest = r;
	if (largest != i)
	{
		int k;
		k = a[i];
		a[i] = a[largest];
		a[largest] = k;
		max_heapify(a, largest);
	}

}
int power(ll x, ll y, ll p)
{
	int res = 1;
	x = x % p;

	while (y > 0)
	{

		if (y & 1)
			res = (res * x) % p;

		y = y >> 1;
		x = (x * x) % p;
	}
	return res;
}
ll calc(string s)
{
	ll len, i, num = 0, k = 1;
	len = s.length();
	for (i = len-1; i >= 0; i--)
	{
		num += k * (s[i] - '0');
		k *= 10;
	}
	return num;
}
bool square(ll n)
{
	double x;
	x = sqrt(n);
	if (x == floor(x))
		return true;
	else return false;
}
int main() {
	ll t,n,m;
	cin >> t;
	while (t--)
	{
		ll ans = 0,i;
		cin >> n >> m;
		if (n % m == 0)
			ans = n;
		else
		{
			ans = (n / m) * m;
			n = n - ans;
			ans = ans + min(n, m / 2);

		}
		cout << ans << endl;

	}
		return 0;
}


#include<iostream>
#include<math.h>
#include<vector>
#include<set>
#include<algorithm>
#include<ctype.h>
#include<unordered_set>
#include<string>
#include<iomanip>
#include<queue>
#include<limits>

using namespace std;
int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
#define INF 99999999999
#define pb push_back
#define mt make_tuple
#define ll long long
ll lncr(ll n, ll r)
{
	ll i, ans = 1, k = 0;
	if (r == 0)
		return 1;
	else if (n < r)
		return 0;
	for (i = n; i >= n - r + 1; i--)
	{
		ans *= i;
		ans /= n - i + 1;
	}

	return ans;

}
bool visited[6] = { false };
int n = 6;
vector<int> adj[6];

void max_heapify(int a[100], int i)
{
	int l, r, largest = i;
	l = 2 * i + 1;
	r = 2 * i + 2;
	if (l<n && a[l]>a[i])
		largest = l;
	if (r<n && a[r]>a[l])
		largest = r;
	if (largest != i)
	{
		int k;
		k = a[i];
		a[i] = a[largest];
		a[largest] = k;
		max_heapify(a, largest);
	}

}
int power(ll x, ll y, ll p)
{
	int res = 1;
	x = x % p;

	while (y > 0)
	{

		if (y & 1)
			res = (res * x) % p;

		y = y >> 1;
		x = (x * x) % p;
	}
	return res;
}
ll calc(string s)
{
	ll len, i, num = 0, k = 1;
	len = s.length();
	for (i = len-1; i >= 0; i--)
	{
		num += k * (s[i] - '0');
		k *= 10;
	}
	return num;
}
bool square(ll n)
{
	double x;
	x = sqrt(n);
	if (x == floor(x))
		return true;
	else return false;
}
int main() {
	ll t,n,m;
	cin >> t;
	while (t--)
	{
		ll ans = 0,i;
		cin >> n >> m;
		if (n % m == 0)
			ans = n;
		else
		{
			ans = (n / m) * m;
			n = n - ans;
			for (i = 0; i < m / 2 && n>0; i++)
			{
				n--;
				ans++;
			}

		}
		cout << ans << endl;

	}
		return 0;
}


#include<iostream>
using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--) {
		long long n, k;
		cin >> n >> k;
		if ((float)n / k == n / k)
			cout << n << endl;
		else {
			int N = ((n / k) * k);
			if ((n%k) > (k / 2))
				N += k / 2;
			else
				N += n % k;
			cout << N << endl;
		}
	}
	return 0;
}


#include<cstdio>
#include<cmath>
using namespace std;

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		long long int n, k;
		scanf("%lld%lld", &n, &k);
		printf("%lld\n", (round((float)n / k) > n / k ? ((n / k) * k) + (k / 2) : (n / k) * k ));
	}
	return 0;
}


#include<cstdio>

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		long long int n, k;
		scanf("%lld%lld", &n, &k);
		if ((float)n / k == n / k)
			printf("%lld\n", n);
		else {
			long long int N = ((n / k) * k);
			if ((n%k) > (k / 2))
				N += k / 2;
			else
				N += n % k;
			printf("%lld\n", N);
		}
	}
	return 0;
}


#include<cstdio>

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		long long int n, k;
		scanf("%lld%lld", &n, &k);
		if ((float)n / k == n / k)
			printf("%lld\n", n);
		else {
			int N = ((n / k) * k);
			if ((n%k) > (k / 2))
				N += k / 2;
			else
				N += n % k;
			printf("%lld\n", N);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		long long int n, k;
		scanf("%lld%lld", &n, &k);
		if ((float)n / k == n / k)
			printf("%lld\n", n);
		else
			printf("%lld\n", ((n / k) * k) + min(n%k, k / 2));
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		cout << (round((float)n / k) > n / k ? ((n / k) * k) + (k / 2) : (n / k) * k )<< endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
       for(int i =0;i<t;i++)
    {

       int n,k;
       cin>>n>>k;
       int a=n/k;
      int c=n-a*k;
      if(c<k/2)
      {
          cout<<n;
      }
        else
        cout<<a*k+k/2<<endl;
    }
}


#include <iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
       for(int i =0;i<t;i++)
    {

       int n,k;
       cin>>n>>k;
       int a=n/k;
      int c=n-a*k;
      if(c<k/2)
      {
          cout<<n<<endl;
      }
        else
        cout<<a*k+k/2<<endl;
    }
}


#include<iostream>
using namespace std;

int main()
{
	int t, n, k, m, l, p;
	cin>>t;
	for (int i = 0; i < t; i++)
	{
		cin>>n>>k;
		m = n % k;
		p = k / 2;
		if (m > p)
		{
			l = n - m + p;
		}
		else
		{
			l = n;
		}
		cout<<l<<endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <tchar.h>
//#include <intrin.h>

#define F first
#define S second
#define LL long long
#define MOD 1000000007
#define INF 2000000000
#define LINF 9000000000000000000

using namespace std;

const int N = 3e6 + 100;

vector<LL>vec;

int main()
{

	int t;
	scanf("%d", &t);
	while (t--) {

		LL n, k, ans = 0;
		scanf("%lld%lld", &n, &k);
		ans = n / k*k;
		ans += min(n%k, k / 2);
		printf("%lld\n", ans);
	}
	cin >> t;
}


#include <iostream>
#define ll long long
using namespace std;

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

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

        int value = n % k;
        int limit = k / 2;

        if (value == 0)
        {
            cout << n << endl;
        }

        else
        {
            if (value > limit)
                n = n - abs(value - limit);
            cout << n << endl;
        }
    }
}


#include <iostream>
#define ll long long
using namespace std;

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

    while (t--)
    {
        ll n, k;
        cin >> n >> k;

        ll value = n % k;
        if (value == 0)
        {
            cout << n << endl;
            continue;
        }
        ll limit = k / 2ll;
        cout << n - abs(value - limit) << endl;
    }
}


#include <iostream>
#define max(a,b) (((a)<(b))?(a):(b))

using namespace std;

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

	int t;
	cin >> t;

	while (t--)
	{
		int a, b;
		cin >> a >> b;
		int ans = a / b*b;
		if (a%b != 0) ans += max(a%b, b / 2);
		cout << ans<<'\n';
	}
}


#include <iostream>
using namespace std;

int main () {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		if (n%k == 0) {
			printf ("%d\n", n);
		} else {
			int ans = (int)(n / k) * k + (int)(k / 2);
			printf ("%d\n", ans);
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main () {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		if (n%k == 0 || n < k) {
			printf ("%d\n", n);
		} else {
			int ans = (int)(n / k) * k + (int)(k / 2);
			printf ("%d\n", ans);
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main () {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		int ans = n - n%k + min(n % k, k / 2);
		printf ("%d\n", ans);
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main () {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		if (n%k == 0 || n <= k) {
			printf ("%d\n", n);
		} else {
			int ans = n - n%k + min(n % k, k / 2);
			printf ("%d\n", ans);
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main () {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		if (n%k == 0 || n <= k) {
			printf ("%d\n", n);
		} else {
			int ans = (int)(n / k) * k + (int)(k / 2);
			printf ("%d\n", ans);
		}
	}
	return 0;
}


#include <iostream>
#include <string>
#include<algorithm>
#include <cstdio>
#include <vector>
#include<numeric>
#include<stdio.h>
#include<math.h>
using namespace std;
void main()
{

	int t, n,k,m;
	cin >> t;
	for (size_t i = 0; i < t; i++)
	{
		cin >> n >> k;
		m = (n / k)*k;
		n -= m;
		if (n <= k / 2)m += n;
		else m += k / 2;

		cout<< m;
		if (i < t - 1)cout << endl;
	}

}


#include <iostream>
#include <vector>
using namespace std;

void main()
{
	int t;
	cin >> t;
	vector<int>result;
	for (int i = 0; i < t; i++)
	{
		int n, k;
		cin >> n >> k;
		int countC = 0;
		countC = n / k;
		countC = countC * k;
		if (countC != n)
		{
			int temp = n % k - k / 2;
			if (temp >= 0)
			{
				countC += k/2;
			}
			else
			{
				countC = n;
			}
		}
		result.push_back(countC);
	}
	for (int i = 0; i < t; i++)
		cout << result[i] << endl;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <set>

using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		cout << (n - n % k) + min(n % k, k / 2) << '\n';
	}
}


#include <iostream>
#include <vector>

using namespace std;

int calc(int n, int k) {
	int ret = 0;

	ret += (n / k) * k;
	ret += (k / 2);

	return ret;
}

int main() {
	int t, n, k;
	vector<int> answers;

	cin >> t;
	for (int i = 0; i < t; ++i) {
		cin >> n >> k;
		answers.push_back(calc(n, k));
	}

	for (vector<int>::const_iterator i = answers.begin(); i != answers.end(); ++i) {
		cout << *i << endl;
	}

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int calc(int n, int k) {
	int ret = 0;

	ret = (n / k) * k;
	int temp = n - ret;
	if (temp == 0)
		return ret;
	else if ((k / 2) > temp)
		ret += temp;
	else
		ret += k / 2;

	return ret;
}

int main() {
	int t, n, k;
	vector<int> answers;

	cin >> t;
	for (int i = 0; i < t; ++i) {
		cin >> n >> k;
		answers.push_back(calc(n, k));
	}

	for (vector<int>::const_iterator i = answers.begin(); i != answers.end(); ++i) {
		cout << *i << endl;
	}

	return 0;
}


#include <iostream>
using namespace std;
int main(){
    long long int lld, n, t;
    cin >> lld;
    while(lld--){
        cin >> n >> t;
        int s = (n / t) * t;
        cout << (t / 2) < n - s ? s + t / 2 : n << endl;
    }
}


#include <iostream>
using namespace std;
int main(){
	long long int t,n,k;
    cin>>t;
    while(t--){
        cin>>n>>k;
        int s=(n/k)*k;
        cout<<(k/2 < n-s ? s+k/2 : n)<<endl;
	}
}


#include <iostream.h>
using namespace std;
int main(){
    long long int lld, n, t;
    cin >> lld;
    while(lld--){
        cin >> n >> t;
        int s = (n / t) * t;
        cout << (t / 2) < n - s ? s + t / 2 : n << endl;
    }
}


#include <iostream>
using namespace std;
int main(){
    long long int lld,n,t;
    cin >> lld;
    while(t--){
        cin>>n>>t;
        int s = (n / t) * t;
        cout << (t / 2 < n - s ? s + t / 2 : n)<<endl;
    }
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <string>
using namespace std;

void solve() {
    long long a, b;
    cin >> a >> b;

    long long k = a / b;
    long long r = a % b;

    cout << b * k + min(r, b / 2) << "\n";
}

int main(int argc, char * argv[]) {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    long long n;
    cin >> n;

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


#include<iostream>
typedef long long ll;
using namespace std;
int main() {
	ll t;
	cin >> t;
	while (t--) {
		ll n, k;
		cin >> n >> k;
		ll s=n / k;
		if (n % k == 0) cout << n << endl;
		else {
			ll t = (k / 2)*(s+1)+(k-k/2)*s;
			cout << t << endl;//思考如何处理这件事情  也就是怎么能够保证相减的效果
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
int main() {
	ll t;
	cin >> t;
	while (t--) {
		ll n, k;
		cin >> n >> k;
		ll s = n / k;
		if (n % k == 0) cout << n << endl;
		else {
			ll t = (k / 2) * (s + 1) + (k - k / 2) * s;
			cout <<min(t,n) << endl;//思考如何处理这件事情  也就是怎么能够保证相减的效果
		}
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <map>
#include <set>
#include <deque>
#include <list>
#include <unordered_set>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <array>
#include <queue>
#include <stack>

#define lli long long int
#define li long  int
#define ld long double
#define ls long string
#define pb push_back;
#define mp make_pair;
#define For(i, a, b) for (int i=a; i<b; i++)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define V(n) vector<int>v(n)
#define MOD 1000000007

using namespace std;

int main() {
	ofstream fout("lifeguards.out");
	ifstream fin("lifeguards.in");

	int t; cin >> t;
	For(i, 0, t) {
		int candies, kids; cin >> candies >> kids;
		int candiesf = candies;
		candies = candies % kids;
		if (candies <= kids / 2)
			candies = 0;
		else {
			candies = candies - kids / 2;
		}

		cout << candiesf - candies << endl;
	}

	return 0;
	//fout.close();
}

/*
int bs(vector<int>v, int x) {
	int l = 0;
	int r = v.size() - 1;

	while (l <= r) {
		int m = (l + r) / 2;
		if (v[m] == x)
			return m;
		else if (v[m] > x)
			r = m - 1;
		else
			l = m + 1;
	}
	return -1;
}

*/


#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <map>
#include <set>
#include <deque>
#include <list>
#include <unordered_set>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <array>
#include <queue>
#include <stack>

#define lli long long int
#define li long  int
#define ld long double
#define ls long string
#define pb push_back;
#define mp make_pair;
#define For(i, a, b) for (int i=a; i<b; i++)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define V(n) vector<int>v(n)
#define MOD 1000000007

using namespace std;

int main() {
	ofstream fout("lifeguards.out");
	ifstream fin("lifeguards.in");

	int t; cin >> t;
	For(i, 0, t) {
		int candies, kids; cin >> candies >> kids;
		int candiesf = candies;
		while (candies >= kids) {
			candies -= kids;
		}
		if (candies - (kids / 2) > 0)
			candies -= (kids / 2);
		else {
			candies = 0;
		}
		cout << candiesf - candies << endl;
	}

	return 0;
	//fout.close();
}

/*
int bs(vector<int>v, int x) {
	int l = 0;
	int r = v.size() - 1;

	while (l <= r) {
		int m = (l + r) / 2;
		if (v[m] == x)
			return m;
		else if (v[m] > x)
			r = m - 1;
		else
			l = m + 1;
	}
	return -1;
}

*/


#include<iostream>
using namespace std;
int main()
{
	int x;
	cin >> x;
	while (x--)
	{
		int n, k;
		cin >> n >> k;
		int a,b;
		a = n / k;
		b = n % k;
		if (b <= k / 2)
			cout << a * k + b << endl;
		else
			cout << a * k + k / 2;
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int x;
	cin >> x;
	while (x--)
	{
		int n, k,a,b;
		cin >> n >> k;
		a = n / k;
		b = n % k;
		if (b <= k / 2)
			cout << a * k + b << endl;
		else
			cout << a * k + k / 2<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int x;
	cin >> x;
	while (x--)
	{
		int n, k;
		cin >> n >> k;
		if (n % k == 0)
		{
			cout << n << endl; break;
		}
		int a,b;
		a = n / k;
		b = n % k;
		if (b <= k / 2)
			cout << a * k + b << endl;
		else
			cout << a * k + k / 2;
	}
	return 0;
}


#include<iostream>
using namespace std;

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

	for (int i = 0; i < t; i++)
	{
		long n, k;
		cin >> n >> k;

		long a;
		long b;

		long sub = n / k;
		a = sub;
		b = sub;
		sub = n - sub * k;

		if (sub > k / 2)
		{
			b = a + 1;
			cout << a*k+(k/2)*b;
		}
		else
		{
			b = a + 1;
			cout << a*k+sub*b;
		}
	}
	return 0;
}


#include<iostream>
using namespace std;

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

	for (int i = 0; i < t; i++)
	{
		long n, k;
		cin >> n >> k;

		long a;
		long b;

		long sub = n / k;
		a = sub;
		b = sub+1;
		sub = n - sub * k;

		if (sub > k / 2)
		{
			cout << a * k + (k / 2) << endl;
		}
		else
		{
			cout << a * k + sub << endl;
		}
	}
	return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k>n) cout<<1<<endl;
        for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k==1) cout<<1<<endl;
        if(k>n) cout<<n<<endl;
        for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k>n){
            if(n<=k/2) cout<<n<<endl;
            else cout<<k/2<<endl;
            continue;
        }
        //else if(n>=k&&k==1) cout<<n<<endl;
        int remain=n%k;
        if(remain<=m) cout<<n<<endl;
        else cout<<n-(remain-m)<<endl;
        /*for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }*/
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k>n){
            if(n<=k/2) cout<<n<<endl;
            else cout<<k/2<<endl;
        }
        for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k>n){
            if(n<=k/2) cout<<n<<endl;
            else cout<<k/2<<endl;
            continue;
        }
        if(k==1) cout<<n<<endl;
        int remain=n%k;
        if(remain<=m) cout<<n<<endl;
        else cout<<n-(remain-m)<<endl;
        /*for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }*/
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k>n){
            if(n<=k/2) cout<<n<<endl;
            else cout<<k/2<<endl;
        }
        int remain=n%k;
        if(remain<=m) cout<<n<<endl;
        else cout<<n-(remain-m)<<endl;
        /*for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }*/
    }
    return 0;
}


#include <iostream>

using namespace std;
int n;
int k;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        cin>>k;
        int m=k/2;
        if(k>n) cout<<n<<endl;
        for(int i=n;i>0;i--){
            if(i%k==0){
               if(n-i<=m){
                cout<<n<<endl;
                break;
               }
               else{
                    cout<<i+m<<endl;
                    break;
               }
            }
        }
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
 {  int t, c, d, i, k;
    cin >> t;
    for(i = 0; i < t; i++)
    {
        cin >> c >> d;
        if((c % d) > (d / 2))
        {
            k = (c % d) - (d/2);
            cout <<  c - k << endl;
        }
        else
            cout << c << endl;
    }
 }


#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int t;
	long long n,k;
	long long a;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&k);
		a=n/k;n=n-a*k;
		long long s;
		s=a*k+min(n,k/2);
		printf("%lld\n",s);
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n,k;
		cin >> n;
		cin >>k;
		int a=n/k;
		int b=n%k;
		if (b>(k/2)) b=k/2;
		int c=a*k+b;
		cout << c<< endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> T;

const ll mod = 1000000007;
const ll INF = 100000010000;

ll t, n, k;

ll ans;

ll gcd(ll x, ll y)
{
	if (y == 0) return x;
	return gcd(y, x % y);
}

ll pow_mod(ll x, ll y)
{
	if (y == 0) return 1;
	if (y % 2 == 1)
	{
		return x * pow_mod(x, y - 1) % mod;
	}
	else
	{
		ll half = pow_mod(x, y / 2);
		return half * half % mod;
	}
}

ll div_mod(ll x)
{
	return pow_mod(x, mod - 2);
}

void init()
{

}

void output()
{

}

void solve()
{
	cin >> t;
	while (t)
	{
		cin >> n >> k;

		n -= max(n % k - k / 2, (ll)0);
		cout << n << endl;
		t--;
	}
}

int main()
{
	init();

	solve();

	output();
	ll N;
	cin >> N;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <cstring>
#define ll long long
using namespace std;
const int M=4e5+10;
int n,k;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&k);
		if(n%k==0)
			printf("%d\n",n);
		else
		{
			int m=n/k;
			int ans=0;
			int p=m+1;
			int c1=k/2;
			int c2=k-c1;
			ans+=p*c1;
			ans+=m*c2;
			printf("%d\n",ans);
		}
	}
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <cstring>
#define ll long long
using namespace std;
const int M=4e5+10;
int n,k;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&k);
		if(n%k==0)
			printf("%d\n",n);
		else
		{
			int m=n/k;
			int ans=0;
			int p=m+1;
			int c1=k/2;
			int c2=k-c1;
			ans+=p*c1;
			ans+=m*c2;
			if(ans>n)
				ans=n;
			printf("%d\n",ans);
		}
	}
    return 0;
}


#include<stdio.h>
int fun(int n,int k)
{
	int a,b;
	a=n/k;
	b=n-a*k;
	if(b<=k/2) return n;
	else return fun(n-1,k);
}
int main()
{
	int t;
	int n,k,i;
	int a,b;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d%d",&n,&k);
		a=fun(n,k);
		printf("%d\n",a);
	}
	return 0;
 }


#include <bits/stdc++.h>

using namespace std;

int n,m,i,j;
long long l,r,mid,ans;
long long a[200100];
long long b[200100];

bool check(long long K) {
    int uk=1;
    for (int i=1;i<=n;i++) {
        if (uk>m) break;
        if (b[uk]<a[i]) {

            long long Seg=a[i]-b[uk];
            if (Seg>K) return false;
            while (uk<=m && min(2*Seg+b[uk]-a[i],Seg+2*(b[uk]-a[i]))<=K) ++uk;

        } else {

            while (uk<=m && b[uk]-a[i]<=K) ++uk;

        }
    }
    return (uk>m);
}

int main() {
    //freopen("data.in", "r", stdin);

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

    l=0; r=ans=1e10;
    for (j=0;j<64;j++) {
        mid =(l+r)/2;
        if (check(mid)) {
            ans=min(ans,mid);
            r=mid;
        } else l=mid+1;
    }

    cout<<ans <<"\n";

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
int n, m;
long long h[100000], p[100000];
bool check(long long mid)
{
    int i = 0, j = 0;
    while(i < n && j < m)
    {
        long long s = h[i] > p[j] ? h[i] - p[j] : 0;
        if(s > mid)
            return 0;
        while(p[j] <= h[i] && j < m)
            j++;
        while(j < m && p[j] > h[i] && 2 * min(s, p[j] - h[i]) + max(s, p[j] - h[i]) <= mid)
            j++;
        i++;
    }
    if(j < m)
        return 0;
    return 1;
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i++)
        cin >> h[i];
    for(int i = 0; i < m; i++)
        cin >> p[i];
    long long lft = -1, rht = max(h[n - 1], p[m - 1]) - min(h[0], p[0]) << 1, pre = -2;
    while(lft < rht)
    {
        long long mid = (lft + rht) >> 1;
        if(pre == mid)
            break;
        pre = mid;
        if(check(mid))
            rht = mid;
        else
            lft = mid;
    }
    cout << rht << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <queue>
#include <deque>
#include <cstring>
#include <stack>
#include <time.h>
#include <cmath>
#include <memory.h>
#include <algorithm>

using namespace std;

#define pb push_back
#define si size
#define mp make_pair
#define x first
#define y second
#define pob pop_back

typedef long long ll;
typedef double de;
typedef string str;
typedef vector<ll> vecl;
typedef pair<ll, ll> pll;
typedef vector<bool> veb;

const ll maxn = 1e6, INF = 2 * 1e10 + 1;

ll i, j, k, n, m, answer, a[maxn], b[maxn];
vecl v, g;

bool f(ll x){
  ll id = 0, jd = 0, l, r;
  while(id != n && jd != m){
    l = max(v[id] - g[jd], (ll)0);
    r = max(g[jd] - v[id], ll(0));
    while(min(l, r) * 2 + max(l, r) <= x){
      jd++;
      if(jd == m){
        break;
      }
      r = max(g[jd] - v[id], ll(0));
    }
    id++;
  }
  if(jd == m){
    return 1;
  }
  return 0;
}

ll bin_pow(){
	ll i1, l = 0, r = k, mid;
	for(i1 = 0; i1 < 100; ++i1){
		mid = (l + r) / 2;
		if(f(mid)){
			r = mid;
		}
		else{
			l = mid;
		}
	}
	if(f(l)){
		return l;
	}
	return r;
}

int main(){

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
#endif

  cin >> n >> m;
  for(i = 0; i < n; ++i){
    cin >> a[i];
  }
  for(i = 0; i < m; ++i){
    cin >> b[i];
  }
  j = 0;
  k = 0;
  for(i = 0; i < n + m; ++i){
    if(k == m && j == n){
      break;
    }
    if(j != n && (k == m || a[j] < b[k])){
      v.pb(a[j]);
      j++;
      continue;
    }
    if(k != m && (j == n || b[k] < a[j])){
      g.pb(b[k]);
      k++;
      continue;
    }
    if(b[k] == a[j]){
      v.pb(a[j]);
      j++;
      k++;
      continue;
    }
  }
  m = g.si();
  n = v.si();
  if(m != 0 && n != 0){
    k = INF;
    answer = bin_pow();
  }
  else{
    answer = 0;
  }
  cout << answer;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <queue>
#include <deque>
#include <cstring>
#include <stack>
#include <time.h>
#include <cmath>
#include <memory.h>
#include <algorithm>

using namespace std;

#define pb push_back
#define si size
#define mp make_pair
#define x first
#define y second
#define pob pop_back

typedef long long ll;
typedef double de;
typedef string str;
typedef vector<ll> vecl;
typedef pair<ll, ll> pll;
typedef vector<bool> veb;

const ll maxn = 1e6, INF = 2 * 1e10 + 1;

ll i, j, k, n, m, answer, a[maxn], b[maxn];
vecl v, g;

bool f(ll x){
  ll id = 0, jd = 0, l, r;
  while(id != n && jd != m){
    l = max(v[id] - g[jd], (ll)0);
    r = max(g[jd] - v[id], ll(0));
    while(min(l, r) * 2 + max(l, r) <= x){
      jd++;
      if(jd == m){
        break;
      }
      r = max(g[jd] - v[id], ll(0));
    }
    id++;
  }
  if(jd == m){
    return 1;
  }
  return 0;
}

ll bin_pow(){
	ll i1, l = 0, r = k, mid;
	for(i1 = 0; i1 < 100; ++i1){
		mid = (l + r) / 2;
		if(f(mid)){
			r = mid;
		}
		else{
			l = mid;
		}
	}
	if(f(l)){
		return l;
	}
	return r;
}

int main(){

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
#endif

  cin >> n >> m;
  for(i = 0; i < n; ++i){
    cin >> a[i];
  }
  for(i = 0; i < m; ++i){
    cin >> b[i];
  }
  j = 0;
  k = 0;
  for(i = 0; i < n + m; ++i){
    if(j == m && i == m){
      break;
    }
    if(j != n && (k == m || a[j] < b[k])){
      v.pb(a[j]);
      j++;
      continue;
    }
    if(k != m && (j == n || b[k] < a[j])){
      g.pb(b[k]);
      k++;
      continue;
    }
    if(b[k] == a[j]){
      v.pb(a[j]);
      j++;
      k++;
      continue;
    }
  }
  m = g.si();
  n = v.si();
  if(m != 0 && n != 0){
    k = INF;
    answer = bin_pow();
  }
  else{
    answer = 0;
  }
  cout << answer;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <queue>
#include <deque>
#include <cstring>
#include <stack>
#include <time.h>
#include <cmath>
#include <memory.h>
#include <algorithm>

using namespace std;

#define pb push_back
#define si size
#define mp make_pair
#define x first
#define y second
#define pob pop_back

typedef long long ll;
typedef double de;
typedef string str;
typedef vector<ll> vecl;
typedef pair<ll, ll> pll;
typedef vector<bool> veb;

const ll maxn = 1e6, INF = 1e10 + 1;

ll i, j, k, n, m, answer, a[maxn], b[maxn];
vecl v, g;

bool f(ll x){
  ll id = 0, jd = 0, l, r;
  while(id != n && jd != m){
    l = max(v[id] - g[jd], (ll)0);
    r = max(g[jd] - v[id], ll(0));
    while(min(l, r) * 2 + max(l, r) <= x){
      jd++;
      if(jd == m){
        break;
      }
      r = max(g[jd] - v[id], ll(0));
    }
    id++;
  }
  if(jd == m){
    return 1;
  }
  return 0;
}

ll bin_pow(){
	ll i1, l = 0, r = k, mid;
	for(i1 = 0; i1 < 100; ++i1){
		mid = (l + r) / 2;
		if(f(mid)){
			r = mid;
		}
		else{
			l = mid;
		}
	}
	if(f(l)){
		return l;
	}
	return r;
}

int main(){

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
#endif

  cin >> n >> m;
  for(i = 0; i < n; ++i){
    cin >> a[i];
  }
  for(i = 0; i < m; ++i){
    cin >> b[i];
  }
  j = 0;
  k = 0;
  for(i = 0; i < n + m; ++i){
    if(j == m && i == m){
      break;
    }
    if(j != n && (k == m || a[j] < b[k])){
      v.pb(a[j]);
      j++;
      continue;
    }
    if(k != m && (j == n || b[k] < a[j])){
      g.pb(b[k]);
      k++;
      continue;
    }
    if(b[k] == a[j]){
      v.pb(a[j]);
      j++;
      k++;
      continue;
    }
  }
  m = g.si();
  n = v.si();
  if(m != 0 && n != 0){
    k = INF;
    answer = bin_pow();
  }
  else{
    answer = 0;
  }
  cout << answer;
}


#include <bits/stdc++.h>
#define endl "\n"
#define FOR(x, y, z) for (int x = y; x < z; ++x)
#define FORR(x, y, z) for (int x = y; x > z; --x)
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define GET(a, n) for (int i = 0; i < n; ++i) cin >> a[i];
#define GETM(a, n, m) for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> a[i][j];
#define PRINTM(a, n, m) for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) cout << a[i][j] << " ";  cout << endl; };
#define PRINT(a, n) for (int i = 0; i < n; ++i) cout << a[i] << " ";
#define LL long long
#define ULL unsigned long long
#define VI vector <int>
#define VVI vector <vector<int>>
#define PII pair <int, int>
#define GETSUM(sum, n) sum = 0; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; sum += tmp; }
#define SUM(a, n, sum) sum = 0; for (int i = 0; i < n; ++i) { sum += a[i]; }
#define IT(a) a.begin(), a.end()
using namespace std;
template <class T> typename T::value_type arr_sum(const T& v, int n) { typename T::value_type sum = 0; FOR(i, 0, n) sum += v[i]; return sum; }
struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;

vector <LL> v;
vector <LL> w;

int
f(LL t)
{
    int j = 0;
    FOR (i, 0, v.size()) {
        int l = j;
        while (j < w.size() &&
abs(w[l] - w[j]) + min(abs(v[i] - w[l]), abs(v[i] - w[j])) <= t) {
            ++j;
        }
        if (l == j) {
            continue;
        }
    }
    if (j == w.size()) {
        return 1;
    }
    return -1;
}

long long
bsearch_answer(long long l, long long r, int (*f)(long long t))
{
    while (l < r) {
        long long t = (l + r) / 2;
        int p = f(t);
        if (!p) {
            l = t;
            break;
        } else if (p > 0) {
            r = t;
        } else {
            l = t + 1;
        }
    }
    return l;
}

int main()
{
    LL n, m;
    cin >> n >> m;
    v.resize(n);
    w.resize(m);
    GET(v, n);
    GET(w, m);
    cout << bsearch_answer(0, 30000000000LL, f) << endl;
}


#include <bits/stdc++.h>
#define endl "\n"
#define FOR(x, y, z) for (int x = y; x < z; ++x)
#define FORR(x, y, z) for (int x = y; x > z; --x)
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define GET(a, n) for (int i = 0; i < n; ++i) cin >> a[i];
#define GETM(a, n, m) for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> a[i][j];
#define PRINTM(a, n, m) for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) cout << a[i][j] << " ";  cout << endl; };
#define PRINT(a, n) for (int i = 0; i < n; ++i) cout << a[i] << " ";
#define LL long long
#define ULL unsigned long long
#define VI vector <int>
#define VVI vector <vector<int>>
#define PII pair <int, int>
#define GETSUM(sum, n) sum = 0; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; sum += tmp; }
#define SUM(a, n, sum) sum = 0; for (int i = 0; i < n; ++i) { sum += a[i]; }
#define IT(a) a.begin(), a.end()
using namespace std;
template <class T> typename T::value_type arr_sum(const T& v, int n) { typename T::value_type sum = 0; FOR(i, 0, n) sum += v[i]; return sum; }
struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;

vector <LL> v;
vector <LL> w;

int
f(LL t)
{
    int j = 0;
    FOR (i, 0, v.size()) {
        int l = j;
        while (j < w.size() &&
abs(w[l] - w[j]) + min(abs(v[i] - w[l]), abs(v[i] - w[j])) <= t) {
            ++j;
        }
        if (l == j) {
            continue;
        }
    }
    if (j == w.size()) {
        return 1;
    }
    return -1;
}

long long
bsearch_answer(long long l, long long r, int (*f)(long long t))
{
    while (l < r) {
        long long t = (l + r) / 2;
        int p = f(t);
        if (!p) {
            l = t;
            break;
        } else if (p > 0) {
            r = t;
        } else {
            l = t + 1;
        }
    }
    return l;
}

int main()
{
    LL n, m;
    cin >> n >> m;
    v.resize(n);
    w.resize(m);
    GET(v, n);
    GET(w, m);
    cout << bsearch_answer(0, 3000000000LL, f) << endl;
}


#include <cstdio>
#include <iomanip>
#include <utility>
#include <stack>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <numeric>
#include <queue>
#include <deque>
#include <climits>
#include <cstdlib>
#include <list>

using namespace std;

const int MAXSZ = (int)1e5 + 100;

int n;
long long h[MAXSZ];

int m;
long long p[MAXSZ];

bool check(long long t)
{
   long long rlast = 0;
   for(int i = 0; i < n; ++i)
   {
      long long x = h[i];
      long long l = *lower_bound(p, p + m, rlast + 1);
      if(x - l > t)
         return false;

      if(l < x)
      {
         long long t1_left = t - (x - l);
         long long r1 = l + t1_left;
         long long next_rlast_1 = max(r1, x);

         long long t2_need_l = x - l;
         long long t2_left = t - t2_need_l;
         long long d_r = t2_left / 2;
         long long r2 = x + d_r;

         rlast = max(rlast, max(next_rlast_1, r2));
      }
      else
      {
         rlast = max(rlast, x + t);
      }
      if(rlast >= p[m - 1])
         return true;
   }
   return false;
}

int main()
{
#ifdef LOCAL
   freopen("input.txt", "rt", stdin);
   freopen("output.txt", "wt", stdout);
#endif

   scanf("%d%d", &n, &m);
   for(int i = 0; i < n; ++i)
      scanf("%I64d", &h[i]);

   for(int i = 0; i < m; ++i)
      scanf("%I64d", &p[i]);

   long long l = 0;
   long long r = (long long)(2e10 + 1000);

   while(l + 1 < r)
   {
      long long m = (l + r) >> 1;
      if(check(m))
         r = m;
      else
         l = m + 1;
   }
   if(!check(l))
      l = r;

   cout << l << endl;

   return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstddef>
#include <complex>
#include <ctime>
#include <climits>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <functional>
#include <iterator>
#include <typeinfo>
#include <utility>
#include <memory>

#include <cassert>

#if __cplusplus >= 201103L
#include <tuple>
#include <ratio>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long int64;
const int inf = 2000000000;
static inline int Rint()
{
	struct X{ int dig[256]; X(){
	for(int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1;
	}};
	static 	X fuck;int s = 1, v = 0, c;
	for (;!fuck.dig[c = getchar()];);
	if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48;
	for (;fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48));
	return s ? v : -v;
}
typedef vector<int> vi;
typedef map<int, int> mii;
typedef set<int> si;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, s, e) for (int i = (s); i < (e); ++i)

#if __cplusplus >= 201103L
#define foreach(iter, c) for(auto& iter : c)
#else
#define foreach(itr, c) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#endif

template<typename T> static inline void cmax(T& a, const T& b){if(b>a)a=b;}
template<typename T> static inline void cmin(T& a, const T& b){if(b<a)a=b;}
#define SL static inline

#define dbg(x) cerr << (#x) << " = " << (x) << endl

const int maxn = 100005;

int64 u[maxn], v[maxn];

int check(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = 1, j = 1; i <= m;)
	{
		if (v[i] >= l && v[i] <= r)
		{
			++i;
			continue;
		}
		if (j <= n)
		{
			if (u[j] <= v[i])
			{
				l = u[j];
				r = u[j] + val;
			}
			else
			{
				if (v[i] >= u[j] - val)
				{
					l = v[i];
					r = max(u[j], v[i] + (val - (u[j]-v[i])));
				}
			}
			++j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}

int main()
{
	const int n = Rint(), m = Rint();
	for (int i = 1; i <= n; ++i)
	scanf("%I64d", u+i);
	for (int i = 1; i <= m; ++i)
	scanf("%I64d", v+i);

	int64 l = 0, r = 10*10000000000LL;
	while (l <= r)
	{
		const int64 mid = l + r >> 1;
		if (check(n, m, mid)) r = mid - 1; else l = mid + 1;
	}
	printf("%I64d\n", l);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstddef>
#include <complex>
#include <ctime>
#include <climits>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <functional>
#include <iterator>
#include <typeinfo>
#include <utility>
#include <memory>

#include <cassert>

#if __cplusplus >= 201103L
#include <tuple>
#include <ratio>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long int64;
const int inf = 2000000000;
static inline int Rint()
{
	struct X{ int dig[256]; X(){
	for(int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1;
	}};
	static 	X fuck;int s = 1, v = 0, c;
	for (;!fuck.dig[c = getchar()];);
	if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48;
	for (;fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48));
	return s ? v : -v;
}
typedef vector<int> vi;
typedef map<int, int> mii;
typedef set<int> si;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, s, e) for (int i = (s); i < (e); ++i)

#if __cplusplus >= 201103L
#define foreach(iter, c) for(auto& iter : c)
#else
#define foreach(itr, c) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#endif

template<typename T> static inline void cmax(T& a, const T& b){if(b>a)a=b;}
template<typename T> static inline void cmin(T& a, const T& b){if(b<a)a=b;}
#define SL static inline

#define dbg(x) cerr << (#x) << " = " << (x) << endl

const int maxn = 100005;

int64 u[maxn], v[maxn];

int check(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = 1, j = 1; i <= m;)
	{
		if (v[i] >= l && v[i] <= r)
		{
			++i;
			continue;
		}
		if (j <= n)
		{
			if (u[j] <= v[i])
			{
				l = u[j];
				r = u[j] + val;
			}
			else
			{
				if (v[i] >= u[j] - val)
				{
					l = v[i];
					r = max(u[j], u[j] + (val - 2*(u[j]-v[i])));
				}
			}
			++j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}

int main()
{
	const int n = Rint(), m = Rint();
	for (int i = 1; i <= n; ++i)
	scanf("%I64d", u+i);
	for (int i = 1; i <= m; ++i)
	scanf("%I64d", v+i);

	int64 l = 0, r = 10000000000LL;
	while (l <= r)
	{
		const int64 mid = l + r >> 1;
		if (check(n, m, mid)) r = mid - 1; else l = mid + 1;
	}
	printf("%I64d\n", l);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstddef>
#include <complex>
#include <ctime>
#include <climits>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <functional>
#include <iterator>
#include <typeinfo>
#include <utility>
#include <memory>

#include <cassert>

#if __cplusplus >= 201103L
#include <tuple>
#include <ratio>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long int64;
const int inf = 2000000000;
static inline int Rint()
{
	struct X{ int dig[256]; X(){
	for(int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1;
	}};
	static 	X fuck;int s = 1, v = 0, c;
	for (;!fuck.dig[c = getchar()];);
	if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48;
	for (;fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48));
	return s ? v : -v;
}
typedef vector<int> vi;
typedef map<int, int> mii;
typedef set<int> si;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, s, e) for (int i = (s); i < (e); ++i)

#if __cplusplus >= 201103L
#define foreach(iter, c) for(auto& iter : c)
#else
#define foreach(itr, c) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#endif

template<typename T> static inline void cmax(T& a, const T& b){if(b>a)a=b;}
template<typename T> static inline void cmin(T& a, const T& b){if(b<a)a=b;}
#define SL static inline

#define dbg(x) cerr << (#x) << " = " << (x) << endl

const int maxn = 100005;

void merged(int64& l, int64& r, int64 a, int64 b)
{
	if (a > b) return;
	if (a > r) return;
	if (b < l) return;
	r = max(r, b);
	l = min(l, a);
}

int64 u[maxn], v[maxn];
int check0(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = 1, j = 1; i <= m;)
	{
		const int64 a = l, b = r;
		if (v[i] >= l && v[i] <= r)
		{
			++i;
			continue;
		}
		if (j <= n)
		{
			if (u[j] <= v[i])
			{
				l = u[j];
				r = u[j] + val;
				merged(l, r, a, b);
			}
			else
			{
				if (v[i] >= u[j] - val)
				{
					l = v[i];
					r = max(u[j], v[i] + (val - (u[j]-v[i])));
					cmax(r, u[j] + (val - (u[j]-v[i])) / 2);
					merged(l, r, a, b);
				}
			}
			++j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}

int check(int n, int m, int64 val)
{
	return check0(n, m, val);
}

int main()
{
	const int n = Rint(), m = Rint();
	for (int i = 1; i <= n; ++i)
	scanf("%I64d", u+i);
	for (int i = 1; i <= m; ++i)
	scanf("%I64d", v+i);

	int64 l = 0, r = 10*10000000000LL;
	while (l <= r)
	{
		const int64 mid = l + r >> 1;
		if (check(n, m, mid)) r = mid - 1; else l = mid + 1;
	}
	printf("%I64d\n", l);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstddef>
#include <complex>
#include <ctime>
#include <climits>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <functional>
#include <iterator>
#include <typeinfo>
#include <utility>
#include <memory>

#include <cassert>

#if __cplusplus >= 201103L
#include <tuple>
#include <ratio>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long int64;
const int inf = 2000000000;
static inline int Rint()
{
	struct X{ int dig[256]; X(){
	for(int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1;
	}};
	static 	X fuck;int s = 1, v = 0, c;
	for (;!fuck.dig[c = getchar()];);
	if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48;
	for (;fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48));
	return s ? v : -v;
}
typedef vector<int> vi;
typedef map<int, int> mii;
typedef set<int> si;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, s, e) for (int i = (s); i < (e); ++i)

#if __cplusplus >= 201103L
#define foreach(iter, c) for(auto& iter : c)
#else
#define foreach(itr, c) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#endif

template<typename T> static inline void cmax(T& a, const T& b){if(b>a)a=b;}
template<typename T> static inline void cmin(T& a, const T& b){if(b<a)a=b;}
#define SL static inline

#define dbg(x) cerr << (#x) << " = " << (x) << endl

const int maxn = 100005;

int64 u[maxn], v[maxn];
int check0(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = 1, j = 1; i <= m;)
	{
		if (v[i] >= l && v[i] <= r)
		{
			++i;
			continue;
		}
		if (j <= n)
		{
			if (u[j] <= v[i])
			{
				l = u[j];
				r = u[j] + val;
			}
			else
			{
				if (v[i] >= u[j] - val)
				{
					l = v[i];
					r = max(u[j], v[i] + (val - (u[j]-v[i])));
				}
			}
			++j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}
int check1(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = m, j = n; i >= 1;)
	{
		if (v[i] >= l && v[i] <= r)
		{
			--i;
			continue;
		}
		if (j >= 1)
		{
			if (u[j] >= v[i])
			{
				l = u[j] - val;
				r = u[j];
			}
			else
			{
				if (v[i] <= u[j] + val)
				{
					r = v[i];
					l = min(u[j], v[i] - (val - (v[i]-u[j])));
				}
			}
			--j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}
int check(int n, int m, int64 val)
{
	return check0(n, m, val) || check1(n, m, val);
}

int main()
{
	const int n = Rint(), m = Rint();
	for (int i = 1; i <= n; ++i)
	scanf("%I64d", u+i);
	for (int i = 1; i <= m; ++i)
	scanf("%I64d", v+i);

	int64 l = 0, r = 10*10000000000LL;
	while (l <= r)
	{
		const int64 mid = l + r >> 1;
		if (check(n, m, mid)) r = mid - 1; else l = mid + 1;
	}
	printf("%I64d\n", l);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstddef>
#include <complex>
#include <ctime>
#include <climits>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <functional>
#include <iterator>
#include <typeinfo>
#include <utility>
#include <memory>

#include <cassert>

#if __cplusplus >= 201103L
#include <tuple>
#include <ratio>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long int64;
const int inf = 2000000000;
static inline int Rint()
{
	struct X{ int dig[256]; X(){
	for(int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1;
	}};
	static 	X fuck;int s = 1, v = 0, c;
	for (;!fuck.dig[c = getchar()];);
	if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48;
	for (;fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48));
	return s ? v : -v;
}
typedef vector<int> vi;
typedef map<int, int> mii;
typedef set<int> si;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, s, e) for (int i = (s); i < (e); ++i)

#if __cplusplus >= 201103L
#define foreach(iter, c) for(auto& iter : c)
#else
#define foreach(itr, c) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#endif

template<typename T> static inline void cmax(T& a, const T& b){if(b>a)a=b;}
template<typename T> static inline void cmin(T& a, const T& b){if(b<a)a=b;}
#define SL static inline

#define dbg(x) cerr << (#x) << " = " << (x) << endl

const int maxn = 100005;

void merged(int64& l, int64& r, int64 a, int64 b)
{
	if (a > b) return;
	if (a > r) return;
	if (b < l) return;
	r = max(r, b);
	l = min(l, a);
}

int64 u[maxn], v[maxn];
int check0(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = 1, j = 1; i <= m;)
	{
		const int64 a = l, b = r;
		if (v[i] >= l && v[i] <= r)
		{
			++i;
			continue;
		}
		if (j <= n)
		{
			if (u[j] <= v[i])
			{
				l = u[j];
				r = u[j] + val;
				merged(l, r, a, b);
			}
			else
			{
				if (v[i] >= u[j] - val)
				{
					l = v[i];
					r = max(u[j], v[i] + (val - (u[j]-v[i])));
					merged(l, r, a, b);
				}
			}
			++j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}
int check1(int n, int m, int64 val)
{
	int64 l = -1, r = -2;
	for (int i = m, j = n; i >= 1;)
	{
		const int64 a = l, b = r;
		if (v[i] >= l && v[i] <= r)
		{
			--i;
			continue;
		}
		if (j >= 1)
		{
			if (u[j] >= v[i])
			{
				l = u[j] - val;
				r = u[j];
				merged(l, r, a, b);
			}
			else
			{
				if (v[i] <= u[j] + val)
				{
					r = v[i];
					l = min(u[j], v[i] - (val - (v[i]-u[j])));
					merged(l, r, a, b);
				}
			}
			--j;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}
int check(int n, int m, int64 val)
{
	return check0(n, m, val) || check1(n, m, val);
}

int main()
{
	const int n = Rint(), m = Rint();
	for (int i = 1; i <= n; ++i)
	scanf("%I64d", u+i);
	for (int i = 1; i <= m; ++i)
	scanf("%I64d", v+i);

	int64 l = 0, r = 10*10000000000LL;
	while (l <= r)
	{
		const int64 mid = l + r >> 1;
		if (check(n, m, mid)) r = mid - 1; else l = mid + 1;
	}
	printf("%I64d\n", l);
	return 0;
}


//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];

      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long low = 0L;
      long high = 2L * (Math.max(pengPos[n - 1], bearPos[m - 1]) - Math.min(pengPos[0], bearPos[0]));

      while (true) {
        long mid = (low + high) / 2;
        if (canDoit(pengPos, bearPos, n, m, mid)) {
          high = mid;
          if (low == high) {
            minTime = mid;
            break;
          }
        } else {
          low = mid + 1;
        }
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, int n, int m, long allotedTime) {
      int bIdx = 0;

      for (int pIdx = 0; pIdx < n && bIdx < m; ++pIdx) {
        long minPos = Math.min(pengPos[pIdx], bearPos[bIdx]);
        while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx] && walkTime(pengPos[pIdx], minPos, pengPos[pIdx]) <= allotedTime) {
          ++bIdx;
        }
        if (bIdx < m && bearPos[bIdx] <= pengPos[pIdx]) {
          return false;
        }
        while (bIdx < m && walkTime(pengPos[pIdx], minPos, bearPos[bIdx]) <= allotedTime) {
          ++bIdx;
        }
      }
      if (bIdx < m) {
        return false;
      }
      return true;
    }

    long walkTime(long pengPos, long minPos, long maxPos) {
        return maxPos - minPos + Math.min(maxPos - pengPos, pengPos - minPos);
    }

  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


import java.util.*;
import java.io.*;

public class e {
   public static int n, m;
   public static long[] heads;
   public static long[] datas;
   public static void main(String[] Args) throws Exception {
      FS sc = new FS(System.in);
      n = sc.nextInt();
      m = sc.nextInt();
      long min = 0;
      long max = 1;
      heads = new long[n];
      datas = new long[m];
      for (int i = 0; i < n; i++)
         heads[i] = sc.nextLong();
      for (int i = 0; i < m; i++)
         datas[i] = sc.nextLong();
      while (!valid(max)){
         min = max + 1;
         max *= 2;
      }
      long ans = max;
      max--;
      while (max >= min) {
         long mid = (max + min) >> 1;
         if (valid(mid)) {
            max = mid - 1;
            ans = mid;
         } else
            min = mid + 1;
      }
      System.out.println(ans);
   }

   public static boolean can(long s, long e, long dist, long pos) {
      if (pos >= e)
         return dist >= s - pos;
      if (pos <= s)
         return dist >= pos - e;
      long pot1 = e - s + (pos - s);
      if (e - pos < pos - s)
         pot1 = e - s + (e - pos);
      return pot1 <= dist;
   }

   public static boolean valid(long pos) {
      int dptr = 0;
      for (int i = 0; i < n; i++) {
         long st = datas[dptr];
         if (st + pos < heads[i])
            return false;
         if (st - pos > heads[i])
            continue;
         long en = datas[dptr++];
         while (dptr < m && can(st, datas[dptr], pos, heads[i]))
            dptr++;
         if (dptr >= m)
            return true;
      }
      return false;
   }

   public static class FS {
      BufferedReader br;
      StringTokenizer st;
      FS(InputStream in) {
         br = new BufferedReader(new InputStreamReader(in));
         st = new StringTokenizer("");
      }
      String next() throws Exception {
         if (st.hasMoreTokens())
            return st.nextToken();
         st = new StringTokenizer(br.readLine());
         return next();
      }
      int nextInt() throws Exception {
         return Integer.parseInt(next());
      }
      long nextLong() throws Exception {
         return Long.parseLong(next());
      }
   }
}


import java.util.*;
import java.io.*;

public class e {
   public static int n, m;
   public static long[] heads;
   public static long[] datas;
   public static void main(String[] Args) throws Exception {
      FS sc = new FS(System.in);
      n = sc.nextInt();
      m = sc.nextInt();
      long min = 0;
      long max = 1;
      heads = new long[n];
      datas = new long[m];
      for (int i = 0; i < n; i++)
         heads[i] = sc.nextInt();
      for (int i = 0; i < m; i++)
         datas[i] = sc.nextInt();
      while (!valid(max)){
         min = max + 1;
         max *= 2;
      }
      long ans = max;
      max--;
      while (max >= min) {
         long mid = (max + min) >> 1;
         if (valid(mid)) {
            max = mid - 1;
            ans = mid;
         } else
            min = mid + 1;
      }
      System.out.println(ans);
   }

   public static boolean can(long s, long e, long dist, long pos) {
      if (pos >= e)
         return dist >= s - pos;
      if (pos <= s)
         return dist >= pos - e;
      long pot1 = e - s + (pos - s);
      if (e - pos < pos - s)
         pot1 = e - s + (e - pos);
      return pot1 <= dist;
   }

   public static boolean valid(long pos) {
      int dptr = 0;
      for (int i = 0; i < n; i++) {
         long st = datas[dptr];
         if (st + pos < heads[i])
            return false;
         if (st - pos > heads[i])
            continue;
         long en = datas[dptr++];
         while (dptr < m && can(st, datas[dptr], pos, heads[i]))
            dptr++;
         if (dptr >= m)
            return true;
      }
      return false;
   }

   public static class FS {
      BufferedReader br;
      StringTokenizer st;
      FS(InputStream in) {
         br = new BufferedReader(new InputStreamReader(in));
         st = new StringTokenizer("");
      }
      String next() throws Exception {
         if (st.hasMoreTokens())
            return st.nextToken();
         st = new StringTokenizer(br.readLine());
         return next();
      }
      int nextInt() throws Exception {
         return Integer.parseInt(next());
      }
   }
}


import java.util.*;
import java.io.*;

public class e {
   public static int n, m;
   public static long[] heads;
   public static long[] datas;
   public static void main(String[] Args) throws Exception {
      FS sc = new FS(System.in);
      n = sc.nextInt();
      m = sc.nextInt();
      long min = 0;
      long max = 1;
      heads = new long[n];
      datas = new long[m];
      for (int i = 0; i < n; i++)
         heads[i] = sc.nextLong();
      for (int i = 0; i < m; i++)
         datas[i] = sc.nextLong();
      while (!valid(max)){
         min = max + 1;
         max *= 2;
      }
      long ans = max;
      max--;
      while (max >= min) {
         long mid = (max + min) >> 1;
         if (valid(mid)) {
            max = mid - 1;
            ans = mid;
         } else
            min = mid + 1;
      }
      System.out.println(ans);
   }

   public static boolean can(long s, long e, long dist, long pos) {
      if (pos >= e)
         return (dist >= (pos - s));
      if (pos <= s)
         return (dist >= (e - pos));
      long pot1 = e - s + (pos - s);
      if (e - pos < pos - s)
         pot1 = e - s + (e - pos);
      return pot1 <= dist;
   }

   public static boolean valid(long pos) {
      int dptr = 0;
      for (int i = 0; i < n; i++) {
         long st = datas[dptr];
         if (st + pos < heads[i])
            return false;
         if (st - pos > heads[i])
            continue;
         while (dptr < m && can(st, datas[dptr], pos, heads[i]))
            dptr++;
         if (dptr >= m)
            return true;
      }
      return false;
   }

   public static class FS {
      BufferedReader br;
      StringTokenizer st;
      FS(InputStream in) {
         br = new BufferedReader(new InputStreamReader(in));
         st = new StringTokenizer("");
      }
      String next() throws Exception {
         if (st.hasMoreTokens())
            return st.nextToken();
         st = new StringTokenizer(br.readLine());
         return next();
      }
      int nextInt() throws Exception {
         return Integer.parseInt(next());
      }
      long nextLong() throws Exception {
         return Long.parseLong(next());
      }
   }
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF_344E {

	static int N, M;

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		N = sc.nextInt(); M = sc.nextInt();

		long [] heads = new long[N];
		for(int i = 0; i < N; ++i) heads[i] = sc.nextLong();

		long [] toRead = new long[M];
		for(int i = 0; i < M; ++i) toRead[i] = sc.nextLong();

		long l = 0, r = (long) 1e11, mid;
		long ans = -1;

		while(l <= r) {
			mid = (l + r >> 1);

			int i = 0, j = 0;

			long toRight;

			while(i < N && j < M) {
				toRight = 0;
				if(heads[i] <= toRead[j])
					toRight = heads[i] + mid;
				else {
					long diff = heads[i] - toRead[j];
					if(diff > mid)
						break;
					long max = (mid - (diff << 1));
					max = Math.max(max, (mid - diff) >> 1);
					if(max < 0) max = 0;
					toRight = heads[i] + max;
				}
				i++;
				for(; j < M && toRead[j] <= toRight; j++);
			}
			if(j == M) {
				ans = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}

		out.println(ans);

		out.flush();
		out.close();
	}

	static class Scanner{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public Scanner(FileReader r){	br = new BufferedReader(r);}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {return Integer.parseInt(next());}

		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException { return Double.parseDouble(next()); }

		public boolean ready() throws IOException {return br.ready();}
	}
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF_344E {

	static int N, M;

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		N = sc.nextInt(); M = sc.nextInt();

		long [] heads = new long[N];
		for(int i = 0; i < N; ++i) heads[i] = sc.nextLong();

		long [] toRead = new long[M];
		for(int i = 0; i < M; ++i) toRead[i] = sc.nextLong();

		long l = 0, r = (long) 1e11, mid;
		long ans = -1;

		while(l <= r) {
			mid = (l + r >> 1);

			int i = 0, j = 0;

			long toRight;

			while(i < N && j < M) {
				toRight = 0;
				if(heads[i] <= toRead[j])
					toRight = heads[i] + mid;
				else {
					long diff = heads[i] - toRead[j];
					long max = (mid - (diff << 1));
					max = Math.max(max, (mid - diff) >> 1);
					toRight = heads[i] + max;
				}
				i++;
				for(; j < M && toRead[j] <= toRight; j++);
			}
			if(j == M) {
				ans = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}

		out.println(ans);

		out.flush();
		out.close();
	}

	static class Scanner{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public Scanner(FileReader r){	br = new BufferedReader(r);}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {return Integer.parseInt(next());}

		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException { return Double.parseDouble(next()); }

		public boolean ready() throws IOException {return br.ready();}
	}
}