#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	tot = min(n, 150);
	for (int i = 0; i < tot; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[i]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 10, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<ll, ll> ma;

ll Hash(vector<int> v) {
	ll ans = 0;
	for (int i : v) ans = (ans * kP + i) % kMod;
	return ans;
}

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ma[Hash(ng[i])] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 880301, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<ll, ll> ma;

ll Hash(vector<int> v) {
	ll ans = 0;
	for (int i : v) ans = (ans * kP + i) % kMod;
	return ans;
}

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) ma[Hash(ng[i])] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
ll solve() {
	int n, m, l, r, now = 1;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	while (now <= n) {
		l = now++;
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[l]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 10, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<vector<int>, ll> ma;

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ma[ng[i]] += c[i];
	for (pair<vector<int>, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot;
	ll ans = 0, tmp;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	tot = min(n, 200);
	for (int i = 0; i < tot; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[i]);
			}
			for (int j : graph[l]) went[j] = false;
		}
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r;
	ll ans = 0, tmp;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	for (int i = 0; i < 200; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[i]);
			}
			for (int j : graph[l]) went[j] = false;
		}
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 10, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<vector<int>, ll> ma;

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) sort(ng[i].begin(), ng[i].end());
	ma.clear();
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ma[ng[i]] += c[i];
	for (pair<vector<int>, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
int rng(int n) {
	static ull x = 19487129459uLL, y = 1238947123895uLL;
	x ^= y;
	y += x;
	x = x % n + 1;
	return x;
}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
bool went[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
ll solve() {
	int n, m, l, r;
	ll ans = 0, tmp, x, y, z;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 0; i < 500; i++) {
		l = rng(n), r = rng(n);
		if (l == r) {
			tmp = 0;
			for (int j : graph[l]) tmp += c[j];
			ans = gcd(ans, tmp);
		}
		else {
			x = 0, y = 0, z = 0;
			for (int j : graph[l]) {
				x += c[j];
				z += c[j];
				went[j] = true;
			}
			for (int j : graph[r]) {
				y += c[j];
				if (!went[j]) z += c[j];
			}
			for (int j : graph[l]) went[j] = false;
			ans = gcd(ans, gcd(gcd(x, y), z));
		}
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 10, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<ll, ll> ma;

ll Hash(vector<int> v) {
	ll ans = 0;
	for (int i : v) ans = (ans * kP + i) % kMod;
	return ans;
}

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ma[Hash(ng[i])] += c[i];
	for (pair<ll, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	tot = min(n, 500);
	for (int i = 0; i < tot; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[i]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot, cnt = 0, now = 1;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	while (cnt < tot && now <= n) {
		l = pos[now++];
		if (used[l]) continue;
		else {
			cnt++;
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[l]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
int rng(int n) {
	static ll x = 19487129459LL, y = 1238947123895LL;
	x ^= y;
	y += x;
	x = x % n + 1;
	return x;
}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
bool went[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
ll solve() {
	int n, m, l, r;
	ll ans = 0, tmp, x, y, z;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 0; i < 500; i++) {
		l = rng(n), r = rng(n);
		if (l == r) {
			tmp = 0;
			for (int j : graph[l]) tmp += c[j];
			ans = gcd(ans, tmp);
		}
		else {
			x = 0, y = 0, z = 0;
			for (int j : graph[l]) {
				x += c[j];
				z += c[j];
				went[j] = true;
			}
			for (int j : graph[r]) {
				y += c[j];
				if (!went[j]) z += c[j];
			}
			for (int j : graph[l]) went[j] = false;
			ans = gcd(ans, gcd(gcd(x, y), z));
		}
	}

	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	tot = min(n, 150);
	for (int i = 0; i < tot; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[l]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
int rng(int n) {
	static ull x = 19487129459uLL, y = 1238947123895uLL;
	x ^= y;
	y += x;
	x = x % n + 1;
	return x;
}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
ll solve() {
	int n, m, l, r;
	ll ans = 0, tmp, ori;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 0; i < 500; i++) {
		l = rng(n);
		if (graph[l].empty()) continue;
		else {
			ori = 0;
			for (int j : graph[l]) {
				ori += c[j];
				went[j] = true;
			}
			for (int j = 1; j <= n; j++) if (j != l) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + ori);
			}
			for (int j : graph[l]) went[j] = false;
		}
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
ll solve() {
	int n, m, l, r, now = 1;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	//ma.clear();
	//for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	//for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	while (now <= n) {
		l = now++;
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[l]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include<bits/stdc++.h>
#include<vector>

using namespace std;

bool sortinasce(const pair<long long int,long long int>&a,const pair<long long int,long long int>&b)
{
	return (a.first<b.first);
}
long long int binary_search(vector<pair<long long int ,long long int>>base,long long int s,long long int e,long long int a)
{
	long long int sum=0,in=-1,l=s,r=e;
	long long int mid;
	while(l<r)
	{
		mid=(l+r)/2;
		if(base[mid].first==a)
		{
			while(mid+1<=e && base[mid+1].first==a)
			{
				mid++;
			}
			in=mid;
			break;
		}
		else if(base[mid].first>a)
		{
			r=mid;
		}
		else
		l=mid+1;
	}
	while(mid>-1 && base[mid].first>a)
	mid--;

	for(long long int i=0;i<=mid;i++)
	{
		sum=sum+base[i].second;
	}
	return sum;

}
int main()
{
	long long int s,b,i,j,k,d,ans,g;
	cin>>s>>b;
	long long int arr[s];
	for(i=0;i<s;i++)
	{
		cin>>arr[i];
	}
	vector<pair<long long int ,long long int>>base;
	for(i=0;i<b;i++)
	{
		cin>>d>>g;
		base.push_back(make_pair(d,g));
	}
	sort(base.begin(),base.end(),sortinasce);
	for(i=0;i<s;i++)
	{
		ans=binary_search(base,0,s-1,arr[i]);
		cout<<ans<<" ";
	}

}


#include<bits/stdc++.h>
#include<vector>

using namespace std;

bool sortinasce(const pair<long long int,long long int>&a,const pair<long long int,long long int>&b)
{
	return (a.first<b.first);
}
long long int binary_search(vector<pair<long long int ,long long int>>base,long long int s,long long int e,long long int a)
{
	long long int sum=0,in=-1,l=s,r=e;
	long long int mid;
	while(l<=r)
	{
		mid=(l+r)/2;
		if(base[mid].first==a)
		{
			while(mid+1<=e && base[mid+1].first==a)
			{
				mid++;
			}
			in=mid;
			break;
		}
		else if(base[mid].first>a)
		{
			r=mid;
		}
		else
		l=mid+1;
	}
	while(mid>-1 && base[mid].first>a)
	mid--;

	for(long long int i=0;i<=mid;i++)
	{
		sum=sum+base[i].second;
	}
	return sum;

}
int main()
{
	long long int s,b,i,j,k,d,ans,g;
	cin>>s>>b;
	long long int arr[s];
	for(i=0;i<s;i++)
	{
		cin>>arr[i];
	}
	vector<pair<long long int ,long long int>>base;
	for(i=0;i<b;i++)
	{
		cin>>d>>g;
		base.push_back(make_pair(d,g));
	}
	sort(base.begin(),base.end(),sortinasce);
	for(i=0;i<s;i++)
	{
		ans=binary_search(base,0,s-1,arr[i]);
		cout<<ans<<" ";
	}

}


#include<bits/stdc++.h>
#include<vector>

using namespace std;

bool sortinasce(const pair<long long int,long long int>&a,const pair<long long int,long long int>&b)
{
	return (a.first<b.first);
}
long long int binary_search(vector<pair<long long int ,long long int>>base,long long int s,long long int n,long long int a)
{
	long long int sum=0,in=-1,l=s,r=e;
	long long int mid;
	while(l<r)
	{
		mid=(l+r)/2;
		if(base[mid].first==a)
		{
			while(mid+1<n && base[mid+1].first==a)
			{
				mid++;
			}
			in=mid;
			break;
		}
		else if(base[mid].first>a)
		{
			r=mid;
		}
		else
		l=mid+1;
	}
	while(mid>-1 && base[mid].first>a)
	mid--;

	for(long long int i=0;i<=mid;i++)
	{
		sum=sum+base[i].second;
	}
	return sum;

}
int main()
{
	long long int s,b,i,j,k,d,ans,g;
	cin>>s>>b;
	long long int arr[s];
	for(i=0;i<s;i++)
	{
		cin>>arr[i];
	}
	vector<pair<long long int ,long long int>>base;
	for(i=0;i<b;i++)
	{
		cin>>d>>g;
		base.push_back(make_pair(d,g));
	}
	sort(base.begin(),base.end(),sortinasce);
	for(i=0;i<s;i++)
	{
		ans=binary_search(base,0,b,arr[i]);
		cout<<ans<<" ";
	}

}


#include<bits/stdc++.h>
#include<vector>

using namespace std;

bool sortinasce(const pair<long long int,long long int>&a,const pair<long long int,long long int>&b)
{
	return (a.first<b.first);
}
long long int binary_search(vector<pair<long long int ,long long int>>base,long long int s,long long int n,long long int a)
{
	long long int sum=0,in=-1,l=s,r=n;
	long long int mid;
	while(l<r)
	{
		mid=(l+r)/2;
		if(base[mid].first==a)
		{
			while(mid+1<n && base[mid+1].first==a)
			{
				mid++;
			}
			in=mid;
			break;
		}
		else if(base[mid].first>a)
		{
			r=mid;
		}
		else
		l=mid+1;
	}
	while(mid>-1 && base[mid].first>a)
	mid--;

	for(long long int i=0;i<=mid;i++)
	{
		sum=sum+base[i].second;
	}
	return sum;

}
int main()
{
	long long int s,b,i,j,k,d,ans,g;
	cin>>s>>b;
	long long int arr[s];
	for(i=0;i<s;i++)
	{
		cin>>arr[i];
	}
	vector<pair<long long int ,long long int>>base;
	for(i=0;i<b;i++)
	{
		cin>>d>>g;
		base.push_back(make_pair(d,g));
	}
	sort(base.begin(),base.end(),sortinasce);
	for(i=0;i<s;i++)
	{
		ans=binary_search(base,0,b,arr[i]);
		cout<<ans<<" ";
	}

}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int s, b, d, g, psum[100004], _s[100004];
vector<pair<int, int>> a;
int main(){
    cin >> s >> b;
    for(int i = 0; i < s; i++){
        cin >> _s[i];
    }
    for(int i = 0; i < b; i++){
        cin >> d >> g;
        a.push_back({d, g});
    }
    sort(a.begin(), a.end());
    psum[0] = a[0].second;
    for(int i = 1; i < b; i++){
        psum[i] = psum[i - 1] + a[i].second;
    }
    for(int i = 0; i < s; i++){
        for(int j = b - 1; j >= 0; j--){
            if(a[j].first <= _s[i]){
                cout << psum[j] << "\n";
                break;
            }
        }
    }
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int s, b, d, g, psum[100004], temp, real_ret[100004];
vector<pair<int, int>> a, ret;
int main(){
    cin >> s >> b;
    for(int i = 0; i < s; i++){
        cin >> temp;
        ret.push_back({temp, i});
    }
    for(int i = 0; i < b; i++){
        cin >> d >> g;
        a.push_back({d, g});
    }
    sort(ret.begin(), ret.end());
    sort(a.begin(), a.end());
    psum[0] = a[0].second;
    for(int i = 1; i < b; i++){
        psum[i] = psum[i - 1] + a[i].second;
    }
    int cnt = 0, idx = 0, _ret;
    for(int i = 0; i < s; i++){
        for(int j = idx; j < b; j++){
            if(a[j].first > ret[i].first){
                break;
            }else{
                idx++;
                _ret = psum[j];
            }
        }
        real_ret[ret[i].second] = _ret;
    }
    for(int i = 0; i < s; i++){
        cout << real_ret[i] << "\n";
    }
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
long long s, b, d, g, psum[100004], temp, real_ret[100004];
vector<pair<int, int>> a, ret;
int main(){
    cin >> s >> b;
    for(int i = 0; i < s; i++){
        cin >> temp;
        ret.push_back({temp, i});
    }
    for(int i = 0; i < b; i++){
        cin >> d >> g;
        a.push_back({d, g});
    }
    sort(ret.begin(), ret.end());
    sort(a.begin(), a.end());
    psum[0] = a[0].second;
    for(int i = 1; i < b; i++){
        psum[i] = psum[i - 1] + a[i].second;
    }
    long long cnt = 0, idx = 0, _ret;
    for(int i = 0; i < s; i++){
        for(int j = idx; j < b; j++){
            if(a[j].first > ret[i].first){
                break;
            }else{
                idx++;
                _ret = psum[j];
            }
        }
        real_ret[ret[i].second] = _ret;
    }
    for(int i = 0; i < s; i++){
        cout << real_ret[i] << "\n";
    }
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
long long s, b, d, g, psum[100004], temp, real_ret[100004];
vector<pair<int, int>> a, ret;
int main(){
    cin >> s >> b;
    for(int i = 0; i < s; i++){
        cin >> temp;
        ret.push_back({temp, i});
    }
    for(int i = 0; i < b; i++){
        cin >> d >> g;
        a.push_back({d, g});
    }
    sort(ret.begin(), ret.end());
    sort(a.begin(), a.end());
    psum[0] = a[0].second;
    for(int i = 1; i < b; i++){
        psum[i] = psum[i - 1] + a[i].second;
    }
    long long cnt = 0, idx = 0, _ret = 0;
    for(int i = 0; i < s; i++){
        for(int j = idx; j < b; j++){
            if(a[j].first > ret[i].first){
                break;
            }else{
                idx++;
                _ret = psum[j];
            }
        }
        real_ret[ret[i].second] = _ret;
    }
    for(int i = 0; i < s; i++){
        cout << real_ret[i] << "\n";
    }
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int s, b, d, g, psum[100004], _s[100004];
vector<pair<int, int>> a;
int main(){
    cin >> s >> b;
    for(int i = 0; i < s; i++){
        cin >> _s[i];
    }
    for(int i = 0; i < b; i++){
        cin >> d >> g;
        a.push_back({d, g});
    }
    sort(a.begin(), a.end());
    psum[a[0].first] = a[0].second;
    for(int i = 1; i < b; i++){
        int _i = a[i].first;
        int before = a[i - 1].first;
        int cost = a[i].second;
        psum[_i] = psum[before] + cost;
    }
    for(int i = 0; i < s; i++){
        for(int j = _s[i]; j >= 0; j--){
            if(psum[j]){
                cout << psum[j] << "\n";
                break;
            }
        }
    }
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
long long s, b, d, g, psum[100004], temp, real_ret[100004];
vector<pair<int, int>> a, ret;
int main(){
    cin >> s >> b;
    for(int i = 0; i < s; i++){
        cin >> temp;
        ret.push_back({temp, i});
    }
    for(int i = 0; i < b; i++){
        cin >> d >> g;
        a.push_back({d, g});
    }
    sort(ret.begin(), ret.end());
    sort(a.begin(), a.end());
    psum[0] = a[0].second;
    for(int i = 1; i < b; i++){
        psum[i] = psum[i - 1] + a[i].second;
    }
    int cnt = 0, idx = 0, _ret;
    for(int i = 0; i < s; i++){
        for(int j = idx; j < b; j++){
            if(a[j].first > ret[i].first){
                break;
            }else{
                idx++;
                _ret = psum[j];
            }
        }
        real_ret[ret[i].second] = _ret;
    }
    for(int i = 0; i < s; i++){
        cout << real_ret[i] << "\n";
    }
	return 0;
}


#include <utility>
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int main(){
	  ll N=1e9;
      ll s,b;
	  ll x=0;
	  cin>>s>>b;
	  ll a[s];
      for(int i=0; i<s; i++)
	   cin>>a[i];
	   pair<ll,ll>p[b];
      for(int i=0;i<b;i++)
	  cin>>p[i]. first>>p[i]. second;
	  sort(p, p+b) ;
      for(int i=0;i<s;i++){
		  ll l=0;
       x=lower_bound(p, p+b, make_pair(a[i], N))-p;
	   for(int n=0;n<x;n++)
	   l+=p[n]. second;
	   cout<<l<<" ";
	  }
      return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define _for(i,a,b) for(int i = (a); i < (b); ++i)
#define _fore(i,a,b) for(int i = (a); i <= (b); ++i)
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define MEM(a,b) memset(a,b,sizeof(a))

using namespace std;

const int N = (int)1e5 + 5;

struct ARR {
    ll num;
    int id;
    ll ans;
}arr[N];

struct Node {
    ll d, g;
} node[N];

int main() {
    int s, b;
    cin >> s >> b;
    _for(i, 0, s) {
        cin >> arr[i].num;
        arr[i].id = i;
    }
    _for(i, 0, b) {
        cin >> node[i].d >> node[i].g;
    }
    sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.num < a2.num;
    });
    sort(node, node + b, [](const Node & n1, const Node & n2) {
         return n1.d < n2.d;
    });
    ll ans = 0, sum = 0;
    int index = 0;
    _for (i, 0, s) {
        ans = sum;
        _for(j, index, b) {
            if (arr[i].num >= node[j].d) {
                ans += node[j].g;
            } else {
                index = j;
                break;
            }
            sum += node[j].g;
        }
        arr[i].ans = ans;
    }
    sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.id < a2.id;
    });
    _for (i, 0, s) {
        cout << arr[i].ans << " \n"[i == s - 1];
    }
    return 0;
}


#include <utility>
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int main(){
	  ll N=1e9;
      ll s,b;
	  ll x=0;
	  cin>>s>>b;
	  ll a[s];
      for(register ll i=0; i<s; i++)
	   cin>>a[i];
	   pair<ll,ll>p[b];
      for(register ll i=0;i<b;i++)
	  cin>>p[i]. first>>p[i]. second;
	  sort(p, p+b) ;
      for(register ll i=0;i<s;i++){
		  ll l=0;
       x=lower_bound(p, p+b, make_pair(a[i], N))-p;
	   for(register ll n=0;n<x;n++)
	   l+=p[n]. second;
	   cout<<l<<" ";
	  }
      return 0;
}


#include <utility>
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef register long long ll;
int main(){
	  ll N=1e9;
      ll s,b;
	  ll x=0;
	  cin>>s>>b;
	  ll a[s];
      for(ll i=0; i<s; i++)
	   cin>>a[i];
	   pair<ll,ll>p[b];
      for(ll i=0;i<b;i++)
	  cin>>p[i]. first>>p[i]. second;
	  sort(p, p+b) ;
      for(ll i=0;i<s;i++){
		  ll l=0;
       x=lower_bound(p, p+b, make_pair(a[i], N))-p;
	   for(ll n=0;n<x;n++)
	   l+=p[n]. second;
	   cout<<l<<" ";
	  }
      return 0;
}


#include <utility>
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int main(){
      ios::sync_with_stdio(false);
	  ll N=1e9;
      ll s,b;
	  ll x=0;
	  cin>>s>>b;
	  ll a[s];
      for(ll i=0; i<s; i++)
	   cin>>a[i];
	   pair<ll,ll>p[b];
      for(ll i=0;i<b;i++)
	  cin>>p[i]. first>>p[i]. second;
	  sort(p, p+b) ;
      for(ll i=0;i<s;i++){
		  ll l=0;
       x=lower_bound(p, p+b, make_pair(a[i], N))-p;
	   for(ll n=0;n<x;n++)
	   l+=p[n]. second;
	   cout<<l<<" ";
	  }
      return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define _for(i,a,b) for(ll i = (a); i < (b); ++i)
#define _fore(i,a,b) for(ll i = (a); i <= (b); ++i)
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define MEM(a,b) memset(a,b,sizeof(a))

using namespace std;

const int N = (int)1e5 + 5;

struct ARR {
    ll num;
    ll id;
    ll ans;
}arr[N];

struct Node {
    ll d, g;
} node[N];

int main() {
    ll s, b;
    cin >> s >> b;
    _for(i, 0, s) {
        cin >> arr[i].num;
        arr[i].id = i;
    }
    _for(i, 0, b) {
        cin >> node[i].d >> node[i].g;
    }
    sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.num < a2.num;
    });
    sort(node, node + b, [](const Node & n1, const Node & n2) {
         return n1.d < n2.d;
    });
    ll ans = 0, sum = 0;
    ll index = 0;
    _for (i, 0, s) {
        ans = sum;
        _for(j, index, b) {
            if (arr[i].num >= node[j].d) {
                ans += node[j].g;
            } else {
                index = j;
                break;
            }
            sum += node[j].g;
        }
        arr[i].ans = ans;
    }
    sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.id < a2.id;
    });
    _for (i, 0, s) {
        cout << arr[i].ans << " \n"[i == s - 1];
    }
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define _for(i,a,b) for(int i = (a); i < (b); ++i)
#define _fore(i,a,b) for(int i = (a); i <= (b); ++i)
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define MEM(a,b) memset(a,b,sizeof(a))

using namespace std;

const int N = (int)1e5 + 5;

struct ARR {
    int num;
    int id;
    int ans;
}arr[N];

struct Node {
    int d, g;
} node[N];

int main() {
    int s, b;
    cin >> s >> b;
    _for(i, 0, s) {
        cin >> arr[i].num;
        arr[i].id = i;
    }
    _for(i, 0, b) {
        cin >> node[i].d >> node[i].g;
    }
    sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.num < a2.num;
    });
    sort(node, node + b, [](const Node & n1, const Node & n2) {
         return n1.d < n2.d;
    });
    int ans = 0, index = 0, sum = 0;
    _for (i, 0, s) {
        ans = sum;
        _for(j, index, b) {
            if (arr[i].num >= node[j].d) {
                ans += node[j].g;
            } else {
                index = j;
                break;
            }
            sum += node[j].g;
        }
        arr[i].ans = ans;
    }
    sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.id < a2.id;
    });
    _for (i, 0, s) {
        cout << arr[i].ans << " \n"[i == s - 1];
    }
    return 0;
}


#include <utility>
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
int main(){
      ios::sync_with_stdio(false);
	  ll N=1e9;
      ll s,b;
	  ll x=0;
	  cin>>s>>b;
	  ll a[s];
      for(ll i=0; i<s; i++)
	   cin>>a[i];
	   pair<ll,ll>p[b];
      for(ll i=0;i<b;i++)
	  cin>>p[i]. first>>p[i]. second;
	  sort(p, p+b) ;
      for(ll i=0;i<s;i++){
		  ll l=0;
       x=lower_bound(p, p+b, make_pair(a[i], N))-p;
	   for(ll n=0;n<x;n++)
	   l+=p[n]. second;
	   cout<<l<<" ";
	  }
      return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define _for(i,a,b) for(int i = (a); i < (b); ++i)
#define _fore(i,a,b) for(int i = (a); i <= (b); ++i)
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define MEM(a,b) memset(a,b,sizeof(a))

using namespace std;

const int N = (int)1e5 + 5;

struct ARR {
    ll num;
    int id;
    ll ans;
}arr[N];

struct Node {
    ll d, g;
} node[N];

int main() {
    int s, b;
    cin >> s >> b;
    _for(i, 0, s) {
        cin >> arr[i].num;
        arr[i].id = i;
    }
    _for(i, 0, b) {
        cin >> node[i].d >> node[i].g;
    }
    stable_sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.num < a2.num;
    });
    stable_sort(node, node + b, [](const Node & n1, const Node & n2) {
         return n1.d < n2.d;
    });
    ll ans = 0, sum = 0;
    int index = 0;
    _for (i, 0, s) {
        ans = sum;
        _for(j, index, b) {
            if (arr[i].num >= node[j].d) {
                ans += node[j].g;
            } else {
                index = j;
                break;
            }
            sum += node[j].g;
        }
        arr[i].ans = ans;
    }
    stable_sort(arr, arr + s, [](const ARR & a1, const ARR & a2){
         return a1.id < a2.id;
    });
    _for (i, 0, s) {
        cout << arr[i].ans << " \n"[i == s - 1];
    }
    return 0;
}


#include <bits/stdc++.h>
//#define int long long

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,b;
	cin>>n>>b;
	vector<int>arr(n);
	for(int i=0;i<n;++i) cin>>arr[i];
	vector<pair<int,long long> >base(b);
	for(int i=0;i<b;++i) cin>>base[i].first>>base[i].second;

	sort(base.begin(),base.end());
	for(int i=1;i<b;++i) base[i].second+=base[i-1].second;

	for(int i=0;i<n;++i){
		int lo=0,hi=b-1,mid;
		while(lo<=hi){
			mid=(lo+hi)/2;
			if(base[mid].first<=arr[i]){
				lo=mid+1;
			}else{
				hi=mid-1;
			}
		}
		cout<<base[lo-1].second<<" ";
	}
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
//#define int long long

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,b;
	cin>>n>>b;
	vector<int>arr(n);
	for(int i=0;i<n;++i) cin>>arr[i];
	vector<pair<int,long long> >base(b);
	for(int i=0;i<b;++i) cin>>base[i].first>>base[i].second;

	sort(base.begin(),base.end());
	for(int i=1;i<n;++i) base[i].second+=base[i-1].second;

	for(int i=0;i<n;++i){
		int lo=0,hi=n-1,mid;
		while(lo<=hi){
			mid=(lo+hi)/2;
			if(base[mid].first<=arr[i]){
				lo=mid+1;
			}else{
				hi=mid-1;
			}
		}
		cout<<base[lo-1].second<<" ";
	}
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,b;
	cin>>n>>b;
	vector<int>arr(n);
	for(int i=0;i<n;++i) cin>>arr[i];
	vector<pair<int,int> >base(b);
	for(int i=0;i<b;++i) cin>>base[i].first>>base[i].second;

	sort(base.begin(),base.end());
	for(int i=1;i<n;++i) base[i].second+=base[i-1].second;

	for(int i=0;i<n;++i){
		int lo=0,hi=n-1,mid;
		while(lo<=hi){
			mid=(lo+hi)/2;
			if(base[mid].first<=arr[i]){
				lo=mid+1;
			}else{
				hi=mid-1;
			}
		}
		cout<<base[lo-1].second<<" ";
	}
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,b;
	cin>>n>>b;
	vector<pair<int,int> >arr(n);
	vector<int>ans(n);
	for(int i=0;i<n;++i){
		cin>>arr[i].first;
		arr[i].second=i;
	}
	sort(arr.begin(),arr.end());
	vector<pair<int,int> >base(b);
	for(int i=0;i<b;++i) cin>>base[i].first>>base[i].second;
	sort(base.begin(),base.end());
	for(int i=1;i<b;++i) base[i].second += base[i-1].second;
	//for(auto i:base) cout<<i.first<<" "<<i.second<<endl;
	for(int i=0;i<n;++i){
		int lo=0,hi=b-1,mid;
		while(lo<=hi){
			mid=(lo+hi)/2;
			if(arr[i].first>=base[mid].first){
				lo=mid+1;
			}else{
				hi=mid-1;
			}
		}
		//cout<<arr[i].first<<" "<<lo-1<<endl;
		ans[arr[i].second]=base[lo-1].second;
	}
	for(int i:ans)cout<<i<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
//#define int long long

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,b;
	cin>>n>>b;
	vector<int>arr(n);
	for(int i=0;i<n;++i) cin>>arr[i];
	vector<pair<int,long long> >base(b);
	for(int i=0;i<b;++i) cin>>base[i].first>>base[i].second;

	sort(base.begin(),base.end());
	for(int i=1;i<b;++i) base[i].second+=base[i-1].second;

	for(int i=0;i<n;++i){
		int lo=0,hi=b-1,mid;
		while(lo<=hi){
			mid=(lo+hi)/2;
			if(base[mid].first<=arr[i]){
				lo=mid+1;
			}else{
				hi=mid-1;
			}
		}
		if(arr[i]<base[0].first)cout<<0<<" ";
		else cout<<base[lo-1].second<<" ";
	}
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
//#define int long long

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n,b;
	cin>>n>>b;
	vector<int>arr(n);
	for(int i=0;i<n;++i) cin>>arr[i];
	vector<pair<int,long long> >base(b);
	for(int i=0;i<b;++i) cin>>base[i].first>>base[i].second;

	sort(base.begin(),base.end());
	for(int i=1;i<b;++i) base[i].second+=base[i-1].second;

	for(int i=0;i<n;++i){
		int lo=0,hi=n-1,mid;
		while(lo<=hi){
			mid=(lo+hi)/2;
			if(base[mid].first<=arr[i]){
				lo=mid+1;
			}else{
				hi=mid-1;
			}
		}
		cout<<base[lo-1].second<<" ";
	}
	cout<<endl;
	return 0;
}


#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
	int n, k;
	cin >> n >> k;

	vector<long long> a(n), pref(k), pp(k);
	vector<pair<int, int> > b(k);

	for(int i = 0; i < n; i++) cin >> a[i];

	for(int i = 0; i < k; i++) cin >> b[i].first >> b[i].second;

	sort(b.begin(), b.end());

	pref[0] = b[0].second;
	for(int i = 1; i < k; i++) pref[i] = pref[i-1]+b[i].second;
	for(int i = 0; i < k; i++) pp[i] = b[i].first;

	int ans = 0;
	for(int i = 0; i < n; i++)
	{
		int k = lower_bound(pp.begin(), pp.end(), a[i])-pp.begin();
		//cout << k << '\n';
		if (k) cout << pref[k] << ' ';
	}
}


#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

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

	int n, k;
	cin >> n >> k;

	vector<long long> a(n), pref(k), pp(k);
	vector<pair<long long, long long> > b(k);

	for(int i = 0; i < n; i++) cin >> a[i];

	for(int i = 0; i < k; i++) cin >> b[i].first >> b[i].second;

	sort(b.begin(), b.end());

	pref[0] = b[0].second;
	for(int i = 1; i < k; i++) pref[i] = pref[i-1]+b[i].second;
	for(int i = 0; i < k; i++) pp[i] = b[i].first;

	for(int i = 0; i < n; i++)
	{
		int k = upper_bound(pp.begin(), pp.end(), a[i])-pp.begin();
		//cout << k << '\n';
		if (k) cout << pref[k-1] << ' ';
		else cout << 0 << ' ';
	}
}


#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

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

	int n, k;
	cin >> n >> k;

	vector<long long> a(n), pref(k), pp(k);
	vector<pair<long long, long long> > b(k);

	for(int i = 0; i < n; i++) cin >> a[i];

	for(int i = 0; i < k; i++) cin >> b[i].first >> b[i].second;

	sort(b.begin(), b.end());

	pref[0] = b[0].second;
	for(int i = 1; i < k; i++) pref[i] = pref[i-1]+b[i].second;
	for(int i = 0; i < k; i++) pp[i] = b[i].first;

	int ans = 0;
	for(int i = 0; i < n; i++)
	{
		int k = upper_bound(pp.begin(), pp.end(), a[i])-pp.begin();
		//cout << k << '\n';
		if (k) cout << pref[k-1] << ' ';
	}
}


#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

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

	int n, k;
	cin >> n >> k;

	vector<long long> a(n), pref(k), pp(k);
	vector<pair<int, int> > b(k);

	for(int i = 0; i < n; i++) cin >> a[i];

	for(int i = 0; i < k; i++) cin >> b[i].first >> b[i].second;

	sort(b.begin(), b.end());

	pref[0] = b[0].second;
	for(int i = 1; i < k; i++) pref[i] = pref[i-1]+b[i].second;
	for(int i = 0; i < k; i++) pp[i] = b[i].first;

	int ans = 0;
	for(int i = 0; i < n; i++)
	{
		int k = upper_bound(pp.begin(), pp.end(), a[i])-pp.begin();
		//cout << k << '\n';
		if (k) cout << pref[k-1] << ' ';
	}
}


#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
	int n, k;
	cin >> n >> k;

	vector<int> a(n), pref(k), pp(k);
	vector<pair<int, int> > b(k);

	for(int i = 0; i < n; i++) cin >> a[i];

	for(int i = 0; i < k; i++) cin >> b[i].first >> b[i].second;

	sort(b.begin(), b.end());

	pref[0] = b[0].second;
	for(int i = 1; i < k; i++) pref[i] = pref[i-1]+b[i].second;
	for(int i = 0; i < k; i++) pp[i] = b[i].first;

	int ans = 0;
	for(int i = 0; i < n; i++)
	{
		int k = upper_bound(pp.begin(), pp.end(), a[i])-pp.begin();
		//cout << k << '\n';
		if (k) cout << pref[k-1] << ' ';
	}
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;

    fori(0, space_ship-1)
    {
        int index = upper_bound(empire, empire+base, rebel[i]+1, compare2) - &empire[0];

        pfl(index > 0 ? empire[index-1].gold : 0);
        pfc(' ');
    }

    newline;
    ret;
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;

    fori(0, space_ship-1)
    {
        pfl(empire[upper_bound(empire, empire+base, rebel[i]-1, compare2) - &empire[0]].gold);
        pfc(' ');
    }

    newline;
    ret;
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;

    fori(0, space_ship-1)
    {
        pfl(empire[upper_bound(empire, empire+base, rebel[i]-1ll, compare2) - &empire[0]].gold);
        pfc(' ');
    }

    newline;
    ret;
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;

    fori(0, space_ship-1)
    {
        int index = upper_bound(empire, empire+base, rebel[i]+1, compare2) - &empire[0];

        pfl(empire[index-1].gold);
        pfc(' ');
    }

    newline;
    ret;
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;
    fori(0, base-1) pfl(empire[i].defense), pfc(' '); newline;

    fori(0, space_ship-1)
    {
        pfl(empire[upper_bound(empire, empire+base, rebel[i]-1, compare2) - &empire[0]].gold);
        pfc(' ');
    }

    newline;
    ret;
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;

    fori(0, space_ship-1)
    {
        int index = upper_bound(empire, empire+base, rebel[i]-1, compare2) - &empire[0];
        pfl(index < space_ship ? empire[index].gold : 0);
        pfc(' ');
    }

    newline;
    ret;
}


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

#define ll          long long int
#define ull         unsigned long long int
#define fastio      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define sf(a)       scanf("%d",&a)
#define sfl(a)      scanf("%lld",&a)
#define sfd(a)      scanf("%lf",&a)
#define sfs(a)      scanf("%s",a)
#define sfc(a)      scanf("%c",&a)
#define p(a)        printf(a)
#define pf(a)       printf("%d",a)
#define pfl(a)      printf("%lld",a)
#define pfd(a)      printf("%lf",a)
#define pfc(a)      printf("%c",a)
#define pfs(a)      printf("%s",a)
#define ci(a)       cin>>a
#define co(a)       cout<<a
#define pb(a)       push_back(a)
#define eb(a)       emplace_back(a)

#define vsum(a)            accumulate(a.begin(), a.end(), 0)
#define v_range_sum(a,i,j) accumulate(a.begin() + i, a.begin() + j + 1, 0)
#define asum(a)            accumulate(a, a + sizeof a / sizeof a[0], 0)
#define a_range_sum(a,i,j) accumulate(a + i, a + j + 1, 0)
#define vmin(a)            *min_element(a.begin(), a.end())
#define amin(a)            *min_element(a, a + sizeof a / sizeof a[0])
#define vmax(a)            *max_element(a.begin(), a.end())
#define amax(a)            *max_element(a, a + sizeof a / sizeof a[0])

#define newline     printf("\n")
#define ret         return 0
#define pi          acos(-1.0)
#define mod         1000000007
#define mxn         100005

#define fori(m, n)  for(int i=m;i<=n;i++)
#define forj(m, n)  for(int j=m;j<=n;j++)
#define fordi(m, n) for(int i=m;i>=n;i--)
#define fordj(m, n) for(int j=m;j>=n;j--)

struct val
{
    ll defense, gold;
};

bool compare1(val a, val b)
{
    return a.defense <= b.defense;
}

bool compare2(ll a, val b)
{
    return a <= b.defense;
}

int main()
{
    ll space_ship, base;
    sfl(space_ship), sfl(base);

    ll rebel[space_ship];
    fori(0, space_ship-1) sfl(rebel[i]);

    val empire[base];
    fori(0, base-1) sfl(empire[i].defense), sfl(empire[i].gold);

    sort(empire, empire+base, compare1);

    fori(1, base-1) empire[i].gold += empire[i-1].gold;

    fori(0, space_ship-1)
    {
        int index = upper_bound(empire, empire+base, rebel[i]-1, compare2) - &empire[0];
        pfl(empire[index - (index<base ? 0:1)].gold);
        pfc(' ');
    }

    newline;
    ret;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

#define all(x) x.begin(), x.end()
#define reg(i,x) for(ll i=0;i<x;i++)
#define regi(i,a,x) for(ll i=a;i<x;i++)
#define rev(i,n) for(ll i=n-1;i>0;i--)
#define MP make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

 ll maxn=10e9;
//bool sortbysec(const std::pair<int,int> &a, const std::pair<int,int> &b) return (a.second < b.second);
// sort(v.begin(),v.end(),sortbysec);
//vector<vector<char>>v(n,vector<char>(m));
int main()
{
   // vl h(maxn,0);
    int s,b;
    cin>>s>>b;
    vi v(s);
    reg(i,s) cin>>v[i];
    vpii a(b);
    reg(i,b)
    {
        int d,g;
        cin>>d>>g;
        a[i].first=d;
        a[i].second=g;
        //if(i) a[i].second+=a[i-1].second;
    }
    sort(all(a));
    vi x(b),y(b);
    reg(i,b)
    {
        x[i]=a[i].first;
       // h[x[i]]++;
        y[i]=a[i].second;
        if(i) y[i]+=y[i-1];
    }
    reg(i,s)
    {
        int ans=0;
        int temp=ub(all(x),v[i])-x.begin()-1;
       // if(find(all(x),v[i])!=x.end()) temp+=1;
        ans=y[temp];
        cout<<ans<<" ";
    }
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

#define all(x) x.begin(), x.end()
#define reg(i,x) for(ll i=0;i<x;i++)
#define regi(i,a,x) for(ll i=a;i<x;i++)
#define rev(i,n) for(ll i=n-1;i>0;i--)
#define MP make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

 ll maxn=10e9;
//bool sortbysec(const std::pair<int,int> &a, const std::pair<int,int> &b) return (a.second < b.second);
// sort(v.begin(),v.end(),sortbysec);
//vector<vector<char>>v(n,vector<char>(m));
int main()
{
   // vl h(maxn,0);
    int s,b;
    cin>>s>>b;
    vi v(s);
    reg(i,s) cin>>v[i];
    vpii a(b);
    reg(i,b)
    {
        int d,g;
        cin>>d>>g;
        a[i].first=d;
        a[i].second=g;
        //if(i) a[i].second+=a[i-1].second;
    }
    sort(all(a));
    vi x(b),y(b);
    reg(i,b)
    {
        x[i]=a[i].first;
       // h[x[i]]++;
        y[i]=a[i].second;
        if(i) y[i]+=y[i-1];
    }
    reg(i,s)
    {
        int ans=0;
        int temp=ub(all(x),v[i])-x.begin()-1;
       // if(find(all(x),v[i])!=x.end()) temp+=1;
        ans=y[temp];
        if(temp>=0) cout<<ans<<" ";
        else cout<<0<<" ";
    }
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

#define all(x) x.begin(), x.end()
#define reg(i,x) for(ll i=0;i<x;i++)
#define regi(i,a,x) for(ll i=a;i<x;i++)
#define rev(i,n) for(ll i=n-1;i>0;i--)
#define MP make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

 ll maxn=10e9;
//bool sortbysec(const std::pair<int,int> &a, const std::pair<int,int> &b) return (a.second < b.second);
// sort(v.begin(),v.end(),sortbysec);
//vector<vector<char>>v(n,vector<char>(m));
int main()
{
   // vl h(maxn,0);
    int s,b;
    cin>>s>>b;
    vi v(s);
    reg(i,s) cin>>v[i];
    vpii a(b);
    reg(i,b)
    {
        int d,g;
        cin>>d>>g;
        a[i].first=d;
        a[i].second=g;
        //if(i) a[i].second+=a[i-1].second;
    }
    sort(all(a));
    vi x(b),y(b);
    reg(i,b)
    {
        x[i]=a[i].first;
       // h[x[i]]++;
        y[i]=a[i].second;
        if(i) y[i]+=y[i-1];
    }
    reg(i,s)
    {
        int ans=0;
        int temp=ub(all(x),v[i])-x.begin()-1;
       // if(find(all(x),v[i])!=x.end()) temp+=1;
        ans=y[temp];
        if(ans>=0) cout<<ans<<" ";
        else cout<<0<<" ";
    }
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

#define all(x) x.begin(), x.end()
#define reg(i,x) for(ll i=0;i<x;i++)
#define regi(i,a,x) for(ll i=a;i<x;i++)
#define rev(i,n) for(ll i=n-1;i>0;i--)
#define MP make_pair
#define pb push_back
#define lb lower_bound
#define up upper_bound
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

 ll maxn=10e9;
//bool sortbysec(const std::pair<int,int> &a, const std::pair<int,int> &b) return (a.second < b.second);
// sort(v.begin(),v.end(),sortbysec);
//vector<vector<char>>v(n,vector<char>(m));
int main()
{
   // vl h(maxn,0);
    int s,b;
    cin>>s>>b;
    vi v(s);
    reg(i,s) cin>>v[i];
    vpii a(b);
    reg(i,b)
    {
        int d,g;
        cin>>d>>g;
        a[i].first=d;
        a[i].second=g;
        //if(i) a[i].second+=a[i-1].second;
    }
    sort(all(a));
    vi x(b),y(b);
    reg(i,b)
    {
        x[i]=a[i].first;
       // h[x[i]]++;
        y[i]=a[i].second;
        if(i) y[i]+=y[i-1];
    }
    reg(i,s)
    {
        int ans=0;
        int temp=lb(all(x),v[i])-x.begin()-1;
        if(find(all(x),v[i])!=x.end()) temp+=1;
        ans=y[temp];
        cout<<ans<<" ";
    }
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

#define all(x) x.begin(), x.end()
#define reg(i,x) for(ll i=0;i<x;i++)
#define regi(i,a,x) for(ll i=a;i<x;i++)
#define rev(i,n) for(ll i=n-1;i>0;i--)
#define MP make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

 ll maxn=10e9;
//bool sortbysec(const std::pair<int,int> &a, const std::pair<int,int> &b) return (a.second < b.second);
// sort(v.begin(),v.end(),sortbysec);
//vector<vector<char>>v(n,vector<char>(m));
int main()
{
   // vl h(maxn,0);
    int s,b;
    cin>>s>>b;
    vi v(s);
    reg(i,s) cin>>v[i];
    vpii a(b);
    reg(i,b)
    {
        int d,g;
        cin>>d>>g;
        a[i].first=d;
        a[i].second=g;
        //if(i) a[i].second+=a[i-1].second;
    }
    sort(all(a));
    vi x(b),y(b);
    reg(i,b)
    {
        x[i]=a[i].first;
       // h[x[i]]++;
        y[i]=a[i].second;
        if(i) y[i]+=y[i-1];
    }
    reg(i,s)
    {
        int ans=0;
        int temp=ub(all(x),v[i])-x.begin()-1;
       // if(find(all(x),v[i])!=x.end()) temp+=1;
        ans=y[temp];
        if(ans>0) cout<<ans<<" ";
        else cout<<0<<" ";
    }
}


//Sha3ban_El-Bay
#include<bits/stdc++.h>
using namespace std ;
//======================================================
#define El_Bay ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Mset(v)  sort(all(v));v.erase(unique(all(v)),v.end());
#define all(v)     (v).begin(),(v).end()
#define PI         3.14159265359
#define ll         long long
#define sz(v)	   v.size()
#define pb         push_back
#define S          second
#define bgn        begin
#define F          first
#define N          (int)(2e5+5)
#define	MOD        (int)(1e9+7)
#define inf        1LL*1e18
//======================================================
int dx[] = { 0,  0, 1, -1 ,  1,  -1,  1,  -1 };
int dy[] = { 1, -1, 0,  0 , -1,   1,  1,  -1 };
//======================================================
int s,b,a[N];
ll pref[N];
int main()
{
	cin >> s >> b;
	for(int i=0 ; i<s ; i++)
		cin >> a[i];
	vector<pair<int,int>>v;
	for(int i=0 ; i<b ; i++)
	{
		ll x,y;
		cin >> x >> y;
		v.pb({x,y});
	}
	sort(all(v));
	pref[0] = v[0].S;
	for(int i=1 ; i<s; i++)
		pref[i] = pref[i-1] + 1LL * v[i].S;
	for(int i=0 ; i<s ; i++)
	{
		int u = upper_bound(all(v),make_pair(a[i],INT_MAX))-v.begin();
		cout << pref[u-1] << " ";
	}
	return 0;
}


//Sha3ban_El-Bay
#include<bits/stdc++.h>
using namespace std ;
//======================================================
#define El_Bay ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Mset(v)  sort(all(v));v.erase(unique(all(v)),v.end());
#define all(v)     (v).begin(),(v).end()
#define PI         3.14159265359
#define ll         long long
#define sz(v)	   v.size()
#define pb         push_back
#define S          second
#define bgn        begin
#define F          first
#define N          (int)(2e5+5)
#define	MOD        (int)(1e9+7)
#define inf        1LL*1e18
//======================================================
int dx[] = { 0,  0, 1, -1 ,  1,  -1,  1,  -1 };
int dy[] = { 1, -1, 0,  0 , -1,   1,  1,  -1 };
//======================================================
int s,b,a[N],pref[N];
int main()
{
	cin >> s >> b;
	for(int i=0 ; i<s ; i++)
		cin >> a[i];
	vector<pair<int,int>>v;
	for(int i=0 ; i<b ; i++)
	{
		int x,y;
		cin >> x >> y;
		v.pb({x,y});
	}
	sort(all(v));
	pref[0] = v[0].S;
	for(int i=1 ; i<s; i++)
		pref[i] = pref[i-1] + v[i].S;
	for(int i=0 ; i<s ; i++)
	{
		int u = upper_bound(all(v),make_pair(a[i],INT_MAX))-v.begin();
		cout << pref[u-1] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){long long x=1e5;
	int s,b,a[x];
	cin>>s>>b;
	pair <int,int> dg[b];
	for(int i=0;i<s;i++){
	cin>>a[i];
	}
	for(int i=0;i<b;i++){
	cin>>dg[i].first>>dg[i].second;
	}

	sort(dg,dg+b);
	for(int i=0;i<s;i++){
	int f=lower_bound(dg,dg+b,make_pair(a[i],10000000))-dg;int sum=0;
	for(int j=0;j<f;j++){
	sum+=dg[j].second;
	}
	cout<<sum<<" ";
	}

    return 0;}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];
    }

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b;++j){
        if(a[i]>=d[j]) ans[i]+=g[j];

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];
    }

    sort(a,a+s);
    sort(d,d+b);

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b:++j){
        if(a[i]>=d[j]) ans[i]+=g[j];
        else break;

        }

    }

    for(int i=0;i<s;++i){
        cout << ans[i];
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];
    }

    sort(a,a+s);
    sort(d,d+b);

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b;++j){
        if(a[i]>=d[j]) ans[i]+=g[j];
        else break;

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct enem{
  long long d;
  int g;

};

int main(){
    int s,b,x=0;
    long long a[100000],ans[100000];
    enem enemy[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>enemy[i].d>>enemy[i].g;

    }

    sort(a,a+s);
    sort(enemy,enemy+b);

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b;++j){
        if(a[i]>=enemy[j].d) ans[i]+=enemy[j].g;
        else break;

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
       scanf("%d ", a+i);
    }

        for(int i=0;i<b;++i){
        scanf("%d %d ", d+i , g+i );
    }

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b;++j){
        if(a[i]>=d[j]) ans[i]+=g[j];

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


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

int main(){
    int s,b,x=0;
    long long a[100000],ans[100000],d[100000],g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];

    }

    sort(a,a+s);
    long long y;
    for(int i=0;i<b-1;i++){
        bool night=0;
        for(int j=0;j<n-1-i;j++){
            if(d[j]>d[j+1]){
                y=d[j]; d[j]=d[j+1];d[j+1]=y;
                y=g[j]; g[j]=g[j+1];g[j+1]=y;
                night=1;
            }
        }
        if (night=0) break;
    }

    for(int i=0;i<s;++i){
        ans[i]= (i=0)? 0 : a[i-1] ;
        int k=0
        for(k;k<b;++k){

        if(a[i]>=d[k]) ans[i]+=g[k];
        else break;

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ", * (ans+i));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];
    }

    sort(a,a+s);
    sort(d,d+b);

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b:++j){
        if(a[i]>=d[j]) ans[i]+=g[j];
        else break;

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long help[100000],a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];
    }

    sort(a,a+s);

    for(int i=0;i<s;++i){
        ans[i]=0;
     if(a[i]>a[i-1]){ans[i]+=ans[i-1];}
        for(int j=0;j<b;++j){
            if(j=help[j])break;
        if(a[i]=a[i-1]){ans[i]=ans[i-1]; break;}

        if(a[i]>=d[j]){ ans[i]+=g[j];
                        help[j]=j;
        }

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int s,b;
    long long a[100000],d[100000],ans[100000];
    int g[100000];
    cin>>s>>b;
    for(int i=0;i<s;++i){
        cin>>a[i];
    }

        for(int i=0;i<b;++i){
        cin>>d[i]>>g[i];
    }

    for(int i=0;i<s;++i){
        ans[i]=0;
        for(int j=0;j<b;++j){
        if(a[i]>=d[j]) ans[i]+=g[j];
        else break;

        }

    }

    for(int i=0;i<s;++i){
        printf("%d ",*(ans+i));
    }

    return 0;
}


                                                                               #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   ll s,b;
   cin>>s>>b;
   ll a[s],i;
   for(i=0;i<s;i++)
   {cin>>a[i];}

   vector<pair<ll,ll>>v;
   ll x,y;
   for(i=0;i<b;i++)
   {
         cin>>x>>y;
         v.push_back(make_pair(x,y));
   }
   ll sum=0;
   sort(v.begin(),v.end());
  for(ll j=0;j<s;j++)
 { ll e=a[j];
                              for(auto i:v)
   { ll p=i.first;
   ll p1=i.second;
   if(p<=e)
       {
                                    sum+=p1;
       }
       else
       {
                                    break;
       }
   }
   cout<<sum<<" ";
   sum=0;
}

return 0;}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll n,m;
	cin>>n>>m;
	ll ar[n+10],p[m+10],pr[m+1];
	pair<ll,ll> couple[m];
	for(ll i=0;i<n;i++)
		cin>>ar[i];
	for(ll i=0;i<m;i++)
		cin>>p[i]>>couple[i].second,couple[i].first=p[i];
	sort(couple,couple+m);
	sort(p,p+m);
	pr[0]=0;
	for(ll i=1;i<=m;i++)
		pr[i]=pr[i-1]+couple[i-1].second;
	for(ll i=0;i<n;i++)
		cout<<pr[upper_bound(p,p+m,ar[i])-p]<<" ";
}


                                                                               #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   ll s,b;
   cin>>s>>b;
   ll i;
   vector<ll>a(s);
   for(i=0;i<s;i++)
   {cin>>a[i];}

   vector<pair<ll,ll>>v;
   ll x,y;
   for(i=0;i<b;i++)
   {
         cin>>x>>y;
         v.push_back(make_pair(x,y));
   }
   ll sum=0;
   sort(v.begin(),v.end());
  for(ll j=0;j<s;j++)
 {
   for(i=0;i<b;i++)
   {
   if(v[i].first<=a[j])
       {
                                    sum+=v[i].second;
       }
       else
       {
                                    break;
       }
   }
   cout<<sum<<" ";
   sum=0;
}

}


                                                                               #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   ll s,b;
   cin>>s>>b;
   ll a[s],i;
   for(i=0;i<s;i++)
   {cin>>a[i];}

   vector<pair<ll,ll>>v;
   ll x,y;
   for(i=0;i<b;i++)
   {
         cin>>x>>y;
         v.push_back(make_pair(x,y));
   }
   ll sum=0;
   sort(v.begin(),v.end());
  for(ll j=0;j<s;j++)
 { ll e=a[j];
                              for(auto i:v)
   { ll p=i.first;
   ll p1=i.second;
   if(p<=e)
       {
                                    sum+=p1;
       }
   }
   cout<<sum<<" ";
   sum=0;
}

return 0;}


                                                                               #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   ll s,b;
   cin>>s>>b;
   ll i;
   vector<ll>a(s);
   for(i=0;i<s;i++)
   {cin>>a[i];}

   vector<pair<ll,ll>>v(b);
   ll x,y;
   for(i=0;i<b;i++)
   {
         cin>>x>>y;
         v[i]=(make_pair(x,y));
   }
   ll sum=0;
   sort(v.begin(),v.end());
  for(ll j=0;j<s;j++)
 {
   for(i=0;i<b;i++)
   {
   if(v[i].first<=a[j])
       {
                                    sum+=v[i].second;
       }
       else
       {
                                    break;
       }
   }
   cout<<sum<<" ";
   sum=0;
}

}


                                                                               #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   ll s,b;
   cin>>s>>b;
   ll a[s],i;
   for(i=0;i<s;i++)
   {cin>>a[i];}

   vector<pair<ll,ll>>v;
   ll x,y;
   for(i=0;i<b;i++)
   {
         cin>>x>>y;
         v.push_back(make_pair(x,y));
   }
   ll sum=0;
   sort(v.begin(),v.end());
  for(ll j=0;j<s;j++)
 {
   for(i=0;i<b;i++)
   {
   if(v[i].first<=a[j])
       {
                                    sum+=v[i].second;
       }
       else
       {
                                    break;
       }
   }
   cout<<sum<<" ";
   sum=0;
}

return 0;}


                                                                               #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   ll s,b;
   cin>>s>>b;
   ll a[s],i;
   for(i=0;i<s;i++)
   {cin>>a[i];}

   vector<pair<ll,ll>>v;
   ll x,y;
   for(i=0;i<b;i++)
   {
         cin>>x>>y;
         v.push_back(make_pair(x,y));
   }
   ll sum=0;
   sort(v.begin(),v.end());
  for(ll j=0;j<s;j++)
 { ll e=a[j];
   for(i=0;i<b;i++)
   {
   if(v[i].first<=e)
       {
                                    sum+=v[i].second;
       }
       else
       {
                                    break;
       }
   }
   cout<<sum<<" ";
   sum=0;
}

return 0;}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,b;cin>>n>>b;
	vector<long int>v(n);
	for(int i=0;i<n;i++) cin>>v[i];
	vector<pair<long int,long int> >vec(b);
	for(int i=0;i<b;i++){
		long int d,g;cin>>d>>g;vec[i]=(make_pair(d,g));
	}
	sort(vec.begin(),vec.end());
	for(int i=0;i<n;i++){
		long long int sum=0;
		for(int j=0;j<b;j++){
			if(vec[j].first<=v[i])
			sum+=vec[j].second;
			else
			break;
		}
		cout<<sum<<" ";
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(b);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		auto it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],mod1));
		ll x = it - vect.begin();
		if(it==vect.end())
		{
			cout << vect[vect.size()-1].second << endl;
		}
		else if(vect[x].first>power[i])
		{
			if(x!=0)
			cout << vect[x-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[x].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		auto it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],0));
		ll x = it - vect.begin();
		if(it==vect.end())
		{
			cout << vect[vect.size()-1].second << endl;
		}
		else if(vect[x].first>power[i])
		{
			if(x!=0)
			cout << vect[x-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[x].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		ll it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],0)) - vect.begin();

		if(vect[it].first>power[i])
		{
			if(it!=0)
			cout << vect[it-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[it].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		auto it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],0LL));
		ll x = it - vect.begin();
		if(it==vect.end())
		{
			cout << vect[vect.size()-1].second << endl;
		}
		else if(vect[x].first>power[i])
		{
			if(x!=0)
			cout << vect[x-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[x].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<n ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		auto it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],mod1));
		ll x = it - vect.begin();
		if(it==vect.end())
		{
			cout << vect[vect.size()-1].second << endl;
		}
		else if(vect[x].first>power[i])
		{
			if(x!=0)
			cout << vect[x-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[x].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		ll it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],0)) - vect.begin();

		if(vect[it].first>power[i])
		{
			if(it!=1)
			cout << vect[it-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[it].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		ll it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],0)) - vect.begin();

		if(vect[it].first>power[i])
		{
			//if(it!=0)
			cout << vect[it-1].second << endl;
			//else
			//cout << 0 << endl;
		}
		else
		cout << vect[it].second << endl;
	}
}


#include<bits/stdc++.h>
#include <fstream>
using namespace std;
typedef unsigned long long int ll;
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 998244353LL
#define deb(x) cout << #x << " is " << x << " ";
#define PI 3.14159265;
#define all(a)            a.begin(),a.end()
//#define push_back(x)      pb(x)
#define mod1 1000000007LL
#define MAXN 1000002

signed main()
{
	FIO;

	ll n,b; cin>>n>>b;

	ll power[n]; for(ll i=0 ; i<n ; i++)cin>>power[i];

	vector<pair<ll,ll>>vect(n);

	for(ll i=0 ; i<b ; i++)
	{
		cin>>vect[i].first;
		cin>>vect[i].second;
	}

	sort(vect.begin(),vect.end());
	ll sum=vect[0].second;
	for(int i=1 ; i<vect.size() ; i++)
	{
		vect[i].second += sum;
		sum = vect[i].second;
	}

	for(ll i=0 ; i<n ; i++)
	{
		auto it = lower_bound(vect.begin(),vect.end(),pair<ll,ll>(power[i],mod1));
		ll x = it - vect.begin();
		if(it==vect.end())
		{
			cout << vect[vect.size()-1].second << endl;
		}
		else if(vect[x].first>power[i])
		{
			if(x!=0)
			cout << vect[x-1].second << endl;
			else
			cout << 0 << endl;
		}
		else
		cout << vect[x].second << endl;
	}
}


#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define ll long long
#define ldb long double
#define db double
#define str string

#define fr first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair

#define pi pair<int,int>
#define pll pair<ll,ll>
#define pdb pair<db,db>
#define pldb pair<ldb,ldb>

#define vi vector<int>
#define vll vector<ll>
#define vdb vector<db>
#define vldb vector<ldb>
#define vpi vector< pi >
#define vpll vector< pll >

#define mi map<int,int>
#define mll map<ll,ll>
#define mdb map<db,db>
#define mldb map<ldb,ldb>

#define forr(i,a,b) for (int i = (a); i < (b); ++i)
#define rrof(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9+7;
const ldb PI = 4*atan((ldb)1);

ll power(ll a,ll n){
	ll res=1;

	while(n){
		if(n%2)
		res*=a;
		a*=a;
		n/=2;
	}

	return res;
}

int main(){
	fast;

	ll int s,b;
	cin>>s>>b;

	ll int a[s];
	forr(i,0,s)
	cin>>a[i];

	vll d(b),g(b);
	vpll v;

	forr(i,0,b){
		cin>>d[i]>>g[i];
		v.pb({d[i],g[i]});
	}

	sort(v.begin(),v.end());
	ll int sum[b+1]={0};

	forr(i,0,b)
	sum[i+1]=sum[i]+v[i].se;

	sort(d.begin(),d.end());

	forr(i,0,s){
		int ind=upper_bound(d.begin(),d.end(),a[i])-d.begin();
		cout<<sum[ind]<<" ";
	}

	return 0;
}


#include<bits/stdc++.h>
#define ll long long

using namespace std;

int binarysearch(int*,int,ll int);

int main(){
	int s,b;
	cin>>s>>b;

	ll int a[s];
	for(int i=0;i<s;i++){
		cin>>a[i];
	}

	map<ll int,int>m;
	ll int p[b];
	int q[b];
	for(int i=0;i<b;i++){
		cin>>p[i]>>q[i];
		m[p[i]]=q[i];
	}

	sort(p,p+b);

	ll int s2=0,sum[b],k=1;
	sum[0]=m[p[0]];
	for(int i=1;i<b;i++){
		sum[i]=sum[i-1]+m[p[i]];
	}

	/*map<int,int>mp;
	for(int i=0;i<b;i++){
		mp[i]=p[i];
	}*/

	for(int i=0;i<s;i++){
		int z=binarysearch(p,b,a[i]);
		cout<<sum[z]<<" ";
	}

	return 0;
}

int binarysearch(int*a,int b,ll int x){
	int low=0,high=b-1,mid;

	while(high>=low){
		mid=(high+low)/2;

		if(a[mid]==x){
			return mid;
		}

		else if(a[mid]>x){
			high=mid-1;

			if(high<0){
				return 0;
			}

			if(a[high]<x){
				return high;
			}
		}

		else if(a[mid]<x){
			low=mid+1;

			if(low>=b){
				return b-1;
			}

			if(a[low]>x){
				return low-1;
			}
		}
	}
}


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

int binarysearch(int*,int,int);

int main(){
	int s,b;
	cin>>s>>b;

	int a[s];
	for(int i=0;i<s;i++){
		cin>>a[i];
	}

	map<int,int>m;
	int p[b],q[b];
	for(int i=0;i<b;i++){
		cin>>p[i]>>q[i];
		m[p[i]]=q[i];
	}

	sort(p,p+b);

	int s2=0,sum[b],k=1;
	sum[0]=m[p[0]];
	for(int i=1;i<b;i++){
		sum[i]=sum[i-1]+m[p[i]];
	}

	/*map<int,int>mp;
	for(int i=0;i<b;i++){
		mp[i]=p[i];
	}*/

	for(int i=0;i<s;i++){
		int z=binarysearch(p,b,a[i]);
		cout<<sum[z]<<" ";
	}

	return 0;
}

int binarysearch(int*a,int b,int x){
	int low=0,high=b-1,mid;

	while(high>=low){
		mid=(high+low)/2;

		if(a[mid]==x){
			return mid;
		}

		else if(a[mid]>x){
			high=mid-1;

			if(high<=0){
				return 0;
			}

			if(a[high]<x){
				return high;
			}
		}

		else if(a[mid]<x){
			low=mid+1;

			if(low>=b){
				return b-1;
			}

			if(a[low]>x){
				return low-1;
			}
		}
	}
}


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

int binarysearch(int*,int,int);

int main(){
	int s,b;
	cin>>s>>b;

	int a[s];
	for(int i=0;i<s;i++){
		cin>>a[i];
	}

	map<int,int>m;
	int p[b],q[b];
	for(int i=0;i<b;i++){
		cin>>p[i]>>q[i];
		m[p[i]]=q[i];
	}

	sort(p,p+b);
	/*map<int,int>mp;

	for(int i=0;i<b;i++){
		mp[i]=p[i];
	}*/

	for(int i=0;i<s;i++){
		int z=binarysearch(p,b,a[i]);

		int sum=0;
		for(int j=0;j<=z;j++){
			sum+=m[p[j]];
		}

		cout<<sum<<" ";
	}

	return 0;
}

int binarysearch(int*a,int b,int x){
	int low=0,high=b-1,mid;

	while(high>=low){
		mid=(high+low)/2;

		if(a[mid]==x){
			return mid;
		}

		else if(a[mid]>x){
			high=mid-1;

			if(a[high]<x){
				return high;
			}
		}

		else if(a[mid]<x){
			low=mid+1;

			if(a[low]>x){
				return low-1;
			}
		}
	}
}


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

int binarysearch(int*,int,int);

int main(){
	int s,b;
	cin>>s>>b;

	int a[s];
	for(int i=0;i<s;i++){
		cin>>a[i];
	}

	map<int,int>m;
	int p[b],q[b];
	for(int i=0;i<b;i++){
		cin>>p[i]>>q[i];
		m[p[i]]=q[i];
	}

	sort(p,p+b);

	int s2=0,sum[b],k=1;
	sum[0]=m[p[0]];
	for(int i=1;i<b;i++){
		sum[i]=sum[i-1]+m[p[i]];
	}

	/*map<int,int>mp;
	for(int i=0;i<b;i++){
		mp[i]=p[i];
	}*/

	for(int i=0;i<s;i++){
		int z=binarysearch(p,b,a[i]);
		cout<<sum[z]<<" ";
	}

	return 0;
}

int binarysearch(int*a,int b,int x){
	int low=0,high=b-1,mid;

	while(high>=low){
		mid=(high+low)/2;

		if(a[mid]==x){
			return mid;
		}

		else if(a[mid]>x){
			high=mid-1;

			if(a[high]<x){
				return high;
			}
		}

		else if(a[mid]<x){
			low=mid+1;

			if(a[low]>x){
				return low-1;
			}
		}
	}
}


#include<bits/stdc++.h>
#define ll long long

using namespace std;

int binarysearch(ll int*,int,ll int);

int main(){
	int s,b;
	cin>>s>>b;

	ll int a[s];
	for(int i=0;i<s;i++){
		cin>>a[i];
	}

	ll int p[b];
	int q[b];
	for(int i=0;i<b;i++){
		cin>>p[i]>>q[i];
	}

	int l[100005]={0};
	for(int i=0;i<b;i++){
		l[p[i]]+=q[i];
	}

	sort(p,p+b);

	ll int s2=0,sum[b],k=1;
	sum[0]=l[p[0]];
	for(int i=1;i<b;i++){
		sum[i]=sum[i-1]+l[p[i]];
	}

	/*map<int,int>mp;
	for(int i=0;i<b;i++){
		mp[i]=p[i];
	}*/

	for(int i=0;i<s;i++){
		int z=binarysearch(p,b,a[i]);
		cout<<sum[z]<<" ";
	}

	return 0;
}

int binarysearch(ll int*a,int b,ll int x){
	int low=0,high=b-1,mid;

	while(high>=low){
		mid=(high+low)/2;

		if(a[mid]==x){
			return mid;
		}

		else if(a[mid]>x){
			high=mid-1;

			if(high<0){
				return 0;
			}

			if(a[high]<x){
				return high;
			}
		}

		else if(a[mid]<x){
			low=mid+1;

			if(low>=b){
				return b-1;
			}

			if(a[low]>x){
				return low-1;
			}
		}
	}
}


#include<bits/stdc++.h>
#define ll long long

using namespace std;

int binarysearch(ll int*,int,ll int);

int main(){
	int s,b;
	cin>>s>>b;

	ll int a[s];
	for(int i=0;i<s;i++){
		cin>>a[i];
	}

	map<ll int,int>m;
	ll int p[b];
	int q[b];
	for(int i=0;i<b;i++){
		cin>>p[i]>>q[i];
		m[p[i]]+=q[i];
	}

	sort(p,p+b);

	ll int s2=0,sum[b],k=1;
	sum[0]=m[p[0]];
	for(int i=1;i<b;i++){
		sum[i]=sum[i-1]+m[p[i]];
	}

	/*map<int,int>mp;
	for(int i=0;i<b;i++){
		mp[i]=p[i];
	}*/

	for(int i=0;i<s;i++){
		int z=binarysearch(p,b,a[i]);
		cout<<sum[z]<<" ";
	}

	return 0;
}

int binarysearch(ll int*a,int b,ll int x){
	int low=0,high=b-1,mid;

	while(high>=low){
		mid=(high+low)/2;

		if(a[mid]==x){
			return mid;
		}

		else if(a[mid]>x){
			high=mid-1;

			if(high<0){
				return 0;
			}

			if(a[high]<x){
				return high;
			}
		}

		else if(a[mid]<x){
			low=mid+1;

			if(low>=b){
				return b-1;
			}

			if(a[low]>x){
				return low-1;
			}
		}
	}
}