#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
#define int long long
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
signed main() {
	int n, m, t;	
	cin >> n >> m >> t;
	vector<int> v(m);
	for (int i = 0; i < m; i++) {
		cin >> v[i];
	}
	sort(v.begin(), v.end());
	while (t--) {
		int ans = 1e9;
		int x, y;	cin >> x >> y;
		int t = *lower_bound(v.begin(), v.end(), x);
//		cout << t << endl;
		if(y < x) {
			swap(x, y);
			ans = min(ans, 1 + t - x + abs((n + t) - y));
		}
		ans = min(ans, y - x);
		ans = min(ans, n + n + x - y);
//		cout << " 1 " << ans << endl;
		if(y - x >= n) ans = min(ans, (abs(n + x - y) + 1));
//		cout << " 2 " << ans << endl;
		ans = min(ans, abs( n + n + x - y - n + 1));
//		cout << " * ";
		cout << ans<< endl;
	}
	return 0; 
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |