#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006, M = 1000000007;

int n;
int a[N];

void solv()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    if (n == 1)
    {
        printf("0\n");
        return;
    }
    int u = 1;
    for (int i = 1; i <= n - 1; ++i)
        u = (u * 1LL * i) % M;
    sort(a + 1, a + n + 1);
    int ans = 0;
    int q = 0;
    for (int i = n; i >= 1; --i)
    {
        ans = (ans + ((((a[i] * 1LL * q) % M) * 1LL * u) % M)) % M;
        if (a[i] != a[i - 1])
        {
            for (int j = i; ; ++j)
            {
                if (a[j] != a[i])
                    break;
                ++q;
            }
        }
    }
    printf("%d\n", ans);
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006, M = 1000000007;

int ast(int x, int n)
{
    int ans = 1;
    while (n)
    {
        if (n % 2 == 1)
            ans = (ans * 1LL * x) % M;
        x = (x * 1LL * x) % M;
        n /= 2;
    }
    return ans;
}

int f[N], rf[N];

void pre()
{
    f[0] = 1;
    for (int i = 1; i < N; ++i)
        f[i] = (f[i - 1] * 1LL * i) % M;
    rf[N - 1] = ast(f[N - 1], M - 2);
    for (int i = N - 2; i >= 0; --i)
        rf[i] = (rf[i + 1] * 1LL * (i + 1)) % M;
}

int C(int n, int k)
{
    return (((f[n] * 1LL * rf[k]) % M) * 1LL * rf[n - k]) % M;
}

int n;
int a[N];

void solv()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    if (n == 1)
    {
        printf("0\n");
        return;
    }
    sort(a + 1, a + n + 1);
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (a[i] == a[n])
            break;
        if (a[i] != a[i - 1])
        {
            int q = i - 1;
            int t = n - i;
            int u = 0;
            for (int j = i; ; ++j)
            {
                if (a[j] != a[i])
                    break;
                ++u;
            }
            int yans = (((C(t + q + 1, t + 1) * 1LL * a[i]) % M) * ((f[t] * 1LL * f[q]) % M)) % M;
            yans = (yans * 1LL * u) % M;
            ans = (ans + yans) % M;
        }
    }
    printf("%d\n", ans);
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    pre();
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006, M = 1000000007;

int f[N];
int ast[N];

void pre()
{
    f[0] = 1;
    for (int i = 1; i < N; ++i)
        f[i] = (f[i - 1] * 1LL * i) % M;
    ast[0] = 1;
    for (int i = 1; i < N; ++i)
        ast[i] = (ast[i - 1] * 2) % M;
}

int n;
int a[N];

void solv()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    if (n == 1)
    {
        printf("0\n");
        return;
    }
    sort(a + 1, a + n + 1);
    int ans = 0;
    for (int i = 2; i <= n; ++i)
    {
        if (a[i] != a[i - 1])
        {
            ans = (ans + (((ast[i - 2] * 1LL * f[i - 1]) % M) * 1LL * a[i - 1]) % M) % M;
        }
    }
    printf("%d\n", ans);
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    pre();
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
#define int long long
#define F first
#define S second
const int MX = 2e5 + 100;
const int mo = 1e9 + 7;
int inverse(int v) {
	int ans = 1;
	for(int p = mo - 2; p; p >>= 1) {
		if(p & 1) ans = ans * v % mo;
		v = v * v % mo;
	}
	return ans;
}
int p[MX] = {1};
int C(int n, int r) {
	return p[n] * inverse(p[r]) % mo * inverse(p[n - r]) % mo;
}
main () {
	for(int i = 1; i < MX; i ++) p[i] = p[i - 1] * i % mo;
	int ans = 0;
	int n; cin >> n;
	map<int, int> mp;
	for(int i = 1; i <= n; i ++) {
		int v; cin >> v; mp[v] ++;
	}
	int a = 0;
	for(auto it: mp) {
		if(a + it.S == n) break;
		int b = n - a - 1;
		ans = (ans + it.F * C(n, a) % mo * p[a] % mo * p[b] % mo * p[it.S] % mo) % mo;
		a += it.S;
		// cout << ans << endl;
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
#define int long long
#define F first
#define S second
const int MX = 1e6 + 100;
const int mo = 1e9 + 7;
int inverse(int v) {
	int ans = 1;
	for(int p = mo - 2; p; p >>= 1) {
		if(p & 1) ans = ans * v % mo;
		v = v * v % mo;
	}
	return ans;
}
int p[MX] = {1};
int C(int n, int r) {
	return p[n] * inverse(p[r]) % mo * inverse(p[n - r]) % mo;
}
main () {
	for(int i = 1; i < MX; i ++) p[i] = p[i - 1] * i % mo;
	int ans = 0;
	int n; cin >> n;
	map<int, int> mp;
	for(int i = 1; i <= n; i ++) {
		int v; cin >> v; mp[v] ++;
	}
	int a = 0;
	for(auto it: mp) {
		if(a + it.S == n) break;
		int b = n - a - 1;
		ans = (ans + it.F * C(n, a) % mo * p[a] % mo * p[b] % mo * p[it.S] % mo) % mo;
		a += it.S;
		// cout << ans << endl;
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
#define int long long
#define F first
#define S second
const int MX = 1e6 + 100;
const int mo = 1e9 + 7;
inline int inverse(int v) {
	int ans = 1;
	for(int p = mo - 2; p; p >>= 1) {
		if(p & 1) ans = ans * v % mo;
		v = v * v % mo;
	}
	return ans;
}
int p[MX] = {1};
inline int C(int n, int r) {
	return p[n] * inverse(p[r]) % mo * inverse(p[n - r]) % mo;
}
main () {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	for(int i = 1; i < MX; i ++) p[i] = p[i - 1] * i % mo;
	int ans = 0;
	int n; cin >> n;
	map<int, int> mp;
	for(int i = 1; i <= n; i ++) {
		int v; cin >> v; mp[v] ++;
	}
	int a = 0;
	for(auto it: mp) {
		if(a + it.S == n) break;
		int b = n - a - 1;
		ans = (ans + it.F * C(n, a) % mo * p[a] % mo * p[b] % mo * it.S % mo) % mo;
		a += it.S;
		// cout << ans << endl;
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
#define int long long
#define F first
#define S second
const int MX = 1e6 + 100;
const int mo = 1e9 + 7;
int inverse(int v) {
	int ans = 1;
	for(int p = mo - 2; p; p >>= 1) {
		if(p & 1) ans = ans * v % mo;
		v = v * v % mo;
	}
	return ans;
}
int p[MX] = {1};
int C(int n, int r) {
	return p[n] * inverse(p[r]) % mo * inverse(p[n - r]) % mo;
}
main () {
	for(int i = 1; i < MX; i ++) p[i] = p[i - 1] * i % mo;
	int ans = 0;
	int n; cin >> n;
	map<int, int> mp;
	for(int i = 1; i <= n; i ++) {
		int v; cin >> v; mp[v] ++;
	}
	int a = 0;
	for(auto it: mp) {
		if(a + it.S == n) break;
		int b = n - a - 1;
		ans = (ans + it.F * C(n, a) % mo * p[a] % mo * p[b] % mo * it.S % mo) % mo;
		a += it.S;
		// cout << ans << endl;
	}
	cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <string>
#include <stdlib.h>
#include <set>
#include <vector>
#include <list>
#include <algorithm>
#include <regex>
#include <math.h>
#include <map>
#include <iterator>
#include <climits>
#include <numeric>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <array>
#include <valarray>
#include <complex>
#include <random>
#include <functional>

using namespace std;

#define int long long
#define ll long long
typedef int hv;
#define double long double
#define make(type, x) type x; cin>>x
#define make2(type, x, y) type x, y; cin>>x>>y
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z
#define make4(type, x, y, z, q) type x, y, z, q; cin>>x>>y>>z>>q
#define ALL(x) (x).begin(), (x).end()
#define FORIT(p) for(auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for(signed i = 0; i <(b); i++)
#define VI vector<int>
#define VVI vector<vector<int>>
#define PII pair<int, int>
#define PPII pair<PII, int>
#define M7 1000000007l
#define M9 1000000009l
#define make_v(a, n) VI a(n); FOR(n) cin >> a[i];

int pw(int a, int b) {
	if (b == 0) return 1;
	if (b & 1) return (a * pw(a, b ^ 1)) % M7;
	int d = pw(a, b >> 1);
	return (d * d) % M7;
}

void Solve() {
	signed n;
	scanf("%d", &n);
	vector<signed> a(n);
	FOR(n) scanf("%d", &a[i]);
	sort(ALL(a));
	int fn = 1;
	for (int i = 2; i <= n; ++i) {
		fn = (fn * i) % M7;
	}
	int cntless = 0, mx = *max_element(ALL(a));
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		if (a[i] == mx) break;
		ans += (a[i] * pw(n - cntless, M7 - 2)) % M7;
		if (a[i] != a[i + 1]) cntless = i + 1;
	}
	ans = (ans%M7)*fn;
	printf("%d", ans%M7);
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	Solve();
	while (!cin.eof()) {
		string s = "_";
		do getline(cin, s);
		while (!cin.eof() && !s.empty());
		if (cin.eof()) return 0;
		FOR(2) cout << endl;
		Solve();
	}
#else
	Solve();
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <string>
#include <stdlib.h>
#include <set>
#include <vector>
#include <list>
#include <algorithm>
#include <regex>
#include <math.h>
#include <map>
#include <iterator>
#include <climits>
#include <numeric>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <array>
#include <valarray>
#include <complex>
#include <random>
#include <functional>

using namespace std;

#define int long long
#define ll long long
typedef int hv;
#define double long double
#define make(type, x) type x; cin>>x
#define make2(type, x, y) type x, y; cin>>x>>y
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z
#define make4(type, x, y, z, q) type x, y, z, q; cin>>x>>y>>z>>q
#define ALL(x) (x).begin(), (x).end()
#define FORIT(p) for(auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for(signed i = 0; i <(b); i++)
#define VI vector<int>
#define VVI vector<vector<int>>
#define PII pair<int, int>
#define PPII pair<PII, int>
#define M7 1000000007l
#define M9 1000000009l
#define make_v(a, n) VI a(n); FOR(n) cin >> a[i];

int pw(int a, int n) {
	int res = 1;
	while (n) {
		if (n & 1)
			res = (res * a) % M7;
		a = (a*a) % M7;
		n >>= 1;
	}
	return res;
}

void Solve() {
	signed n;
	scanf("%d", &n);
	vector<signed> a(n);
	FOR(n) scanf("%d", &a[i]);
	sort(ALL(a));
	int fn = 1;
	for (int i = 2; i <= n; ++i) {
		fn = (fn * i) % M7;
	}
	int cntless = 0, mx = *max_element(ALL(a));
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		if (a[i] == mx) break;
		ans += (a[i] * pw(n - cntless, M7 - 2)) % M7;
		if (a[i] != a[i + 1]) cntless = i + 1;
	}
	ans = (ans%M7)*fn;
	printf("%d", ans%M7);
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	Solve();
	while (!cin.eof()) {
		string s = "_";
		do getline(cin, s);
		while (!cin.eof() && !s.empty());
		if (cin.eof()) return 0;
		FOR(2) cout << endl;
		Solve();
	}
#else
	Solve();
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <string>
#include <stdlib.h>
#include <set>
#include <vector>
#include <list>
#include <algorithm>
#include <regex>
#include <math.h>
#include <map>
#include <iterator>
#include <climits>
#include <numeric>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <array>
#include <valarray>
#include <complex>
#include <random>
#include <functional>

using namespace std;

#define int long long
#define ll long long
typedef int hv;
#define double long double
#define make(type, x) type x; cin>>x
#define make2(type, x, y) type x, y; cin>>x>>y
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z
#define make4(type, x, y, z, q) type x, y, z, q; cin>>x>>y>>z>>q
#define ALL(x) (x).begin(), (x).end()
#define FORIT(p) for(auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for(signed i = 0; i <(b); i++)
#define VI vector<int>
#define VVI vector<vector<int>>
#define PII pair<int, int>
#define PPII pair<PII, int>
#define M7 1000000007l
#define M9 1000000009l
#define make_v(a, n) VI a(n); FOR(n) cin >> a[i];

int pw(int a, int b) {
	if (b == 0) return 1;
	if (b & 1) return (a * pw(a, b ^ 1)) % M7;
	int d = pw(a, b >> 1);
	return (d * d) % M7;
}

void Solve() {
	make(int, n);
	VI a(n);
	FOR(n) cin >> a[i];
	sort(ALL(a));
	int fn = 1;
	for (int i = 2; i <= n; ++i) {
		fn = (fn * i) % M7;
	}
	int cntless = 0, mx = *max_element(ALL(a));
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		if (a[i] == mx) break;
		ans += (a[i] * pw(n - cntless, M7 - 2)) % M7;
		if (a[i] != a[i + 1]) cntless = i + 1;
	}
	ans = (ans%M7)*fn;
	cout << ans % M7;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	Solve();
	while (!cin.eof()) {
		string s = "_";
		do getline(cin, s);
		while (!cin.eof() && !s.empty());
		if (cin.eof()) return 0;
		FOR(2) cout << endl;
		Solve();
	}
#else
	Solve();
#endif
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cinttypes>

#define MODULO 1000000007

long long int arr[1000000];
long long int fact[1000001];
long long int ifact[1000001];
long long int inv[1000001];

int main()
{
    long long int n;
    std::cin >> n;

    for (long long int *p = arr; p != arr + n; p++)
        std::cin >> *p;

    fact[0] = 1;
    ifact[0] = 1;

    fact[1] = 1;
    ifact[1] = 1;
    inv[1] = 1;

    for (long long int ind = 2; ind <= n; ind++)
    {
        fact[ind] = fact[ind - 1] * ind % MODULO;
        inv[ind] = MODULO - MODULO / ind * inv[MODULO % ind] % MODULO;
        ifact[ind] = ifact[ind - 1] * inv[ind] % MODULO;
    }

    std::sort(arr, arr + n);

    long long int acc_count = 1;
    long long int val_count = 1;
    long long int prev = arr[0];
    long long int answer = 0;

    for (long long int ind = 1; ind < n; ind++)
    {
        if (prev != arr[ind])
        {
            long long int left = acc_count - val_count;
            long long int comb_term = fact[n] * ifact[left] % MODULO * ifact[n - left] % MODULO;
            answer += fact[left] * fact[n - left - 1] % MODULO * comb_term % MODULO * val_count % MODULO * prev % MODULO;
            answer %= MODULO;

            prev = arr[ind];
            val_count = 1;
        }
        else
            val_count++;

        acc_count++;
    }

    std::cout << answer << std::endl;

    return 0;
}


//--------------------------------
//Sriram Narayanan - itisalways42
//--------------------------------
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define endl "\n"
#define si(a) scanf("%d",&a)
#define slld(a) scanf("%lld",&a)
#define slf(a) scanf("%lf",&a)

using namespace std;
typedef long long LL;
typedef vector<LL> VI;
typedef vector< vector<int> > VVI;
typedef pair< LL, LL > PII;

const int N = 200010;
const LL INF = (LL)(1e15);
LL e,s,n,m;

vector<PII> pos[3];
vector<pair<PII,LL> > v;
PII tmpv[N],dp[N],cost[N];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>e>>s>>n>>m;
	for(int i=0; i<n; i++) cin>>tmpv[i].ss>>tmpv[i].ff;
	sort(tmpv,tmpv+n);
	for(int i=0; i<n; i++)
	{
		LL type,x;
		type = tmpv[i].ss-1; x = tmpv[i].ff;
		if(x > e) break;
		pos[type].pb(mp(x,i));
		v.pb(mp(mp(x,type),pos[type].size()-1));
		dp[i] = mp(INF,INF);
		cost[i] = mp(INF,INF);
	}
	pos[2].pb(mp(e,v.size()));
	v.pb(mp(mp(e,2),pos[2].size()-1));
	n = v.size();
	dp[n-1] = mp(0,0);
	for(int i=n-2; i>=0; i--)
	{
		LL t,x,idx,j;
		x = v[i].ff.ff; t = v[i].ff.ss; idx = v[i].ss;
		if(t==2)
		{
			cost[i] = mp(0,0);
			if(idx+1 < pos[2].size() && pos[2][idx+1].ff <= x+s)
			{
				dp[i] = dp[pos[2][idx+1].ss];
				continue;
			}
			j = upper_bound(all(pos[1]),mp(x+s,INF)) - pos[1].begin();
			j--;
			if(pos[1][j].ff > x && pos[1][j].ff <= x+s)
			{
				LL k = pos[1][j].ff - x;
				dp[i] = dp[pos[1][j].ss];
				dp[i].ss -= min(cost[pos[1][j].ss].ss,s-k);
				continue;
			}
			j = upper_bound(all(pos[0]),mp(x+s,INF)) - pos[0].begin();
			j--;
			if(pos[0][j].ff > x && pos[0][j].ff <= x+s)
			{
				LL k = pos[0][j].ff - x;
				dp[i] = dp[pos[0][j].ss];
				dp[i].ff -= min(cost[pos[0][j].ss].ff,s-k);
				continue;
			}
		}
		if(t==1)
		{
			j = upper_bound(all(pos[2]),mp(x,-1LL)) - pos[2].begin();
			if(j < pos[2].size() && pos[2][j].ff <= x+s)
			{
				LL k = pos[2][j].ff - x;
				dp[i] = dp[pos[2][j].ss];
				dp[i].ss += k;
				cost[i] = mp(0,k);
				continue;
			}
			if(idx+1 < pos[1].size() && pos[1][idx+1].ff <= x+s)
			{
				LL k = pos[1][idx+1].ff - x;
				dp[i] = dp[pos[1][idx+1].ss];
				dp[i].ss += k;
				cost[i] = mp(0,max(s,k+cost[pos[1][idx+1].ss].ss));
				continue;
			}
			j = upper_bound(all(pos[0]),mp(x+s,INF)) - pos[0].begin();
			j--;
			if(pos[0][j].ff > x && pos[0][j].ff <= x+s)
			{
				LL k = pos[0][j].ff - x;
				dp[i] = dp[pos[0][j].ss];
				LL tmp = min(cost[pos[0][j].ss].ff,s-k);
				dp[i].ff -= tmp; dp[i].ss += tmp+k;
				cost[i] = mp(0,tmp+k);
				continue;
			}
			dp[i] = cost[i] = mp(INF,INF);
		}
		if(t==0)
		{
			LL k1,k2,x1,x2;
			k1 = upper_bound(all(pos[2]),mp(x,-1LL)) - pos[2].begin();
			k2 = upper_bound(all(pos[1]),mp(x,-1LL)) - pos[1].begin();
			x1 = (k1<pos[2].size())?pos[2][k1].ff:INF; x2 = (k2<pos[1].size())?pos[1][k2].ff:INF;
			if(x1 <= x2 && x1 >= x && x1 <= x+s)
			{
				dp[i] = dp[pos[2][k1].ss];
				dp[i].ff += x1-x;
				cost[i] = mp(x1-x,0);
				continue;
			}
			else if(x2 >= x && x2 <= x+s)
			{
				dp[i] = dp[pos[1][k2].ss];
				dp[i].ff += x2-x;
				cost[i] = mp(x2-x,0);
				continue;
			}
			else if(idx+1 < pos[0].size() && pos[0][idx+1].ff <= x+s)
			{
				LL k = pos[0][idx+1].ff - x;
				dp[i] = dp[pos[0][idx+1].ss];
				dp[i].ff += k;
				cost[i] = mp(max(s,k+cost[pos[0][idx+1].ss].ff),0);
				continue;
			}
		}
	}
	//for(int i=0; i<n; i++)
	//	cout<<v[i].ff.ff<<" "<<v[i].ff.ss<<" "<<dp[i].ff<<" "<<dp[i].ss<<endl;
	PII ans;
	for(int i=0; i<m; i++)
	{
		LL f,x,j; cin>>f;
		ans = mp(INF,INF);
		j = upper_bound(all(pos[2]),mp(f,-1LL)) - pos[2].begin();
		if(j < pos[2].size() && pos[2][j].ff <= f+s)
		{
			ans = dp[pos[2][j].ss];
			goto done;
		}
		j = upper_bound(all(pos[1]),mp(f,-1LL)) - pos[1].begin();
		if(j < pos[1].size() && pos[1][j].ff <= f+s)
		{
			LL k = pos[1][j].ff - f;
			ans = dp[pos[1][j].ss];
			ans.ss -= min(s-k,cost[pos[1][j].ss].ss);
			goto done;
		}
		j = upper_bound(all(pos[0]),mp(f,-1LL)) - pos[0].begin();
		if(j < pos[0].size() && pos[0][j].ff <= f+s)
		{
			LL k = pos[0][j].ff - f;
			ans = dp[pos[0][j].ss];
			ans.ff -= min(s-k,cost[pos[0][j].ss].ff);
			goto done;
		}
done:
		if(ans.ff > (1e10)) ans.ff = -1;
		if(ans.ss > (1e10)) ans.ss = -1;
		cout<<ans.ff<<" "<<ans.ss<<endl;
	}

	return 0;
}


//--------------------------------
//Sriram Narayanan - itisalways42
//--------------------------------
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define endl "\n"
#define si(a) scanf("%d",&a)
#define slld(a) scanf("%lld",&a)
#define slf(a) scanf("%lf",&a)

using namespace std;
typedef long long LL;
typedef vector<LL> VI;
typedef vector< vector<int> > VVI;
typedef pair< LL, LL > PII;

const int N = 300010;
const LL INF = (LL)(1e15);
LL e,s,n,m;

vector<PII> pos[3];
vector<pair<PII,LL> > v;
PII tmpv[N],dp[N],cost[N];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>e>>s>>n>>m;
	for(int i=0; i<n; i++) cin>>tmpv[i].ss>>tmpv[i].ff;
	sort(tmpv,tmpv+n);
	for(int i=0; i<n; i++)
	{
		LL type,x;
		type = tmpv[i].ss-1; x = tmpv[i].ff;
		if(x > e) break;
		pos[type].pb(mp(x,i));
		v.pb(mp(mp(x,type),pos[type].size()-1));
		dp[i] = mp(INF,INF);
		cost[i] = mp(INF,INF);
	}
	pos[2].pb(mp(e,v.size()));
	v.pb(mp(mp(e,2),pos[2].size()-1));
	n = v.size();
	dp[n-1] = mp(0,0);
	for(int i=n-2; i>=0; i--)
	{
		LL t,x,idx,j;
		x = v[i].ff.ff; t = v[i].ff.ss; idx = v[i].ss;
		if(t==2)
		{
			cost[i] = mp(0,0);
			if(idx+1 < pos[2].size() && pos[2][idx+1].ff <= x+s)
			{
				dp[i] = dp[pos[2][idx+1].ss];
				continue;
			}
			j = upper_bound(all(pos[1]),mp(x+s,INF)) - pos[1].begin();
			j--;
			if(j>=0 && pos[1][j].ff > x && pos[1][j].ff <= x+s)
			{
				LL k = pos[1][j].ff - x;
				dp[i] = dp[pos[1][j].ss];
				dp[i].ss -= min(cost[pos[1][j].ss].ss,s-k);
				continue;
			}
			j = upper_bound(all(pos[0]),mp(x+s,INF)) - pos[0].begin();
			j--;
			if(j>=0 && pos[0][j].ff > x && pos[0][j].ff <= x+s)
			{
				LL k = pos[0][j].ff - x;
				dp[i] = dp[pos[0][j].ss];
				dp[i].ff -= min(cost[pos[0][j].ss].ff,s-k);
				continue;
			}
			dp[i] = cost[i] = mp(INF,INF);
		}
		if(t==1)
		{
			j = upper_bound(all(pos[2]),mp(x,-1LL)) - pos[2].begin();
			if(j < pos[2].size() && pos[2][j].ff <= x+s)
			{
				LL k = pos[2][j].ff - x;
				dp[i] = dp[pos[2][j].ss];
				dp[i].ss += k;
				cost[i] = mp(0,k);
				continue;
			}
			if(idx+1 < pos[1].size() && pos[1][idx+1].ff <= x+s)
			{
				LL k = pos[1][idx+1].ff - x;
				dp[i] = dp[pos[1][idx+1].ss];
				dp[i].ss += k;
				cost[i] = mp(0,max(s,k+cost[pos[1][idx+1].ss].ss));
				continue;
			}
			j = upper_bound(all(pos[0]),mp(x+s,INF)) - pos[0].begin();
			j--;
			if(j>=0 && pos[0][j].ff > x && pos[0][j].ff <= x+s)
			{
				LL k = pos[0][j].ff - x;
				dp[i] = dp[pos[0][j].ss];
				LL tmp = min(cost[pos[0][j].ss].ff,s-k);
				dp[i].ff -= tmp; dp[i].ss += tmp+k;
				cost[i] = mp(0,tmp+k);
				continue;
			}
			dp[i] = cost[i] = mp(INF,INF);
		}
		if(t==0)
		{
			LL k1,k2,x1,x2;
			k1 = upper_bound(all(pos[2]),mp(x,-1LL)) - pos[2].begin();
			k2 = upper_bound(all(pos[1]),mp(x,-1LL)) - pos[1].begin();
			x1 = (k1<pos[2].size())?pos[2][k1].ff:INF; x2 = (k2<pos[1].size())?pos[1][k2].ff:INF;
			if(x1 <= x2 && x1 >= x && x1 <= x+s)
			{
				dp[i] = dp[pos[2][k1].ss];
				dp[i].ff += x1-x;
				cost[i] = mp(x1-x,0);
				continue;
			}
			else if(x2 >= x && x2 <= x+s)
			{
				dp[i] = dp[pos[1][k2].ss];
				dp[i].ff += x2-x;
				cost[i] = mp(x2-x,0);
				continue;
			}
			else if(idx+1 < pos[0].size() && pos[0][idx+1].ff <= x+s)
			{
				LL k = pos[0][idx+1].ff - x;
				dp[i] = dp[pos[0][idx+1].ss];
				dp[i].ff += k;
				cost[i] = mp(max(s,k+cost[pos[0][idx+1].ss].ff),0);
				continue;
			}
			dp[i] = cost[i] = mp(INF,INF);
		}
	}
	//for(int i=0; i<n; i++)
	//	cout<<v[i].ff.ff<<" "<<v[i].ff.ss<<" "<<dp[i].ff<<" "<<dp[i].ss<<endl;
	PII ans;
	for(int i=0; i<m; i++)
	{
		LL f,x,j; cin>>f;
		ans = mp(INF,INF);
		j = upper_bound(all(pos[2]),mp(f,-1LL)) - pos[2].begin();
		if(j < pos[2].size() && pos[2][j].ff <= f+s)
		{
			ans = dp[pos[2][j].ss];
			goto done;
		}
		j = upper_bound(all(pos[1]),mp(f,-1LL)) - pos[1].begin();
		if(j < pos[1].size() && pos[1][j].ff <= f+s)
		{
			LL k = pos[1][j].ff - f;
			ans = dp[pos[1][j].ss];
			ans.ss -= min(s-k,cost[pos[1][j].ss].ss);
			goto done;
		}
		j = upper_bound(all(pos[0]),mp(f,-1LL)) - pos[0].begin();
		if(j < pos[0].size() && pos[0][j].ff <= f+s)
		{
			LL k = pos[0][j].ff - f;
			ans = dp[pos[0][j].ss];
			ans.ff -= min(s-k,cost[pos[0][j].ss].ff);
			goto done;
		}
done:
		if(ans.ff > (1e11)) ans.ff = -1;
		if(ans.ss > (1e11)) ans.ss = -1;
		cout<<ans.ff<<" "<<ans.ss<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define fr(a, b, c) for(int (a) = (b), __ = c; (a) < __; ++(a))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<

using namespace std;

int main() {
    int e, s, n, m;
    cin >> e >> s >> n >> m;
    vector<pair<int, int>> v, f;
    for (int i = 0; i < n; i++) {
        int x, t;
        cin >> t >> x;
        if (x < e) v.emplace_back(-x, t-1);
    }
    e = -e;
    v.emplace_back(e, 2);
    for (int i = 0; i < m; i++) {
        int ff;
        cin >> ff;
        f.emplace_back(-ff, i);
    }
    sort(v.begin(), v.end());
    sort(f.begin(), f.end());
    int p = 0, j = 0, i;
    deque<int> gas[3];
    vector<vector<int>> pans(v.size()), ans(m, vector<int>(3, -1));
    pans[0] = vector<int>(3, 0);
    gas[2].push_back(0);
    for (i = 1; i < v.size(); i++) {
        while (p < f.size() and f[p].first < v[i].first) {
            while (j < i and f[p].first - v[j].first > s) {
                gas[v[j].second].pop_back();
                j++;
            }
            if (j == i) break;
            if (gas[2].size()) {
                ans[f[p].second] = pans[gas[2].front()];
            } else {
                int g;
                for (g = 1; g > -1 and gas[g].empty(); g--);
                assert(g > -1);
                ans[f[p].second] = pans[gas[g].back()];
                int d = f[p].first - v[gas[g].back()].first;
                ans[f[p].second][g] -= s - d;
            }
            p++;
        }
        while (j < i and v[i].first - v[j].first > s) {
            gas[v[j].second].pop_back();
            j++;
        }
        if (i == j) break;
        int nx = -1;
        for (int g = v[i].second; g < 3; g++) {
            if (gas[g].size()) nx = max(nx, gas[g].front());
        }
        if (nx != -1) {
            int d = v[i].first - v[nx].first;
            pans[i] = pans[nx];
            pans[i][v[i].second] += d;
        } else {
            int g;
            for (g = v[i].second - 1; g > -1 and gas[g].empty(); g--);
            assert(g > -1);
            pans[i] = pans[gas[g].back()];
            int d = v[i].first - v[gas[g].back()].first;
            pans[i][v[i].second] += s;
            pans[i][g] -= s - d;
        }
        gas[v[i].second].push_front(i);
    }
    while (p < f.size()) {
        while (j < i and f[p].first - v[j].first > s) {
            gas[v[j].second].pop_back();
            j++;
        }
        if (j == i) break;
        if (gas[2].size()) {
            ans[f[p].second] = pans[gas[2].front()];
        } else {
            int g;
            for (g = 1; g > -1 and gas[g].empty(); g--);
            ans[f[p].second] = pans[gas[g].back()];
            int d = f[p].first - v[gas[g].back()].first;
            ans[f[p].second][g] -= s - d;
        }
        p++;
    }
    for (int i = 0; i < m; i++) {
        cout << ans[i][0] << ' ' << ans[i][1] << endl;
    }
}


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

using namespace std;

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long double ld;

long long rdtsc() {
  long long tmp;
  asm("rdtsc" : "=A"(tmp));
  return tmp;
}

inline int myrand() {
  return abs((rand() << 15) ^ rand());
}

inline int rnd(int x) {
  return myrand() % x;
}

#define pb push_back
#define mp make_pair
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define sz(x) ((int)(x).size())
#define TASKNAME "text"

const int INF = (int) 1.01e9;
const ld EPS = 1e-9;

void precalc() {
}

int e, s, n, m;
const int maxn = (int) 2e5 + 10;
int starts[maxn];

pair<int, int> a[maxn];

bool read() {
  if (scanf("%d%d%d%d", &e, &s, &n, &m) < 4) {
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &a[i].second, &a[i].first);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", starts + i);
  }
  return 1;
}

int ar1[maxn], ar2[maxn];
int sum1[maxn], sum2[maxn];

void countSums(int n, int *a, int *sum) {
  /*eprintf("cnt = %d\n", n);
  for (int i = 0; i < n; ++i) {
    eprintf("%d%c", a[i], " \n"[i == n - 1]);
  }*/
  sum[0] = 0;
  for (int i = 0; i < n; ++i) {
    int touse = s;
    if (i) {
      touse = min(touse, a[i] - a[i - 1]);
    }
    //eprintf("%d%c", touse, " \n"[i == n - 1]);
    sum[i + 1] = sum[i] + touse;
  }
}

int get(int x, int n, int *a, int *sum) {
  int pos = lower_bound(a, a + n, x) - a;
  assert(pos < n);
  int res = sum[n] - sum[pos + 1];
  res += min(s, a[pos] - x);

  res = max(0, e - x - res);
  return res;
}

void solve() {
  sort(a, a + n);
  int last = e - s;
  for (int i = n - 1; i >= 0; --i) {
    if (a[i].first < last) {
      break;
    }
    last = min(last, a[i].first - s);
  }

  int cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i].first < last || a[i].first >= e) {
      continue;
    }
    if (a[i].second == 3) {
      ar1[cnt1++] = a[i].first;
    }
    if (a[i].second >= 2) {
      ar2[cnt2++] = a[i].first;
    }
  }
  ar1[cnt1++] = e;
  ar2[cnt2++] = e;

  countSums(cnt1, ar1, sum1);
  countSums(cnt2, ar2, sum2);

  for (int iter = 0; iter < m; ++iter) {
    int x = starts[iter];
    if (x < last) {
      printf("-1 -1\n");
      continue;
    }
    int bad = get(x, cnt2, ar2, sum2);
    int norm = get(x, cnt1, ar1, sum1) - bad;
    printf("%d %d\n", bad, norm);
  }
}

int main() {
  srand(rdtsc());
#ifdef DEBUG
  freopen(TASKNAME".out", "w", stdout);
  assert(freopen(TASKNAME".in", "r", stdin));
#endif

  precalc();
  while (1) {
    if (!read()) {
      break;
    }
    solve();
    //break;
#ifdef DEBUG
    eprintf("Time: %.18lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


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

using namespace std;

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long double ld;

long long rdtsc() {
  long long tmp;
  asm("rdtsc" : "=A"(tmp));
  return tmp;
}

inline int myrand() {
  return abs((rand() << 15) ^ rand());
}

inline int rnd(int x) {
  return myrand() % x;
}

#define pb push_back
#define mp make_pair
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define sz(x) ((int)(x).size())
#define TASKNAME "text"

const int INF = (int) 1.01e9;
const ld EPS = 1e-9;

void precalc() {
}

int e, s, n, m;
const int maxn = (int) 2e5 + 10;
int starts[maxn];

pair<int, int> a[maxn];

bool read() {
  if (scanf("%d%d%d%d", &e, &s, &n, &m) < 4) {
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &a[i].second, &a[i].first);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", starts + i);
  }
  return 1;
}

int ar1[maxn], ar2[maxn];
int sum1[maxn], sum2[maxn];

void countSums(int n, int *a, int *sum) {
  /*eprintf("cnt = %d\n", n);
  for (int i = 0; i < n; ++i) {
    eprintf("%d%c", a[i], " \n"[i == n - 1]);
  }*/
  sum[0] = 0;
  for (int i = 0; i < n; ++i) {
    int touse = s;
    if (i) {
      touse = min(touse, a[i] - a[i - 1]);
    }
    //eprintf("%d%c", touse, " \n"[i == n - 1]);
    sum[i + 1] = sum[i] + touse;
  }
}

int get(int x, int n, int *a, int *sum) {
  int pos = lower_bound(a, a + n, x) - a;
  int res = sum[n] - sum[pos + 1];
  res += min(s, a[pos] - x);

  res = max(0, e - x - res);
  return res;
}

void solve() {
  sort(a, a + n);
  int last = e - s;
  for (int i = n - 1; i >= 0; --i) {
    if (a[i].first > last) {
      continue;
    }
    if (a[i].first < last) {
      break;
    }
    last = a[i].first - s;
  }

  int cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i].first < last || a[i].first >= e) {
      continue;
    }
    if (a[i].second == 3) {
      ar1[cnt1++] = a[i].first;
    }
    if (a[i].second >= 2) {
      ar2[cnt2++] = a[i].first;
    }
  }
  ar1[cnt1++] = e;
  ar2[cnt2++] = e;

  countSums(cnt1, ar1, sum1);
  countSums(cnt2, ar2, sum2);

  for (int iter = 0; iter < m; ++iter) {
    int x = starts[iter];
    if (x < last) {
      printf("-1 -1\n");
      continue;
    }
    int bad = get(x, cnt2, ar2, sum2);
    int norm = get(x, cnt1, ar1, sum1) - bad;
    printf("%d %d\n", bad, norm);
  }
}

int main() {
  srand(rdtsc());
#ifdef DEBUG
  freopen(TASKNAME".out", "w", stdout);
  assert(freopen(TASKNAME".in", "r", stdin));
#endif

  precalc();
  while (1) {
    if (!read()) {
      break;
    }
    solve();
    //break;
#ifdef DEBUG
    eprintf("Time: %.18lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fillchar(a, s) memset((a), (s), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define rep(it, v) for (auto it = (v).begin(); it != (v).end(); it++)

typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 10;

int E, S, N, M;
map<int, int> psum[3];
int F[MAXN];
map<int, vector<int> > ans;

int main() {
	if (fopen("input.txt", "r")) {
		freopen("input.txt", "r", stdin);
	}
	scanf("%d %d %d %d", &E, &S, &N, &M);
	for (int i = 0; i < 3; i++) {
		psum[i][E];
	}
	for (int i = 0, t, x; i < N; i++) {
		scanf("%d %d", &t, &x);
		t = 3 - t;
		psum[t][x]++;
		psum[t][x + S]--;
		for (int j = 0; j < 3; j++) {
			if (j != t) {
				psum[j][x];
				psum[j][x + S];
			}
		}
	}
	for (int i = 0; i < M; i++) {
		scanf("%d", &F[i]);
		F[i] += S;
		for (int j = 0; j < 3; j++) {
			psum[j][F[i]];
		}
	}
	for (int i = 0; i < 3; i++) {
		auto j = psum[i].begin();
		while (true) {
			auto k = next(j);
			if (k == psum[i].end()) {
				break;
			}
			k->second += j->second;
			j = k;
			// printf("psum[%d][%d] = %d\n", i, k->first, k->second);
		}
	}
	map<int, int>::iterator its[3];
	bool dead = false;	//no way to go
	for (int i = 0; i < 3; i++) {
		its[i] = psum[i].find(E);
	}
	while (true) {
		int nx = its[0]->first;
		for (int i = 0; i < 3; i++) {
			its[i]--;
		}
		int x = its[0]->first;
		vector<int> &pans = ans[x];
		if (!dead) {
			bool ok = false;
			for (int i = 0; i < 3; i++) {
				if (its[i]->second) {
					ok = true;
					pans.resize(3);
					pans[i] += nx - x;
					// printf("ans[%d][%d] += %d\n", x, i, nx - x);
					break;
				}
			}
			dead |= !ok;
		}
		if (dead) {
			// printf("dead x = %d\n", x);
			pans = {-1, -1, -1};
		} else {
			auto it = next(ans.find(x));
			if (it != ans.end()) {
				for (int i = 0; i < 3; i++) {
					pans[i] += it->second[i];
				}
			}
		}
		if (its[0] == psum[0].begin()) {
			break;
		}
	}
	for (int i = 0; i < M; i++) {
		if (F[i] >= E) {
			//shoot
			puts("0 0");
			continue;
		}
		vector<int> p = ans[F[i]];
		printf("%d %d\n", p[2], p[1]);
	}
}


#include <bits/stdc++.h>

using namespace std;

#define fillchar(a, s) memset((a), (s), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define rep(it, v) for (auto it = (v).begin(); it != (v).end(); it++)

typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 10;

int E, S, N, M;
map<int, int> psum[3];
int F[MAXN];
map<int, vector<int> > ans;

int main() {
	if (fopen("input.txt", "r")) {
		freopen("input.txt", "r", stdin);
	}
	scanf("%d %d %d %d", &E, &S, &N, &M);
	for (int i = 0; i < 3; i++) {
		psum[i][E];
	}
	for (int i = 0, t, x; i < N; i++) {
		scanf("%d %d", &t, &x);
		t = 3 - t;
		psum[t][x]++;
		psum[t][x + S]--;
		for (int j = 0; j < 3; j++) {
			if (j != t) {
				psum[j][x];
				psum[j][x + S];
			}
		}
	}
	for (int i = 0; i < M; i++) {
		scanf("%d", &F[i]);
		F[i] += S;
		for (int j = 0; j < 3; j++) {
			psum[j][F[i]];
		}
	}
	for (int i = 0; i < 3; i++) {
		auto j = psum[i].begin();
		while (true) {
			auto k = next(j);
			if (k == psum[i].end()) {
				break;
			}
			k->second += j->second;
			j = k;
			// printf("psum[%d][%d] = %d\n", i, k->first, k->second);
		}
	}
	map<int, int>::iterator its[3];
	bool dead = false;	//no way to go
	for (int i = 0; i < 3; i++) {
		its[i] = psum[i].find(E);
	}
	while (true) {
		int nx = its[0]->first;
		for (int i = 0; i < 3; i++) {
			its[i]--;
		}
		int x = its[0]->first;
		vector<int> &pans = ans[x];
		if (!dead) {
			bool ok = false;
			for (int i = 0; i < 3; i++) {
				if (its[i]->second) {
					ok = true;
					pans.resize(3);
					pans[i] += nx - x;
					// printf("ans[%d][%d] += %d\n", x, i, nx - x);
					break;
				}
			}
			dead |= !ok;
		}
		if (dead) {
			// printf("dead x = %d\n", x);
			pans = {-1, -1, -1};
		} else {
			auto it = next(ans.find(x));
			if (it != ans.end()) {
				for (int i = 0; i < 3; i++) {
					pans[i] += it->second[i];
				}
			}
		}
		if (its[0] == psum[0].begin()) {
			break;
		}
	}
	for (int i = 0; i < M; i++) {
		vector<int> p = ans[F[i]];
		printf("%d %d\n", p[2], p[1]);
	}
}


#include <bits/stdc++.h>

#define mp make_pair
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back

#define all(s) (s).begin(), (s).end()
#define sz(s) ((int) ((s).size()))

typedef long long ll;
typedef long double ld;

const int INF = 1e9;
const ll lINF = 1e18;
const double EPS = 1e-12;

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

using namespace std;

const int N = 2e5 + 100;

int e, s, n, m;
int t[N], x[N];
vector<int> v[4];
vector<int> len[4];

int get(int t, int x) {
    int i = (int) (lower_bound(all(v[t]), x) - v[t].begin());
    return len[t][i] + max(v[t][i] - x - s, 0);
}

int main()
{
#ifdef DEBUG
    freopen("text.in", "r", stdin);
#endif
    scanf("%d%d%d%d", &e, &s, &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", t + i, x + i);
        if (x[i] >= e) {
            continue;
        }
        for (int j = 1; j <= t[i]; j++) {
            v[j].pb(x[i]);
        }
    }
    for (int t = 1; t <= 3; t++) {
        v[t].pb(e);
        sort(all(v[t]));
        v[t].resize(distance(v[t].begin(), unique(all(v[t]))));
        len[t].resize(sz(v[t]));
        for (int i = sz(v[t]) - 2; i >= 0; i--) {
            len[t][i] = len[t][i + 1] + max(0, v[t][i + 1] - v[t][i] - s);
        }
    }
    for (int i = 0; i < m; i++) {
        int f;
        scanf("%d", &f);
        int a1 = get(1, f);
        int a2 = get(2, f);
        int a3 = get(3, f);
        if (a1 > 0) {
            printf("-1 -1\n");
        } else {
            printf("%d %d\n", a2, a3 - a2);
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#define pb push_back

#define all(s) (s).begin(), (s).end()
#define sz(s) ((int) ((s).size()))

using namespace std;

const int N = 2e5 + 100;

int e, s, n, m;
int t[N], x[N];
vector<int> v[4];
vector<int> len[4];

int get(int t, int x) {
    int i = (int) (lower_bound(all(v[t]), x) - v[t].begin());
    return len[t][i] + max(v[t][i] - x - s, 0);
}

int main()
{
    scanf("%d%d%d%d", &e, &s, &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", t + i, x + i);
        if (x[i] >= e) {
            continue;
        }
        for (int j = 1; j <= t[i]; j++) {
            v[j].pb(x[i]);
        }
    }
    for (int t = 1; t <= 3; t++) {
        v[t].pb(e);
        sort(all(v[t]));
        v[t].resize(distance(v[t].begin(), unique(all(v[t]))));
        len[t].resize(sz(v[t]));
        for (int i = sz(v[t]) - 2; i >= 0; i--) {
            len[t][i] = len[t][i + 1] + max(0, v[t][i + 1] - v[t][i] - s);
        }
    }
    for (int i = 0; i < m; i++) {
        int f;
        scanf("%d", &f);
        int a1 = get(1, f);
        int a2 = get(2, f);
        int a3 = get(3, f);
        if (a1 > 0) {
            printf("-1 -1\n");
        } else {
            printf("%d %d\n", a2, a3 - a2);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
using ll = long long;
int e,s,n,m;
vector<int> d[3];
ll dp[3][N];
ll calc(int a,int b)
{
    int cur = lower_bound(d[a].begin(),d[a].end(),b) - d[a].begin();
    return dp[a][cur] + max(d[a][cur] - b - s,0);
}
int main ()
{
    ios::sync_with_stdio(false);
    cin >> e >> s >> n >> m;
    int t1,t2;
    for (int i = 1;i <= n;i++)
    {
        cin >> t1 >> t2;if (t2 >= e) continue;
        for (int j = 0;j < t1;j++) d[j].push_back(t2);
    }
    for (int i = 0;i < 3;i++)
    {
        d[i].push_back(e);sort(d[i].begin(),d[i].end());
        for (int j = d[i].size() - 2;j >= 0;--j)
            dp[i][j] = dp[i][j + 1] + max(d[i][j + 1] - d[i][j] - s,0);
    }
    for (int i = 1;i <= m;i++)
    {
        int sd;cin >> sd;
        if (calc(0,sd)) puts("-1 -1");
        else printf("%lld %lld\n",calc(1,sd),calc(2,sd) - calc(1,sd));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define maxn 200111

vector<int> v[4];
int dp[4][maxn];
int e, s, n, m;

int f(int a, int b){
	int x = lower_bound(v[a].begin(), v[a].end(), b) - v[a].begin();
	return dp[a][x] + max(0, v[a][x] - b - s);
}

int main(){
	scanf("%d%d%d%d", &e, &s, &n, &m);
	for(int i = 0; i < n; i++){
		int a, b;
		scanf("%d%d", &a, &b);

		if(b >= e)
			continue;

		for(int j = 1; j <= a; j++)
			v[j].push_back(b);
	}

	for(int i = 1; i <= 3; i++){
		v[i].push_back(e);
		sort(v[i].begin(), v[i].end());
		for(int j = v[i].size() - 1 - 1; j >= 0; j--)   // last pos before e
			dp[i][j] = dp[i][j + 1] + max(0, v[i][j + 1] - v[i][j] - s);
	}

	while(m--){
		int a;
		scanf("%d", &a);
		if(f(1, a) != 0)
			printf("-1 -1\n");
		else
			printf("%d %d\n", f(2, a), f(3, a) - f(2, a));
	}

}


/*
 * Author:heroming
 * File:heroming.cpp
 * Time:2015/10/10 15:56:22
 */
#include <vector>
#include <list>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;

#define px first
#define py second
#define pb push_back
#define mp make_pair
#define pque priority_queue
#define lowbit(x) ((x) & -(x))
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define clr(v, e) memset(v, e, sizeof(v))
#define rep(it, v) for (auto it : v)
#define forn(i, n) for (int i = 0; i < (n); ++ i)
#define form(i, a, b) for (int i = (a); i <= (b); ++ i)
#define forv(i, v) for (int i = 0; i < sz(v); ++ i)
#define iter(it, v) for (auto it = v.begin(); it != v.end(); ++ it)

typedef long long lint;
typedef vector<int> vint;
typedef vector<string> vstring;
typedef pair<int, int> pint;
typedef vector<long long> vlint;

const int maxn = 200010;

vint vet[4], sum[4];
int s, t[maxn], x[maxn];

int calc(int t, int f) {
    int k = lower_bound(all(vet[t]), f) - vet[t].begin();
    return sum[t][k] + max(vet[t][k] - f - s, 0);
}

int main() {
    int n, m, e, f;
    while (scanf("%d%d%d%d", &e, &s, &n, &m) != EOF) {
        form (i, 1, 3) vet[i].clear();
        forn (i, n) {
            scanf("%d%d", &t[i], &x[i]);
            if (x[i] < e) {
                for (int j = 1; j <= t[i]; ++ j) {
                    vet[j].pb(x[i]);
                }
            }
        }
        form (i, 1, 3) {
            vet[i].pb(e);
            sort(all(vet[i]));
            sum[i].resize(sz(vet[i]));
            sum[i].back() = 0;
            for (int j = sz(sum[i]) - 2; j >= 0; -- j) {
                sum[i][j] = sum[i][j + 1] + max(vet[i][j + 1] - vet[i][j] - s, 0);
            }
        }
        forn (i, m) {
            scanf("%d", &f);
            if (calc(1, f) > 0) {
                printf("-1 -1\n");
            } else {
                int a = calc(2, f);
                int b = calc(3, f);
                printf("%d %d\n", a, b - a);
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define N 300000

int e,s,n,m,x,y,f[3][N];
vector<int> d[3];

int calc(int x,int k){
	int t=lower_bound(d[k].begin(),d[k].end(),x)-d[k].begin();
	return f[k][t]+max(d[k][t]-x-s,0);
}

int main(){
	scanf("%d%d%d%d",&e,&s,&n,&m);
	for (int i=1;i<=n;++i){
		scanf("%d%d",&x,&y);
		if (y<e) for (int j=0;j<x;++j) d[j].push_back(y);
	}
	for (int i=0;i<3;++i){
		d[i].push_back(e);
		sort(d[i].begin(),d[i].end());
		n=d[i].size(); --n;
		for (int j=n-1;j>=0;--j)
			f[i][j]=f[i][j+1]+max(d[i][j+1]-d[i][j]-s,0);
	}
	while (m--){
		scanf("%d",&x);
		if (calc(x,0)) puts("-1 -1");
		else printf("%d %d\n",calc(x,1),calc(x,2)-calc(x,1));
	}

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<18;

int kap;
vector <P> E;
P rje[MAX];

P MaxPop(P pop, int sum)
{
  if (pop.X >= sum)
    return P(sum, 0);
  if (pop.X + pop.Y >= sum)
    return P(pop.X, sum - pop.X);
  return pop;
}

void Spoji(int tip, int udalj, P treb, P prom, P &ntreb, P &nprom)
{
  if (udalj > kap || treb == P(-1, -1)) {
    ntreb = P(-1, -1);
    return;
  }

  if (tip == 1) {
    ntreb = P(treb.X + udalj, treb.Y);
    nprom = MaxPop(P(prom.X + udalj, prom.Y), kap);
  }
  else if (tip == 2) {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    ntreb = P(treb.X - popX, treb.Y + udalj + popX);
    nprom = MaxPop(P(0, ntreb.Y), kap);
  }
  else {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    int popY = min(ost - popX, prom.Y);
    ntreb = P(treb.X - popX, treb.Y - popY);
    nprom = P(0, 0);
  }
}

int main()
{
  int kraj, n, q, zadx;
  P treb = P(0, 0), prom = P(0, 0);

  scanf("%d%d%d%d", &kraj, &kap, &n, &q); zadx = kraj;

  for (int i=0; i<n; i++) {
    int tip, x;
    scanf("%d%d", &tip, &x);
    E.push_back(P(x, tip));
  }

  for (int i=0; i<q; i++) {
    int x;
    scanf("%d", &x);
    E.push_back(P(x, -i));
  }

  sort(E.begin(), E.end(), greater<P>());

  for (int i=0; i<(int) E.size(); i++) {
    int udalj = zadx - E[i].X;

    int tip = E[i].Y;
    if (E[i].Y <= 0)
      tip = 3;

    P ntreb, nprom;
    Spoji(tip, udalj, treb, prom, ntreb, nprom);

    if (E[i].Y <= 0)
      rje[-E[i].Y] = ntreb;
    else {
      zadx = E[i].X;
      treb = ntreb;
      prom = nprom;
    }
  }

  for (int i=0; i<q; i++)
    printf("%d %d\n", rje[i].X, rje[i].Y);

  return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<18, VELB = 1<<16, KAND = (1<<16) - 1;

int kap;
vector <P> E;
P rje[MAX];
vector <P> V[2][VELB];

void Sortiraj()
{
  for (int i=0; i<(int) E.size(); i++)
    V[0][E[i].X & KAND].push_back(E[i]);

  for (int i=0; i<VELB; i++)
    for (int j=0; j<(int) V[0][i].size(); j++)
      V[1][V[0][i][j].X >> 16].push_back(V[0][i][j]);

  E.clear();

  for (int i=0; i<VELB; i++)
    for (int j=0; j<(int) V[1][i].size(); j++)
      E.push_back(V[1][i][j]);

  reverse(E.begin(), E.end());
}

P MaxPop(P pop, int sum)
{
  if (pop.X >= sum)
    return P(sum, 0);
  if (pop.X + pop.Y >= sum)
    return P(pop.X, sum - pop.X);
  return pop;
}

void Spoji(int tip, int udalj, P treb, P prom, P &ntreb, P &nprom)
{
  if (udalj > kap || treb == P(-1, -1)) {
    ntreb = P(-1, -1);
    return;
  }

  if (tip == 1) {
    ntreb = P(treb.X + udalj, treb.Y);
    nprom = MaxPop(P(prom.X + udalj, prom.Y), kap);
  }
  else if (tip == 2) {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    ntreb = P(treb.X - popX, treb.Y + udalj + popX);
    nprom = MaxPop(P(0, prom.Y + udalj + popX), kap);
  }
  else {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    int popY = min(ost - popX, prom.Y);
    ntreb = P(treb.X - popX, treb.Y - popY);
    nprom = P(0, 0);
  }
}

int main()
{
  int kraj, n, q, zadx;
  P treb = P(0, 0), prom = P(0, 0);

  scanf("%d%d%d%d", &kraj, &kap, &n, &q); kraj += (int) (1e9); zadx = kraj;

  for (int i=0; i<n; i++) {
    int tip, x;
    scanf("%d%d", &tip, &x); x += (int) (1e9);
    if (x < kraj)
      E.push_back(P(x, tip));
  }

  for (int i=0; i<q; i++) {
    int x;
    scanf("%d", &x); x += (int) (1e9);
    E.push_back(P(x, -i));
  }

  Sortiraj();

  for (int i=0; i<(int) E.size(); i++) {
    int udalj = zadx - E[i].X;

    int tip = E[i].Y;
    if (E[i].Y <= 0)
      tip = 3;

    P ntreb, nprom;
    Spoji(tip, udalj, treb, prom, ntreb, nprom);

    if (E[i].Y <= 0)
      rje[-E[i].Y] = ntreb;
    else {
      zadx = E[i].X;
      treb = ntreb;
      prom = nprom;
    }
  }

  for (int i=0; i<q; i++)
    printf("%d %d\n", rje[i].X, rje[i].Y);

  return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<18, VELB = 1<<16, KAND = (1<<16) - 1;

int kap;
vector <P> E;
P rje[MAX];
vector <P> V[2][VELB];

void Sortiraj()
{
  for (int i=0; i<(int) E.size(); i++)
    V[0][E[i].X & KAND].push_back(E[i]);

  for (int i=0; i<VELB; i++)
    for (int j=0; j<(int) V[0][i].size(); j++)
      V[1][V[0][i][j].X >> 16].push_back(V[0][i][j]);

  E.clear();

  for (int i=0; i<VELB; i++)
    for (int j=0; j<(int) V[1][i].size(); j++)
      E.push_back(V[1][i][j]);

  for (int i=0; i<(int) E.size() / 2; i++)
    swap(E[i], E[E.size() - 1 - i]);
}

P MaxPop(P pop, int sum)
{
  if (pop.X >= sum)
    return P(sum, 0);
  if (pop.X + pop.Y >= sum)
    return P(pop.X, sum - pop.X);
  return pop;
}

void Spoji(int tip, int udalj, P treb, P prom, P &ntreb, P &nprom)
{
  if (udalj > kap || treb == P(-1, -1)) {
    ntreb = P(-1, -1);
    return;
  }

  if (tip == 1) {
    ntreb = P(treb.X + udalj, treb.Y);
    nprom = MaxPop(P(prom.X + udalj, prom.Y), kap);
  }
  else if (tip == 2) {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    ntreb = P(treb.X - popX, treb.Y + udalj + popX);
    nprom = MaxPop(P(0, prom.Y + udalj + popX), kap);
  }
  else {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    int popY = min(ost - popX, prom.Y);
    ntreb = P(treb.X - popX, treb.Y - popY);
    nprom = P(0, 0);
  }
}

int main()
{
  int kraj, n, q, zadx;
  P treb = P(0, 0), prom = P(0, 0);

  scanf("%d%d%d%d", &kraj, &kap, &n, &q); kraj += (int) (1e9); zadx = kraj;

  for (int i=0; i<n; i++) {
    int tip, x;
    scanf("%d%d", &tip, &x); x += (int) (1e9);
    if (x < kraj)
      E.push_back(P(x, tip));
  }

  for (int i=0; i<q; i++) {
    int x;
    scanf("%d", &x); x += (int) (1e9);
    E.push_back(P(x, -i));
  }

  Sortiraj();

  for (int i=0; i<(int) E.size(); i++) {
    int udalj = zadx - E[i].X;

    int tip = E[i].Y;
    if (E[i].Y <= 0)
      tip = 3;

    P ntreb, nprom;
    Spoji(tip, udalj, treb, prom, ntreb, nprom);

    if (E[i].Y <= 0)
      rje[-E[i].Y] = ntreb;
    else {
      zadx = E[i].X;
      treb = ntreb;
      prom = nprom;
    }
  }

  for (int i=0; i<q; i++)
    printf("%d %d\n", rje[i].X, rje[i].Y);

  return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<18;

int kap;
vector <P> E;
P rje[MAX];

P MaxPop(P pop, int sum)
{
  if (pop.X >= sum)
    return P(sum, 0);
  if (pop.X + pop.Y >= sum)
    return P(pop.X, sum - pop.X);
  return pop;
}

void Spoji(int tip, int udalj, P treb, P prom, P &ntreb, P &nprom)
{
  if (udalj > kap || treb == P(-1, -1)) {
    ntreb = P(-1, -1);
    return;
  }

  if (tip == 1) {
    ntreb = P(treb.X + udalj, treb.Y);
    nprom = MaxPop(P(prom.X + udalj, prom.Y), kap);
  }
  else if (tip == 2) {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    ntreb = P(treb.X - popX, treb.Y + udalj + popX);
    nprom = MaxPop(P(0, prom.Y + udalj + popX), kap);
  }
  else {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    int popY = min(ost - popX, prom.Y);
    ntreb = P(treb.X - popX, treb.Y - popY);
    nprom = P(0, 0);
  }
}

int main()
{
  int kraj, n, q, zadx;
  P treb = P(0, 0), prom = P(0, 0);

  scanf("%d%d%d%d", &kraj, &kap, &n, &q); zadx = kraj;

  for (int i=0; i<n; i++) {
    int tip, x;
    scanf("%d%d", &tip, &x);
    E.push_back(P(x, tip));
  }

  for (int i=0; i<q; i++) {
    int x;
    scanf("%d", &x);
    E.push_back(P(x, -i));
  }

  sort(E.begin(), E.end(), greater<P>());

  for (int i=0; i<(int) E.size(); i++) {
    int udalj = zadx - E[i].X;

    int tip = E[i].Y;
    if (E[i].Y <= 0)
      tip = 3;

    P ntreb, nprom;
    Spoji(tip, udalj, treb, prom, ntreb, nprom);

    if (E[i].Y <= 0)
      rje[-E[i].Y] = ntreb;
    else {
      zadx = E[i].X;
      treb = ntreb;
      prom = nprom;
    }
  }

  for (int i=0; i<q; i++)
    printf("%d %d\n", rje[i].X, rje[i].Y);

  return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<18;

int kap;
vector <P> E;
P rje[MAX];

P MaxPop(P pop, int sum)
{
  if (pop.X >= sum)
    return P(sum, 0);
  if (pop.X + pop.Y >= sum)
    return P(pop.X, sum - pop.X);
  return pop;
}

void Spoji(int tip, int udalj, P treb, P prom, P &ntreb, P &nprom)
{
  if (udalj > kap || treb == P(-1, -1)) {
    ntreb = P(-1, -1);
    return;
  }

  if (tip == 1) {
    ntreb = P(treb.X + udalj, treb.Y);
    nprom = MaxPop(P(prom.X + udalj, prom.Y), kap);
  }
  else if (tip == 2) {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    ntreb = P(treb.X - popX, treb.Y + udalj + popX);
    nprom = MaxPop(P(0, prom.Y + udalj + popX), kap);
  }
  else {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    int popY = min(ost - popX, prom.Y);
    ntreb = P(treb.X - popX, treb.Y - popY);
    nprom = P(0, 0);
  }
}

int main()
{
  int kraj, n, q, zadx;
  P treb = P(0, 0), prom = P(0, 0);

  scanf("%d%d%d%d", &kraj, &kap, &n, &q); zadx = kraj;

  for (int i=0; i<n; i++) {
    int tip, x;
    scanf("%d%d", &tip, &x);
    if (x < kraj)
      E.push_back(P(x, tip));
  }

  for (int i=0; i<q; i++) {
    int x;
    scanf("%d", &x);
    E.push_back(P(x, -i));
  }

  sort(E.begin(), E.end(), greater<P>());

  for (int i=0; i<(int) E.size(); i++) {
    int udalj = zadx - E[i].X;

    int tip = E[i].Y;
    if (E[i].Y <= 0)
      tip = 3;

    P ntreb, nprom;
    Spoji(tip, udalj, treb, prom, ntreb, nprom);

    if (E[i].Y <= 0)
      rje[-E[i].Y] = ntreb;
    else {
      zadx = E[i].X;
      treb = ntreb;
      prom = nprom;
    }
  }

  for (int i=0; i<q; i++)
    printf("%d %d\n", rje[i].X, rje[i].Y);

  return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<18, VELB = 1<<16;

int kap;
vector <P> E;
P rje[MAX];
vector <P> V[2][VELB];

void Sortiraj()
{
  for (int i=0; i<(int) E.size(); i++)
    V[0][E[i].X % VELB].push_back(E[i]);

  for (int i=0; i<VELB; i++)
    for (int j=0; j<(int) V[0][i].size(); j++)
      V[1][V[0][i][j].X / VELB].push_back(V[0][i][j]);

  E.clear();

  for (int i=0; i<VELB; i++)
    for (int j=0; j<(int) V[1][i].size(); j++)
      E.push_back(V[1][i][j]);

  for (int i=0; i<(int) E.size() / 2; i++)
    swap(E[i], E[E.size() - 1 - i]);
}

P MaxPop(P pop, int sum)
{
  if (pop.X >= sum)
    return P(sum, 0);
  if (pop.X + pop.Y >= sum)
    return P(pop.X, sum - pop.X);
  return pop;
}

void Spoji(int tip, int udalj, P treb, P prom, P &ntreb, P &nprom)
{
  if (udalj > kap || treb == P(-1, -1)) {
    ntreb = P(-1, -1);
    return;
  }

  if (tip == 1) {
    ntreb = P(treb.X + udalj, treb.Y);
    nprom = MaxPop(P(prom.X + udalj, prom.Y), kap);
  }
  else if (tip == 2) {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    ntreb = P(treb.X - popX, treb.Y + udalj + popX);
    nprom = MaxPop(P(0, prom.Y + udalj + popX), kap);
  }
  else {
    int ost = kap - udalj;
    int popX = min(ost, prom.X);
    int popY = min(ost - popX, prom.Y);
    ntreb = P(treb.X - popX, treb.Y - popY);
    nprom = P(0, 0);
  }
}

int main()
{
  int kraj, n, q, zadx;
  P treb = P(0, 0), prom = P(0, 0);

  scanf("%d%d%d%d", &kraj, &kap, &n, &q); kraj += (int) (1e9); zadx = kraj;

  for (int i=0; i<n; i++) {
    int tip, x;
    scanf("%d%d", &tip, &x); x += (int) (1e9);
    if (x < kraj)
      E.push_back(P(x, tip));
  }

  for (int i=0; i<q; i++) {
    int x;
    scanf("%d", &x); x += (int) (1e9);
    E.push_back(P(x, -i));
  }

  Sortiraj();

  for (int i=0; i<(int) E.size(); i++) {
    int udalj = zadx - E[i].X;

    int tip = E[i].Y;
    if (E[i].Y <= 0)
      tip = 3;

    P ntreb, nprom;
    Spoji(tip, udalj, treb, prom, ntreb, nprom);

    if (E[i].Y <= 0)
      rje[-E[i].Y] = ntreb;
    else {
      zadx = E[i].X;
      treb = ntreb;
      prom = nprom;
    }
  }

  for (int i=0; i<q; i++)
    printf("%d %d\n", rje[i].X, rje[i].Y);

  return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>

#define N 300000

using namespace std;

int result_good[N];
int result_bad[N];
int s = 0;

void solve(vector<int *> & positions) {
    int ends[3] = {0, 0, 0};
    int starts[3] = {0, 0, 0};
    int gas[3] = {0, 0, 0};
    for (vector<int *>::iterator it = positions.begin(); it != positions.end(); ++it) {
        int * a = (*it);
        int pos = a[0], t = a[1], id = a[2];
        if (pos - s > *max_element(ends, ends + 3))
            break;
        for (int i = 0; i < 3; ++i) {
            if (starts[i] < pos - s)
                starts[i] = pos - s;
            if (ends[i] < pos - s)
                ends[i] = pos - s;
        }
        if (t == 3) {
            result_bad[id] = gas[0] - (ends[0] - starts[0]);
            result_good[id] = gas[1] - (ends[1] - starts[1]);
        } else {
            for (int i = 0; i < t + 1; ++i) {
                gas[i] -= ends[i] - starts[i];
                ends[i] = pos - s;
                starts[i] = pos - s;
            }
            starts[t] = *max_element(ends, ends + 3);
            ends[t] = pos;
            gas[t] += ends[t] - starts[t];
        }
    }
}

bool comp(int * a, int * b) {
    return a[0] < b[0];
}

int main() {
    memset(result_bad, -1, sizeof(int) * N);
    memset(result_good, -1, sizeof(int) * N);
    int e, n, m;
    scanf("%d %d %d %d", &e, &s, &n, &m);
    vector<int *> positive, negative;
    int t, x;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &t, &x);
        int * a = new int[3];
        a[1] = t - 1;
        a[2] = -1;
        if (x > e) {
            a[0] = x - e;
            positive.push_back(a);
        } else {
            a[0] = e - x;
            negative.push_back(a);
        }
    }
    for (int i = 0; i < m; ++i) {
        scanf("%d", &x);
        int * a = new int[3];
        a[1] = 3;
        a[2] = i;
        if (x > e) {
            a[0] = x - e;
            positive.push_back(a);
        } else if (x < e) {
            a[0] = e - x;
            negative.push_back(a);
        } else {
            result_bad[i] = 0;
            result_good[i] = 0;
        }
    }
    sort(positive.begin(), positive.end(), comp);
    sort(negative.begin(), negative.end(), comp);
    solve(positive);
    solve(negative);

    for (int i = 0; i < m; ++i) {
        printf("%d %d\n", result_bad[i], result_good[i]);
    }
    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <memory.h>

#define N 300000

using namespace std;

int result_good[N];
int result_bad[N];
int s = 0;

void solve(vector<int *> & positions) {
    int ends[3] = {0, 0, 0};
    int starts[3] = {0, 0, 0};
    int gas[3] = {0, 0, 0};
    for (vector<int *>::iterator it = positions.begin(); it != positions.end(); ++it) {
        int * a = (*it);
        int pos = a[0], t = a[1], id = a[2];
        if (pos - s > *max_element(ends, ends + 3))
            break;
        for (int i = 0; i < 3; ++i) {
            if (starts[i] < pos - s)
                starts[i] = pos - s;
            if (ends[i] < pos - s)
                ends[i] = pos - s;
        }
        if (t == 3) {
            result_bad[id] = gas[0] - (ends[0] - starts[0]);
            result_good[id] = gas[1] - (ends[1] - starts[1]);
        } else {
            for (int i = 0; i < t + 1; ++i) {
                gas[i] -= ends[i] - starts[i];
                ends[i] = pos - s;
                starts[i] = pos - s;
            }
            starts[t] = *max_element(ends, ends + 3);
            ends[t] = pos;
            gas[t] += ends[t] - starts[t];
        }
    }
}

bool comp(int * a, int * b) {
    return a[0] < b[0];
}

int main() {
    memset(result_bad, -1, sizeof(int) * N);
    memset(result_good, -1, sizeof(int) * N);
    int e, n, m;
    scanf("%d %d %d %d", &e, &s, &n, &m);
    vector<int *> positive, negative;
    int t, x;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &t, &x);
        int * a = new int[3];
        a[1] = t - 1;
        a[2] = -1;
        if (x > e) {
            a[0] = x - e;
            positive.push_back(a);
        } else {
            a[0] = e - x;
            negative.push_back(a);
        }
    }
    for (int i = 0; i < m; ++i) {
        scanf("%d", &x);
        int * a = new int[3];
        a[1] = 3;
        a[2] = i;
        if (x > e) {
            a[0] = x - e;
            positive.push_back(a);
        } else if (x < e) {
            a[0] = e - x;
            negative.push_back(a);
        } else {
            result_bad[i] = 0;
            result_good[i] = 0;
        }
    }
    sort(positive.begin(), positive.end(), comp);
    sort(negative.begin(), negative.end(), comp);
    solve(positive);
    solve(negative);

    for (int i = 0; i < m; ++i) {
        printf("%d %d\n", result_bad[i], result_good[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000000000007ll

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> iii;
const int maxn=400007;

int e,s,n,m,f[maxn],N;
vector<int> pos[4];
pii d[maxn];
vector<iii> edge[maxn];
bool free1[maxn];
set<iii> mys;

int getId(int i, int j){
    int r=0;
    for(int i0=0; i0<i; ++i0) r+=sz(pos[i0]);
    r+=j;
    return r;
}

bool cmp(const int &a, const int &b){
    return f[a]>f[b];
}

map<int,pii> ans;

int main(){
//    freopen("input.txt","r",stdin);
    scanf("%d%d%d%d",&e,&s,&n,&m);
    for(int i=1; i<=n; ++i){
        int t,x;
        scanf("%d%d",&t,&x);
        if(x<e) pos[t].pb(x);
    }
    for(int i=1; i<=m; ++i){
        int x;
        scanf("%d",&x);
        f[i]=x;
        if(x<e) pos[0].pb(x);
    }
    pos[3].pb(e);
    for(int t=0; t<=3; ++t) sort(pos[t].begin(),pos[t].end());

    for(int i=1; i<=3; ++i){
        for(int j=0; j<sz(pos[i]); ++j){
            int p=pos[i][j];
            int u=getId(i,j);
            bool found=0;

            for(int k=i; k<=3; ++k){
                int it=lower_bound(pos[k].begin(),pos[k].end(),p+1)-pos[k].begin();
                if(it>=sz(pos[k])) continue;

                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0,y=0;
                    if(i==1) x+=p2-p; else if(i==2) y+=p2-p;
                    int v=getId(k,it);
                    edge[u].pb(iii(pii(x,y),v));
                    found=1;
                }
            }

            if(found) continue;

            for(int k=i-1; k>=1; --k){
                int it=upper_bound(pos[k].begin(),pos[k].end(),p+s)-pos[k].begin();
                if(it==0) continue;
                --it;
                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0, y=0;
                    if(i==2){
                        y+=s;
                        x-=s-(p2-p);
                    }else if(i==3){
                        if(k==2) y-=s-(p2-p);
                        else x-=s-(p2-p);
                    }
                    int v=getId(k,it);
                    edge[u].pb(iii(pii(x,y),v));
                    break;
                }
            }
        }
    }

    for(int j=0; j<sz(pos[0]); ++j){
        int p=pos[0][j];
        int u=getId(0,j);
        bool found=0;
        for(int k=3; k<=3; ++k){
            int it=lower_bound(pos[k].begin(),pos[k].end(),p+1)-pos[k].begin();
            if(it>=sz(pos[k])) continue;

            int p2=pos[k][it];
            if(p<p2 && p+s>=p2){
                int x=0,y=0;
                int v=getId(k,it);
                edge[u].pb(iii(pii(x,y),v));
                found=1;
            }
        }

        if(found) continue;

        for(int k=2; k>=1; --k){
            int it=upper_bound(pos[k].begin(),pos[k].end(),p+s)-pos[k].begin();
            if(it==0) continue;
            --it;
            int p2=pos[k][it];
            if(p<p2 && p+s>=p2){
                int x=0, y=0;

                if(k==2) y-=s-(p2-p);
                else x-=s-(p2-p);

                int v=getId(k,it);
                edge[u].pb(iii(pii(x,y),v));
                break;
            }
        }
    }

    N=0;
    for(int i=0; i<=3; ++i) N==sz(pos[i]);

    d[N-1] = pii(0,0);
    int it[4];
    for(int i=0; i<=3; ++i) it[i]=sz(pos[i])-1;
    it[3]--;

    while(it[0]>=0 || it[1]>=0 || it[2]>=0 || it[3]>=0){
        int maxY=-1,i=-1,v2=-1;
        for(int v=0; v<=3; ++v)
            if(it[v]>=0 && (v2==-1 || maxY < pos[v][it[v]])){
                maxY = pos[v][it[v]];
                i=getId(v,it[v]);
                v2=v;
            }
        --it[v2];

        d[i]=pii(oo,oo);
        for(int j=0; j<sz(edge[i]); ++j){
            int v=edge[i][j].second;
            if(d[v].first!=oo){
                pii tmp = pii(d[v].first+edge[i][j].first.first,d[v].second+edge[i][j].first.second);
                if(d[i] > tmp){
                    d[i] = tmp;
                }
            }
        }
    }

    for(int i=0; i<sz(pos[0]); ++i){
        int p=pos[0][i];
        int u=getId(0,i);
        if(d[u].first!=oo)
            ans[p] = d[u];
    }

    for(int i=1; i<=m; ++i)
        if(ans.count(f[i])){
            pii t=ans[f[i]];
            printf("%I64d %I64d\n",t.first,t.second);
        }else{
            puts("-1 -1");
        }

}


#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000000000007ll

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> iii;
const int maxn=400007;

int e,s,n,m,f[maxn],N;
vector<int> pos[4];
pii d[maxn];
vector<iii> edge[maxn];
bool free1[maxn];
set<iii> mys;

int getId(int i, int j){
    int r=0;
    for(int i0=0; i0<i; ++i0) r+=sz(pos[i0]);
    r+=j;
    return r;
}

bool cmp(const int &a, const int &b){
    return f[a]>f[b];
}

map<int,pii> ans;

int main(){
//    freopen("input.txt","r",stdin);
    scanf("%d%d%d%d",&e,&s,&n,&m);
    for(int i=1; i<=n; ++i){
        int t,x;
        scanf("%d%d",&t,&x);
        pos[t].pb(x);
    }
    for(int i=1; i<=m; ++i){
        int x;
        scanf("%d",&x);
        f[i]=x;
        pos[0].pb(x);
    }
    pos[3].pb(e);
    for(int t=0; t<=3; ++t) sort(pos[t].begin(),pos[t].end());

    for(int i=1; i<=3; ++i){
        for(int j=0; j<sz(pos[i]); ++j){
            int p=pos[i][j];
            int u=getId(i,j);
            for(int k=1; k<=3; ++k){
                int it=lower_bound(pos[k].begin(),pos[k].end(),p+1)-pos[k].begin();
                if(it<sz(pos[k])){
                    int p2=pos[k][it];
                    if(p<p2 && p+s>=p2){
                        int x=0,y=0;
                        if(i==1) x+=p2-p; else if(i==2) y+=p2-p;
                        edge[getId(k,it)].pb(iii(pii(x,y),u));
                    }
                }
            }
        }
    }

    for(int j=0; j<sz(pos[0]); ++j){
        int p=pos[0][j];
        int u=getId(0,j);
        for(int k=1; k<=3; ++k){
            int it=lower_bound(pos[k].begin(), pos[k].end(),p)-pos[k].begin();
            if(it<sz(pos[k])){
                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0,y=0;
                    if(k==1) x=-(s-(p2-p)); else if(k==2) y=-(s-(p2-p));
                    edge[getId(k,it)].pb(iii(pii(x,y),u));
                }
            }
        }
    }

    N=n+m+1;
    for(int i=0; i<N; ++i) d[i]=pii(oo,oo), free1[i]=1;
    d[N-1] = pii(0,0);
    mys.insert(iii(d[N-1],N-1));
    while(!mys.empty()){
        int u = mys.begin()->second; mys.erase(mys.begin());
        free1[u]=0;
        for(int i=0; i<sz(edge[u]); ++i){
            iii p = edge[u][i];
            int v=p.second;
            pii cost = pii(d[u].first + p.first.first, d[u].second + p.first.second);
            if(free1[v] && d[v] > cost){
                if(v>=m){ //just update the the station vertex
                    if(d[v].first!=oo) mys.erase(iii(d[v],v));
                    mys.insert(iii(cost,v));
                }
                d[v]=cost;
            }
        }
    }

    for(int i=0; i<sz(pos[0]); ++i){
        int p=pos[0][i];
        int u=getId(0,i);
        if(d[u].first!=oo)
            ans[p] = d[u];
    }

    for(int i=1; i<=m; ++i)
        if(ans.count(f[i])){
            pii t=ans[f[i]];
            printf("%I64d %I64d\n",t.first,t.second);
        }else{
            puts("-1 -1");
        }
}


#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000000000007ll

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> iii;
const int maxn=400007;

int e,s,n,m,f[maxn],N;
vector<int> pos[4];
pii d[maxn];
vector<iii> edge[maxn];
bool free1[maxn];
set<iii> mys;

int getId(int i, int j){
    int r=0;
    for(int i0=0; i0<i; ++i0) r+=sz(pos[i0]);
    r+=j;
    return r;
}

bool cmp(const int &a, const int &b){
    return f[a]>f[b];
}

map<int,pii> ans;

int main(){
//    freopen("input.txt","r",stdin);
    scanf("%d%d%d%d",&e,&s,&n,&m);
    for(int i=1; i<=n; ++i){
        int t,x;
        scanf("%d%d",&t,&x);
        pos[t].pb(x);
    }
    for(int i=1; i<=m; ++i){
        int x;
        scanf("%d",&x);
        f[i]=x;
        pos[0].pb(x);
    }
    pos[3].pb(e);
    for(int t=0; t<=3; ++t) sort(pos[t].begin(),pos[t].end());

    for(int i=1; i<=3; ++i){
        for(int j=0; j<sz(pos[i]); ++j){
            int p=pos[i][j];
            int u=getId(i,j);
            for(int k=i; k<=3; ++k){
                int it=lower_bound(pos[k].begin(),pos[k].end(),p+1)-pos[k].begin();
                if(it>=sz(pos[k])) continue;

                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0,y=0;
                    if(i==1) x+=p2-p; else if(i==2) y+=p2-p;
                    int v=getId(k,it);
                    edge[v].pb(iii(pii(x,y),u));
                }
            }

            for(int k=1; k<i; ++k){
                int it=upper_bound(pos[k].begin(),pos[k].end(),p+s)-pos[k].begin();
                if(it==0) continue;
                --it;
                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0, y=0;
                    if(i==2){
                        y+=s;
                        x-=s-(p2-p);
                    }else if(i==3){
                        if(k==2) y-=s-(p2-p);
                        else x-=s-(p2-p);
                    }
                    int v=getId(k,it);
                    edge[v].pb(iii(pii(x,y),u));
                }
            }
        }
    }

    for(int j=0; j<sz(pos[0]); ++j){
        int p=pos[0][j];
        int u=getId(0,j);
        for(int k=3; k<=3; ++k){
            int it=lower_bound(pos[k].begin(),pos[k].end(),p+1)-pos[k].begin();
            if(it>=sz(pos[k])) continue;

            int p2=pos[k][it];
            if(p<p2 && p+s>=p2){
                int x=0,y=0;
                int v=getId(k,it);
                edge[v].pb(iii(pii(x,y),u));
            }
        }

        for(int k=1; k<3; ++k){
            int it=upper_bound(pos[k].begin(),pos[k].end(),p+s)-pos[k].begin();
            if(it==0) continue;
            --it;
            int p2=pos[k][it];
            if(p<p2 && p+s>=p2){
                int x=0, y=0;

                if(k==2) y-=s-(p2-p);
                else x-=s-(p2-p);

                int v=getId(k,it);
                edge[v].pb(iii(pii(x,y),u));
            }
        }
    }

    N=n+m+1;
    for(int i=0; i<N; ++i) d[i]=pii(oo,oo), free1[i]=1;
    d[N-1] = pii(0,0);
    mys.insert(iii(d[N-1],N-1));
    while(!mys.empty()){
        int u = mys.begin()->second; mys.erase(mys.begin());
        free1[u]=0;
        for(int i=0; i<sz(edge[u]); ++i){
            iii p = edge[u][i];
            int v=p.second;
            pii cost = pii(d[u].first + p.first.first, d[u].second + p.first.second);
            if(free1[v] && d[v] > cost){
                if(v>=m){ //just update the the station vertex
                    if(d[v].first!=oo) mys.erase(iii(d[v],v));
                    mys.insert(iii(cost,v));
                }
                d[v]=cost;
            }
        }
    }

    for(int i=0; i<sz(pos[0]); ++i){
        int p=pos[0][i];
        int u=getId(0,i);
        if(d[u].first!=oo)
            ans[p] = d[u];
    }

    for(int i=1; i<=m; ++i)
        if(ans.count(f[i])){
            pii t=ans[f[i]];
            printf("%I64d %I64d\n",t.first,t.second);
        }else{
            puts("-1 -1");
        }
}


#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000000000007ll

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> iii;
const int maxn=400007;

int e,s,n,m,f[maxn],N;
vector<int> pos[4];
pii d[maxn];
vector<iii> edge[maxn];
bool free1[maxn];
set<iii> mys;

int getId(int i, int j){
    int r=0;
    for(int i0=0; i0<i; ++i0) r+=sz(pos[i0]);
    r+=j;
    return r;
}

bool cmp(const int &a, const int &b){
    return f[a]>f[b];
}

map<int,pii> ans;

int main(){
//    freopen("input.txt","r",stdin);
    scanf("%d%d%d%d",&e,&s,&n,&m);
    for(int i=1; i<=n; ++i){
        int t,x;
        scanf("%d%d",&t,&x);
        pos[t].pb(x);
    }
    for(int i=1; i<=m; ++i){
        int x;
        scanf("%d",&x);
        f[i]=x;
        pos[0].pb(x);
    }
    pos[3].pb(e);
    for(int t=0; t<=3; ++t) sort(pos[t].begin(),pos[t].end());

    for(int i=1; i<=3; ++i){
        for(int j=0; j<sz(pos[i]); ++j){
            int p=pos[i][j];
            int u=getId(i,j);
            for(int k=1; k<=3; ++k){
                int it=lower_bound(pos[k].begin(),pos[k].end(),p+1)-pos[k].begin();
                if(it>=sz(pos[k])) continue;

                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0,y=0;
                    if(i==1) x+=p2-p; else if(i==2) y+=p2-p;
                    int v=getId(k,it);
                    edge[v].pb(iii(pii(x,y),u));
                    if(i>k){
                        for(int l=1; l<=3; ++l){
                            int it2=lower_bound(pos[l].begin(),pos[l].end(),p2+1)-pos[l].begin();
                            if(it2>=sz(pos[l])) continue;

                            int p3=pos[l][it2];
                            if(p2<p3 && p2+s>=p3){
                                x=0; y=0;
                                if(i==2){
                                    //k=1
                                    y=min(p3-p,s); x=p3-p-y;
                                }else if(i==3){
                                    if(k==2) y=p3-p-min(p3-p,s);
                                    else x=p3-p-min(p3-p,s);
                                }
                                edge[getId(l,it2)].pb(iii(pii(x,y),u));
                            }
                        }
                    }
                }

            }
        }
    }

    for(int j=0; j<sz(pos[0]); ++j){
        int p=pos[0][j];
        int u=getId(0,j);
        for(int k=1; k<=3; ++k){
            int it=lower_bound(pos[k].begin(), pos[k].end(),p)-pos[k].begin();
            if(it>=sz(pos[k])) continue;

            int p2=pos[k][it];
            if(p<p2 && p+s>=p2){
                int x=0,y=0;
                if(k==1) x=0; else if(k==2) y=0;
                edge[getId(k,it)].pb(iii(pii(x,y),u));

                for(int l=1; l<=3; ++l){
                    int it2=lower_bound(pos[l].begin(),pos[l].end(),p2+1)-pos[l].begin();
                    if(it2>=sz(pos[l])) continue;

                    int p3=pos[l][it2];
                    if(p2<p3 && p2+s>=p3){
                        x=0; y=0;

                        if(k==2) y=p3-p-min(p3-p,s);
                        else x=p3-p-min(p3-p,s);

                        edge[getId(l,it2)].pb(iii(pii(x,y),u));
                    }
                }

            }

        }
    }

    N=n+m+1;
    for(int i=0; i<N; ++i) d[i]=pii(oo,oo), free1[i]=1;
    d[N-1] = pii(0,0);
    mys.insert(iii(d[N-1],N-1));
    while(!mys.empty()){
        int u = mys.begin()->second; mys.erase(mys.begin());
        free1[u]=0;
        for(int i=0; i<sz(edge[u]); ++i){
            iii p = edge[u][i];
            int v=p.second;
            pii cost = pii(d[u].first + p.first.first, d[u].second + p.first.second);
            if(free1[v] && d[v] > cost){
                if(v>=m){ //just update the the station vertex
                    if(d[v].first!=oo) mys.erase(iii(d[v],v));
                    mys.insert(iii(cost,v));
                }
                d[v]=cost;
            }
        }
    }

    for(int i=0; i<sz(pos[0]); ++i){
        int p=pos[0][i];
        int u=getId(0,i);
        if(d[u].first!=oo)
            ans[p] = d[u];
    }

    for(int i=1; i<=m; ++i)
        if(ans.count(f[i])){
            pii t=ans[f[i]];
            printf("%I64d %I64d\n",t.first,t.second);
        }else{
            puts("-1 -1");
        }
}


#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000000000007ll

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,int> iii;
const int maxn=500007;

int e,s,n,m,f[maxn],N;
vector<int> pos[4];
pii d[maxn];
vector<iii> edge[maxn];
bool free1[maxn];
set<iii> mys;

int getId(int i, int j){
    int r=0;
    for(int i0=0; i0<i; ++i0) r+=sz(pos[i0]);
    r+=j;
    return r;
}

bool cmp(const int &a, const int &b){
    return f[a]>f[b];
}

map<int,pii> ans;

int main(){
//    freopen("input.txt","r",stdin);
    scanf("%d%d%d%d",&e,&s,&n,&m);
    for(int i=1; i<=n; ++i){
        int t,x;
        scanf("%d%d",&t,&x);
        pos[t].pb(x);
    }
    for(int i=1; i<=m; ++i){
        int x;
        scanf("%d",&x);
        f[i]=x;
        pos[0].pb(x);
    }
    pos[3].pb(e);
    for(int t=0; t<=3; ++t) sort(pos[t].begin(),pos[t].end());

    for(int i=1; i<=3; ++i){
        for(int j=0; j<sz(pos[i]); ++j){
            int p=pos[i][j];
            int u=getId(i,j);
            for(int k=1; k<=3; ++k){
                int it=lower_bound(pos[k].begin(),pos[k].end(),p)-pos[k].begin();
                if(it>0){
                    --it;
                    int p2=pos[k][it];
                    if(p2<p && p2+s>=p){
                        int x=0,y=0;
                        if(i==1) x+=p-p2; else if(i==2) y+=p-p2;
                        edge[u].pb(iii(pii(x,y),getId(k,it)));
                    }
                }
            }
        }
    }

    for(int j=0; j<sz(pos[0]); ++j){
        int p=pos[0][j];
        int u=getId(0,j);
        for(int k=1; k<=3; ++k){
            int it=lower_bound(pos[k].begin(), pos[k].end(),p)-pos[k].begin();
            if(it<sz(pos[k])){
                int p2=pos[k][it];
                if(p<p2 && p+s>=p2){
                    int x=0,y=0;
                    if(k==1) x+=p2-p; else if(k==2) y+=p2-p;
                    edge[getId(k,it)].pb(iii(pii(x,y),u));
                }
            }
        }
    }

    N=n+m+1;
    for(int i=0; i<N; ++i) d[i]=pii(oo,oo), free1[i]=1;
    d[N-1] = pii(0,0);
    mys.insert(iii(d[N-1],N-1));
    while(!mys.empty()){
        int u = mys.begin()->second; mys.erase(mys.begin());
        free1[u]=0;
        for(int i=0; i<sz(edge[u]); ++i){
            iii p = edge[u][i];
            int v=p.second;
            pii cost = pii(d[u].first + p.first.first, d[u].second + p.first.second);
            if(free1[v] && d[v] > cost){
                if(d[v].first!=oo) mys.erase(iii(d[v],v));
                mys.insert(iii(cost,v));
                d[v]=cost;
            }
        }
    }

    for(int i=0; i<sz(pos[0]); ++i){
        int p=pos[0][i];
        int u=getId(0,i);
        if(d[u].first!=oo)
            ans[p] = d[u];
    }

    for(int i=1; i<=m; ++i)
        if(ans.count(f[i])){
            pii t=ans[f[i]];
            printf("%I64d %I64d\n",t.first,t.second);
        }else{
            puts("-1 -1");
        }
}


#include <map>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

int main() {
    int ie, is, in, im;
    ll e,s,n,m;
    scanf("%d%d%d%d", &ie, &is, &in, &im);
    e = ie, s = is, n = in, m = im;
    vector<pair<ll, ll> > g, f;
    for (int i=0; i<n; i++) {
        int t, p;
        scanf("%d%d", &t, &p);
        g.push_back({t, e-p});
    }
    for (int i=0; i<m; i++) {
        int p;
        scanf("%d", &p);
        f.push_back({i, e-p});
    }
    auto comp = [](const pair<ll, ll> & a, const pair<ll, ll>& b) {return a.second < b.second;};

    sort(g.begin(), g.end(), comp);
    sort(f.begin(), f.end(), comp);

    ll pos, carb, ig, idf;
    pos = 0;
    carb = s;
    ig = idf = 0;
    vector<ll> conso (2, 0);
    vector<ll> last (3, -1);

    vector<pair<ll,ll> > res(f.size(), make_pair(-1, -1));

    while(pos <=f[f.size()-1].second) {

        // printf("DEBUG %d\n", pos);

        if (ig<g.size() && g[ig].second == pos) {
            last[g[ig].first -1] = g[ig].second;
            ig++;
        }

        if (idf<f.size() && f[idf].second == pos) {
            res[f[idf].first] = make_pair(conso[0], conso[1]);
            idf++;
        }

        if (pos == f[f.size()-1].second) break;

        ll nextstep = f[f.size()-1].second;

        if (ig<g.size()) {
            nextstep = min(nextstep, g[ig].second);
        }

        if (idf<f.size()) {
            nextstep = min(nextstep, f[idf].second);
        }

        if (carb>0) {
            nextstep = min(nextstep, pos+carb);
        }

        else if (pos - last[2] < s) {
            nextstep = min(nextstep, last[2]+s);
        }

        else if (pos - last[1] < s) {
            nextstep = min(nextstep, last[1]+s);
        }

        else if (pos - last[0] < s) {
            nextstep = min(nextstep, last[0]+s);
        }

        ll delta = nextstep - pos;

        if (carb > 0) {
            pos+=delta;
            carb -= delta;
        }

        else if (pos-last[2] < s) {
            pos+=delta;
        }

        else if (pos-last[1] < s) {
            pos+=delta;
            conso[1]+=delta;
        }

        else if (pos-last[0] < s) {
            pos+=delta;
            conso[0]+=delta;
        }

        else  {
            break;
        }
    }

    for (pair<ll, ll>& p : res) {
        printf("%d %d\n", int(p.first), int(p.second));
    }

    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

int main() {
    int e,s,n,m;
    scanf("%d%d%d%d", &e, &s, &n, &m);
    vector<pair<int, int> > g, f;
    for (int i=0; i<n; i++) {
        int t, p;
        scanf("%d%d", &t, &p);
        g.push_back({t, e-p});
    }
    for (int i=0; i<m; i++) {
        int p;
        scanf("%d", &p);
        f.push_back({i, e-p});
    }
    auto comp = [](const pair<int, int> & a, const pair<int, int>& b) {return a.second < b.second;};

    sort(g.begin(), g.end(), comp);
    sort(f.begin(), f.end(), comp);

    int pos, carb, ig, idf;
    pos = 0;
    carb = s;
    ig = idf = 0;
    vector<int> conso (2, 0);
    vector<int> last (3, -1);

    vector<pair<int,int> > res(f.size(), make_pair(-1, -1));

    while(pos <=f[f.size()-1].second) {

        if (ig<g.size() && g[ig].second == pos) {
            last[g[ig].first -1] = g[ig].second;
            ig++;
        }

        if (idf<f.size() && f[idf].second == pos) {
            res[f[idf].first] = make_pair(conso[0], conso[1]);
            idf++;
        }

        if (pos == f[f.size()-1].second) break;

        int nextstep = f[f.size()-1].second;

        if (ig<g.size()) {
            nextstep = min(nextstep, g[ig].second);
        }

        if (idf<f.size()) {
            nextstep = min(nextstep, f[idf].second);
        }

        if (carb>0) {
            nextstep = min(nextstep, pos+carb);
        }

        else if (pos - last[2] < s) {
            nextstep = min(nextstep, last[2]+s);
        }

        else if (pos - last[1] < s) {
            nextstep = min(nextstep, last[1]+s);
        }

        else if (pos - last[0] < s) {
            nextstep = min(nextstep, last[0]+s);
        }

        int delta = nextstep - pos;

        if (carb > 0) {
            pos+=delta;
            carb -= delta;
        }

        else if (pos-last[2] < s) {
            pos+=delta;
        }

        else if (pos-last[1] < s) {
            pos+=delta;
            conso[1]+=delta;
        }

        else if (pos-last[0] < s) {
            pos+=delta;
            conso[0]+=delta;
        }

        else  {
            break;
        }
    }

    for (pair<int, int>& p : res) {
        printf("%d %d\n", p.first, p.second);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define maxn 200111

vector<int> v[4];
int dp[4][maxn];
int e, s, n, m;

int f(int a, int b){
	int x = lower_bound(v[a].begin(), v[a].end(), b) - v[a].begin();
	return dp[a][x] + max(0, v[a][x] - b - s);
}

int main(){
	scanf("%d%d%d%d", &e, &s, &n, &m);
	for(int i = 0; i < n; i++){
		int a, b;
		scanf("%d%d", &a, &b);

		if(b >= e)
			continue;

		for(int j = 1; j <= a; j++)
			v[j].push_back(b);
	}

	for(int i = 1; i <= 3; i++){
		v[i].push_back(e);
		sort(v[i].begin(), v[i].end());
		for(int j = v[i].size() - 1 - 1; j >= 0; j--)   // last pos before e
			dp[i][j] = dp[i][j + 1] + max(0, v[i][j + 1] - v[i][j] - s);
	}

	while(m--){
		int a;
		scanf("%d", &a);
		if(f(1, a) != 0)
			printf("-1 -1\n");
		else
			printf("%d %d\n", f(2, a), f(3, a) - f(2, a));
	}

}


#include <bits/stdc++.h>

using namespace std;

#define maxn 200111

vector<int> v[4];
int dp[4][maxn];
int e, s, n, m;

int f(int a, int b){
	int x = lower_bound(v[a].begin(), v[a].end(), b) - v[a].begin();
	return dp[a][x] + max(0, v[a][x] - b - s);
}

int main(){
	scanf("%d%d%d%d", &e, &s, &n, &m);
	for(int i = 0; i < n; i++){
		int a, b;
		scanf("%d%d", &a, &b);

		if(b >= e)
			continue;

		for(int j = 1; j <= a; j++)
			v[j].push_back(b);
	}

	for(int i = 1; i <= 3; i++){
		v[i].push_back(e);
		sort(v[i].begin(), v[i].end());
		for(int j = v[i].size() - 1 - 1; j >= 0; j--)   // last pos before e
			dp[i][j] = dp[i][j + 1] + max(0, v[i][j + 1] - v[i][j] - s);
	}

	while(m--){
		int a;
		scanf("%d", &a);
		if(f(1, a) != 0)
			printf("-1 -1\n");
		else
			printf("%d %d\n", f(2, a) - f(1, a), f(3, a) - f(2, a));
	}

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
using namespace std;
const int MAXN = 200010;
typedef long long LL;
typedef pair<int, LL> PIL;
int n, m, cnt[4][MAXN << 2];
LL e, s, Q[MAXN], S[4][MAXN << 2];
PIL P[MAXN];
vector<LL> A;
int main() {
  scanf("%I64d%I64d%d%d", &e, &s, &n, &m);
  A.push_back(e);
  for (int i = 1; i <= n; i++) {
    scanf("%d%I64d", &P[i].first, &P[i].second);
    A.push_back(P[i].second);
    A.push_back(P[i].second + s);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", &Q[i]);
    A.push_back(Q[i] + s);
  }
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  for (int i = 1; i <= n; i++) {
    int l = lower_bound(A.begin(), A.end(), P[i].second) - A.begin();
    int r = lower_bound(A.begin(), A.end(), P[i].second + s) - A.begin();
    for (int k = 1; k <= P[i].first; k++) cnt[k][l]++, cnt[k][r]--;
  }
  for (int k = 1; k <= 3; k++) {
    for (int i = 1; i < A.size(); i++) cnt[k][i] += cnt[k][i - 1];
    for (int i = 1; i < A.size(); i++)
      S[k][i] = S[k][i - 1] + (cnt[k][i - 1] > 0 ? A[i] - A[i - 1] : 0);
  }
  int T = lower_bound(A.begin(), A.end(), e) - A.begin();
  for (int i = 1; i <= m; i++) {
    int x = lower_bound(A.begin(), A.end(), Q[i] + s) - A.begin();
    LL ans1, ans2;
    if (x >= T)
      ans1 = 0, ans2 = 0;
    else {
      LL dis = A[T] - A[x];
      LL dis3 = S[3][T] - S[3][x];
      LL dis2 = S[2][T] - S[2][x];
      LL dis1 = S[1][T] - S[1][x];
      if (dis == dis3)
        ans1 = 0, ans2 = 0;
      else if (dis == dis2)
        ans1 = 0, ans2 = dis - dis3;
      else if (dis == dis1)
        ans1 = dis - dis2, ans2 = dis - dis3 - ans1;
      else
        ans1 = -1, ans2 = -1;
    }
    printf("%I64d %I64d\n", ans1, ans2);
  }
  return 0;
}


#include <bits/stdc++.h>

#define FOR(i,a,b) for( int i = (a); i < (int)(b); i++ )
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort( (x).begin(),(x).end() )
#define REVERSE(x) reverse( (x).begin(),(x).end() )
#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )
#define PW(x) (1LL<<(x))
#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vi> mi;
typedef vector<vl> ml;
typedef vector<vd> md;

const int INF = 1e9+10;
const ll INFLL = 1e18+10;
const ld EPS = 1e-8;
const ld EPSLD = 1e-14;
const ll MOD = 1e9+7;

template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }

const int MAX_N = 200010;

ll e, s, n, m;

vl xs[3];

ll dp[3][MAX_N];

int main(){

  scanf( "%lld %lld %lld %lld" , &e , &s , &n , &m );
  REP( gas , n ){
    ll t, x;
    scanf( "%lld %lld" , &t , &x );
    if( x < e ) REP( i , t ) xs[i].pb( x );
  }
  REP( i , 3 ) xs[i].pb( e );
  REP( i , 3 ){
    SORT( xs[i] );
    UNIQUE( xs[i] );
  }

  REP( i , 3 ) for( int j = (int)xs[i].size()-2; j >= 0; j-- ){
    dp[i][j] = dp[i][j+1] + max( 0LL , xs[i][j+1] - xs[i][j] - s );
  }

  REP( st , m ){
    ll f, shortage[3];
    scanf( "%lld" , &f );
    REP( i , 3 ){
      int p = lower_bound( ALL(xs[i]) , f ) - xs[i].begin();
      shortage[i] = dp[i][p] + max( 0LL , xs[i][p] - f - s );
    }
    if( shortage[0] > 0 ) printf( "-1 -1\n" );
    else printf( "%lld %lld\n" , shortage[1] , shortage[2]-shortage[1] );
  }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

unordered_map<int,int> mapping;

vector<int> gas[3];
vector<pair<int,int> > positions;
int dp[100001][2];
vector<int> start;
unordered_map<int,int> cindex;
vector<pair<int,pair<int,int> > > solution;
bool flag = false;
void solve(int n, int fin, int s){
	int e[3];
	int ee[3];
	ee[0] = e[0] = gas[0].size()-1;
	ee[1] = e[1] = gas[1].size()-1;
	ee[2] = e[2] = gas[2].size()-1;

	for(int i=n-2;i>=0;i--){
		while(ee[0] >= 0 && gas[0].at(ee[0]) > positions[i].first+s)
			ee[0]--;
		while(ee[1] >= 0 && gas[1].at(ee[1]) > positions[i].first+s)
			ee[1]--;
		while(ee[2] >= 0 && gas[2].at(ee[2]) > positions[i].first+s)
			ee[2]--;
		while(e[0] >= 0 && gas[0].at(e[0]) > positions[i].first)
			e[0]--;
		while(e[1] >= 0 && gas[1].at(e[1]) > positions[i].first)
			e[1]--;
		while(e[2] >= 0 && gas[2].at(e[2]) > positions[i].first)
			e[2]--;
		if((ee[0] == -1 || gas[0].at(ee[0]) <= positions[i].first) && (ee[1] == -1 || gas[1].at(ee[1]) <= positions[i].first) && (ee[2] == -1 || gas[2].at(ee[2]) <= positions[i].first))
			dp[i][0] = -1, dp[i][1] = -1, flag=true;
		if(flag){
			dp[i][0] = -1, dp[i][1] = -1;
			continue;
		}
		else{
			if(positions[i].second == 0){
				dp[i][0] = positions[i+1].first-positions[i].first+dp[i+1][0];
				dp[i][1] = dp[i+1][1];
			}
			else if(positions[i].second == 1 && ((ee[1] != -1 && gas[1].at(ee[1]) > positions[i].first) || (ee[2] != -1 && gas[2].at(ee[2]) > positions[i].first))){
				int go_to;
				if(ee[2] == -1 || gas[2].at(ee[2]) <= positions[i].first)
					go_to = gas[1].at(e[1]+1);
				else if(ee[1] == -1 || gas[1].at(ee[1]) <= positions[i].first)
					go_to = gas[2].at(e[2]+1);
				else
					go_to = min(gas[1].at(e[1]+1),gas[2].at(e[2]+1));

				dp[i][0] = dp[mapping[go_to]][0];
				dp[i][1] = go_to-positions[i].first+dp[mapping[go_to]][1];
			}
			else if(positions[i].second == 2 && ee[2] != -1 && gas[2].at(ee[2]) > positions[i].first){
				dp[i][0] = dp[mapping[gas[2].at(e[2]+1)]][0];
				dp[i][1] = dp[mapping[gas[2].at(e[2]+1)]][1];
			}
			else if(positions[i].second == 1){
				int go_to;
				if(ee[0] == -1){
					go_to = gas[1].at(ee[1]);
					dp[i][0] = dp[mapping[go_to]][0];
					dp[i][1] = go_to-positions[i].first+dp[mapping[go_to]][1];
				}
				else if(ee[1] == -1 || (gas[0].at(ee[0]) > gas[1].at(ee[1]))){
					go_to = gas[0].at(ee[0]);
					dp[i][0] = dp[mapping[go_to]][0]-s-positions[i].first+gas[0].at(ee[0]);
					dp[i][1] = s+dp[mapping[go_to]][1];
				}
				else{
					go_to = max(gas[0].at(ee[0]),gas[1].at(ee[1]));
					dp[i][0] = dp[mapping[go_to]][0];
					dp[i][1] = go_to-positions[i].first+dp[mapping[go_to]][1];
				}
			}
			else if(positions[i].second == 2){
				if(ee[1] == -1 || gas[1].at(ee[1]) <= positions[i].first){
					dp[i][0] = dp[mapping[gas[0].at(ee[0])]][0]-s-positions[i].first+gas[0].at(ee[0]);
					dp[i][1] = dp[mapping[gas[0].at(ee[0])]][1];
				}
				else{
					dp[i][0] = dp[mapping[gas[1].at(ee[1])]][0];
					dp[i][1] = dp[mapping[gas[1].at(ee[1])]][1]-s-positions[i].first+gas[1].at(ee[1]);
				}
			}
		}
	}
}
int main(){

	int e,s,n,m;
	cin >> e >> s >> n >> m;
	for(int i=0;i<n;i++){
		int type, pos;
		cin >> type >> pos;
		if(pos < e){
			positions.push_back(make_pair(pos,type-1));
			gas[type-1].push_back(pos);
		}
		else{
			i--;
			n--;
		}
	}
	positions.push_back(make_pair(e,2));
	gas[2].push_back(e);
	sort(positions.begin(),positions.end());
	sort(gas[0].begin(),gas[0].end());
	sort(gas[1].begin(),gas[1].end());
	sort(gas[2].begin(),gas[2].end());
	for(int i=0;i<positions.size();i++)
		mapping[positions[i].first] = i;
	solve(++n,e,s);
	for(int i=0;i<m;i++){
		int pos;
		cin >> pos;
		cindex[pos] = i;
		start.push_back(pos);
	}

	int ee[3];
	ee[0] = gas[0].size()-1;
	ee[1] = gas[1].size()-1;
	ee[2] = gas[2].size()-1;

	sort(start.begin(),start.end());
	for(int i=m-1;i>=0;i--){
		if(start.at(i)+s >= e){
			solution.push_back(make_pair(cindex[start.at(i)],make_pair(0,0)));
			continue;
		}
		while(ee[0] >= 0 && gas[0].at(ee[0]) > start.at(i)+s && dp[mapping[gas[0].at(ee[0])]][0] != -1)
			ee[0]--;
		while(ee[1] >= 0 && gas[1].at(ee[1]) > start.at(i)+s && dp[mapping[gas[1].at(ee[1])]][0] != -1)
			ee[1]--;
		while(ee[2] >= 0 && gas[2].at(ee[2]) > start.at(i)+s && dp[mapping[gas[2].at(ee[2])]][0] != -1)
			ee[2]--;
		if(ee[2] != -1 && gas[2].at(ee[2]) > start.at(i) && dp[mapping[gas[2].at(ee[2])]][0] != -1)
			solution.push_back(make_pair(cindex[start.at(i)],make_pair(dp[mapping[gas[2].at(ee[2])]][0],dp[mapping[gas[2].at(ee[2])]][1])));
		else if(ee[1] != -1 && gas[1].at(ee[1]) > start.at(i) && dp[mapping[gas[1].at(ee[1])]][0] != -1)
			solution.push_back(make_pair(cindex[start.at(i)],make_pair(dp[mapping[gas[1].at(ee[1])]][0],dp[mapping[gas[1].at(ee[1])]][1]-s+gas[1].at(ee[1])-start.at(i))));
		else if(ee[0] != -1 && gas[0].at(ee[0]) > start.at(i) && dp[mapping[gas[0].at(ee[0])]][0] != -1)
			solution.push_back(make_pair(cindex[start.at(i)],make_pair(dp[mapping[gas[0].at(ee[0])]][0]-s+gas[0].at(ee[0])-start.at(i),dp[mapping[gas[0].at(ee[0])]][1])));
		else
			solution.push_back(make_pair(cindex[start.at(i)],make_pair(-1,-1)));
	}
	sort(solution.begin(),solution.end());
	for(int i=0;i<m;i++)
		cout << solution[i].second.first << " " << solution[i].second.second << endl;
	return 0;
}