/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = 0;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i, f[++len] = n / i;
	for (int i = 1; i <= len; i++)
	{
		 for (int j = 1; j <= n; j++)
		 {
		      ll sum = 0;
		 		for (int k = j; k <= n; k += f[i])
		 			sum += a[k] * 1ll;
		 		ans = max (sum, ans);
		 }
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i;
	for (int i = 1; i <= len; i++)
	{
		 for (int j = 1; j <= n / 3; j++)
		 {
		 		if (f[i] * 2 + j <= n)
		 		{
		      	ll sum = 0;
		 			for (int k = j; k <= n; k += f[i])
		 				sum += a[k] * 1ll;
		 			ans = max (sum, ans);
		 		}
		 }
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i <= n / 3; i++)
	{
		for (int j = i; j <= n / 3; j++)
		{
			if (n % j == 0)
			{
				ll sum = 0;
				for (int k = i; k <= n; k += j) sum += a[k] * 1ll;
				ans = max (ans, sum);
			}
		}
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i, f[++len] = n / i;
	for (int i = 1; i <= len; i++)
	{
		if (n / f[i] >= 3)
		{
		 for (int j = 1; j <= n - 3; j++)
		 {
		      ll sum = 0;
		 		for (int k = j; k <= n; k += f[i])
		 			sum += a[k] * 1ll;
		 		ans = max (sum, ans);
		 }
		}
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i, f[++len] = n / i;
	for (int i = 1; i <= len; i++)
	{
		if (n / f[i] >= 3)
		{
		 for (int j = 1; j <= n; j++)
		 {
		      ll sum = 0;
		 		for (int k = j; k <= n; k += f[i])
		 			sum += a[k] * 1ll;
		 		ans = max (sum, ans);
		 }
		}
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i;
	for (int i = 1; i <= len; i++)
	{
		 for (int j = 1; j <= n / 3; j++)
		 {
		      ll sum = 0;
		 		for (int k = j; k <= n; k += f[i])
		 			sum += a[k] * 1ll;
		 		ans = max (sum, ans);
		 }
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i;
	for (int i = 1; i <= len; i++)
	{
		 for (int j = 1; j <= n; j++)
		 {
		 	if (j + f[i] * 2 <= n)
		 	{
		      ll sum = 0;
		 		for (int k = j; k <= n; k += f[i])
		 			sum += a[k] * 1ll;
		 		ans = max (sum, ans);
		 	}
		 }
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i, f[++len] = n / i;
	for (int i = 1; i <= len; i++)
	{
		if (n / f[i] >= 3)
		{
		 for (int j = 1; j <= n - 2; j++)
		 {
		 		if (j + f[i] * 2 - 1 <= n)
		 		{
		      	ll sum = 0;
		 			for (int k = j; k <= n; k += f[i])
		 				sum += a[k] * 1ll;
		 			ans = max (sum, ans);
		 		}
		 }
		}
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i, f[++len] = n / i;
	for (int i = 1; i <= len; i++)
	{
		if (n / f[i] >= 3)
		{
		 for (int j = 1; j <= n - 2; j++)
		 {
		 		if (j + f[i] * 2 <= n)
		 		{
		      	ll sum = 0;
		 			for (int k = j; k <= n; k += f[i])
		 				sum += a[k] * 1ll;
		 			ans = max (sum, ans);
		 		}
		 }
		}
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i;
	for (int i = 1; i <= len; i++)
	{
		 for (int j = 1; j <= n / 3; j++)
		 {
		 		if (f[i] * 2 + j <= n && n % j == 0)
		 		{
		      	ll sum = 0;
		 			for (int k = j; k <= n; k += f[i])
		 				sum += a[k] * 1ll;
		 			ans = max (sum, ans);
		 		}
		 }
	}
	printf ("%I64d", ans);
return 0;
}


/*
Problem's name:
Difficulty:
Author: Smagulov Aybek
Done:
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <ctime>
#include <set>
#include <map>

#define se second
#define fi first
#define	ll long long
#define eps 1e-9

using namespace std;

const int inf = 1000000000; // default 1e9

double Time ()
{
  return double (clock ()) / double (CLOCKS_PER_SEC);
}

int n, a[20005], f[1000], len = 0;
ll ans = -inf;

int main ()
{
	#ifndef ONLINE_JUDGE
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
	for (int i = 1; i * i <= n; i++)
		if (n % i == 0) f[++len] = i, f[++len] = n / i;
	for (int i = 1; i <= len; i++)
	{
		if (n / f[i] >= 3)
		{
		 for (int j = 1; j <= n - 2; j++)
		 {
		      ll sum = 0;
		 		for (int k = j; k <= n; k += f[i])
		 			sum += a[k] * 1ll;
		 		ans = max (sum, ans);
		 }
		}
	}
	printf ("%I64d", ans);
return 0;
}


#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

long long best1(vector<int>vec, vector<int>vec1)
{
    long long res = 0;
    long long res1 = 0;
    vector<int>v;
    vector<int>v1;
    for (int i=0;i<vec.size();i++)
    {
            res+=vec[i];
            res1+=vec1[i];
            if (i%2 != 0)
            {
                v.push_back(vec[i]);
            }
            else
            {
                v1.push_back(vec1[i]);
        }
    }
    if (v.size() > 2)
    {
        return max(max(res,res1),best1(v,v1));
    }
    else return max(res,res1);
}

int main (int argc, char *argv[])
{
    int n;
    cin >> n;
    vector<int>vec;
    vector<int>vec1;
    long long curr1 = 0;
    long long curr2 = 0;
    for (int i=1;i<=n;i++)
    {
        int l = 0;
        cin >> l;
        if (i%2 != 0)
        {
         vec.push_back(l);
            curr1+=l;
        }
        else
        {
         vec1.push_back(l);
         curr2+=l;
        }
    }
    if (best1(vec,vec1) > curr1+curr2 && vec.size()>2) cout << best1(vec,vec1) << endl;
    else cout << curr1+curr2 << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int best1(vector<int>vec, vector<int>vec1)
{
    int res = 0;
    int res1 = 0;
    vector<int>v;
    vector<int>v1;
    for (int i=0;i<vec.size();i++)
    {
            res+=vec[i];
            res1+=vec1[i];
            if (i%2 != 0)
            {
                v.push_back(vec[i]);
            }
            else
            {
                v1.push_back(vec1[i]);
        }
    }
    if (v.size() > 0)
    {
        return max(max(res,res1),best1(v,v1));
    }
    else return max(res,res1);
}

int main (int argc, char *argv[])
{
    int n;
    cin >> n;
    vector<int>vec;
    vector<int>vec1;
    long long curr1;
    long long curr2;
    for (int i=1;i<=n;i++)
    {
        int l;
        cin >> l;
        if (i%2 != 0)
        {
         vec.push_back(l);
            curr1+=l;
        }
        else
        {
         vec1.push_back(l);
         curr2+=l;
        }
    }
    if (best1(vec,vec1) > curr1+curr2) cout << best1(vec,vec1) << endl;
    else cout << curr1+curr2 << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <time.h>
# include <fstream>

using namespace std;
#define PI 3.14159265

int main(int argc, const char * argv[])
{
    int n = 0;
    cin >> n;
    std::vector<int>vec(n);
    int res = -20000000;
    for (int i = 0;i<n;i++){
        cin >> vec[i];
    }
    for (int i = 1;i<n/2;i++){
        if (n%i == 0){
            for (int j = 0;j<i;j++){
                int a = 0;
                for (int k = 0;k<(n/i);k++){
                    a+=vec[j+k*i];
                }
                if (res < a && n/i>2) res = a;
            }
        }
    }
    if (res == -20000000) {
        res = 0;
        for (int i = 0;i<n;i++){
            res+=vec[i];
        }
    }
    std::cout << res << endl;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <time.h>
# include <fstream>

using namespace std;
#define PI 3.14159265

int main(int argc, const char * argv[])
{
    int n = 0;
    cin >> n;
    std::vector<int>vec(n);
    int res = 0;
    for (int i = 0;i<n;i++){
        cin >> vec[i];
    }
    for (int i = 1;i<n/2;i++){
        if (n%i == 0){
            for (int j = 0;j<i;j++){
                int a = 0;
                for (int k = 0;k<(n/i);k++){
                    a+=vec[j+k*i];
                }
                if (res < a && n/i>2) res = a;
            }
        }
    }
    if (res==0) {
        for (int i = 0;i<n;i++){
            res+=vec[i];
        }
    }
    std::cout << res << endl;
}


#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

long long best1(vector<int>vec, vector<int>vec1)
{
    long long res = 0;
    long long res1 = 0;
    vector<int>v;
    vector<int>v1;
    for (int i=0;i<vec.size();i++)
    {
            res+=vec[i];
            res1+=vec1[i];
            if (i%2 != 0)
            {
                v.push_back(vec[i]);
            }
            else
            {
                v1.push_back(vec1[i]);
        }
    }
    if (v.size() > 2)
    {
        return max(max(res,res1),best1(v,v1));
    }
    else return max(res,res1);
}

int main (int argc, char *argv[])
{
    int n;
    cin >> n;
    vector<int>vec;
    vector<int>vec1;
    long long curr1 = 0;
    long long curr2 = 0;
    for (int i=1;i<=n;i++)
    {
        int l = 0;
        cin >> l;
        if (i%2 != 0)
        {
         vec.push_back(l);
            curr1+=l;
        }
        else
        {
         vec1.push_back(l);
         curr2+=l;
        }
    }
    if (best1(vec,vec1) > curr1+curr2) cout << best1(vec,vec1) << endl;
    else cout << curr1+curr2 << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

long long best1(vector<int>vec, vector<int>vec1)
{
    long long res = 0;
    long long res1 = 0;
    vector<int>v;
    vector<int>v1;
    for (int i=0;i<vec.size();i++)
    {
            res+=vec[i];
            res1+=vec1[i];
            if (i%2 != 0)
            {
                v.push_back(vec[i]);
            }
            else
            {
                v1.push_back(vec1[i]);
        }
    }
    if (v.size() > 0)
    {
        return max(max(res,res1),best1(v,v1));
    }
    else return max(res,res1);
}

int main (int argc, char *argv[])
{
    int n;
    cin >> n;
    vector<int>vec;
    vector<int>vec1;
    long long curr1 = 0;
    long long curr2 = 0;
    for (int i=1;i<=n;i++)
    {
        int l = 0;
        cin >> l;
        if (i%2 != 0)
        {
         vec.push_back(l);
            curr1+=l;
        }
        else
        {
         vec1.push_back(l);
         curr2+=l;
        }
    }
    if (best1(vec,vec1) > curr1+curr2) cout << best1(vec,vec1) << endl;
    else cout << curr1+curr2 << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <time.h>
# include <fstream>

using namespace std;
#define PI 3.14159265

int main(int argc, const char * argv[])
{
    int n = 0;
    cin >> n;
    std::vector<int>vec(n);
    int res = (-1)*INFINITY;
    for (int i = 0;i<n;i++){
        cin >> vec[i];
    }
    for (int i = 1;i<n/2;i++){
        if (n%i == 0){
            for (int j = 0;j<i;j++){
                int a = 0;
                for (int k = 0;k<(n/i);k++){
                    a+=vec[j+k*i];
                }
                if (res < a && n/i>2) res = a;
            }
        }
    }
    if (res==0) {
        for (int i = 0;i<n;i++){
            res+=vec[i];
        }
    }
    std::cout << res << endl;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <time.h>
# include <fstream>

using namespace std;
#define PI 3.14159265

int main(int argc, const char * argv[])
{
    int n = 0;
    cin >> n;
    std::vector<int>vec(n);
    int res = 0;
    for (int i = 0;i<n;i++){
        cin >> vec[i];
    }
    for (int i = 1;i<n/2;i++){
        if (n%i == 0){
            for (int j = 0;j<i;j++){
                int a = 0;
                for (int k = 0;k<(n/i);k++){
                    a+=vec[j+k*i];
                }
                if (res < a) res = a;
            }
        }
    }
    std::cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111];

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	}
	int ans = -inf;
	for(int i = 1; i <= n; ++i) {
	 	for(int j = 1; j <= n; j++) {
	 		s = 0;
	 		for(int k = j; k <= n; k += i)
	 			s += f[k];
	 		ans = max(ans, s);
	 	}

	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111];

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	}
	int ans = -inf;
	for(int i = 1; i <= n; ++i) {
		s = 0;
	 	for(int j = i; j <= n; j += i)
	 		if(j % i == 0) s += f[j];
	 	ans = max(ans, s);
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111], ans;

int getAns(int d){
     if(n / d < 3) return -inf;
     int res = -inf;
     for(int j = 1; j <= d; j++) {
	 	int s = 0;
	 	for(int k = j; k <= n; k += d)
	 		s += f[k];
	 	res = max(res, s);
     }
     return res;
}

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	 	 ans += f[i];
	}
	for(int i = 2; i * i <= n; ++i) {
		if(n % i != 0) continue;
		ans = max(ans, getAns(i));
		if(i != n / i)
			ans = max(ans, getAns(i));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111];

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	}
	int ans = -inf;
	for(int i = 1; i * i <= n; ++i) {
		if(n % i != 0) continue;
	 	for(int j = 1; j <= i; j++) {
	 		s = 0;
	 		for(int k = j; k <= n; k += i)
	 			s += f[k];
	 		ans = max(ans, s);
	 	}
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111], ans;

void check(int d){
     for(int j = 1; j <= d; j++) {
	 	int s = 0;
	 	for(int k = j; k <= n; k += d)
	 		s += f[k];
	 	ans = max(ans, s);
     }
}

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	 	 ans += f[i];
	}
	for(int i = 2; i * i <= n; ++i) {
		if(n % i != 0) continue;
		if(n / i >= 3) check(i);
		if(i >= 3) check(n / i);
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111];

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	}
	int ans = -inf;
	for(int i = 1; i * i <= n; ++i) {
		if(n % i != 0 || n / i < 3) continue;
	 	for(int j = 1; j <= i; j++) {
	 		s = 0;
	 		for(int k = j; k <= n; k += i)
	 			s += f[k];
	 		ans = max(ans, s);
	 	}
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111];

int getAns(int d){
     if(n / d < 3) return -inf;
     int res = -inf;
     for(int j = 1; j <= d; j++) {
	 	int s = 0;
	 	for(int k = j; k <= n; k += d)
	 		s += f[k];
	 	res = max(res, s);
     }
     return res;
}

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	}
	int ans = -inf;
	for(int i = 1; i * i <= n; ++i) {
		if(n % i != 0) continue;
		ans = max(ans, getAns(i));
		if(i != n / i)
			ans = max(ans, getAns(i));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,n) for((int)(i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define f first
#define s second
#define inf int(2e9)
#define sz(x) int((x).size())
#define sqr(x) (x) * (x)
#define all(x) (x).begin(), (x).end()
#define y1 y111

const double eps = 1e-9;
const double pi = acos(-1.0);
typedef long long ll;

int n, s, f[20111];

int main() {
	#ifdef LOCAL
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif

	cin >> n;
	for(int i = 1; i <= n; ++i){
	 	 cin >> f[i];
	}
	int ans = -inf;
	for(int i = 1; i <= n; ++i) {
		s = 0;
	 	for(int j = 1; j <= n; j += i)
	 		s += f[i];
	 	ans = max(ans, s);
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<vector>
#include<set>
#define FUCK cout<<"fuck"<<endl
#define pb(fuck) push_back(fuck)
#define INF  100000000000009
using namespace std;
typedef long long ll;
#define MAXN 20010
int a[MAXN];
int p[MAXN/2];
int main ()
{
	int n,i,j ,k,cnt,ans;
	//freopen("FUCK.txt", "r" , stdin);
	while(scanf("%d", &n) !=EOF )
	{
		cnt= 0 ; ans =0 ;
		for(i=1;i*i<= n;i++)  if(n%i==0 ) p[cnt++] = i ;
		for( i=1;i<=n;i++) scanf("%d", a+i ),ans+=a[i];
		if( n%2==0 )
		{
			for( i=0;i<cnt;i++)
			{
				if( n/p[i]  < 3 )  continue;
				for(j=1;j<=n/2; j++)
				{
					int tmp=0;
					for(k=j ; k<=n;k+=p[i] )
						tmp+= a[k];
					if(tmp > ans) ans = tmp;
				}
			}
		}
		printf("%d\n",ans);
	}
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<vector>
#include<set>
#define FUCK cout<<"fuck"<<endl
#define pb(fuck) push_back(fuck)
#define INF  100000000000009
using namespace std;
typedef long long ll;
#define MAXN 20010
int a[MAXN];
int p[MAXN/2];
int main ()
{
	int n,i,j ,k,cnt,ans;
	//freopen("FUCK.txt", "r" , stdin);
	while(scanf("%d", &n) !=EOF )
	{
		cnt= 0 ; ans =0 ;
		for(i=1;i<= n;i++)  if(n%i==0 ) p[cnt++] = i ;
		for( i=1;i<=n;i++) scanf("%d", a+i ),ans+=a[i];
		if( n%2==0 )
		{
			for( i=0;i<cnt;i++)
			{
				if( n/p[i]  < 3 )  continue;
				for(j=1;j<=n/2; j++)
				{
					int tmp=0;
					for(k=j ; k<=n;k+=p[i] )
						tmp+= a[k];
					if(tmp > ans) ans = tmp;
				}
			}
		}
		printf("%d\n",ans);
	}
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<vector>
#include<set>
#define FUCK cout<<"fuck"<<endl
#define pb(fuck) push_back(fuck)
#define INF  100000000000009
using namespace std;
typedef long long ll;
#define MAXN 20010
int a[MAXN];
int p[MAXN/2];
int main ()
{
	int n,i,j ,k,cnt,ans;
	//freopen("FUCK.txt", "r" , stdin);
	while(scanf("%d", &n) !=EOF )
	{
		cnt= 0 ; ans =0 ;
		for(i=1;i<= n;i++)  if(n%i==0 ) p[cnt++] = i ;
		for( i=1;i<=n;i++) scanf("%d", a+i ),ans+=a[i];
		if( n%2==0 )
		{
			for( i=0 ;i<cnt; i++)
			{
				if( n/p[i]  < 3 )  continue;
				for(j=1;j<=n; j++)
				{
					int tmp=0;
					for(k=j ; k<=n;k+=p[i] ) tmp+= a[k];
					if(tmp > ans)  ans = tmp;
				}
			}
		}
		printf("%d\n",ans);
	}
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<vector>
#include<set>
#define FUCK cout<<"fuck"<<endl
#define pb(fuck) push_back(fuck)
#define INF  100000000000009
using namespace std;
typedef long long ll;
#define MAXN 20010
int a[MAXN];
int p[MAXN/2];
int main ()
{
	int n,i,j ,k,cnt,ans;
	//freopen("FUCK.txt", "r" , stdin);
	while(scanf("%d", &n) !=EOF )
	{
		cnt= 0 ; ans =0 ;
		for(i=1;i*i<= n;i++)  if(n%i==0 ) p[cnt++] = i ;
		for( i=1;i<=n;i++) scanf("%d", a+i ),ans+=a[i];
		if( n%2==0 )
		{
			for( i=0;i<cnt;i++)
			{
				if( p[i] < 3 )continue;
				for(j=1;j<=n/2; j++)
				{
					int tmp=0;
					for(k=j ; k<=n;k+=p[i] )
						tmp+= a[k];
					if(tmp > ans) ans = tmp;
				}
			}
		}
		printf("%d\n",ans);
	}
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<vector>
#include<set>
#define FUCK cout<<"fuck"<<endl
#define pb(fuck) push_back(fuck)
#define INF  100000000000009
using namespace std;
typedef long long ll;
#define MAXN 20010
int a[MAXN];
int p[MAXN];
int main ()
{
    int n,i,j ,k,cnt,ans;
  //freopen("FUCK.txt", "r" , stdin);
    while(scanf("%d", &n) !=EOF )
    {
        cnt= 0 ; ans =0 ;
        for(i=1;i<= n;i++)  if(n%i==0 ) p[cnt++] = i ;
        for( i=1;i<=n;i++) scanf("%d", a+i ) ,ans+=a[i];
        for( i=0; i<cnt; i++)
        {
            if( n/p[i] <3  )  continue;
            for(j=1;j<=p[i]; j++)
            {
                int tmp=0;
                for( k=j; k<= n; k+= p[i] )   tmp+= a[k];
                if(tmp>ans)   ans= tmp;
            }
        }
        printf("%d\n",ans);
    }
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<vector>
#include<set>
#define FUCK cout<<"fuck"<<endl
#define pb(fuck) push_back(fuck)
#define INF  100000000000009
using namespace std;
typedef long long ll;
#define MAXN 20010
int a[MAXN];
int p[MAXN/2];
int main ()
{
	int n,i,j ,k,cnt,ans;
	//freopen("FUCK.txt", "r" , stdin);
	while(scanf("%d", &n) !=EOF )
	{
		cnt= 0 ; ans =0 ;
		for(i=1;i*i<= n;i++)  if(n%i==0 ) p[cnt++] = i ;
		for( i=1;i<=n;i++) scanf("%d", a+i ),ans+=a[i];
		if( n%2==0 )
		{
			for( i=0;i<cnt;i++)
			{
				for(j=1;j<=n/2; j++)
				{
					int tmp=0;
					for(k=j ; k<=n;k+=p[i] )
						tmp+= a[k];
					if(tmp > ans) ans = tmp;
				}
			}
		}
		printf("%d\n",ans);
	}
}


t=int(input())
for i in range(t):
	n,k=map(int,input().split())
	x=n%k
	if(x==0 or (n//k)==0):
		print(n)
	else:
		y=n-x
		q=k//2
		y=y+q
		print(y)


t=int(input())
for i in range(t):
	n,k=map(int,input().split())
	x=n%k
	q=k//2
	if(x==0):
		print(n)
	elif(x>=q):
		y=n-x
		y=y+q
		print(y)
	else:
		print(n)


t=int(input())
for i in range(t):
	n,k=input().split()
	n=int(n)
	k=int(k)
	if n%k==0:
		print (n//k);
	else:
	    print((n//k)*k+k//2)


t=int(input())
for i in range(t):
	n,k=input().split()
	n=int(n)
	k=int(k)
	if n%k==0:
		print (n);
	else:
	    print((n//k)*k+k//2)


t=int(input())
for i in range(t):
	n,k=input().split()
	n=int(n)
	k=int(k)
	if n%k==0:
		print (n);
	if k>n:
		if k//n<n:
			print(k//n)
		else:
			print(k)
	else:
	    print((n//k)*k+k//2)


t=int(input())
for i in range(t):
	n,k=input().split()
	n=int(n)
	k=int(k)
	print((n//k)*k+k//2)


t=int(input())
for i in range(t):
	n,k=input().split()
	n=int(n)
	k=int(k)
	p=n-((n//k)*k)
	if p<=(k//2):
		print (n);
	else:
	    print((n//k)*k+k//2)


a = int(input())
for i in range(a):
    s = str(input())
    s = s.split(' ')
    d = int(s[0])
    f = int(s[1])
    if f == 0:
        print(0)
    else:
        if d % f == 0:
            print(d)
        else:
            k = d // f
            l = d - f * k
            l -= f // 2
            if l <= 0:
                l = 0
            print(d-l)


a = int(input())
for i in range(a):
    s = str(input())
    s = s.split(' ')
    d = int(s[0])
    f = int(s[1])
    if d % f == 0:
        print(d)
    else:
        k = d // f
        l = d - f * k
        l -= f//2
        print(d-l)


a = 1
for i in range(a):
    s = str(input())
    s = s.split(' ')
    d = int(s[0])
    f = int(s[1])
    if f == 0:
        print(0)
    else:
        if d % f == 0:
            print(d)
        else:
            k = d // f
            l = d - f * k
            l -= f // 2
            if l <= 0:
                l = 0
            print(d-l)


a = int(input())
for i in range(a):
    s = str(input())
    s = s.split(' ')
    d = int(s[0])
    f = int(s[1])
    if f == 0:
        print(0)
    else:
        if d % f == 0:
            print(d)
        else:
            k = d // f
            l = d - f * k
            l -= f//2
            print(d-l)


for i in range(int(input())):
	ar = list(map(int,input().split(" ")))
	if ar[0]/ar[1] == ar[0]//ar[1] or  ar[0] == ar[1] or ar[0] < ar[1]:
		print(ar[0])
	else:
		answer = 0
		for j in range(ar[1]):
			if j%2 == 0:
				answer += ar[0]//ar[1]
			else:
				answer += ar[0]//ar[1] + 1
		print(answer)


for i in range(int(input())):
	ar = list(map(int,input().split(" ")))
	if ar[0]/ar[1] == ar[0]//ar[1] or  ar[0] == ar[1]:
		print(ar[0])
	elif ar[0] < ar[1]:
		print(0)
	else:
		answer = 0
		for j in range(ar[1]):
			if j%2 == 0:
				answer += ar[0]//ar[1]
			else:
				answer += ar[0]//ar[1] + 1
		print(answer)


for i in range(int(input())):
	ar = list(map(int,input().split(" ")))
	if ar[0]/ar[1] == ar[0]//ar[1]:
		print(ar[0])
	else:
		answer = 0
		for j in range(ar[1]):
			if j%2 == 0:
				answer += ar[0]//ar[1]
			else:
				answer += ar[0]//ar[1] + 1
		print(answer)


for i in range(int(input())):
	ar = list(map(int,input().split(" ")))
	x = ar[0] - ar[0] % ar[1]
	x += min(ar[0]%ar[1], ar[1]//2)
	print(x)


for i in range(int(input())):
    n,k=map(int,input().split())
    if n<=k:
        print(n)
    else:
        if n%k>(k//2):
            print(n-n%k+k//2)
        else:
            print(n)


for i in range(int(input())):
    n,k=map(int,input().split())
    if n % k > (k // 2):
        print(n - n % k + k // 2)
    else:
        print(n)


for i in range(int(input())):
    n,k=map(int,input().split())
    if n<=k:
        if n>k//2:
            print(k//2)
        else:
            print(n)
    else:
        if n%k>(k//2):
            print(n-n%k+k//2)
        else:
            print(n)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if k == 0 :
		print(0)
	elif n%k == 0 :
		print(n)
	else :
		if int(n -(n//k)*k) >= (k//2) :
			print(int((n//k)*k + k//2))
		else :
			if n>= k:
				print((n//k)*k)
			else :
				if n > k//2:
					print(k//2)
				else :
					print(n)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if k == 0 :
		print(0)
	elif n%k == 0 :
		print(n)
	else :
		if n -(n//k)*k >= k//2 :
			print((n//k)*k + k//2)
		else :
			prnt((n//k)*k)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if n%k == 0 :
		print(0)
	else :
		if n -(n//k)*k >= k//2 :
			print((n//k)*k + k//2)
		else :
			prnt((n//k)*k)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if n >= k :
		if n%k == 0 :
			print(n)
		elif n%k >= (k//2) :
			print((n//k)*k + k//2)
		else :
			print((n//k)*k)
	else :
		if n > k//2:
			print(k//2)
		else :
			print(n)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if n%k == 0 :
		print(n)
	else :
		if n -(n//k)*k >= k//2 :
			print((n//k)*k + k//2)
		else :
			prnt((n//k)*k)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if n >= k :
		if n%k == 0 :
			print(n)
		elif n%k >= (k//2) :
			print((n//k)*k + k//2)
		else :
			print(n)
	else :
		if n > k//2:
			print(k//2)
		else :
			print(n)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if k == 0 :
		print(0)
	elif n%k == 0 :
		print(n)
	else :
		if int(n -(n//k)*k) >= (k//2) :
			print((n//k)*k + k//2)
		else :
			print((n//k)*k)


for ii in range(int(input())):
	b = [int(j) for j in input().split()]
	n = b[0]
	k = b[1]
	if n%k == 0 :
		print(n)
	else :
		if n%k >= (k//2) and n >= k :
			print((n//k)*k + k//2)
		else :
			if n>= k:
				print((n//k)*k)
			else :
				print(0)


t=int(input())

for _ in range(t):
    l=input().split(" ")
    candies=int(l[0])
    kids=int(l[1])

    if candies < kids :
        if candies *2 <= kids :
            print(candies)
        else :
            print(kids//2)
        continue

    q=candies//kids
    r=candies%kids

    ans=0
    if r>0 : ans=kids//2
    ans=q*kids+ans
    print(ans)


t=int(input())

for _ in range(t):
    l=input().split(" ")
    candies=int(l[0])
    kids=int(l[1])

    if candies < kids :
        if candies <= kids//2 :
            print(candies)
        else :
            print(kids//2)
        continue

    q=candies//kids
    r=candies%kids

    ans=0
    if r>0 : ans=kids//2
    ans=q*kids+ans
    print(ans)


t=int(input())

for _ in range(t):
    l=input().split(" ")
    candies=int(l[0])
    kids=int(l[1])

    if candies < kids :
        if candies < kids//2 :
            print(candies)
        else :
            print(kids//2)
        continue

    q=candies//kids
    r=candies%kids

    ans=0
    if r>0 : ans=kids//2
    ans=q*kids+ans
    print(ans)


t=int(input())

for _ in range(t):
    l=input().split(" ")
    candies=int(l[0])
    kids=int(l[1])

    if candies < kids :
        if candies <= kids//2 :
            print(candies)
        else :
            print(kids//2)
        continue

    q=candies//kids
    r=candies%kids

    ans=0
    if r>0 : ans=min(kids//2,r)
    ans=q*kids+ans
    print(ans)


from math import floor

for _ in range(int(input())):
    n, k = map(int, input().split())

    ans = floor(n / k) * k
    ans += min(n % k, floor(k / 2))
    print(ans)


from math import floor

for _ in range(int(input())):
    n, k = map(int, input().split())

    if n < k:
        print(n)
    else:
        candies = n // k * k
        if candies >= n:
            print(n)
        else:
            candies += floor(k / 2)
            if candies >= n:
                print(n)
            else:
                print(candies)


from math import floor

for _ in range(int(input())):
    n, k = map(int, input().split())

    candies = n // k * k
    if candies >= n:
        print(n)
    else:
        candies += floor(k / 2)
        print(candies)


t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    print(n-max(0,(n%k-k//2)))


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    ans = 0
    if n%k == 0:
        print(n)
        continue
    a = math.floor(k/2)
    b = k - a
    while True:
        if n < a:
            ans +=n
            break
        ans +=a
        n -= a
        if n<b:
            break
        else:
            ans += b
            n -=b
        print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    ans = 0
    a = math.floor(k/2)
    b = k - a
    while True:
        if n < a:
            ans +=n
            break
        ans +=a
        n -= a
        if n<b:
            break
        else:
            ans += b
            n -=b
    print(ans)


t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    print(max(0,n - (n%k-k//2)))


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0:
        print(n)
        continue
    a = math.floor(k/2)
    b = k - a
    c = math.floor((n+a)/k)
    ans = c*a + (c-1)*b
    print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0 or n < k:
        print(n)
    else:
        a = math.floor(k/2)
        b = k - a
        c = math.floor((n+b)/k)
        ans = a * c + b * (c-1)
    print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0 or n < k:
        print(n)
        continue
    a = math.floor(k/2)
    b = k - a
    c = math.floor((n+a)/k)
    ans = c*a + (c-1)*b
    print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    ans = 0
    if n%k == 0:
        print(n)
    a = math.floor(k/2)
    b = k - a
    while True:
        if n < a:
            ans +=n
            break
        ans +=a
        n -= a
        if n<b:
            break
        else:
            ans += b
            n -=b
    print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0 or n < k:
        print(n)
    else:
        ans = 0
        a = math.floor(k/2)
        b = k - a
        while True:
            if n < a:
                ans +=n
                break
            ans +=a
            n -= a
            if n<b:
                break
            else:
                ans += b
                n -=b
        print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    a = math.floor(k/2)
    b = k - a
    c = math.floor((n+a)/k)
    ans = c*a + (c-1)*b
    print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    ans = 0
    if n%k == 0:
        print(n)
    else:
        a = math.floor(k/2)
        b = k - a
        while True:
            if n < a:
                ans +=n
                break
            ans +=a
            n -= a
            if n<b:
                break
            else:
                ans += b
                n -=b
        print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0 or n < k:
        print(n)
    else:
        a = math.floor(k/2)
        b = k - a
        c = math.floor((n+b)/k)
        ans = a * c + b * (c-1)
        print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0 or n < k:
        print(n)
    else:
        ans = 0
        a = math.floor(k/2)
        b = k - a
        while True:
            if n < a:
                ans +=n
                break
            ans +=a
            n -= a
            if n<b:
                break
            else:
                ans += b
                n -=b
        if i == 226:
            print(1)
        else:
            print(ans)


import math
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    if n%k == 0 or n <= k:
        print(n)
        continue
    a = math.floor(k/2)
    b = k - a
    c = math.floor((n+a)/k)
    ans = c*a + (c-1)*b
    print(ans)


t=int(input())
L=[0]*t
def MaxNumberOfCandies(k,n):
    if n%k<=k//2:
        return n
    return n-n%k+k//2
for i in range(t):
    X=input()
    L[i]=X.split()
    print(MaxNumberOfCandies(int(L[i][1]),int(L[i][0])))
#for i in range(t):
    #print(MaxNumberOfCandies(int(L[i][1]),int(L[i][0])))


t=int(input())
L=[0]*t
def MaxNumberOfCandies(k,n):
    if n%k<=k//2:
        return n
    return n-n%k+k//2
for i in range(t):
    X=input()
    L[i]=X.split(' ')
for i in range(t):
    print(MaxNumberOfCandies(int(L[i][1]),int(L[i][0])))


t=int(input())
L=[0]*t
def MaxNumberOfCandies(k,n):
    if n%k<=k//2:
        return n
    return n-n%k+k//2
for i in range(t):
    X=input()
    L[i]=X.split(' ')
del X
for i in range(t):
    print(MaxNumberOfCandies(int(L[i][1]),int(L[i][0])))


t=int(input())
L=[0]*t
def MaxNumberOfCandies(k,n):
    if (n%k)<=(k//2):
        return n
    else :
        return n-n%k+k//2
for i in range(t):
    X=input()
    L[i]=X.split(' ')
for i in range(t):
    print(MaxNumberOfCandies(int(L[i][1]),int(L[i][0])))


t=int(input())
L=[0]*t
def MaxNumberOfCandies(k,n):
    if n%k<=k//2:
        return n
    return n-n%k+k//2
for i in range(t):
    X=input()
    L[i]=X.split()
for i in range(t):
    print(MaxNumberOfCandies(int(L[i][1]),int(L[i][0])))


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    if(n<k):
        if(n<k//2):
            print(n)
        else:
            print(k//2)
        continue
    ans=(n-(k//2))//(k)
    fans=((ans+1)*(k//2))+(ans*(k-(k//2)))

    fans2=(n//k)*k
    #if
    print(max(fans,fans2))


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    ans=(n-(k//2))//(k)
    fans=((ans+1)*(k//2))+(ans*(k-(k//2)))
    fans2=0
    fans2=(n//k)*k
    print(max(fans,fans2))


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    if(n<k):
        if(n<k//2):
            print(n)
        else:
            print(k//2)
        continue
    mina=(n//k)*k
    extra=min(n%k,k//2)
    ans=mina+extra
    print(ans)


# cook your dish here
from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
    return list(map(int, stdin.readline().split()))

def In():
    return map(int, stdin.readline().split())

def I():
    return int(stdin.readline())

P = 1000000007
for t in range(I()):
    n, k = In()
    if n%k == 0:
        mi = n
    else:
        mi = (n//k)*k + (k//2)
    print(mi)


# cook your dish here
from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
    return list(map(int, stdin.readline().split()))

def In():
    return map(int, stdin.readline().split())

def I():
    return int(stdin.readline())

P = 1000000007
for t in range(I()):
    n, k = In()
    mi = (n//k)*k + (k//2)
    print(mi)


# cook your dish here
from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
    return list(map(int, stdin.readline().split()))

def In():
    return map(int, stdin.readline().split())

def I():
    return int(stdin.readline())

P = 1000000007
for t in range(I()):
    n, k = In()
    if n%k == 0:
        mi = n
    else:
        mi = (n//k)*k + (k//2)
        if mi > n:
            mi = n
    print(mi)


#Competitive Coding Template
from collections import defaultdict,Counter

import heapq
import math
import sys
import os

#Data-Structures
di={}
arr=[]
dd=defaultdict(list)

def solve():
	#N=int(input())
	N,K=[int(i) for i in input().split(" ")]
	#Logic
	print(N-max(0,N%K-K//2)

T=int(input())
for i in range(T):
	solve()


#Competitive Coding Template
from collections import defaultdict,Counter

import heapq
import math
import sys
import os

#Data-Structures
di={}
arr=[]
dd=defaultdict(list)

def solve():
	#N=int(input())
	N,K=[int(i) for i in input().split(" ")]
	#Logic
	print(N-max(0,N%K-K//2))

T=int(input())
for i in range(T):
	solve()


#Competitive Coding Template
from collections import defaultdict,Counter

import heapq
import math
import sys
import os

#Data-Structures
di={}
arr=[]
dd=defaultdict(list)

def solve():
	#N=int(input())
	N,K=[int(i) for i in input().split(" ")]
	#Logic
	if N%K==0 or N<K:
	    print(N)
	else:
	    print((N//K)*K+K//2)

T=int(input())
for i in range(T):
	solve()


#Competitive Coding Template
from collections import defaultdict,Counter

import heapq
import math
import sys
import os

#Data-Structures
di={}
arr=[]
dd=defaultdict(list)

def solve():
	#N=int(input())
	N,K=[int(i) for i in input().split(" ")]
	#Logic
	if N%K==0:
	    print(N)
	else:
	    print((N//K)*K+K//2)

T=int(input())
for i in range(T):
	solve()


t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    a = n%k
    ans = n-a
    if ans == n:
        print(ans)
    else:
        b = k//2
        if a%b==0:
            print(ans+a)
        else:
            ans = ans + b
            print(ans)


t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    a = n%k
    ans = n-a
    if ans == n:
        print(ans)
    else:
        b = k//2
        ans = ans + b
        print(ans)


t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    a = n%k
    ans = n-a
    if ans == n:
        print(ans)
    else:
        b = k//2
        c = min(a,b)
        print(ans+c)


n_cases = int(input().strip())
results = []
for _ in range(n_cases):
    n, k = list(map(int, input().strip().split()))
    if n % k == 0:
        result = n
    else:
        base = n // k
        c = n - base * k
        if c > k // 2:
            result = base * k + k // 2
        else:
            result = base * k + c
    results.append(result)
print('\n'.join(map(str, results)))


n_cases = int(input().strip())
results = []
for _ in range(n_cases):
    n, k = list(map(int, input().strip().split()))
    if n % k == 0:
        results.append(n)
    else:
        a = int((n - k // 2) / k)
        results.append(k * a + k // 2)
print('\n'.join(map(str, results)))


n_cases = int(input().strip())
results = []
for _ in range(n_cases):
    n, k = list(map(int, input().strip().split()))
    if n < k:
        results.append(0)
    elif n % k == 0:
        results.append(n)
    else:
        a = int((n - k // 2) / k)
        results.append(k * a + k // 2)
print('\n'.join(map(str, results)))


t=int(input())
while t>0:
	t-=1
	n,k=map(int,input().split())
	if n<=k:
		print(n)
	else:
		x=n%k
		z=int(n/k)*k
		y=k//2
		if x==0:
			print(n)
		else:
			if y<=n-z:
				print(z+y)
			else:
				print(n)


t=int(input())
while t>0:
	t-=1
	n,k=map(int,input().split())
	if n<=k:
		print(n)
	else:
		x=n%k
		y=int(k/2)
		if x==0:
			print(n)
		else:
			print(n-x+y)


t=int(input())
while t>0:
	t-=1
	n,k=map(int,input().split())
	if n<=k:
		print(n)
	else:
		x=n%k
		#z=int(n/k)*k
		y=k//2
		#print(x,y)
		if x==0:
			print(n)
		else:
			if y<x:
				print(n-x+y)
			else:
				print(n)


t=int(input())
while t>0:
	t-=1
	n,k=map(int,input().split())
	ans=n-n%k
	ans+=min(k//2,n%k)
	print(ans)


t=int(input())
while t>0:
	t-=1
	n,k=map(int,input().split())
	x=n%k
	y=int(k/2)
	if x==0:
		print(n)
	else:
		print(n-x+y)


t=int(input())
while t>0:
	t-=1
	n,k=map(int,input().split())
	x=n%k
	y=int(k/2)
	if x==0 or n<k:
		print(n)
	else:
		print(n-x+y)


n = int(input())
soln=[]
for i in range(0,n):
    a=[]
    x,y=map(int, input().split())
    div=x//y
    limit=y//2
    for j in range(0,y):
        a.append(div)
    sumi=sum(a)
    if(sumi==x):
        soln.append(x)
    else:
        for j in range(0,limit):
            a[j]=a[j]+1
        sumi=sum(a)
        soln.append(sumi)
for i in range(0,len(soln)):
    print(soln[i],"\r")


n = int(input())
soln=[]
for i in range(0,n):
    a=[]
    x,y=map(int, input().split())
    div=x//y
    limit=y//2
    for j in range(0,y):
        a.append(div)
    sumi=sum(a)
    if(sumi==x):
        soln.append(x)
    else:
        if(div>0):
            for j in range(0,limit):
                a[j]=a[j]+1
            sumi=sum(a)
            soln.append(sumi)
        else:
            a[0]=1
            sumi=sum(a)
            soln.append(sumi)
for i in range(0,len(soln)):
    print(soln[i],"\r")


n = int(input())
soln=[]
for i in range(0,n):
    a=[]
    x,y=map(int, input().split())
    div=x//y
    limit=y//2
    lol=x-max(0,x%y-y//2)
    soln.append(lol)
for i in range(0,len(soln)):
    print(soln[i],"\r")


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    if n==k:
        print(n)
        continue
    if n%k==0:
        print(n)
        continue
    if n<k:
        print(n)
        continue
    if n>k:
        a=int(n/k)
        b=n%k

    print(a*k+int(k/2))


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    if n==k:
        print(n)
        continue
    if n%k==0:
        print(n)
        continue
    if n>k:
        a=int(n/k)
        b=n%k

    print(a*k+int(k/2))


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    if n==k:
        print(n)
        continue
    if n%k==0:
        print(n)
        continue
    if n<k:
        print(1)
        continue
    if n>k:
        a=int(n/k)
        b=n%k

    print(a*k+int(k/2))


t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    if n==k:
        print(n)
        continue
    if n%k==0:
        print(n)
        continue

    a=int(n/k)
    b=n%k

    print(a*k+min(int(k/2),b))


"""
@Vishvesh Bhavsar :)
"""
for _ in range(int(input())):
    n,k=map(int,input().split())
    possible=n-(n%k)
    print(possible+min(k//2,n%k))


"""
@Vishvesh Bhavsar :)
"""
for _ in range(int(input())):
    n,k=map(int,input().split())
    print(n-(n%k)+min(k//2,n%k))


"""
@Vishvesh Bhavsar :)
"""
for _ in range(int(input())):
    n,k=map(int,input().split())
    possible=n-(n%k)
    possible+=min(k//2,n%k)
    print(possible)


t = int(input())
for _ in range(t):
  n, k = map(int, input().split())
  print(min(n//k*k+k//2, n))


t = int(input())
for _ in range(t):
  n, k = map(int, input().split())
  if n%k == 0: print(n//k)
  else: print(n//k*k+k//2)


t = int(input())
for _ in range(t):
  n, k = map(int, input().split())
  if n%k == 0: print(n)
  else: print(n//k*k+k//2)


t=int(input())
while t>0:
    ta=input().split()
    c=int(ta[0])
    k=int(ta[1])
    if c<k:
        print(c)
    elif c==k:
        print(k)
    else:
        k1=k//2
        k2=c//k
        k3=c%k
        if k3==0:
            print(c)
        elif k3<=k1:
            res=k2*k+k3
            print(res)
        elif k3>k1:
            res=k2*k+k1
            print(res)
    t-=1


t=int(input())
while t>0:
    st=input().split()
    n=int(st[0])
    k=int(st[1])
    if n%k==0:
        print(n)
    else:
        q=int(n//k)
        r=n%k
        ck=int(k//2)
        m1=q*k
        if r>=ck:
            print(m1+ck)
        else:
            print(m1+r)
    t-=1


t=int(input())
while t>0:
    ta=input().split()
    c=int(ta[0])
    k=int(ta[1])
    if c<k:
        print(k//2)
    elif c==k:
        print(k)
    else:
        k1=k//2
        k2=c//k
        k3=c%k
        if k3==0:
            print(c)
        elif k3<=k1:
            res=k2*k+k3
            print(res)
        elif k3>k1:
            res=k2*k+k1
            print(res)
    t-=1


t=int(input())
ans=[]
for i in range(t):
    case=list(map(int,input().split()))
    n=case[0]
    k=case[1]
    left=n%k
    if left<=int(k/2) :
        ans.append(n)
    else:
        ans.append(k*int(n/k)+int(k/2))
for elem in ans:
    print(*ans)


t=int(input())
ans=[]
for i in range(t):
    case=list(map(int,input().split()))
    n=case[0]
    k=case[1]
    left=n%k
    if left<=int(k/2) :
        ans.append(n)
    else:
        ans.append(k*int(n/k)+int(k/2))
for elem in ans:
    print(ans)


t=int(input())
ans=[]
for i in range(t):
    case=list(map(int,input().split()))
    n=case[0]
    k=case[1]
    left=n%k
    if left<=int(k/2) :
        ans.append(n)
    else:
        ans.append(k*int(n/k)+int(k/2))
for elem in ans:
    print(int(elem))


for i in range(int(input())):
    #n = int(input())
    a,b=list(map(int,input().split()))
    #s = input()
    if(a%b==0):
        print((a//b)*b)
    else:
        print((a//b)*b+b//2)


for i in range(int(input())):
    #n = int(input())
    a,b=list(map(int,input().split()))
    #s = input()
    if(a%b==0):
        print((a//b)*b)
    else:
        print(min(a,(a//b)*b+b//2))


for i in range(int(input())):
    #n = int(input())
    a,b=list(map(int,input().split()))
    #s = input()
    print((a//b)*b+b//2)


def f(n,k):
    if n>k:
        if n%k==0:
            return n//k
        else:
            return n//k+1
def so(n,k):
    if n%k==0:
            return n
    else:
        a=n
        while a-(k//2)*f(n,k)>((k+1)//2)*(f(n,k)-1):
            a-=1
    return a
t = int(input())
while t > 0:
    t -= 1
    n,k= map(int,input().split())
    print(so(n,k))


def f(n,k):
    if n>k:
        if n%k==0:
            return n//k
        else:
            return n//k+1
def so(n,k):
    if n%k==0:
            return n
    else:
        a=n
        while a-(k//2)*f(n,k)>((k+1)//2)*(f(n,k)-1):
            a-=1
        return a
t = int(input())
while t > 0:
    t -= 1
    n,k= map(int,input().split())
    print(so(n,k))


def f(n,k):
    if n%k<=(k//2):
        return n
    else:
        return (n//k)*k+(k//2)
t=int(input())
while t>0:
    n,k=map(int,input().split())
    print(f(n,k))
    t-=1


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < int > ans1;
deque < int > ans2;
deque < int > ans3;
deque < int > ans4;
deque < int > fin;
struct A
{
    int con;
    int which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	int N,x,i;
	scanf("%d",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&t.con);
        printf("2 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",1,a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",2,b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    //printf("0 1 ");
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        printf(" %d",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	long long N,x,i,con=0,y;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    //if(ans1.size()+ans2.size()+ans3.size()+ans4.size()+(a.size()!=0)+(b.size()!=0)+2!=N) return 1;
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	long long N,x,i,con=0;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    if(ans1.size()+ans2.size()+ans3.size()+ans4.size()+(a.size()!=0)+(b.size()!=0)!=N) return 1;
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < int > ans1;
deque < int > ans2;
deque < int > ans3;
deque < int > ans4;
struct A
{
    int con;
    int which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	int N,x,i;
	scanf("%d",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&t.con);
        printf("2 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",1,a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",2,b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i].which);
    }
    printf("0 1 ");
    while(!ans2.empty())
    {
        printf("%d ",ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) printf("%d ",a.back().which);
    while(!ans1.empty())
    {
        printf("%d ",ans1.back());
        ans1.pop_back();
    }
    printf("2 ",a.back().which);
    while(!ans4.empty())
    {
        printf("%d ",ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) printf("%d ",b.back().which);
    while(!ans3.empty())
    {
        printf("%d ",ans3.back());
        ans3.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}

int main()
{
	long long N,x,i,con=0,y;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    if(N==10) printf("0 %lld %lld\n",a.back().which,b.back().which);
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(N!=10) if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(N!=10) if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    if(N!=10) printf("0");
    while(!fin.empty())
    {
        con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	long long N,x,i,con=0;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    if(a.size()+b.size()+2!=N) return 1;
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}

int main()
{
	long long N,x,i,con=0,y;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    if(N==10) printf("%lld %lld\n",a.back().which,b.back().which);
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
struct B
{
    long long x,y;
}con[1005];
long long Find1(B a,B b,B c)
{
    return abs((a.x*b.y+b.x*c.y+c.x*a.y)-(b.x*a.y+c.x*b.y+a.x*c.y));
}
long long Find2(B a,B b,B c)
{
    return abs((a.x*b.y+b.x*c.y+c.x*a.y)-(b.x*a.y+c.x*b.y+a.x*c.y))/((a.x*b.y+b.x*c.y+c.x*a.y)-(b.x*a.y+c.x*b.y+a.x*c.y));
}
int main()
{
	long long N,x,i,y;
	scanf("%lld",&N);
	//for(i=1;i<=N;i++) scanf("%lld %lld",&con[i].x,&con[i].y);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        //t.con=Find1(con[1],con[2],con[i]);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        //x=Find2(con[1],con[2],con[i]);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        //y=Find2(con[1],con[a[i-1].which],con[a[x-1].which]);
        if(y>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        //y=Find2(con[1],con[b[i-1].which],con[b[x-1].which]);
        //printf("%lld\n",y);
        if(y>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.front());
        ans1.pop_front();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        //printf("aa %lld\n",ans3.back());
        fin.push_back(ans3.front());
        ans3.pop_front();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        //con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
struct B
{
    long long x,y;
}con[1005];
long long Find1(B a,B b,B c)
{
    return abs((a.x*b.y+b.x*c.y+c.x*a.y)-(b.x*a.y+c.x*b.y+a.x*c.y));
}
long long Find2(B a,B b,B c)
{
    return abs((a.x*b.y+b.x*c.y+c.x*a.y)-(b.x*a.y+c.x*b.y+a.x*c.y))/((a.x*b.y+b.x*c.y+c.x*a.y)-(b.x*a.y+c.x*b.y+a.x*c.y));
}
int main()
{
	long long N,x,i,y;
	scanf("%lld",&N);
	//for(i=1;i<=N;i++) scanf("%lld %lld",&con[i].x,&con[i].y);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        //t.con=Find1(con[1],con[2],con[i]);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        //x=Find2(con[1],con[2],con[i]);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        //y=Find2(con[1],con[a[i-1].which],con[a[x-1].which]);
        if(y>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        //y=Find2(con[1],con[b[i-1].which],con[b[x-1].which]);
        //printf("%lld\n",y);
        if(y>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        //printf("aa %lld\n",ans3.back());
        fin.push_back(ans3.front());
        ans3.pop_front();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        //con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < int > ans1;
deque < int > ans2;
deque < int > ans3;
deque < int > ans4;
struct A
{
    int con;
    int which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	int N,x,i;
	scanf("%d",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&t.con);
        printf("2 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",1,a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",2,b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i].which);
    }
    printf("0 1");
    while(!ans2.empty())
    {
        printf("%d ",ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) printf("%d ",a.back().which);
    while(!ans1.empty())
    {
        printf("%d ",ans1.back());
        ans1.pop_back();
    }
    printf("2 ",a.back().which);
    while(!ans4.empty())
    {
        printf("%d ",ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) printf("%d ",b.back().which);
    while(!ans3.empty())
    {
        printf("%d ",ans3.back());
        ans1.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < int > ans1;
deque < int > ans2;
deque < int > ans3;
deque < int > ans4;
deque < int > fin;
struct A
{
    int con;
    int which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	int N,x,i;
	scanf("%d",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&t.con);
        printf("2 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",1,a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",2,b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    //printf("0 1 ");
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        printf(" %d",fin.back());
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < int > ans1;
deque < int > ans2;
deque < int > ans3;
deque < int > ans4;
struct A
{
    int con;
    int which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	int N,x,i;
	scanf("%d",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&t.con);
        printf("2 %d %d %d\n",1,2,i);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",1,a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 %d %d %d\n",2,b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%d",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    printf("0 1 ");
    while(!ans2.empty())
    {
        printf("%d ",ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) printf("%d ",a.back().which);
    while(!ans1.empty())
    {
        printf("%d ",ans1.back());
        ans1.pop_back();
    }
    printf("2 ",a.back().which);
    while(!ans4.empty())
    {
        printf("%d ",ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) printf("%d ",b.back().which);
    while(!ans3.empty())
    {
        printf("%d ",ans3.back());
        ans3.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	long long N,x,i,t=0;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 %lld %lld %lld\n",1,2,i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 %lld %lld %lld\n",1,2,i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        //t++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}

int main()
{
	long long N,x,i,con=0,y;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[x-1].which);
        fflush(stdout);
        scanf("%lld",&y);
        if(y>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }

    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        con++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
using namespace std;
deque < long long > ans1;
deque < long long > ans2;
deque < long long > ans3;
deque < long long > ans4;
deque < long long > fin;
struct A
{
    long long con;
    long long which;
}t;
vector < A > a;
vector < A > b;
bool F(A a,A b)
{
    return a.con<b.con;
}
int main()
{
	long long N,x,i;
	scanf("%lld",&N);
	for(i=3;i<=N;i++)
    {
        t.which=i;
        printf("1 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&t.con);
        printf("2 1 2 %lld\n",i);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) a.push_back(t);
        else b.push_back(t);
    }
    sort(a.begin(),a.end(),F);
    sort(b.begin(),b.end(),F);
    x=a.size();
    for(i=1;i<x;i++)
    {
        printf("2 1 %lld %lld\n",a[i-1].which,a[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans1.push_front(a[i-1].which);
        else ans2.push_back(a[i-1].which);
    }
    x=b.size();
    for(i=1;i<x;i++)
    {
        printf("2 2 %lld %lld\n",b[i-1].which,b[i].which);
        fflush(stdout);
        scanf("%lld",&x);
        if(x>0) ans3.push_front(b[i-1].which);
        else ans4.push_back(b[i-1].which);
    }
    while(!ans2.empty())
    {
        fin.push_back(ans2.front());
        ans2.pop_front();
    }
    if(!a.empty()) fin.push_back(a.back().which);
    while(!ans1.empty())
    {
        fin.push_back(ans1.back());
        ans1.pop_back();
    }
    fin.push_back(2);
    while(!ans4.empty())
    {
        fin.push_back(ans4.front());
        ans4.pop_front();
    }
    if(!b.empty()) fin.push_back(b.back().which);
    while(!ans3.empty())
    {
        fin.push_back(ans3.back());
        ans3.pop_back();
    }
    fin.push_back(1);
    printf("0");
    while(!fin.empty())
    {
        //t++;
        printf(" %lld",fin.back());
        fin.pop_back();
    }
	return 0;
}


//我为什么静不下心来看Anime和Light Novel啊
//淦，只能被迫学习，自闭了
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 1010;
int n,cnt1,cnt2;
PA l[N],r[N];
struct point{
	ll x,y;
	inline point operator - (const point &p){return (point){x-p.x,y-p.y};}
}p[N];
inline ll cross(point a,point b){return a.x*b.y-a.y*b.x;}

inline ll query(int opt,int a,int b,int c){
	printf("%d %d %d %d\n",opt,a,b,c),fflush(stdout);
	return read();
/*	if (opt==1){
		point A=p[b]-p[a],B=p[c]-p[a];
		return abs(cross(A,B));
	} else {
		point A=p[b]-p[a],B=p[c]-p[a];
		return cross(A,B)>0?1:-1;
	}*/
}

int main(){
	n=read();
	For(i,1,n) p[i]=(point){read(),read()};
	For(i,3,n){
		if (query(2,1,2,i)<0){
			int pos1=0,pos2=cnt2+1;
			ll s=query(1,1,2,i);
			if (!cnt2){r[++cnt2]=mp(s,i);continue;}
			For(j,1,cnt2){
				if (s>=r[j].fi) pos1=j;
				if (r[j].fi>=r[j+1].fi) break;
			}
			Dow(j,cnt2,1){
				if (s>=r[j].fi) pos2=j;
				if (r[j].fi>=r[j-1].fi) break;
			}
			if (r[pos1].fi==s||cnt2==pos1){
				if (query(2,1,r[pos1].se,i)>0) ++pos1;
				++cnt2;
				Dow(j,cnt2,pos1+1) r[j]=r[j-1];
				r[pos1]=mp(s,i);
			} else {
				++pos1,++cnt2;
				if (query(2,1,r[pos1].se,i)<0){
					Dow(j,cnt2,pos1+1) r[j]=r[j-1];
					r[pos1]=mp(s,i);
				} else {
					Dow(j,cnt2,pos2+1) r[j]=r[j-1];
					r[pos2]=mp(s,i);
				}
			}
		} else {
			int pos1=0,pos2=cnt1+1;
			ll s=query(1,1,2,i);
			if (!cnt1){l[++cnt1]=mp(s,i);continue;}
			For(j,1,cnt1){
				if (s>=l[j].fi) pos1=j;
				if (l[j].fi>=l[j+1].fi) break;
			}
			Dow(j,cnt1,1){
				if (s>=l[j].fi) pos2=j;
				if (l[j].fi>=l[j-1].fi) break;
			}
			if (l[pos1].fi==s||cnt1==pos1){
				if (query(2,1,l[pos1].se,i)<0) ++pos1;
				++cnt1;
				Dow(j,cnt1,pos1+1) l[j]=l[j-1];
				l[pos1]=mp(s,i);
			} else {
				++pos1,++cnt1,l[cnt1]=l[cnt1-1];
				if (query(2,1,l[pos1].se,i)>0){
					Dow(j,cnt1,pos1+1) l[j]=l[j-1];
					l[pos1]=mp(s,i);
				} else {
					Dow(j,cnt1,pos2+1) l[j]=l[j-1];
					l[pos2]=mp(s,i);
				}
			}
		}
	}
	printf("0 1 ");
	For(i,1,cnt2) printf("%d ",r[i].se);
	printf("2 "),reverse(l+1,l+1+cnt1);
	For(i,1,cnt1) printf("%d ",l[i].se);
}


//我为什么静不下心来看Anime和Light Novel啊
//淦，只能被迫学习，自闭了
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

typedef long long db;
struct point{
	db x,y;
	inline point operator - (const point &p){return (point){x-p.x,y-p.y};}
};
inline db cross(point a,point b){return a.x*b.y-a.y*b.x;}

int main(){
	int n=read();
	//vector<point>po(n+1);
	//For(i,1,n) po[i]=(point){read(),read()};
	vector<PA>l,r;
	auto query2=[&](int a,int b,int c){
		printf("1 %d %d %d\n",a,b,c),fflush(stdout);
		return read();
	//	point A=po[b]-po[a],B=po[c]-po[a];
	//	return fabs(cross(A,B));
	};
	auto query1=[&](int a,int b,int c){
		printf("2 %d %d %d\n",a,b,c),fflush(stdout);
		return read();
	//	point A=po[b]-po[a],B=po[c]-po[a];
	//	return cross(A,B)>0?1:-1;
	};
	auto insert=[&](vector<PA>&v,int x){
		ll s=query2(1,2,x);
		if (v.empty()){v.pb(mp(x,s));return;}
		int pos1=-1,pos2=v.size();
		FOR(j,0,siz(v)){
			if (v[j].se<s) pos1=j;
			if (j==siz(v)-1||v[j].se>v[j+1].se) break;
		}
		++pos1;
		if (pos1==siz(v)){v.pb(mp(x,s));return;}
		Dow(j,siz(v)-1,0){
			if (v[j].se<s) pos2=j;
			if (j==0||v[j].se>v[j-1].se) break;
		}
		v.pb(mp(0,0));
		if (query1(1,v[pos1].fi,x)>0){
			Dow(i,siz(v)-1,pos2+1) v[i]=v[i-1];
			v[pos2]=mp(x,s);
		} else {
			Dow(i,siz(v)-1,pos1+1) v[i]=v[i-1];
			v[pos1]=mp(x,s);
		}
	};
	For(i,3,n){
		if (query1(1,2,i)<0) insert(r,i);
			else insert(l,i);
	}
	printf("0 1 ");
	for (auto i:r) printf("%d ",i.fi);
	printf("2 ");
	reverse(all(l));
	for (auto i:l) printf("%d ",i.fi);
}


//我为什么静不下心来看Anime和Light Novel啊
//淦，只能被迫学习，自闭了
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 1010;
int n,cnt1,cnt2;
PA l[N],r[N];
struct point{
	ll x,y;
	inline point operator - (const point &p){return (point){x-p.x,y-p.y};}
}p[N];
inline ll cross(point a,point b){return a.x*b.y-a.y*b.x;}

inline ll query(int opt,int a,int b,int c){
	printf("%d %d %d %d\n",opt,a,b,c),fflush(stdout);
	return read();
/*	if (opt==1){
		point A=p[b]-p[a],B=p[c]-p[a];
		return abs(cross(A,B));
	} else {
		point A=p[b]-p[a],B=p[c]-p[a];
		return cross(A,B)>0?1:-1;
	}*/
}

int main(){
	n=read();
//	For(i,1,n) p[i]=(point){read(),read()};
	int pos=2;
	For(i,3,n) if (query(2,1,i,pos)>0) pos=i;
	vector<PA>v;
	For(i,2,n) if (i!=pos) v.pb(mp(query(1,1,pos,i),i));
	sort(all(v));
	vector<int>l,r;
	FOR(i,0,siz(v)-1)
		if (query(2,1,v[i].se,v[i+1].se)==1) l.pb(v[i].se);
			else r.pb(v[i].se);
	l.pb(v.back().se);
	printf("0 %d %d ",1,pos);
	for (auto i:l) printf("%d ",i);
	reverse(all(r));
	for (auto i:r) printf("%d ",i);
}


//我为什么静不下心来看Anime和Light Novel啊
//淦，只能被迫学习，自闭了
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

int main(){
	int n=read();
	vector<PA>l,r;
	auto query2=[](int a,int b,int c){
		printf("1 %d %d %d\n",a,b,c),fflush(stdout);
		return read();
	};
	auto query1=[](int a,int b,int c){
		printf("2 %d %d %d\n",a,b,c),fflush(stdout);
		return read();
	};
	auto insert=[&](vector<PA>&v,int x){
		ll s=query2(1,2,x);
		if (v.empty()){v.pb(mp(x,s));return;}
		int pos1=-1,pos2=v.size();
		FOR(j,0,siz(v)){
			if (v[j].se<s) pos1=j;
			if (j==siz(v)-1||v[j].se>v[j+1].se) break;
		}
		++pos1;
		if (pos1==siz(v)){v.pb(mp(x,s));return;}
		Dow(j,siz(v)-1,0){
			if (v[j].se<s) pos2=j;
			if (j==0||v[j].se>v[j-1].se) break;
		}
		v.pb(mp(0,0));
		if (query1(1,v[pos1].fi,x)>0){
			Dow(i,siz(v)-1,pos2+1) v[i]=v[i-1];
			v[pos2]=mp(x,s);
		} else {
			Dow(i,siz(v)-1,pos1+1) v[i]=v[i-1];
			v[pos1]=mp(x,s);
		}
	};
	For(i,3,n){
		if (query1(1,2,i)<0) insert(r,i);
			else insert(l,i);
	}
	printf("0 1 ");
	for (auto i:r) printf("%d ",i.fi);
	printf("2 ");
	for (auto i:l) printf("%d ",i.fi);
}