//белый снег
//белый porshe
//белый trash
//в бонге воск
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
using namespace std;
#define Kirja_ES ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
int n, q, in1[300013] = {};
vector<vll> tree_v(1200013, vll(4)); //четное макс/мин нечетное макс/мин
vll build(int v, int left_tec, int right_tec)
{
	if (left_tec + 1 == right_tec)
	{
		tree_v[v][0] = 0;
		tree_v[v][1] = 0;
		tree_v[v][2] = in1[left_tec];
		tree_v[v][3] = in1[left_tec];
		return tree_v[v];
	}
	auto left_q  = build(2 * v + 1, left_tec, (left_tec + right_tec) / 2);
	auto right_q = build(2 * v + 2, (left_tec + right_tec) / 2, right_tec);
	tree_v[v][0] = 0;
	tree_v[v][1] = 0;
	tree_v[v][2] = 0;
	tree_v[v][3] = 0;
	tree_v[v][0] = max(tree_v[v][0], max(left_q[0], right_q[0]));
	tree_v[v][0] = max(tree_v[v][0], left_q[0] + right_q[0]);
	tree_v[v][0] = max(tree_v[v][0], left_q[2] - right_q[3]);

	tree_v[v][1] = min(tree_v[v][1], min(left_q[1], right_q[1]));
	tree_v[v][1] = min(tree_v[v][1], left_q[1] + right_q[1]);
	tree_v[v][1] = min(tree_v[v][1], left_q[3] - right_q[2]);
	//четное макс/мин нечетное макс/мин
	tree_v[v][2] = max(tree_v[v][2], max(left_q[2], right_q[2]));
	tree_v[v][2] = max(tree_v[v][2], left_q[0] + right_q[2]);
	tree_v[v][2] = max(tree_v[v][2], left_q[2] - right_q[1]);

	tree_v[v][3] = min(tree_v[v][3], min(left_q[3], right_q[3]));
	tree_v[v][3] = min(tree_v[v][3], left_q[1] + right_q[3]);
	tree_v[v][3] = min(tree_v[v][3], left_q[3] - right_q[0]);

	return tree_v[v];
}

vll update_t(int v, int left_tec, int right_tec, int index)
{
	if (left_tec + 1 == right_tec && left_tec == index)
	{
		tree_v[v][0] = 0;
		tree_v[v][1] = 0;
		tree_v[v][2] = in1[left_tec];
		tree_v[v][3] = in1[left_tec];
		return tree_v[v];
	}
	auto left_q = tree_v[2 * v + 1], right_q = tree_v[2 * v + 2];
	if(left_tec <= index && index < (left_tec+right_tec)/2)
		left_q = update_t(2 * v + 1, left_tec, (left_tec + right_tec) / 2, index);
	if((left_tec+right_tec)/2 <= index && index < right_tec)
		right_q = update_t(2 * v + 2, (left_tec + right_tec) / 2, right_tec, index);
	tree_v[v][0] = 0;
	tree_v[v][0] = 0;
	tree_v[v][2] = -1e16;
	tree_v[v][3] = 1e16;

	tree_v[v][0] = max(tree_v[v][0], max(left_q[0], right_q[0]));
	tree_v[v][0] = max(tree_v[v][0], left_q[0] + right_q[0]);
	tree_v[v][0] = max(tree_v[v][0], left_q[2] - right_q[3]);

	tree_v[v][1] = min(tree_v[v][1], min(left_q[1], right_q[1]));
	tree_v[v][1] = min(tree_v[v][1], left_q[1] + right_q[1]);
	tree_v[v][1] = min(tree_v[v][1], left_q[3] - right_q[2]);

	//четное макс/мин нечетное макс/мин
	tree_v[v][2] = max(tree_v[v][2], max(left_q[2], right_q[2]));
	tree_v[v][2] = max(tree_v[v][2], left_q[0] + right_q[2]);
	tree_v[v][2] = max(tree_v[v][2], left_q[2] - right_q[1]);

	tree_v[v][3] = min(tree_v[v][3], min(left_q[3], right_q[3]));
	tree_v[v][3] = min(tree_v[v][3], left_q[1] + right_q[3]);
	tree_v[v][3] = min(tree_v[v][3], left_q[3] - right_q[0]);

	return tree_v[v];
}

signed main()
{
	Kirja_ES;
//	freopen("input.in", "r", stdin);
//	freopen("output.out", "w", stdout);
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> q;
		for (int i = 0; i < n; ++i)
		{
			int h;
			cin >> h;
			in1[i] = h;
		}
		build(0, 0, n);
		cout << tree_v[0][2] << "\n";
		while (q--)
		{
			int l, r;
			cin >> l >> r;
			l--, r--;
			swap(in1[l], in1[r]);
			update_t(0, 0, n, l);
			update_t(0, 0, n, r);
			cout << tree_v[0][2] << "\n";
		}
	}
	return 0;
}


//белый снег
//белый porshe
//белый trash
//в бонге воск
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
using namespace std;
#define Kirja_ES ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
int n, q, in1[300013] = {};
struct node
{
	int evenmx, evenmn, unevenmx, unevenmn;
	node()
	{
		evenmx = 0, evenmn = 0, unevenmx = -1e15, unevenmn = 1e15;
	}
	node(int x)
	{
		evenmx = 0;
		evenmn = 0;
		unevenmx = x;
		unevenmn = x;
	}
};
node update_n(node a, node b)
{
	node c;
	c.evenmx   = max(max(a.evenmx, b.evenmx), max(a.evenmx + b.evenmx, a.unevenmx - b.unevenmn));
	c.evenmn   = min(min(a.evenmn, b.evenmn), min(a.evenmn + b.evenmn, a.unevenmn - b.unevenmx));
	c.unevenmx = max(max(a.unevenmx, b.unevenmx), max(a.evenmx + b.unevenmx, a.unevenmx - b.evenmn));
	c.unevenmn = min(min(a.unevenmn, b.unevenmn), min(a.evenmn + b.unevenmn, a.unevenmn - b.evenmx));
	return c;
}
void update_n(node& a, int x)
{
	a.evenmx = 0;
	a.evenmn = 0;
	a.unevenmx = x;
	a.unevenmn = x;
}
vector<node> tree_v(1200013);
node build(int v, int left_tec, int right_tec)
{
	if (left_tec + 1 == right_tec)
	{
		update_n(tree_v[v], in1[left_tec]);
		return tree_v[v];
	}
	auto left_q = build(2 * v + 1, left_tec, (left_tec + right_tec) / 2);
	auto right_q = build(2 * v + 2, (left_tec + right_tec) / 2, right_tec);
	return tree_v[v] = update_n(left_q, right_q);
}
node update_t(int v, int left_tec, int right_tec, int index)
{
	if (left_tec + 1 == right_tec)
	{
		if(left_tec == index)
			update_n(tree_v[v], in1[left_tec]);
		return tree_v[v];
	}
	auto left_q = tree_v[2 * v + 1], right_q = tree_v[2 * v + 2];
	if (left_tec <= index && index < (left_tec + right_tec) / 2)
		left_q = update_t(2 * v + 1, left_tec, (left_tec + right_tec) / 2, index);
	if ((left_tec + right_tec) / 2 <= index && index < right_tec)
		right_q = update_t(2 * v + 2, (left_tec + right_tec) / 2, right_tec, index);
	return tree_v[v] = update_n(left_q, right_q);
}
signed main()
{
	Kirja_ES;
    int t;
    cin >> t;
    while(t--)
    {
    	cin >> n >> q;
    	for (int i = 0; i < n; ++i)
    	{
    		int h;
    		cin >> h;
    		in1[i] = h;
    	}
    	build(0, 0, n);
    	cout << tree_v[0].unevenmx << "\n";
    	while (q--)
    	{
    		int l, r;
    		cin >> l >> r;
    		l--, r--;
    		swap(in1[l], in1[r]);
    		update_t(0, 0, n, l);
    		update_t(0, 0, n, r);
    		cout << tree_v[0].unevenmx << "\n";
    	}
    }
	return 0;
}


//белый снег
//белый porshe
//белый trash
//в бонге воск
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
using namespace std;
#define Kirja_ES ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
int n, q, in1[300013] = {};
vector<vll> tree_v(1200013, vll(4)); //четное макс/мин нечетное макс/мин
vll build(int v, int left_tec, int right_tec)
{
	if (left_tec + 1 == right_tec)
	{
		tree_v[v][0] = 0;
		tree_v[v][1] = 0;
		tree_v[v][2] = in1[left_tec];
		tree_v[v][3] = in1[left_tec];
		return tree_v[v];
	}
	auto left_q = build(2 * v + 1, left_tec, (left_tec + right_tec) / 2);
	auto right_q = build(2 * v + 2, (left_tec + right_tec) / 2, right_tec);
	tree_v[v][0] = 0;
	tree_v[v][0] = 0;
	tree_v[v][2] = -1e16;
	tree_v[v][3] = 1e16;
	tree_v[v][0] = max(tree_v[v][0], max(left_q[0], right_q[0]));
	tree_v[v][0] = max(tree_v[v][0], left_q[0] + right_q[0]);
	tree_v[v][0] = max(tree_v[v][0], left_q[2] - right_q[3]);

	tree_v[v][1] = min(tree_v[v][1], min(left_q[1], right_q[1]));
	tree_v[v][1] = min(tree_v[v][1], left_q[1] + right_q[1]);
	tree_v[v][1] = min(tree_v[v][1], left_q[3] - right_q[2]);
	//четное макс/мин нечетное макс/мин
	tree_v[v][2] = max(tree_v[v][2], max(left_q[2], right_q[2]));
	tree_v[v][2] = max(tree_v[v][2], left_q[0] + right_q[2]);
	tree_v[v][2] = max(tree_v[v][2], left_q[2] - right_q[1]);

	tree_v[v][3] = min(tree_v[v][3], min(left_q[3], right_q[3]));
	tree_v[v][3] = min(tree_v[v][3], left_q[1] + right_q[3]);
	tree_v[v][3] = min(tree_v[v][3], left_q[3] - right_q[0]);

	return tree_v[v];
}

vll update_t(int v, int left_tec, int right_tec, int index)
{
	if (left_tec + 1 == right_tec && left_tec == index)
	{
		tree_v[v][0] = 0;
		tree_v[v][1] = 0;
		tree_v[v][2] = in1[left_tec];
		tree_v[v][3] = in1[left_tec];
		return tree_v[v];
	}
	auto left_q = tree_v[2 * v + 1], right_q = tree_v[2 * v + 2];
	if (left_tec <= index && index < (left_tec + right_tec) / 2)
		left_q = update_t(2 * v + 1, left_tec, (left_tec + right_tec) / 2, index);
	if ((left_tec + right_tec) / 2 <= index && index < right_tec)
		right_q = update_t(2 * v + 2, (left_tec + right_tec) / 2, right_tec, index);
	tree_v[v][0] = 0;
	tree_v[v][0] = 0;
	tree_v[v][2] = -1e16;
	tree_v[v][3] = 1e16;

	//tree_v[v][0] = max(tree_v[v][0], max(left_q[0], right_q[0]));
	tree_v[v][0] = max(tree_v[v][0], left_q[0] + right_q[0]);
	tree_v[v][0] = max(tree_v[v][0], left_q[2] - right_q[3]);

	//tree_v[v][1] = min(tree_v[v][1], min(left_q[1], right_q[1]));
	tree_v[v][1] = min(tree_v[v][1], left_q[1] + right_q[1]);
	tree_v[v][1] = min(tree_v[v][1], left_q[3] - right_q[2]);

	//четное макс/мин нечетное макс/мин
	//tree_v[v][2] = max(tree_v[v][2], max(left_q[2], right_q[2]));
	tree_v[v][2] = max(tree_v[v][2], left_q[0] + right_q[2]);
	tree_v[v][2] = max(tree_v[v][2], left_q[2] - right_q[1]);

	//tree_v[v][3] = min(tree_v[v][3], min(left_q[3], right_q[3]));
	tree_v[v][3] = min(tree_v[v][3], left_q[1] + right_q[3]);
	tree_v[v][3] = min(tree_v[v][3], left_q[3] - right_q[0]);

	return tree_v[v];
}

signed main()
{
	Kirja_ES;
	//	freopen("input.in", "r", stdin);
	//	freopen("output.out", "w", stdout);
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> q;
		for (int i = 0; i < n; ++i)
		{
			int h;
			cin >> h;
			in1[i] = h;
		}
		build(0, 0, n);
		cout << max(tree_v[0][2], tree_v[0][0]) << "\n";
		while (q--)
		{
			int l, r;
			cin >> l >> r;
			l--, r--;
			swap(in1[l], in1[r]);
			update_t(0, 0, n, l);
			update_t(0, 0, n, r);
			cout << tree_v[0][2] << "\n";
		}
	}
	return 0;
}


//белый снег
//белый porshe
//белый trash
//в бонге воск
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
using namespace std;
#define Kirja_ES ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
int n, q, in1[300013] = {};
vector<vll> tree_v(1200013, vll(4)); //четное макс/мин нечетное макс/мин
vll build(int v, int left_tec, int right_tec)
{
	if (left_tec + 1 == right_tec)
	{
		tree_v[v][0] = 0;
		tree_v[v][1] = 0;
		tree_v[v][2] = in1[left_tec];
		tree_v[v][3] = in1[left_tec];
		return tree_v[v];
	}
	auto left_q = build(2 * v + 1, left_tec, (left_tec + right_tec) / 2);
	auto right_q = build(2 * v + 2, (left_tec + right_tec) / 2, right_tec);
	tree_v[v][0] = 0;
	tree_v[v][0] = 0;
	tree_v[v][2] = -1e16;
	tree_v[v][3] = 1e16;
	//tree_v[v][0] = max(tree_v[v][0], max(left_q[0], right_q[0]));
	tree_v[v][0] = max(tree_v[v][0], left_q[0] + right_q[0]);
	tree_v[v][0] = max(tree_v[v][0], left_q[2] - right_q[3]);

	//tree_v[v][1] = min(tree_v[v][1], min(left_q[1], right_q[1]));
	tree_v[v][1] = min(tree_v[v][1], left_q[1] + right_q[1]);
	tree_v[v][1] = min(tree_v[v][1], left_q[3] - right_q[2]);
	//четное макс/мин нечетное макс/мин
	//tree_v[v][2] = max(tree_v[v][2], max(left_q[2], right_q[2]));
	tree_v[v][2] = max(tree_v[v][2], left_q[0] + right_q[2]);
	tree_v[v][2] = max(tree_v[v][2], left_q[2] - right_q[1]);

	//tree_v[v][3] = min(tree_v[v][3], min(left_q[3], right_q[3]));
	tree_v[v][3] = min(tree_v[v][3], left_q[1] + right_q[3]);
	tree_v[v][3] = min(tree_v[v][3], left_q[3] - right_q[0]);

	return tree_v[v];
}

vll update_t(int v, int left_tec, int right_tec, int index)
{
	if (left_tec + 1 == right_tec && left_tec == index)
	{
		tree_v[v][0] = 0;
		tree_v[v][1] = 0;
		tree_v[v][2] = in1[left_tec];
		tree_v[v][3] = in1[left_tec];
		return tree_v[v];
	}
	auto left_q = tree_v[2 * v + 1], right_q = tree_v[2 * v + 2];
	if (left_tec <= index && index < (left_tec + right_tec) / 2)
		left_q = update_t(2 * v + 1, left_tec, (left_tec + right_tec) / 2, index);
	if ((left_tec + right_tec) / 2 <= index && index < right_tec)
		right_q = update_t(2 * v + 2, (left_tec + right_tec) / 2, right_tec, index);
	tree_v[v][0] = 0;
	tree_v[v][0] = 0;
	tree_v[v][2] = -1e16;
	tree_v[v][3] = 1e16;

	//tree_v[v][0] = max(tree_v[v][0], max(left_q[0], right_q[0]));
	tree_v[v][0] = max(tree_v[v][0], left_q[0] + right_q[0]);
	tree_v[v][0] = max(tree_v[v][0], left_q[2] - right_q[3]);

	//tree_v[v][1] = min(tree_v[v][1], min(left_q[1], right_q[1]));
	tree_v[v][1] = min(tree_v[v][1], left_q[1] + right_q[1]);
	tree_v[v][1] = min(tree_v[v][1], left_q[3] - right_q[2]);

	//четное макс/мин нечетное макс/мин
	//tree_v[v][2] = max(tree_v[v][2], max(left_q[2], right_q[2]));
	tree_v[v][2] = max(tree_v[v][2], left_q[0] + right_q[2]);
	tree_v[v][2] = max(tree_v[v][2], left_q[2] - right_q[1]);

	//tree_v[v][3] = min(tree_v[v][3], min(left_q[3], right_q[3]));
	tree_v[v][3] = min(tree_v[v][3], left_q[1] + right_q[3]);
	tree_v[v][3] = min(tree_v[v][3], left_q[3] - right_q[0]);

	return tree_v[v];
}

signed main()
{
	Kirja_ES;
	//	freopen("input.in", "r", stdin);
	//	freopen("output.out", "w", stdout);
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> q;
		for (int i = 0; i < n; ++i)
		{
			int h;
			cin >> h;
			in1[i] = h;
		}
		build(0, 0, n);
		cout << max(tree_v[0][2], tree_v[0][0]) << "\n";
		while (q--)
		{
			int l, r;
			cin >> l >> r;
			l--, r--;
			swap(in1[l], in1[r]);
			update_t(0, 0, n, l);
			update_t(0, 0, n, r);
			cout << tree_v[0][2] << "\n";
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;cin >> T;
    while (T--) {
        int n;
        int p;
        cin >> n >> p;
        int a[2 * n];
        rep(i, 1, n) cin >> a[i];
        int flag = 1;
        ll ans = 0;
        int pre = 0;
        rep(i, 1, n - 1) {
            if (flag) {
                if (a[i] <= a[i + 1]) continue;
                else {
                    ans += a[i];
                    flag = 0;
                }
            } else {
                if (a[i] >= a[i + 1]) continue;
                else {
                    ans -= a[i];
                    flag = 1;
                    pre = a[i];
                }
            }
        }
        if (flag) ans += max(pre, a[n]);
        cout << ans << endl;
    }
    return 0;
}


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

int const N = 1e4;
int a[N];

int main() {
    int T;cin >> T;
    while (T--) {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++) cin >> a[i];
        ll ans = 0;
        int flag = 1;
        int pre;
        if (a[0] > a[1]) ans += a[0],flag = 0;
        for (int i = 1; i < n; i++) {
            if (flag) {
                if (a[i] <= a[i - 1]) continue;
                else {
                    ans += a[i];
                    flag = 0;
                }
            } else {
                if (a[i] >= a[i-1]) continue;
                else  {
                    ans -= a[i];
                    flag = 1;
                    pre = a[i];
                }
            }
        }
        ans += max(pre, a[n]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int const N = 1e6;
int a[N];

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++) cin >> a[i];
        ll ans = 0;
        int flag = 1;
        int pre = 0;
        for (int i = 1; i <= n; i++) {
            if (flag) {
                if (a[i] >= a[i - 1]) continue;
                else {
                    ans += a[i - 1];
                    flag = 0;
                }
            } else {
                if (a[i] <= a[i - 1]) continue;
                else {
                    ans -= a[i - 1];
                    flag = 1;
                    pre = a[i - 1];
                }
            }
        }
        if (flag)
            ans += max(pre, a[n]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    cin >> T;
    while (T--) {
        int n;int q;
        cin >> n >> q;
        int a[2 * n];
        rep(i, 1, n) cin >> a[i];
        int flag = 0;
        ll ans = 0;
        int pre = 0;
        rep(i, 1, n - 1) {
            if (!flag && a[i] <= a[i + 1]) {
                continue;
            } else if (!flag) {
                flag = 1;
                ans += a[i];
                continue;
            }
            if (flag && a[i] >= a[i + 1]) {
                continue;
            } else if (flag) {
                flag = 0;
                ans -= a[i];
                pre = a[i];
                continue;
            }
        }
        if (!flag) ans += a[n];
        else ans += pre;
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    cin >> T;
    while (T--) {
        int n;int q;
        cin >> n >> q;
        int a[2 * n];
        rep(i, 1, n) cin >> a[i];
        int flag = 0;
        int ans = 0;
        int pre = 0;
        rep(i, 1, n - 1) {
            if (!flag && a[i] <= a[i + 1]) {
                continue;
            } else if (!flag) {
                flag = 1;
                ans += a[i];
                continue;
            }
            if (flag && a[i] > a[i + 1]) {
                continue;
            } else if (flag) {
                flag = 0;
                ans -= a[i];
                pre = a[i];
                continue;
            }
        }
        if (!flag) ans += a[n];
        else ans += pre;
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int const N = 1e4;
int a[N];

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++) cin >> a[i];
        ll ans = 0;
        int flag = 1;
        int pre = 0;
        for (int i = 1; i <= n; i++) {
            if (flag) {
                if (a[i] >= a[i - 1]) continue;
                else {
                    ans += a[i - 1];
                    flag = 0;
                }
            } else {
                if (a[i] <= a[i - 1]) continue;
                else {
                    ans -= a[i - 1];
                    flag = 1;
                    pre = a[i - 1];
                }
            }
        }
        if (flag)
            ans += max(pre, a[n]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    cin >> T;
    while (T--) {
        int n;int q;
        cin >> n >> q;
        int a[2 * n];
        rep(i, 1, n) cin >> a[i];
        int flag = 0;
        ll ans = 0;
        int pre = 0;
        rep(i, 1, n - 1) {
            if (!flag && a[i] <= a[i + 1]) {
                continue;
            } else if (!flag) {
                flag = 1;
                ans += a[i];
                continue;
            }
            if (flag && a[i] > a[i + 1]) {
                continue;
            } else if (flag) {
                flag = 0;
                ans -= a[i];
                pre = a[i];
                continue;
            }
        }
        if (!flag) ans += a[n];
        else ans += pre;
        cout << ans << endl;
    }
    return 0;
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        if(a.size() == 1) {
            cout << a[0] << '\n';
            continue;
        }
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);

        reverse(holes.begin(), holes.end());
        while(!holes.empty() && holes.back() < peaks[0]) holes.pop_back();
        reverse(holes.begin(), holes.end());
        while(!holes.empty() && holes.back() > peaks.back()) holes.pop_back();

        ll sum = 0, j = 0;
        for(int i = 0; i < peaks.size(); i++) {
            sum += a[peaks[i]];
            if(j >= holes.size())
                break;
            sum -= a[holes[j++]];

        }
        cout << sum << '\n';

    }
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);
        vector<int> answer;
        if(peaks.size() == 1)
            cout << a[peaks[0]] << '\n';
        else {
            int j = 0;
            while (holes[j] < peaks[0])
                ++j;
            int sum = 0;
            for(auto i: peaks)
                sum += a[i];
            for(int cnt = 0; cnt < (int)peaks.size() - 1; ++cnt) {
                sum -= a[holes[j++]];
            }
            cout << sum << '\n';
        }

    }
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        if(a.size() == 1) {
            cout << a[0] << '\n';
            continue;
        }
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);
        if(peaks.size() == 1)
            cout << a[peaks[0]] << '\n';
        else {
            int j = (holes.empty() || holes[0] < peaks[0]);
            int sum = 0;
            for(int i = 0; i < peaks.size(); i++) {
                sum += a[peaks[i]];
                if(j >= holes.size())
                    break;
                if(i + 1 != peaks.size() && j < holes.size() && holes[j] < peaks[i + 1]) {
                    sum -= a[holes[j++]];
                }
            }
            cout << sum << '\n';
        }

    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f first
#define s second

using namespace std;
const int N = 3e5 + 5;
int maxs[N];

void solve(){
    int n, q; cin >> n >> q;
    vector<int> a(n);
    int maxv = a[0];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        maxv = max(maxv, a[i]);
    }
    maxs[n] = 0;
    for(int i = n - 1; i >= 0; i--){
        maxs[i] = max(maxs[i + 1], a[i]);
    }
    ll now = 0, maxans = maxv;
    for(int i = 1; i < n; i++){
        now += max(0, a[i - 1] - a[i]);
        maxans = max(maxans, now + maxs[i + 1]);
    }
    cout << maxans << endl;
}

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


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        if(a.size() == 1) {
            cout << a[0] << '\n';
            continue;
        }
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);
        if(peaks.size() == 1)
            cout << a[peaks[0]] << '\n';
        else {
            int j = (holes.empty() || holes[0] < peaks[0]);
            int sum = 0;
            for(int i = 0; i < peaks.size(); i++) {
                sum += a[peaks[i]];
                if(i + 1 != peaks.size() && j < holes.size() && holes[j] < peaks[i + 1]) {
                    sum -= a[holes[j++]];
                }
            }
            cout << sum << '\n';
        }

    }
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        if(a.size() == 1) {
            cout << a[0] << '\n';
            continue;
        }
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);
        if(peaks.size() == 1)
            cout << a[peaks[0]] << '\n';
        else {
            int j = (holes.empty() || holes[0] < peaks[0]);
            int sum = 0;
            for(int i = 0; i < peaks.size(); i++) {
                sum += a[peaks[i]];
                if(i + 1 != peaks.size() && j < holes.size() && holes[j] < peaks[i + 1])) {
                    sum -= a[holes[j++]];
                }
            }
            cout << sum << '\n';
        }

    }
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        if(a.size() == 1) {
            cout << a[0] << '\n';
            continue;
        }
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);

        reverse(holes.begin(), holes.end());
        while(!holes.empty() && holes.back() < peaks[0]) holes.pop_back();
        reverse(holes.begin(), holes.end());
        while(!holes.empty() && holes.back() > peaks.back()) holes.pop_back();

        int sum = 0, j = 0;
        for(int i = 0; i < peaks.size(); i++) {
            sum += a[peaks[i]];
            if(j >= holes.size())
                break;
            sum -= a[holes[j++]];

        }
        cout << sum << '\n';

    }
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        if(a.size() == 1) {
            cout << a[0] << '\n';
            continue;
        }
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);
        vector<int> answer;
        if(peaks.size() == 1)
            cout << a[peaks[0]] << '\n';
        else {
            int j = (holes.empty() ||holes[0] < peaks[0]);
            int sum = 0;
            for(int i = 0; i < peaks.size(); i++) {
                sum += a[peaks[i]];
                if(j + (holes.back() > peaks.back()) < holes.size()) {
                    sum -= a[holes[j++]];
                }
            }
            cout << sum << '\n';
        }

    }
}


#include "iostream"
#include "vector"
#include "map"
#include "set"
#include "algorithm"

using namespace std;
typedef long long ll;

void fpeaks(vector<int> &x, vector<int> &pk) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] > x[i + 1])
                pk.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] < x[i])
                pk.push_back(i);
        }
        else {
            if(x[i - 1] < x[i] && x[i] > x[i + 1])
                pk.push_back(i);
        }
    }
}
void fholes(vector<int> &x, vector<int> &hl) {
    for(int i = 0; i < x.size(); i++) {
        if(i == 0) {
            if(x[i] < x[i + 1])
                hl.push_back(i);
        }
        else if(i + 1 == x.size()) {
            if(x[i - 1] > x[i])
                hl.push_back(i);
        }
        else {
            if(x[i - 1] > x[i] && x[i] < x[i + 1])
                hl.push_back(i);
        }
    }
}

int main() {
    int t = 1;
    cin >> t;
    while(t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for(auto &i: a)
            cin >> i;
        vector<int> peaks;
        vector<int> holes;
        fpeaks(a, peaks);
        fholes(a, holes);
        vector<int> answer;
        if(peaks.size() == 1)
            cout << a[peaks[0]] << '\n';
        else {
            int j = 0;
            while (j < holes.size() && holes[j] < peaks[0])
                ++j;
            int sum = 0;
            for(auto i: peaks)
                sum += a[i];
            for(int cnt = 0; cnt < (int)peaks.size() - 1 && j < holes.size(); ++cnt) {
                sum -= a[holes[j++]];
            }
            cout << sum << '\n';
        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
#define F first
#define S second
int n;
void check(int p,int *ans){
  //cout<<p<<endl;
  int t=p,k=0;
  while(ans[t]>=k){
    ans[t]=k;
    k++;
    t--;
    if(t<0) t+=n;
  }
  t=p,k=0;
  while(ans[t]>=k){
    ans[t]=k;
    k++;
    t++;
    if(t>=n) t-=n;
  }
}
signed main(){
  scanf("%lld",&n);
  int a[n],b[n];
  for(int i=0;i<n;i++) scanf("%lld",a+i);
  for(int i=0;i<n;i++) scanf("%lld",b+i);

  map<int,int> mb;
  for(int i=0;i<n;i++) mb[b[i]]=i;
  vector<int> v;
  for(int i=0;i<n;i++)
    v.push_back((n+mb[a[i]]-i)%n);
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  int ans[n];
  for(int i=0;i<n;i++) ans[i]=n*2;
  int sz=v.size();
  queue<P> q;
  q.push(P(0,sz-1));
  int used[sz];
  memset(used,0,sizeof(used));
  while(!q.empty()){
    P p=q.front();q.pop();
    //cout<<p.F<<" "<<p.S<<endl;
    //assert(p.F<=p.S);
    if(p.F>p.S) continue;
    if(p.F==p.S){
      check(v[p.F],ans);
      used[p.F]=1;
    }else if(p.F+1==p.S){
      check(v[p.F],ans);
      check(v[p.S],ans);
      used[p.F]=1;
      used[p.S]=1;
    }else{
      int mid=(p.F+p.S)/2;
      check(v[mid],ans);
      used[mid]=1;
      q.push(P(p.F,mid-1));
      q.push(P(mid+1,p.S));
    }
  }
  for(int i=0;i<sz;i++) check(v[i],ans);
  for(int i=0;i<n;i++) printf("%lld\n",ans[i]);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5+15;

int a[N],b[N], oa[N],ob[N], val[N];

int main(){

    //freopen("input.txt","r",stdin);

    int n;
    cin>>n;
    for(int i=1;i<=n;++i)scanf("%d",&a[i]), oa[a[i]]= i;
    for(int i=1;i<=n;++i)scanf("%d",&b[i]), ob[b[i]]= i;

    multiset<int> pos;int valp = 0;
	multiset<int> npos;int valn = 0;
	multiset<int>::iterator it;
	for(int i=1;i<=n;++i){

		int u = b[i];

		int ind = oa[u];

		if(ind-i>=0)
			npos.insert(ind-i);
		else
			pos.insert(i-ind);
	}
	for(int i=1;i<=n;++i){

		int mini = n;
		if(pos.size()>0)
			mini = min( mini,*pos.begin()-valp);

		if(npos.size()>0)
			mini = min(*npos.begin()-valn,mini);
		printf("%d\n",mini);
		int cur  =  oa[b[i]]-1 + valn;

		it = npos.find(cur);
		assert(it!=npos.end());
		npos.erase(it);

		++valp;
		--valn;

		while(pos.size()>0){
			if( *pos.begin() - valp ==0){
				pos.erase(pos.begin());
				npos.insert(valn);
			}
			else
				break;
		}
		if(n-ob[b[i]]>0)
			pos.insert(n-ob[b[i]] + valp);
		else
			npos.insert(valn);
	}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5+15;

int a[N],b[N], oa[N],ob[N], val[N];

int main(){

    //freopen("input.txt","r",stdin);

    int n;
    cin>>n;
    for(int i=1;i<=n;++i)scanf("%d",&a[i]), oa[a[i]]= i;
    for(int i=1;i<=n;++i)scanf("%d",&b[i]), ob[b[i]]= i;

    multiset<int> pos;int valp = 0;
	multiset<int> npos;int valn = 0;
	multiset<int>::iterator it;
	for(int i=1;i<=n;++i){

		int u = b[i];

		int ind = oa[u];

		if(ind-i>=0)
			npos.insert(ind-i);
		else
			pos.insert(i-ind);
	}
	for(int i=1;i<=n;++i){

		int mini = n;
		if(pos.size()>0)
			mini = min( mini,*pos.begin()-valp);

		if(npos.size()>0)
			mini = min(*npos.begin()-valn,mini);
		printf("%d\n",mini);
		int cur  =  oa[b[i]]-1 + valn;

		it = npos.find(cur);
		assert(it!=npos.end());
		npos.erase(it);

		++valp;
		--valn;

		while(pos.size()>0){
			if( *pos.begin() - valp ==0){
				pos.erase(pos.begin());
				npos.insert(valn);
			}
			else
				break;
		}

		if(n-oa[b[i]]>0)
			pos.insert(n-oa[b[i]] + valp);
		else
			npos.insert(valn);
	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int limN = 1e5 + 5;

int N ;
int ord[limN];
int invos[limN];
int nums[limN], ans[limN];
set <int> zerop;
map <int, vector<int> > aprs;

int valo(int k) {
    return k;
    if(k > N)
        return N;
    if(k > N/2)
        return max(0, N - k);
    return k;
}

int main() {
    scanf("%d", &N);
    for(int i=0; i<N; i++) {
        scanf("%d", &invos[i]);
        invos[i] --;
        ord[invos[i]] = i;
    }
    for(int i=0; i<N; i++) {
        scanf("%d", &nums[i]);
        nums[i] = ord[nums[i]-1] ;
    }

    // zerop.insert(-10*N);
    // zerop.insert(10*N);
    for(int k=0, w; k<N; k++) {
        int ro, b ;
        w = nums[k];
        // printf("%d => ", invos[w] + 1);
        if(w == k)
            ro = 0;
        else if(k < w)
            ro = N - w + k;
        else
            ro = k-w;
        zerop.insert(ro);
        zerop.insert(ro-N);
        zerop.insert(ro+N);
        b = N - w -1 ;
        aprs[ro-b].push_back(ro);
        aprs[ro-N-b].push_back(ro-N);
        aprs[ro+N-b].push_back(ro+N);
        // printf("%d >> %d\n", ro, b);
        // printf("\t");
        // for(int i=0; i<N; i++)
        //     printf("%2d ", invos[nums[(i+ro)%N]] + 1);
        // printf("\n");
    }

    // for(const int z : zerop)
    //     printf("%d ", z);
    // printf("\n");

    auto l = zerop.begin();
    auto r = l;
    r ++;
    for(int i=0; i<N; i++) {
        while( *r <= i )
            l++, r++;
        ans[i] = i - *l;
        // printf("%d\n", valo(i - *l));
        // printf("\t%d || %d %d\n", i, *l, *r);
    }

    auto it = aprs.begin();
    set <int> inis;
    for(int i=-N; i<N; i++) {
        while(!inis.empty() && *inis.begin() < i)
            inis.erase(inis.begin());
        while(it->first < i)
            it ++;
        if(it->first == i)
            for(const int w : it->second) {
                // printf("met %d %d\n", i, w);
                inis.insert(w);
            }
        if(i < 0)
            continue;
        ans[i] = min(ans[i], *inis.begin() - i);
    }

    for(int i=0; i<N; i++)
        printf("%d\n", ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
const int limN = 1e5 + 5;

int N ;
int ord[limN];
int invos[limN];
int nums[limN], ans[limN];
set <int> zerop;
map <int, vector<int> > aprs;

int valo(int k) {
    return k;
    if(k > N)
        return N;
    if(k > N/2)
        return max(0, N - k);
    return k;
}

int main() {
    scanf("%d", &N);
    for(int i=0; i<N; i++) {
        scanf("%d", &invos[i]);
        invos[i] --;
        ord[invos[i]] = i;
    }
    for(int i=0; i<N; i++) {
        scanf("%d", &nums[i]);
        nums[i] = ord[nums[i]-1] ;
    }

    zerop.insert(-10*N);
    zerop.insert(10*N);
    for(int k=0, w; k<N; k++) {
        int ro, b ;
        w = nums[k];
        // printf("%d => ", invos[w] + 1);
        if(w == k)
            ro = 0;
        else if(k < w)
            ro = N - w + k;
        else
            ro = k-w;
        zerop.insert(ro);
        zerop.insert(ro-N);
        zerop.insert(ro+N);
        b = N - w -1 ;
        aprs[ro-b].push_back(ro);
        aprs[ro-N-b].push_back(ro-N);
        aprs[ro+N-b].push_back(ro+N);
        // printf("%d >> %d\n", ro, b);
        // printf("\t");
        // for(int i=0; i<N; i++)
        //     printf("%2d ", invos[nums[(i+ro)%N]] + 1);
        // printf("\n");
    }

    // for(const int z : zerop)
    //     printf("%d ", z);
    // printf("\n");

    auto l = zerop.begin();
    auto r = l;
    r ++;
    for(int i=0; i<N; i++) {
        while( *r <= i )
            l++, r++;
        ans[i] = i - *l;
        // printf("%d\n", valo(i - *l));
        // printf("\t%d || %d %d\n", i, *l, *r);
    }

    auto it = aprs.begin();
    set <int> inis;
    for(int i=-N; i<N; i++) {
        while(*inis.begin() < i)
            inis.erase(inis.begin());
        while(it->first < i)
            it ++;
        if(it->first == i)
            for(const int w : it->second) {
                // printf("met %d %d\n", i, w);
                inis.insert(w);
            }
        if(i < 0)
            continue;
        ans[i] = min(ans[i], *inis.begin() - i);
    }

    for(int i=0; i<N; i++)
        printf("%d\n", ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
const int limN = 1e5 + 5;

int N ;
int ord[limN];
int nums[limN];
set <int> zerop;

int valo(int k) {
    if(k > N)
        return N;
    if(k > N/2)
        return max(0, N - k);
    return k;
}

int main() {
    scanf("%d", &N);
    for(int i=0; i<N; i++) {
        scanf("%d", &ord[i]);
        ord[i] --;
    }
    for(int i=0; i<N; i++) {
        scanf("%d", &nums[i]);
        nums[i] = ord[nums[i]-1] ;
    }

    zerop.insert(-10*N);
    zerop.insert(10*N);
    for(int k=0, w; k<N; k++) {
        w = nums[k];
        if(w == k)
            zerop.insert(0);
        else if(k < w)
            zerop.insert(N - w + k);
        else
            zerop.insert(k - w);
    }

    // for(const int z : zerop)
    //     printf("%d ", z);
    // printf("\n");

    auto l = zerop.begin();
    auto r = l;
    r ++;
    for(int i=0; i<N; i++) {
        while( !(*l <= i && i <= *r) )
            l++, r++;
        printf("%d\n", min(valo(i - *l), valo(*r - i)));
    }
}


#include <bits/stdc++.h>
using namespace std;
const int limN = 1e5 + 5;

int N ;
int ord[limN];
int nums[limN];
set <int> zerop;

int main() {
    scanf("%d", &N);
    for(int i=0; i<N; i++) {
        scanf("%d", &ord[i]);
        ord[i] --;
    }
    for(int i=0; i<N; i++) {
        scanf("%d", &nums[i]);
        nums[i] = ord[nums[i]-1] ;
    }

    zerop.insert(-N);
    zerop.insert(2*N);
    for(int k=0, w; k<N; k++) {
        w = nums[k];
        if(w == k)
            zerop.insert(0);
        else if(k < w)
            zerop.insert(N - w + k);
        else
            zerop.insert(k - w);
    }

    // for(const int z : zerop)
    //     printf("%d ", z);
    // printf("\n");

    auto l = zerop.begin();
    auto r = l;
    r ++;
    for(int i=0; i<N; i++) {
        while( !(*l <= i && i <= *r) )
            l++, r++;
        printf("%d\n", min(i - *l, *r - i));
    }
}


#include <bits/stdc++.h>
using namespace std;
const int limN = 1e5 + 5;

int N ;
int ord[limN];
int invos[limN];
int nums[limN], ans[limN];
map <int, vector<int> > zerop;
map <int, vector<int> > aprs;

int valo(int k) {
    return k;
    if(k > N)
        return N;
    if(k > N/2)
        return max(0, N - k);
    return k;
}

int main() {
    scanf("%d", &N);
    for(int i=0; i<N; i++) {
        scanf("%d", &invos[i]);
        invos[i] --;
        ord[invos[i]] = i;
        ans[i] = N ;
    }
    for(int i=0; i<N; i++) {
        scanf("%d", &nums[i]);
        nums[i] = ord[nums[i]-1] ;
    }

    // zerop.insert(-10*N);
    // zerop.insert(10*N);
    for(int k=0, w; k<N; k++) {
        int ro, b ;
        w = nums[k];
        // printf("%d => ", invos[w] + 1);
        if(w == k)
            ro = 0;
        else if(k < w)
            ro = N - w + k;
        else
            ro = k-w;
        zerop[ro+w].push_back(ro);
        zerop[ro+w-N].push_back(ro-N);
        zerop[ro+w+N].push_back(ro+N);
        b = N - w -1 ;
        aprs[ro-b].push_back(ro);
        aprs[ro-N-b].push_back(ro-N);
        aprs[ro+N-b].push_back(ro+N);
        // printf("%d >> %d\n", ro, b);
        // printf("\t");
        // for(int i=0; i<N; i++)
        //     printf("%2d ", invos[nums[(i+ro)%N]] + 1);
        // printf("\n");
    }

    // for(const int z : zerop)
    //     printf("%d ", z);
    // printf("\n");

    auto r = zerop.end();
    r --;
    priority_queue <int> Q;
    for(int i=2*N-1; i>=0; i--) {
        // printf("%d >> %d\n", i, r->first);
        while(!Q.empty() && Q.top() > i)
            Q.pop();
        while(r->first > i)
            r --;
        if(r->first == i)
            for(const int w : r->second) {
                // printf("met %d %d\n", i, w);
                Q.push(w);
            }
        if(i >= N)
            continue;
        ans[i] = i - Q.top();
    }

    auto it = aprs.begin();
    set <int> inis;
    for(int i=-N; i<N; i++) {
        while(!inis.empty() && *inis.begin() < i)
            inis.erase(inis.begin());
        while(it->first < i)
            it ++;
        if(it->first == i)
            for(const int w : it->second) {
                // printf("met %d %d\n", i, w);
                inis.insert(w);
            }
        if(i < 0)
            continue;
        ans[i] = min(ans[i], *inis.begin() - i);
    }

    for(int i=0; i<N; i++)
        printf("%d\n", ans[i]);
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (int)(x).size()
#define unique(arr) arr.erase(unique(all(arr)),arr.end());

using lli   = long long;
using lld   = long double;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vpll  = vector<pll>;
using vl    = vector<lli>;
using vvl   = vector<vector<lli>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T, typename F, typename S>
void __p(pair<pair<T, F>,S> a) { cout << "{ "; __p(a.ff.ff); __p(a.ff.ss); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
inline lli gcd(lli a, lli b) { return ((b==0) ? a : gcd(b,a%b)); }
inline lli LSOne(lli a) {return (a&(-a));}

void tejas_919(int kkkk)
{
    lli n, m, k, q, u, v, temp=0, temp2=0;
    cin>>n;
    set<pll> se1, se2;
    map<int,set<pll>::iterator> mp;
    vl arr(n), brr(n), ans(n,mod), ans2(n,mod);
    fol(i,0,n)
    {
        cin>>u; u--;
        arr[u]=i;
    }
    fol(i,0,n)
    {
        cin>>u; u--;
        brr[u]=i;
    }
    fol(i,0,n)
    {
        if(arr[i]<=brr[i]) se1.insert({brr[i]-arr[i],brr[i]});
        else
        {
            mp[brr[i]]=se2.insert({arr[i]-brr[i],brr[i]}).ff;
        }
    }
    fol(i,0,n)
    {
        while(!se1.empty()&&se1.begin()->ff-i<0)
        {
            mp[se1.begin()->ss]=se2.insert({1-i,se1.begin()->ss}).ff;
            se1.erase(se1.begin());
        }
        while(!mp.empty()&&mp.begin()->ff-i<0)
        {
            auto itr=mp.begin()->ss;
            temp=itr->ff+itr->ss;
            temp2=n+itr->ss-i;
            se1.insert({n-temp-1+i,temp2+i});
            mp.erase(mp.begin());
            se2.erase(itr);
        }
        if(!se1.empty()) { ans[i]=se1.begin()->ff-i; }
        if(!se2.empty()) { ans[i]=min(ans[i],se2.begin()->ff+i);  }
    }
    forr(x,ans) cout<<x<<endl;
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (int)(x).size()
#define unique(arr) arr.erase(unique(all(arr)),arr.end());

using lli   = long long;
using lld   = long double;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vpll  = vector<pll>;
using vl    = vector<lli>;
using vvl   = vector<vector<lli>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T, typename F, typename S>
void __p(pair<pair<T, F>,S> a) { cout << "{ "; __p(a.ff.ff); __p(a.ff.ss); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
inline lli gcd(lli a, lli b) { return ((b==0) ? a : gcd(b,a%b)); }
inline lli LSOne(lli a) {return (a&(-a));}

void tejas_919(int kkkk)
{
    lli n, m, k, q, u, v, temp=0;
    cin>>n;
    set<int> se1;
    vl arr(n), brr(n), ans(n);
    fol(i,0,n)
    {
        cin>>u; u--;
        arr[u]=i;
    }
    fol(i,0,n)
    {
        cin>>u; u--;
        brr[u]=i;
    }
    fol(i,0,n)
    {
        if(arr[i]<=brr[i]) se1.insert(brr[i]-arr[i]);
        else se1.insert(n+brr[i]-arr[i]);
    }
    fol(i,0,n)
    {
        while(*se1.begin()-i<0)
        {
            se1.erase(se1.begin());
            se1.insert(n+i-1);
        }
        ans[i]=*se1.begin()-i;
    }
    se1.clear();
    fol(i,0,n)
    {
        if(arr[i]>=brr[i]) se1.insert(arr[i]-brr[i]);
        else se1.insert(n+arr[i]-brr[i]);
    }
    fol(i,0,n)
    {
        while(*se1.begin()-i<0)
        {
            se1.erase(se1.begin());
            se1.insert(n+i-1);
        }
        u=(n-i)%n;
        ans[u]=min(ans[u],(lli)*se1.begin()-i);
    }
    forr(x,ans) cout<<x<<endl;
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (int)(x).size()
#define unique(arr) arr.erase(unique(all(arr)),arr.end());

using lli   = long long;
using lld   = long double;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vpll  = vector<pll>;
using vl    = vector<lli>;
using vvl   = vector<vector<lli>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T, typename F, typename S>
void __p(pair<pair<T, F>,S> a) { cout << "{ "; __p(a.ff.ff); __p(a.ff.ss); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
inline lli gcd(lli a, lli b) { return ((b==0) ? a : gcd(b,a%b)); }
inline lli LSOne(lli a) {return (a&(-a));}

void tejas_919(int kkkk)
{
    lli n, u, ans;
    cin>>n;
    set<pll> se1, se2;
    map<int,set<pll>::iterator> mp;
    vl arr(n), brr(n);
    fol(i,0,n) cin>>u, arr[u-1]=i;
    fol(i,0,n) cin>>u, brr[u-1]=i;
    fol(i,0,n)
    {
        if(arr[i]<=brr[i]) se1.insert({brr[i]-arr[i],brr[i]});
        else mp[brr[i]]=se2.insert({arr[i]-brr[i],brr[i]}).ff;
    }
    fol(i,0,n)
    {
        while(!se1.empty()&&se1.begin()->ff-i<0)
        {
            mp[se1.begin()->ss]=se2.insert({1-i,se1.begin()->ss}).ff;
            se1.erase(se1.begin());
        }
        while(!mp.empty()&&mp.begin()->ff-i<0)
        {
            auto itr=mp.begin()->ss;
            se1.insert({n-itr->ff+itr->ss-1+i,n+itr->ss});
            mp.erase(mp.begin());
            se2.erase(itr);
        }
        if(!se1.empty()) ans=se1.begin()->ff-i;
        else ans=mod;
        if(!se2.empty()) ans=min(ans,se2.begin()->ff+i);
        cout<<ans<<endl;
    }
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (int)(x).size()
#define unique(arr) arr.erase(unique(all(arr)),arr.end());

using lli   = long long;
using lld   = long double;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vpll  = vector<pll>;
using vl    = vector<lli>;
using vvl   = vector<vector<lli>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T, typename F, typename S>
void __p(pair<pair<T, F>,S> a) { cout << "{ "; __p(a.ff.ff); __p(a.ff.ss); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
inline lli gcd(lli a, lli b) { return ((b==0) ? a : gcd(b,a%b)); }
inline lli LSOne(lli a) {return (a&(-a));}

void tejas_919(int kkkk)
{
    lli n, u, ans;
    cin>>n;
    set<pll> se1, se2;
    map<int,set<pll>::iterator> mp;
    vl arr(n), brr(n);
    fol(i,0,n) cin>>u, arr[u-1]=i;
    fol(i,0,n) cin>>u, brr[u-1]=i;
    fol(i,0,n)
    {
        if(arr[i]<=brr[i]) se1.insert({brr[i]-arr[i],brr[i]});
        else mp[brr[i]]=se2.insert({arr[i]-brr[i],brr[i]}).ff;
    }
    fol(i,0,n)
    {
        while(!se1.empty()&&se1.begin()->ff-i<0)
        {
            mp[se1.begin()->ss]=se2.insert({1-i,se1.begin()->ss}).ff;
            se1.erase(se1.begin());
        }
        while(!mp.empty()&&mp.begin()->ff-i<0)
        {
            auto itr=mp.begin()->ss;
            se1.insert({n-itr->ff-itr->ss-1+i,n+itr->ss});
            mp.erase(mp.begin());
            se2.erase(itr);
        }
        if(!se1.empty()) ans=se1.begin()->ff-i;
        else ans=mod;
        if(!se2.empty()) ans=min(ans,se2.begin()->ff+i);
        cout<<ans<<endl;
    }
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (int)(x).size()
#define unique(arr) arr.erase(unique(all(arr)),arr.end());

using lli   = long long;
using lld   = long double;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vpll  = vector<pll>;
using vl    = vector<lli>;
using vvl   = vector<vector<lli>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T, typename F, typename S>
void __p(pair<pair<T, F>,S> a) { cout << "{ "; __p(a.ff.ff); __p(a.ff.ss); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
inline lli gcd(lli a, lli b) { return ((b==0) ? a : gcd(b,a%b)); }
inline lli LSOne(lli a) {return (a&(-a));}

void tejas_919(int kkkk)
{
    lli n, u, ans;
    cin>>n;
    set<pll> se1, se2;
    map<int,set<pll>::iterator> mp;
    vl arr(n), brr(n);
    fol(i,0,n) cin>>u, arr[u-1]=i;
    fol(i,0,n) cin>>u, brr[u-1]=i;
    fol(i,0,n)
    {
        if(arr[i]<=brr[i]) se1.insert({brr[i]-arr[i],brr[i]});
        else mp[brr[i]]=se2.insert({arr[i]-brr[i],brr[i]}).ff;
    }
    fol(i,0,n)
    {
        while(!se1.empty()&&se1.begin()->ff-i<0)
        {
            mp[se1.begin()->ss]=se2.insert({1-i,se1.begin()->ss}).ff;
            se1.erase(se1.begin());
        }
        while(!mp.empty()&&mp.begin()->ff-i<0)
        {
            auto itr=mp.begin()->ss;
            se1.insert({n-itr->ff+itr->ss-1+i,n+itr->ss});
            mp.erase(mp.begin());
            se2.erase(itr);
        }
        if(!se1.empty()) ans=se1.begin()->ff-i;
        if(!se2.empty()) ans=min(ans,se2.begin()->ff+i);
        cout<<ans<<endl;
    }
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
}


#include<bits/stdc++.h>
using namespace std;
int n,ans,a[1000005],b[1000005],c[1000005];
multiset<int>s;
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++)c[a[i]]=i;
	for(int i=0;i<n;i++)scanf("%d",&b[i]);
	for(int i=0;i<n;i++)s.insert(i-c[b[i]]);
	for(int i=0;i<n;i++)
	{
		ans=1e9+7;
		multiset<int>::iterator it=s.lower_bound(i);
		if(it!=s.end())ans=min(ans,*it-i);
		if(it!=s.begin())ans=min(ans,i-*(--it));
		printf("%d\n",ans);
		it=s.find(i-c[b[i]]);
		s.erase(it);
		s.insert(i-c[b[i]]+n);
	}
	return 0;
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {

	class SegmentTree {

		static final int INF = 4000000;

		int n;
		int t [];
		int add [];

		public SegmentTree(int n) {
			this.n = n;
			t = new int [4 * n];
			add = new int [4 * n];
			build (1, 0, n - 1);
		}

		private void build (int v, int tl, int tr) {
			if (tl == tr) {
				t [v] = INF;
			} else {
				int tm = (tl + tr) >> 1;
				build (v * 2, tl, tm);
				build (v * 2 + 1, tm + 1, tr);
				t [v] = min (t[v * 2], t[v * 2 + 1]);
			}
		}

		private void push (int v) {
			t [v] += add [v];
			add [v * 2] += add [v];
			add [v * 2 + 1] += add [v];
			add [v] = 0;
		}

		private void pop (int v) {
			t [v] = min (t[v * 2] + add [v * 2], t[v * 2 + 1] + add[v * 2 + 1]);
		}

		private void update (int v, int tl, int tr, int pos, int value) {
			if (tl == tr) {
				t [v] = value;
				add [v] = 0;
			} else {
				push (v);
				int tm = (tl + tr) >> 1;
				if (pos <= tm) {
					update (v * 2, tl, tm, pos, value);
				} else {
					update (v * 2 + 1, tm + 1, tr, pos, value);
				}
				pop(v);
			}
		}

		public void update (int pos, int value) {
			update (1, 0, n - 1, pos, value);
		}

		public void increase () {
			add [1]++;
		}

		public void decrease () {
			add [1]--;
		}

		public int getAnswer () {
			return t[1] + add[1];
		}

	}

	public void solve () throws Exception {
		int n = nextInt();

		if (n == 1) {
			out.println(0);
			return;
		}

		int a [] = new int [n];
		int b [] = new int [2 * n];
		int apos [] = new int [n];

		for (int i = 0; i < n; i++) {
			a [i] = nextInt() - 1;
			apos[a[i]] = i;
		}

		int pos [][] = new int [n][2];

		for (int i = 0; i < n; i++) {
			b [i] = b[i + n] = nextInt() - 1;
			pos[b[i]][0] = i;
			pos[b[i]][1] = i + n;
		}

		SegmentTree treeMinus = new SegmentTree(n);
		SegmentTree treePlus = new SegmentTree(n);

		ArrayList<Integer> toPlus [] = new ArrayList [n];
		for (int i = 0; i < n; i++) {
			toPlus [i] = new ArrayList<Integer>();
		}

		for (int i = 0; i < n; i++) {
			if (i < pos[a[i]][0]) {
				treeMinus.update(a[i], pos[a[i]][0] - i);
			} else {
				treePlus.update(a[i], i - pos[a[i]][0]);
			}
			if (pos[a[i]][1] - i < n) {
				toPlus[pos[a[i]][1] - i].add(a[i]);
			}
			if (pos[a[i]][0] - i < n && pos[a[i]][0] - i >= 0) {
				toPlus[pos[a[i]][0] - i].add(a[i]);
			}
		}

		for (int i = 0; i < n; i++) {
			out.println(min (treeMinus.getAnswer(), treePlus.getAnswer()));

			for (int index : toPlus[i]) {
				treeMinus.update(index, SegmentTree.INF);
				treePlus.update(index, 0);
			}

			treePlus.update(b[i], SegmentTree.INF);
			treeMinus.update(b[i], pos[b[i]][1] - apos[b[i]] - i);

			treePlus.increase();
			treeMinus.decrease();
		}

	}

	private static final String fname = "";
	private static long stime = 0;

	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

	private String nextToken () throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}

	private int nextInt () throws Exception {
		return Integer.parseInt(nextToken());
	}

	private long nextLong () throws Exception {
		return Long.parseLong(nextToken());
	}

	private double nextDouble () throws Exception {
		return Double.parseDouble(nextToken());
	}

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve ();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		new Thread(null, new Solution(), "", 1<<26).start();
	}

}


import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {

	class State {

		// type = 1, from + to - with value
		// type = 2, from - to + with value

		int type, index;
		int value;

		public State(int type, int index, int value) {
			this.type = type;
			this.index = index;
			this.value = value;
		}

	}

	class SegmentTree {

		static final int INF = 2000000;

		int n;
		int t [];
		int add [];

		public SegmentTree(int n) {
			this.n = n;
			t = new int [4 * n];
			add = new int [4 * n];
			build (1, 0, n - 1);
		}

		private void build (int v, int tl, int tr) {
			if (tl == tr) {
				t [v] = INF;
			} else {
				int tm = (tl + tr) >> 1;
				build (v * 2, tl, tm);
				build (v * 2 + 1, tm + 1, tr);
				t [v] = min (t[v * 2], t[v * 2 + 1]);
			}
		}

		private void push (int v) {
			t [v] += add [v];
			add [v * 2] += add [v];
			add [v * 2 + 1] += add [v];
			add [v] = 0;
		}

		private void pop (int v) {
			t [v] = min (t[v * 2] + add [v * 2], t[v * 2 + 1] + add[v * 2 + 1]);
		}

		private void update (int v, int tl, int tr, int pos, int value) {
			if (tl == tr) {
				t [v] = value;
				add [v] = 0;
			} else {
				push (v);
				int tm = (tl + tr) >> 1;
				if (pos <= tm) {
					update (v * 2, tl, tm, pos, value);
				} else {
					update (v * 2 + 1, tm + 1, tr, pos, value);
				}
				pop(v);
			}
		}

		public void update (int pos, int value) {
			update (1, 0, n - 1, pos, value);
		}

		public void increase () {
			add [1]++;
		}

		public void decrease () {
			add [1]--;
		}

		public int getAnswer () {
			return t[1] + add[1];
		}

	}

	ArrayList<State> actions [];

	public void solve () throws Exception {
		int n = nextInt();
		if (n == 1) {
			out.println(0);
			return;
		}

		int a [] = new int [n];
		int b [] = new int [2 * n];

		for (int i = 0; i < n; i++) {
			a [i] = nextInt() - 1;
		}

		actions = new ArrayList [n];
		for (int i = 0; i < n; i++) {
			actions [i] = new ArrayList<Solution.State>();
		}

		int pos [][] = new int [n][2];

		for (int i = 0; i < n; i++) {
			b [i] = b[i + n] = nextInt() - 1;
			pos[b[i]][0] = i;
			pos[b[i]][1] = i + n;
		}

		SegmentTree treeMinus = new SegmentTree(n);
		SegmentTree treePlus = new SegmentTree(n);

		for (int i = 0; i < n; i++) {
			int middle = pos[a[i]][0] + (pos[a[i]][1] - pos[a[i]][0]) / 2;
			if (middle - i < n && middle - i >= 0) {
				actions[middle - i].add(new State(1, a[i], pos[a[i]][1] - middle));
			}
			if (pos[a[i]][0] >= i && pos[a[i]][0] - i < n) {
				actions[pos[a[i]][0] - i].add(new State(2, a[i], 0));
			}
			if (pos[a[i]][1] >= i && pos[a[i]][1] - i < n) {
				actions[pos[a[i]][1] - i].add(new State(2, a[i], 0));
			}
			if (i <= pos[a[i]][0]) {
				treeMinus.update(a[i], pos[a[i]][0] - i);
			} else if (i > pos[a[i]][0] && i <= middle) {
				treePlus.update(a[i], i - pos[a[i]][0]);
			} else {
				treeMinus.update(a[i], pos[a[i]][1] - i);
			}
		}

		for (int i = 0; i < n; i++) {
			out.println(min (treeMinus.getAnswer(), treePlus.getAnswer()));
			for (int j = 0; j < actions[i].size(); j++) {
				State action = actions[i].get(j);
				if (action.type == 1) {
					treePlus.update(action.index, SegmentTree.INF);
					treeMinus.update(action.index, action.value);
				} else {
					treeMinus.update(action.index, SegmentTree.INF);
					treePlus.update(action.index, action.value);
				}
			}
			treePlus.increase();
			treeMinus.decrease();
		}

	}

	private static final String fname = "";
	private static long stime = 0;

	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

	private String nextToken () throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}

	private int nextInt () throws Exception {
		return Integer.parseInt(nextToken());
	}

	private long nextLong () throws Exception {
		return Long.parseLong(nextToken());
	}

	private double nextDouble () throws Exception {
		return Double.parseDouble(nextToken());
	}

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve ();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		new Thread(null, new Solution(), "", 1<<26).start();
	}

}


import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {

	class State {

		// type = 1, from + to - with value
		// type = 2, from - to + with value

		int type, index;
		int value;

		public State(int type, int index, int value) {
			this.type = type;
			this.index = index;
			this.value = value;
		}

	}

	class SegmentTree {

		static final int INF = 4000000;

		int n;
		int t [];
		int add [];

		public SegmentTree(int n) {
			this.n = n;
			t = new int [4 * n];
			add = new int [4 * n];
			build (1, 0, n - 1);
		}

		private void build (int v, int tl, int tr) {
			if (tl == tr) {
				t [v] = INF;
			} else {
				int tm = (tl + tr) >> 1;
				build (v * 2, tl, tm);
				build (v * 2 + 1, tm + 1, tr);
				t [v] = min (t[v * 2], t[v * 2 + 1]);
			}
		}

		private void push (int v) {
			t [v] += add [v];
			add [v * 2] += add [v];
			add [v * 2 + 1] += add [v];
			add [v] = 0;
		}

		private void pop (int v) {
			t [v] = min (t[v * 2] + add [v * 2], t[v * 2 + 1] + add[v * 2 + 1]);
		}

		private void update (int v, int tl, int tr, int pos, int value) {
			if (tl == tr) {
				t [v] = value;
				add [v] = 0;
			} else {
				push (v);
				int tm = (tl + tr) >> 1;
				if (pos <= tm) {
					update (v * 2, tl, tm, pos, value);
				} else {
					update (v * 2 + 1, tm + 1, tr, pos, value);
				}
				pop(v);
			}
		}

		public void update (int pos, int value) {
			update (1, 0, n - 1, pos, value);
		}

		public void increase () {
			add [1]++;
		}

		public void decrease () {
			add [1]--;
		}

		public int getAnswer () {
			return t[1] + add[1];
		}

	}

	ArrayList<State> actions [];

	public void solve () throws Exception {
		int n = nextInt();

		if (n == 1) {
			out.println(0);
			return;
		}

		int a [] = new int [n];
		int b [] = new int [2 * n];
		int apos [] = new int [n];

		for (int i = 0; i < n; i++) {
			a [i] = nextInt() - 1;
			apos[a[i]] = i;
		}

		actions = new ArrayList [n];
		for (int i = 0; i < n; i++) {
			actions [i] = new ArrayList<Solution.State>();
		}

		int pos [][] = new int [n][2];

		for (int i = 0; i < n; i++) {
			b [i] = b[i + n] = nextInt() - 1;
			pos[b[i]][0] = i;
			pos[b[i]][1] = i + n;
		}

		SegmentTree treeMinus = new SegmentTree(n);
		SegmentTree treePlus = new SegmentTree(n);

		for (int i = 0; i < n; i++) {
			int middle = pos[a[i]][0] + (pos[a[i]][1] - pos[a[i]][0]) / 2;
			if (middle - i < n && middle - i >= 0) {
				actions[middle - i].add(new State(1, a[i], pos[a[i]][1] - middle));
			}
			if (pos[a[i]][0] >= i && pos[a[i]][0] - i < n) {
				actions[pos[a[i]][0] - i].add(new State(2, a[i], 0));
			}
			if (pos[a[i]][1] >= i && pos[a[i]][1] - i < n) {
				actions[pos[a[i]][1] - i].add(new State(2, a[i], 0));
			}
			if (i <= pos[a[i]][0]) {
				treeMinus.update(a[i], pos[a[i]][0] - i);
			} else if (i > pos[a[i]][0] && i <= middle) {
				treePlus.update(a[i], i - pos[a[i]][0]);
			} else {
				treeMinus.update(a[i], pos[a[i]][1] - i);
			}
		}

		for (int i = 0; i < n; i++) {
			out.println(min (treeMinus.getAnswer(), treePlus.getAnswer()));
			for (int j = 0; j < actions[i].size(); j++) {
				State action = actions[i].get(j);
				if (action.type == 1) {
					treePlus.update(action.index, SegmentTree.INF);
					treeMinus.update(action.index, action.value);
				} else {
					treeMinus.update(action.index, SegmentTree.INF);
					treePlus.update(action.index, action.value);
				}
			}

			treePlus.update(b[i], SegmentTree.INF);
			treeMinus.update(b[i], pos[b[i]][1] - apos[b[i]] - i);

			treePlus.increase();
			treeMinus.decrease();
		}

	}

	private static final String fname = "";
	private static long stime = 0;

	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

	private String nextToken () throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}

	private int nextInt () throws Exception {
		return Integer.parseInt(nextToken());
	}

	private long nextLong () throws Exception {
		return Long.parseLong(nextToken());
	}

	private double nextDouble () throws Exception {
		return Double.parseDouble(nextToken());
	}

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve ();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		new Thread(null, new Solution(), "", 1<<26).start();
	}

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Solution implements Runnable {

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st;

	String nextToken() throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}

	int nextInt() throws Exception {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws Exception {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws Exception {
		return Double.parseDouble(nextToken());
	}

	class Z implements Comparable<Z> {
		int n, dist;
		Z(int nn) {
			n = nn;
		}
		@Override
		public int compareTo(Z arg0) {
			if (dist != arg0.dist) return dist - arg0.dist;
			return n - arg0.n;
		}
	}

	void solve() throws Exception {
		int n = nextInt();
		int[] a = new int[n];
		Z[] b = new Z[n];
		int[] oa = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt() - 1;
			oa[a[i]] = i;
		}
		TreeSet<Z> less = new TreeSet<Z>();
		TreeSet<Z> more = new TreeSet<Z>();
		for (int i = 0; i < n; i++) {
			b[i] = new Z(nextInt() - 1);
			b[i].dist = Math.abs(oa[b[i].n] - i);
			if (oa[b[i].n] <= i) more.add(b[i]); else less.add(b[i]);
		}
		for (int i = 0; i < n; i++) {
			while (!more.isEmpty() && more.first().dist == i) {
				Z t = more.pollFirst();
				t.dist = -i;
				less.add(t);
			}
			int ll = less.isEmpty()? 1000000 : less.first().dist + i;
			int rr = more.isEmpty()? 1000000 : more.first().dist - i;
			out.println(Math.min(ll, rr));
			less.remove(b[i]);
			b[i].dist = n - 1 - oa[b[i].n] + i + 1;
			more.add(b[i]);
		}
	}

	public void run() {
		try {
			Locale.setDefault(Locale.UK);
			in = new BufferedReader(new InputStreamReader(System.in));
			// in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter(System.out);
			// out = new PrintWriter("output.txt");
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		// new Thread(null, new Solution(), "1", 1 << 28).start();
		(new Solution()).run();
	}

}


import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.Locale;
import java.io.OutputStream;
import java.util.RandomAccess;
import java.io.PrintWriter;
import java.util.AbstractList;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.TreeSet;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Jacob Jiang
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
	public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.nextInt();
        int[] a = in.nextIntArray(n);
        int[] b = in.nextIntArray(n);
        ArrayUtils.decreaseByOne(a, b);
        int[] indexA = CombinatoricsUtils.reversePermutation(a);
        int[] indexB = CombinatoricsUtils.reversePermutation(b);
        TreeSet<Integer> set = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            set.add(indexA[i] - indexB[i]);
        }
        for (int zero = 0; zero >= 1 - n; zero--) {
            while (set.last() >= n + zero) {
                int cur = set.last();
                set.remove(cur);
                set.add(cur - n);
            }
            int answer = Integer.MAX_VALUE;
            Integer tmp = set.floor(zero);
            if (tmp != null)
                answer = Math.min(answer, zero - tmp);
            tmp = set.ceiling(zero);
            if (tmp != null)
                answer = Math.min(answer, tmp - zero);
            out.println(answer);
        }
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1 << 16];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c & 15;
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public static boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public int[] nextIntArray(int count) {
        int[] result = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = nextInt();
        }
        return result;
    }

    }

class OutputWriter {
    private PrintWriter writer;

    public OutputWriter(OutputStream stream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void println(int x) {
        writer.println(x);
    }

    public void close() {
        writer.close();
    }

    }

class ArrayUtils {

    public static void decreaseByOne(int[]... arrays) {
        for (int[] array : arrays) {
            for (int i = 0; i < array.length; i++) {
                array[i]--;
            }
        }
    }

    }

class CombinatoricsUtils {

    public static int[] reversePermutation(int[] permutation) {
        int n = permutation.length;
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[permutation[i]] = i;
        }
        return result;
    }

    }


import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.Locale;
import java.io.OutputStream;
import java.util.RandomAccess;
import java.io.PrintWriter;
import java.util.AbstractList;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.TreeSet;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Jacob Jiang
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    private static class Node implements Comparable<Node> {
        int index;
        int value;

        private Node(int index, int value) {
            this.index = index;
            this.value = value;
        }

        public int compareTo(Node o) {
            if (value != o.value) return Integer.compare(value, o.value);
            return Integer.compare(index, o.index);
        }
    }

	public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.nextInt();
        int[] _a = in.nextIntArray(n);
        int[] _b = in.nextIntArray(n);
        ArrayUtils.decreaseByOne(_a, _b);
        int[] a = CombinatoricsUtils.reversePermutation(_a);
        int[] b = CombinatoricsUtils.reversePermutation(_b);
        Node[] nodes = new Node[n];
        TreeSet<Node> set = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            Node cur = new Node(i, a[i] - b[i]);
            nodes[b[i]] = cur;
            set.add(cur);
        }
        int[] answers = new int[n];
        for (int i = 0; i < n; i++) {
            if (i > 0) {
                set.remove(nodes[n - i]);
                nodes[n - i].value += n;
                set.add(nodes[n - i]);
            }
            int answer = Integer.MAX_VALUE;
            Node tmp = set.floor(new Node(Integer.MAX_VALUE, i));
            if (tmp != null)
                answer = Math.min(answer, i - tmp.value);
            tmp = set.ceiling(new Node(Integer.MIN_VALUE, i));
            if (tmp != null)
                answer = Math.min(answer, tmp.value - i);
            answers[i] = answer;
        }
        out.println(answers[0]);
        for (int i = n - 1; i >= 1; i--) {
            out.println(answers[i]);
        }
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1 << 16];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c & 15;
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public static boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public int[] nextIntArray(int count) {
        int[] result = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = nextInt();
        }
        return result;
    }

    }

class OutputWriter {
    private PrintWriter writer;

    public OutputWriter(OutputStream stream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void println(int x) {
        writer.println(x);
    }

    public void close() {
        writer.close();
    }

    }

class ArrayUtils {

    public static void decreaseByOne(int[]... arrays) {
        for (int[] array : arrays) {
            for (int i = 0; i < array.length; i++) {
                array[i]--;
            }
        }
    }

    }

class CombinatoricsUtils {

    public static int[] reversePermutation(int[] permutation) {
        int n = permutation.length;
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[permutation[i]] = i;
        }
        return result;
    }

    }