#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<iomanip>
#include<cassert>
#include<memory>
#include<string>
#include<string.h>

using namespace std;

typedef double ld;
typedef long long li;

struct pt {
	li x, y;
	pt(li x, li y) : x(x), y(y) {}
	pt() : x(0), y(0) {}
	void read() {
		scanf("%I64d%I64d", &x, &y);
	}
};

struct line {
	li a, b, c;
	line(pt & p, pt & q) {
		a = p.y - q.y;
		b = q.x - p.x;
		c = -(a * p.x + b * p.y);
	}
};

ld ang(const pt & p) {
	return atan2(p.y, p.x);
}
pt operator + (pt & a, pt & b) {
	return pt(a.x + b.x, a.y + b.y);
}

pt operator - (pt & a, pt & b) {
	return pt(a.x - b.x, a.y - b.y);
}

pt operator * (pt & a, ld k) {
	return pt(a.x * k, a.y * k);
}

int k, n, q;
const int N = 500 * 1000 + 5;
pt EV[N];
pt P[N];
pt V[N];
int T[N];
int CNT[N];

pt v[N];

bool cmp(const pt & a, const pt & b) {
	return ang(a) < ang(b);
}

void read() {
	scanf("%d%d%d", &k, &n, &q);
	for (int i = 0; i < k; ++i)
		V[i].read();
	for (int i = 0; i < n; ++i) {
		P[i].read();
		scanf("%d", &CNT[i]);
	}
	for (int i = 0; i < q; ++i) {
		EV[i].read();
		scanf("%d", &T[i]);
	}
}

int ans[N];

li f[N];

void inc(int pos, int d) {
	for (; pos < N; pos |= pos + 1)
		f[pos] += d;
}

li get(int pos) {
	li res = 0;
	for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
		res += f[pos];
	return res;
}

li get(int l, int r) {
	return get(r) - get(l - 1);
}

ld PI = 3.1415926535897932384626433832795;

void solve() {
	for (int i = 0; i < k; ++i)
	{
		V[i] = V[i] * 2;
		V[i + k] = V[i] * -1;
	}
	sort(V, V + 2 * k, cmp);

	v[0] = pt(0, 0);
	for (int i = 1; i < 2 * k; ++i)
		v[i] = v[i - 1] + V[i - 1];
	pt c(0, 0);
	for (int i = 0; i < 2 * k; ++i)
		c = c + v[i];
	c = c * (1 / (2.0 * k));

	for (int i = 0; i < 2 * k; ++i)
		v[i] = v[i] - c;

	int sig[33];

	for (int _i = 0; _i < 2 * k; ++_i) {
		int _j = (_i + 1) % (2 * k);
		sig[_i] = 1;
		if (v[_i].x < v[_j].x)
			sig[_i] = -1;
	}

	for (int _i = 0; _i < 2 * k; ++_i) {
		vector<pair<ld, int> > ev;
		vector <li> xs;
		memset(f, 0, sizeof f);

		int _j = (_i + 1) % (2 * k);
		line l(v[_i], v[_j]);
		ld c = -l.a / ld(l.b);
		if (v[_i].x == v[_j].x)
			continue;
		ld add = -1e-9;
		if (v[_i].x < v[_j].x)
			add = 1e-9;

		for (int i = 0; i < n; ++i) {
			ld cy = P[i].y - P[i].x * c + add;
			ev.push_back(make_pair(cy, i));
			xs.push_back(P[i].x);
		}

		for (int i = 0; i < q; ++i) {
			pt p = EV[i] + v[_i] * T[i];
			ld cy = p.y - c * p.x;
			ev.push_back(make_pair(cy, i + n));
			xs.push_back(p.x);
			xs.push_back(p.x - 1);
			xs.push_back((EV[i] + v[_j] * T[i]).x);
			xs.push_back((EV[i] + v[_j] * T[i]).x - 1);
		}
		sort(ev.begin(), ev.end(), [](const pair<ld, int> & a, const pair<ld, int> & b) {
			return a.first < b.first;
		});
		sort(xs.begin(), xs.end());
		xs.resize(unique(xs.begin(), xs.end()) - xs.begin());

		for (int i = 0; i < ev.size(); ++i) {
			if (ev[i].second < n) {
				int j = ev[i].second;
				inc(lower_bound(xs.begin(), xs.end(), P[j].x) - xs.begin(), CNT[j]);
			}
			else {
				int j = ev[i].second - n;
				pt p1 = EV[j] + v[_i] * T[j];
				pt p2 = EV[j] + v[_j] * T[j];
				if (p1.x > p2.x)
					swap(p1.x, p2.x);
				li l = p1.x, r = p2.x;
				if (sig[_i] == -1 && sig[_i] == sig[_j])
					r--;
				if (sig[_i] == 1 && sig[_i] == sig[(_i - 1 + 2 * k) % (2 * k)])
					r--;
				ans[j] += sig[_i] * get(lower_bound(xs.begin(), xs.end(), l) - xs.begin(), lower_bound(xs.begin(), xs.end(), r) - xs.begin());
			}
		}
	}

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

int main() {

	srand(time(NULL));

	read();
	solve();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<iomanip>
#include<cassert>
#include<memory>
#include<string>
#include<string.h>
#include<math.h>

using namespace std;

typedef double ld;
typedef long long li;

struct pt {
	li x, y;
	pt(li x, li y) : x(x), y(y) {}
	pt() : x(0), y(0) {}
	void read() {
		scanf("%I64d%I64d", &x, &y);
	}
	pt operator + (const pt & a) {
		return pt(a.x + x, a.y + y);
	}

	pt operator - (const pt & a) {
		return pt(x - a.x, y - a.y);
	}

	pt operator * (ld k) {
		return pt(x * k, y * k);
	}

};

struct line {
	li a, b, c;
	line(pt & p, pt & q) {
		a = p.y - q.y;
		b = q.x - p.x;
		c = -(a * p.x + b * p.y);
	}
};

ld ang(const pt & p) {
	return atan2(p.y, p.x);
}

int k, n, q;
const int N = 500 * 1000 + 5;
pt EV[N];
pt P[N];
pt V[N];
int T[N];
int CNT[N];

pt v[N];

bool cmp(const pt & a, const pt & b) {
	return ang(a) < ang(b);
}

void read() {
	scanf("%d%d%d", &k, &n, &q);
	for (int i = 0; i < k; ++i)
		V[i].read();
	for (int i = 0; i < n; ++i) {
		P[i].read();
		scanf("%d", &CNT[i]);
	}
	for (int i = 0; i < q; ++i) {
		EV[i].read();
		scanf("%d", &T[i]);
	}
}

li ans[N];

li f[N];

void inc(int pos, int d) {
	for (; pos < N; pos |= pos + 1)
		f[pos] += d;
}

li get(int pos) {
	li res = 0;
	for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
		res += f[pos];
	return res;
}

li get(int l, int r) {
	return get(r) - get(l - 1);
}

ld PI = 3.1415926535897932384626433832795;

void solve() {
	for (int i = 0; i < k; ++i)
	{
		V[i] = V[i] * 2;
		V[i + k] = V[i] * -1;
	}
	sort(V, V + 2 * k, cmp);

	v[0] = pt(0, 0);
	for (int i = 1; i < 2 * k; ++i)
		v[i] = v[i - 1] + V[i - 1];
	pt c(0, 0);
	for (int i = 0; i < 2 * k; ++i)
		c = c + v[i];
	c = c * (1 / (2.0 * k));

	for (int i = 0; i < 2 * k; ++i)
		v[i] = v[i] - c;

	int sig[33];

	li maxx = v[0].x;
	for (int i = 0; i < 2 * k; ++i)
		maxx = max(maxx, v[i].x);

	for (int _i = 0; _i < 2 * k; ++_i) {
		int _j = (_i + 1) % (2 * k);
		sig[_i] = 1;
		if (v[_i].x == v[_j].x)
			sig[_i] = 0;
		if (v[_i].x < v[_j].x)
			sig[_i] = -1;
	}

	for (int _i = 0; _i < 2 * k; ++_i) {
		vector<pair<ld, int> > ev;
		vector <li> xs;
		memset(f, 0, sizeof f);

		int _j = (_i + 1) % (2 * k);
		line l(v[_i], v[_j]);
		ld c = -l.a / ld(l.b);
		if (v[_i].x == v[_j].x)
			continue;
		ld add = 0;
		if (v[_i].x < v[_j].x)
			add = 0;

		for (int i = 0; i < n; ++i) {
			ld cy = P[i].y - P[i].x * c + add;
			ev.push_back(make_pair(cy, i));
			xs.push_back(P[i].x);
		}

		for (int i = 0; i < q; ++i) {
			pt p = EV[i] + v[_i] * T[i];
			ld cy = p.y - c * p.x;
			ev.push_back(make_pair(cy, i + n));
			xs.push_back(p.x);
			xs.push_back(p.x - 1);
			xs.push_back((EV[i] + v[_j] * T[i]).x);
			xs.push_back((EV[i] + v[_j] * T[i]).x - 1);
		}
		sort(ev.begin(), ev.end(), [](const pair<ld, int> & a, const pair<ld, int> & b) {
			return a.first < b.first;
		});
		sort(xs.begin(), xs.end());
		xs.resize(unique(xs.begin(), xs.end()) - xs.begin());

		for (int i = 0; i < ev.size(); ++i) {
			if (ev[i].second < n) {
				int j = ev[i].second;
				inc(lower_bound(xs.begin(), xs.end(), P[j].x) - xs.begin(), CNT[j]);
			}
			else {
				int j = ev[i].second - n;
				pt p1 = EV[j] + v[_i] * T[j];
				pt p2 = EV[j] + v[_j] * T[j];
				if (p1.x > p2.x)
					swap(p1.x, p2.x);
				li l = p1.x, r = p2.x;
				if (v[_i].x != maxx && v[_j].x != maxx)
					r--;
				ans[j] += sig[_i] * get(lower_bound(xs.begin(), xs.end(), l) - xs.begin(), lower_bound(xs.begin(), xs.end(), r) - xs.begin());
			}
		}
	}

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

int main() {

	srand(time(NULL));

	read();
	solve();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<iomanip>
#include<cassert>
#include<memory>
#include<string>
#include<string.h>
#include<math.h>

using namespace std;

typedef double ld;
typedef long long li;

struct pt {
	li x, y;
	pt(li x, li y) : x(x), y(y) {}
	pt() : x(0), y(0) {}
	void read() {
		scanf("%I64d%I64d", &x, &y);
	}
	pt operator + (const pt & a) {
		return pt(a.x + x, a.y + y);
	}

	pt operator - (const pt & a) {
		return pt(x - a.x, y - a.y);
	}

	pt operator * (ld k) {
		return pt(x * k, y * k);
	}

};

struct line {
	li a, b, c;
	line(pt & p, pt & q) {
		a = p.y - q.y;
		b = q.x - p.x;
		c = -(a * p.x + b * p.y);
	}
};

ld ang(const pt & p) {
	return atan2(p.y, p.x);
}

int k, n, q;
const int N = 500 * 1000 + 5;
pt EV[N];
pt P[N];
pt V[N];
int T[N];
int CNT[N];

pt v[N];

bool cmp(const pt & a, const pt & b) {
	return ang(a) < ang(b);
}

void read() {
	scanf("%d%d%d", &k, &n, &q);
	for (int i = 0; i < k; ++i)
		V[i].read();
	for (int i = 0; i < n; ++i) {
		P[i].read();
		scanf("%d", &CNT[i]);
	}
	for (int i = 0; i < q; ++i) {
		EV[i].read();
		scanf("%d", &T[i]);
	}
}

li ans[N];

li f[N];

void inc(int pos, int d) {
	for (; pos < N; pos |= pos + 1)
		f[pos] += d;
}

li get(int pos) {
	li res = 0;
	for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
		res += f[pos];
	return res;
}

li get(int l, int r) {
	return get(r) - get(l - 1);
}

ld PI = 3.1415926535897932384626433832795;

void solve() {
	for (int i = 0; i < k; ++i)
	{
		V[i] = V[i] * 2;
		V[i + k] = V[i] * -1;
	}
	sort(V, V + 2 * k, cmp);

	v[0] = pt(0, 0);
	for (int i = 1; i < 2 * k; ++i)
		v[i] = v[i - 1] + V[i - 1];
	pt c(0, 0);
	for (int i = 0; i < 2 * k; ++i)
		c = c + v[i];
	c = c * (1 / (2.0 * k));

	for (int i = 0; i < 2 * k; ++i)
		v[i] = v[i] - c;

	int sig[33];

	li maxx = v[0].x;
	for (int i = 0; i < 2 * k; ++i)
		maxx = max(maxx, v[i].x);

	for (int _i = 0; _i < 2 * k; ++_i) {
		int _j = (_i + 1) % (2 * k);
		sig[_i] = 1;
		if (v[_i].x == v[_j].x)
			sig[_i] = 0;
		if (v[_i].x < v[_j].x)
			sig[_i] = -1;
	}

	for (int _i = 0; _i < 2 * k; ++_i) {
		vector<pair<ld, int> > ev;
		vector <li> xs;
		memset(f, 0, sizeof f);

		int _j = (_i + 1) % (2 * k);
		line l(v[_i], v[_j]);
		ld c = -l.a / ld(l.b);
		if (v[_i].x == v[_j].x)
			continue;
		ld add = -1e-9;
		if (v[_i].x < v[_j].x)
			add = 1e-9;

		for (int i = 0; i < n; ++i) {
			ld cy = P[i].y - P[i].x * c + add;
			ev.push_back(make_pair(cy, i));
			xs.push_back(P[i].x);
		}

		for (int i = 0; i < q; ++i) {
			pt p = EV[i] + v[_i] * T[i];
			ld cy = p.y - c * p.x;
			ev.push_back(make_pair(cy, i + n));
			xs.push_back(p.x);
			xs.push_back(p.x - 1);
			xs.push_back((EV[i] + v[_j] * T[i]).x);
			xs.push_back((EV[i] + v[_j] * T[i]).x - 1);
		}
		sort(ev.begin(), ev.end(), [](const pair<ld, int> & a, const pair<ld, int> & b) {
			return a.first < b.first;
		});
		sort(xs.begin(), xs.end());
		xs.resize(unique(xs.begin(), xs.end()) - xs.begin());

		for (int i = 0; i < ev.size(); ++i) {
			if (ev[i].second < n) {
				int j = ev[i].second;
				inc(lower_bound(xs.begin(), xs.end(), P[j].x) - xs.begin(), CNT[j]);
			}
			else {
				int j = ev[i].second - n;
				pt p1 = EV[j] + v[_i] * T[j];
				pt p2 = EV[j] + v[_j] * T[j];
				if (p1.x > p2.x)
					swap(p1.x, p2.x);
				li l = p1.x, r = p2.x;
				if (v[_i].x != maxx && v[_j].x != maxx)
					r--;
				ans[j] += sig[_i] * get(lower_bound(xs.begin(), xs.end(), l) - xs.begin(), lower_bound(xs.begin(), xs.end(), r) - xs.begin());
			}
		}
	}

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

int main() {

	srand(time(NULL));

	read();
	solve();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<iomanip>
#include<cassert>
#include<memory>
#include<string>
#include<string.h>
#include<math.h>

using namespace std;

typedef double ld;
typedef long long li;

struct pt {
	li x, y;
	pt(li x, li y) : x(x), y(y) {}
	pt() : x(0), y(0) {}
	void read() {
		scanf("%I64d%I64d", &x, &y);
	}
	pt operator + (const pt & a) {
		return pt(a.x + x, a.y + y);
	}

	pt operator - (const pt & a) {
		return pt(x - a.x, y - a.y);
	}

	pt operator * (ld k) {
		return pt(x * k, y * k);
	}

};

struct line {
	li a, b, c;
	line(pt & p, pt & q) {
		a = p.y - q.y;
		b = q.x - p.x;
		c = -(a * p.x + b * p.y);
	}
};

ld ang(const pt & p) {
	return atan2(p.y, p.x);
}

int k, n, q;
const int N = 500 * 1000 + 5;
pt EV[N];
pt P[N];
pt V[N];
int T[N];
int CNT[N];

pt v[N];

bool cmp(const pt & a, const pt & b) {
	return ang(a) < ang(b);
}

void read() {
	scanf("%d%d%d", &k, &n, &q);
	for (int i = 0; i < k; ++i)
		V[i].read();
	for (int i = 0; i < n; ++i) {
		P[i].read();
		scanf("%d", &CNT[i]);
	}
	for (int i = 0; i < q; ++i) {
		EV[i].read();
		scanf("%d", &T[i]);
	}
}

li ans[N];

li f[N];

void inc(int pos, int d) {
	for (; pos < N; pos |= pos + 1)
		f[pos] += d;
}

li get(int pos) {
	li res = 0;
	for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
		res += f[pos];
	return res;
}

li get(int l, int r) {
	return get(r) - get(l - 1);
}

ld PI = 3.1415926535897932384626433832795;

void solve() {
	for (int i = 0; i < k; ++i)
	{
		V[i] = V[i] * 2;
		V[i + k] = V[i] * -1;
	}
	sort(V, V + 2 * k, cmp);

	v[0] = pt(0, 0);
	for (int i = 1; i < 2 * k; ++i)
		v[i] = v[i - 1] + V[i - 1];
	pt c(0, 0);
	for (int i = 0; i < 2 * k; ++i)
		c = c + v[i];
	c = c * (1 / (2.0 * k));

	for (int i = 0; i < 2 * k; ++i)
		v[i] = v[i] - c;

	int sig[33];

	li maxx = v[0].x;
	for (int i = 0; i < 2 * k; ++i)
		maxx = max(maxx, v[i].x);

	for (int _i = 0; _i < 2 * k; ++_i) {
		int _j = (_i + 1) % (2 * k);
		sig[_i] = 1;
		if (v[_i].x == v[_j].x)
			sig[_i] = 0;
		if (v[_i].x < v[_j].x)
			sig[_i] = -1;
	}

	for (int _i = 0; _i < 2 * k; ++_i) {
		vector<pair<ld, int> > ev;
		vector <li> xs;
		memset(f, 0, sizeof f);

		int _j = (_i + 1) % (2 * k);
		line l(v[_i], v[_j]);
		ld c = -l.a / ld(l.b);
		if (v[_i].x == v[_j].x)
			continue;
		ld add = 1e-11;
		if (v[_i].x < v[_j].x)
			add = 1e-11;

		for (int i = 0; i < n; ++i) {
			ld cy = P[i].y - P[i].x * c + add;
			ev.push_back(make_pair(cy, i));
			xs.push_back(P[i].x);
		}

		for (int i = 0; i < q; ++i) {
			pt p = EV[i] + v[_i] * T[i];
			ld cy = p.y - c * p.x;
			ev.push_back(make_pair(cy, i + n));
			xs.push_back(p.x);
			xs.push_back(p.x - 1);
			xs.push_back((EV[i] + v[_j] * T[i]).x);
			xs.push_back((EV[i] + v[_j] * T[i]).x - 1);
		}
		sort(ev.begin(), ev.end(), [](const pair<ld, int> & a, const pair<ld, int> & b) {
			return a.first < b.first;
		});
		sort(xs.begin(), xs.end());
		xs.resize(unique(xs.begin(), xs.end()) - xs.begin());

		for (int i = 0; i < ev.size(); ++i) {
			if (ev[i].second < n) {
				int j = ev[i].second;
				inc(lower_bound(xs.begin(), xs.end(), P[j].x) - xs.begin(), CNT[j]);
			}
			else {
				int j = ev[i].second - n;
				pt p1 = EV[j] + v[_i] * T[j];
				pt p2 = EV[j] + v[_j] * T[j];
				if (p1.x > p2.x)
					swap(p1.x, p2.x);
				li l = p1.x, r = p2.x;
				if (v[_i].x != maxx && v[_j].x != maxx)
					r--;
				ans[j] += sig[_i] * get(lower_bound(xs.begin(), xs.end(), l) - xs.begin(), lower_bound(xs.begin(), xs.end(), r) - xs.begin());
			}
		}
	}

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

int main() {

	srand(time(NULL));

	read();
	solve();

	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
inline int read()
{
	int x,f=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')f=0;
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return f?x:-x;
}
#define MK 10
#define MN 100000
#define ps(x) (upper_bound(c+1,c+cn+1,x)-c-1)
struct vec{int x,y;}v[MK+5];
struct work{double a;int x,y,z;}w[MN*2+5];
bool cmpv(const vec&a,const vec&b){return atan2(a.y,a.x)<atan2(b.y,b.x);}
bool cmpw(const work&a,const work&b){return fabs(a.a-b.a)<1e-7?a.z<b.z:a.a<b.a;}
int x[MN+5],y[MN+5],a[MN+5],c[MN+5],cn,sx,sy,px[MN+5],py[MN+5],t[MN+5];
ll ans[MN+5],s[MN+5];
void add(int k,int x){for(;k<=MN;k+=k&-k)s[k]+=x;}
ll sum(int k){ll res=0;for(;k;k-=k&-k)res+=s[k];return res;}
int main()
{
	int k,n,q,i,j;
	k=read();n=read();q=read();
	for(i=1;i<=k;++i)
	{
		v[i].x=read();v[i].y=read();
		if(v[i].x<0)v[i].x=-v[i].x,v[i].y=-v[i].y;
		if(!v[i].x&&v[i].y<0)v[i].y=-v[i].y;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1,cmpv);
	for(i=1;i<=n;++i)c[i]=x[i]=read(),y[i]=read(),a[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=q;++i)px[i]=read(),py[i]=read(),t[i]=read(),px[i]-=sx*t[i],py[i]-=sy*t[i],t[i]<<=1;
	for(i=1;i<=k;++i)
	{
		if(!v[i].x){for(j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=sum(ps(w[j].y))-sum(ps(w[j].x-(i<2)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=sum(ps(w[j].y-(i>1)))-sum(ps(w[j].x-1));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=q;++i)printf("%I64d\n",ans[i]);
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 100010
#define MAXK 10

using namespace std;

struct point {
    long long x,y;
    point() {}
    point(long long x, long long y) : x(x), y(y) {}

    point operator + (point p) { return point(x+p.x, y+p.y); }
    point operator - (point p) { return point(x-p.x, y-p.y); }
    point operator * (long long a) { return point(a*x, a*y); }

    bool operator < (point p) const {
        if (x != p.x) return x < p.x;
        return y < p.y;
    }
    bool operator == (point p) const {
        return x == p.x && y == p.y;
    }
};

long long cross(point a, point b) {
    return a.x * b.y - a.y * b.x;
}

int k,n,q;
point v[MAXK],f[MAXN],p[MAXN];
int a[MAXN],t[MAXN];
long long ans[MAXN];

vector<point> m_points;

void gen_points(int i, point x) {
    if (i == k) {
        m_points.push_back(x);
        return;
    }
    gen_points(i + 1, x + v[i]);
    gen_points(i + 1, x - v[i]);
}

vector<point> uh, lh;
void minkowski() {
    gen_points(0, point(0, 0));
    sort(m_points.begin(), m_points.end());
    m_points.erase(unique(m_points.begin(), m_points.end()), m_points.end());

    for (point &p : m_points) {
        while (uh.size() > 1) {
            point a = uh[uh.size()-2], b = uh.back();
            if (cross(p-a,b-a) > 0) break;
            uh.pop_back();
        }
        uh.push_back(p);
    }

    for (point &p : m_points) {
        while (lh.size() > 1) {
            point a = lh[lh.size()-2], b = lh.back();
            if (cross(b-a,p-a) > 0) break;
            lh.pop_back();
        }
        lh.push_back(p);
    }
}

struct event {
    long long y;
    int op, i;
    event(long long y, int op, int i) : y(y), op(op), i(i) {};

    bool operator < (const event& e) const {
        if (y != e.y) return y < e.y;
        return op < e.op;
    }
};

long long num[MAXN];
long long l[MAXN],r[MAXN],x[MAXN];
long long bit[3*MAXN];

void add(int x, long long val) {
    for (; x < 3*MAXN; x += x & -x) bit[x] += val;
}

long long get(int x) {
    long long res = 0;
    for (; x > 0; x -= x & -x) res += bit[x];
    return res;
}

void go(point u, point v, bool incr, bool inctop) {
    point e = v - u;
    vector<event> events;
    vector<long long> cx;
    for (int i = 0; i < q; i++) {
        long long y = cross(e, p[i] + u * t[i]);
        if (e.x == 0) {
            if (inctop) y = p[i].y + t[i] * max(u.y, v.y);
            else y = p[i].y + t[i] * min(u.y, v.y) - 1;
        } else if (!inctop) y--;
        events.push_back(event(y, 1, i));
        l[i] = p[i].x + t[i] * u.x;
        r[i] = p[i].x + t[i] * v.x - 1;
        if (incr) r[i]++;
        cx.push_back(l[i]);
        cx.push_back(r[i]);
    }
    for (int i = 0; i < n; i++) {
        long long y = cross(e, f[i]);
        if (e.x == 0) y = f[i].y;
        events.push_back(event(y, 0, i));
        x[i] = f[i].x;
        cx.push_back(x[i]);
    }

    sort(cx.begin(), cx.end());
    cx.erase(unique(cx.begin(), cx.end()), cx.end());
    for (int i = 0; i < q; i++) {
        l[i] = upper_bound(cx.begin(), cx.end(), l[i]) - cx.begin();
        r[i] = upper_bound(cx.begin(), cx.end(), r[i]) - cx.begin();
    }
    for (int i = 0 ; i < n; i++) {
        x[i] = upper_bound(cx.begin(), cx.end(), x[i]) - cx.begin();
    }

    memset(bit,0,sizeof(bit));
    sort(events.begin(), events.end());
    for (auto &ev : events) {
        int i = ev.i;
        if (ev.op == 1) {
            num[i] = get(r[i]) - get(l[i] - 1);
        } else {
            add(x[i], a[i]);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin >> k >> n >> q;
    for (int i = 0; i < k; i++) {
        cin >> v[i].x >> v[i].y;
    }

    for (int i = 0; i < n; i++) {
        cin >> f[i].x >> f[i].y >> a[i];
    }

    for (int i = 0; i < q; i++) {
        cin >> p[i].x >> p[i].y >> t[i];
    }

    minkowski();

    for (int i = 0; i + 1 < uh.size(); i++) {
        go(uh[i], uh[i + 1], i + 2 == uh.size(), 1);
        //cerr << uh[i].x << "," << uh[i].y << " -> " << uh[i+1].x << "," << uh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] += num[j];
            //cerr << num[j] << endl;
        }
    }
    for (int i = 0; i + 1 < lh.size(); i++) {
        go(lh[i], lh[i + 1], i + 2 == lh.size(), 0);
        //cerr << lh[i].x << "," << lh[i].y << " -> " << lh[i+1].x << "," << lh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] -= num[j];
            //cerr << -num[j] << endl;
        }
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i] << '\n';
    }
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 100010
#define MAXK 10

using namespace std;

struct point {
    long long x,y;
    point() {}
    point(long long x, long long y) : x(x), y(y) {}

    point operator + (point p) { return point(x+p.x, y+p.y); }
    point operator - (point p) { return point(x-p.x, y-p.y); }
    point operator * (long long a) { return point(a*x, a*y); }

    bool operator < (point p) const {
        if (x != p.x) return x < p.x;
        return y < p.y;
    }
    bool operator == (point p) const {
        return x == p.x && y == p.y;
    }
};

long long cross(point a, point b) {
    return a.x * b.y - a.y * b.x;
}

int k,n,q;
point v[MAXK],f[MAXN],p[MAXN];
int a[MAXN],t[MAXN];
long long ans[MAXN];

vector<point> m_points;

void gen_points(int i, point x) {
    if (i == k) {
        m_points.push_back(x);
        return;
    }
    gen_points(i + 1, x + v[i]);
    gen_points(i + 1, x - v[i]);
}

vector<point> uh, lh;
void minkowski() {
    gen_points(0, point(0, 0));
    sort(m_points.begin(), m_points.end());
    m_points.erase(unique(m_points.begin(), m_points.end()), m_points.end());

    for (point &p : m_points) {
        while (uh.size() > 1) {
            point a = uh[uh.size()-2], b = uh.back();
            if (cross(p-a,b-a) > 0) break;
            uh.pop_back();
        }
        uh.push_back(p);
    }

    for (point &p : m_points) {
        while (lh.size() > 1) {
            point a = lh[lh.size()-2], b = lh.back();
            if (cross(b-a,p-a) > 0) break;
            lh.pop_back();
        }
        lh.push_back(p);
    }
}

struct event {
    long long y;
    int op, i;
    event(long long y, int op, int i) : y(y), op(op), i(i) {};

    bool operator < (const event& e) const {
        if (y != e.y) return y < e.y;
        return op < e.op;
    }
};

long long num[MAXN];
long long l[MAXN],r[MAXN],x[MAXN];
long long bit[3*MAXN];

void add(int x, long long val) {
    for (; x < 3*MAXN; x += x & -x) bit[x] += val;
}

long long get(int x) {
    long long res = 0;
    for (; x > 0; x -= x & -x) res += bit[x];
    return res;
}

void go(point u, point v, bool incr, bool inctop) {
    point e = v - u;
    vector<event> events;
    vector<long long> cx;
    for (int i = 0; i < q; i++) {
        long long y = cross(e, p[i] + u * t[i]);
        if (!inctop) y--;
        events.push_back(event(y, 1, i));
        l[i] = p[i].x + t[i] * u.x;
        r[i] = p[i].x + t[i] * v.x - 1;
        if (incr) r[i]++;
        cx.push_back(l[i]);
        cx.push_back(r[i]);
    }
    for (int i = 0; i < n; i++) {
        events.push_back(event(cross(e, f[i]), 0, i));
        x[i] = f[i].x;
        cx.push_back(x[i]);
    }

    sort(cx.begin(), cx.end());
    cx.erase(unique(cx.begin(), cx.end()), cx.end());
    for (int i = 0; i < q; i++) {
        l[i] = upper_bound(cx.begin(), cx.end(), l[i]) - cx.begin();
        r[i] = upper_bound(cx.begin(), cx.end(), r[i]) - cx.begin();
    }
    for (int i = 0 ; i < n; i++) {
        x[i] = upper_bound(cx.begin(), cx.end(), x[i]) - cx.begin();
    }

    memset(bit,0,sizeof(bit));
    sort(events.begin(), events.end());
    for (auto &ev : events) {
        int i = ev.i;
        if (ev.op == 1) {
            num[i] = get(r[i]) - get(l[i] - 1);
        } else {
            add(x[i], a[i]);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin >> k >> n >> q;
    for (int i = 0; i < k; i++) {
        cin >> v[i].x >> v[i].y;
    }

    for (int i = 0; i < n; i++) {
        cin >> f[i].x >> f[i].y >> a[i];
    }

    for (int i = 0; i < q; i++) {
        cin >> p[i].x >> p[i].y >> t[i];
    }

    minkowski();

    for (int i = 0; i + 1 < uh.size(); i++) {
        go(uh[i], uh[i + 1], i + 2 == uh.size(), 1);
        //cerr << uh[i].x << "," << uh[i].y << " -> " << uh[i+1].x << "," << uh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] += num[j];
            //cerr << num[j] << endl;
        }
    }
    for (int i = 0; i + 1 < lh.size(); i++) {
        go(lh[i], lh[i + 1], i + 2 == lh.size(), 0);
        //cerr << lh[i].x << "," << lh[i].y << " -> " << lh[i+1].x << "," << lh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] -= num[j];
            //cerr << -num[j] << endl;
        }
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i] << '\n';
    }
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 100010
#define MAXK 10

using namespace std;

struct point {
    long long x,y;
    point() {}
    point(long long x, long long y) : x(x), y(y) {}

    point operator + (point p) { return point(x+p.x, y+p.y); }
    point operator - (point p) { return point(x-p.x, y-p.y); }
    point operator * (long long a) { return point(a*x, a*y); }

    bool operator < (point p) const {
        if (x != p.x) return x < p.x;
        return y < p.y;
    }
    bool operator == (point p) const {
        return x == p.x && y == p.y;
    }
};

long long cross(point a, point b) {
    return a.x * b.y - a.y * b.x;
}

int k,n,q;
point v[MAXK],f[MAXN],p[MAXN];
int a[MAXN],t[MAXN];
long long ans[MAXN];

vector<point> m_points;

void gen_points(int i, point x) {
    if (i == k) {
        m_points.push_back(x);
        return;
    }
    gen_points(i + 1, x + v[i]);
    gen_points(i + 1, x - v[i]);
}

vector<point> uh, lh;
void minkowski() {
    gen_points(0, point(0, 0));
    sort(m_points.begin(), m_points.end());
    m_points.erase(unique(m_points.begin(), m_points.end()), m_points.end());

    for (point &p : m_points) {
        while (uh.size() > 1) {
            point a = uh[uh.size()-2], b = uh.back();
            if (cross(p-a,b-a) > 0) break;
            uh.pop_back();
        }
        uh.push_back(p);
    }

    for (point &p : m_points) {
        while (lh.size() > 1) {
            point a = lh[lh.size()-2], b = lh.back();
            if (cross(b-a,p-a) > 0) break;
            lh.pop_back();
        }
        lh.push_back(p);
    }
}

struct event {
    long long y;
    int op, i;
    event(long long y, int op, int i) : y(y), op(op), i(i) {};

    bool operator < (const event& e) const {
        if (y != e.y) return y < e.y;
        return op < e.op;
    }
};

long long num[MAXN];
long long l[MAXN],r[MAXN],x[MAXN];
long long bit[3*MAXN];

void add(int x, long long val) {
    for (; x < 3*MAXN; x += x & -x) bit[x] += val;
}

long long get(int x) {
    long long res = 0;
    for (; x > 0; x -= x & -x) res += bit[x];
    return res;
}

void go(point u, point v, bool incr, bool inctop) {
    point e = v - u;
    vector<event> events;
    vector<long long> cx;
    for (int i = 0; i < q; i++) {
        long long y = cross(e, p[i] + u * t[i]);
        if (e.x == 0) {
            if (inctop) y = p[i].y + t[i] * max(u.y, v.y);
            else y = p[i].y + t[i] * min(u.y, v.y) - 1;
        } else if (!inctop) y--;
        events.push_back(event(y, 1, i));
        l[i] = p[i].x + t[i] * u.x;
        r[i] = p[i].x + t[i] * v.x - 1;
        if (incr) r[i]++;
        cx.push_back(l[i]);
        cx.push_back(r[i]);
    }
    for (int i = 0; i < n; i++) {
        events.push_back(event(cross(e, f[i]), 0, i));
        x[i] = f[i].x;
        cx.push_back(x[i]);
    }

    sort(cx.begin(), cx.end());
    cx.erase(unique(cx.begin(), cx.end()), cx.end());
    for (int i = 0; i < q; i++) {
        l[i] = upper_bound(cx.begin(), cx.end(), l[i]) - cx.begin();
        r[i] = upper_bound(cx.begin(), cx.end(), r[i]) - cx.begin();
    }
    for (int i = 0 ; i < n; i++) {
        x[i] = upper_bound(cx.begin(), cx.end(), x[i]) - cx.begin();
    }

    memset(bit,0,sizeof(bit));
    sort(events.begin(), events.end());
    for (auto &ev : events) {
        int i = ev.i;
        if (ev.op == 1) {
            num[i] = get(r[i]) - get(l[i] - 1);
        } else {
            add(x[i], a[i]);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin >> k >> n >> q;
    for (int i = 0; i < k; i++) {
        cin >> v[i].x >> v[i].y;
    }

    for (int i = 0; i < n; i++) {
        cin >> f[i].x >> f[i].y >> a[i];
    }

    for (int i = 0; i < q; i++) {
        cin >> p[i].x >> p[i].y >> t[i];
    }

    minkowski();

    for (int i = 0; i + 1 < uh.size(); i++) {
        go(uh[i], uh[i + 1], i + 2 == uh.size(), 1);
        //cerr << uh[i].x << "," << uh[i].y << " -> " << uh[i+1].x << "," << uh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] += num[j];
            //cerr << num[j] << endl;
        }
    }
    for (int i = 0; i + 1 < lh.size(); i++) {
        go(lh[i], lh[i + 1], i + 2 == lh.size(), 0);
        //cerr << lh[i].x << "," << lh[i].y << " -> " << lh[i+1].x << "," << lh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] -= num[j];
            //cerr << -num[j] << endl;
        }
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i] << '\n';
    }
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 100010
#define MAXK 10

using namespace std;

struct point {
    long long x,y;
    point() {}
    point(long long x, long long y) : x(x), y(y) {}

    point operator + (point p) { return point(x+p.x, y+p.y); }
    point operator - (point p) { return point(x-p.x, y-p.y); }
    point operator * (long long a) { return point(a*x, a*y); }

    bool operator < (point p) const {
        if (x != p.x) return x < p.x;
        return y < p.y;
    }
    bool operator == (point p) const {
        return x == p.x && y == p.y;
    }
};

long long cross(point a, point b) {
    return a.x * b.y - a.y * b.x;
}

int k,n,q;
point v[MAXK],f[MAXN],p[MAXN];
int a[MAXN],t[MAXN];
long long ans[MAXN];

vector<point> m_points;

void gen_points(int i, point x) {
    if (i == k) {
        m_points.push_back(x);
        return;
    }
    gen_points(i + 1, x + v[i]);
    gen_points(i + 1, x - v[i]);
}

vector<point> uh, lh;
void minkowski() {
    gen_points(0, point(0, 0));
    sort(m_points.begin(), m_points.end());
    m_points.erase(unique(m_points.begin(), m_points.end()), m_points.end());

    for (point &p : m_points) {
        while (uh.size() > 1) {
            point a = uh[uh.size()-2], b = uh.back();
            if (cross(p-a,b-a) > 0) break;
            uh.pop_back();
        }
        uh.push_back(p);
    }

    for (point &p : m_points) {
        while (lh.size() > 1) {
            point a = lh[lh.size()-2], b = lh.back();
            if (cross(b-a,p-a) > 0) break;
            lh.pop_back();
        }
        lh.push_back(p);
    }
}

struct event {
    long long y;
    int op, i;
    event(long long y, int op, int i) : y(y), op(op), i(i) {};

    bool operator < (const event& e) const {
        if (y != e.y) return y < e.y;
        return op < e.op;
    }
};

long long num[MAXN];
long long l[MAXN],r[MAXN],x[MAXN];
long long bit[3*MAXN];

void add(int x, long long val) {
    for (; x < 3*MAXN; x += x & -x) bit[x] += val;
}

long long get(int x) {
    long long res = 0;
    for (; x > 0; x -= x & -x) res += bit[x];
    return res;
}

void go(point u, point v, bool incr, bool inctop) {
    point e = v - u;
    vector<event> events;
    vector<long long> cx;
    for (int i = 0; i < q; i++) {
        long long y = cross(e, p[i] + u * t[i]);
        if (e.x == 0) {
            if (inctop) y = p[i].y + t[i] * max(u.y, v.y);
            else y = p[i].y + t[i] * min(u.y, v.y) - 1;
        } else if (!inctop) y--;
        events.push_back(event(y, 1, i));
        l[i] = p[i].x + t[i] * u.x;
        r[i] = p[i].x + t[i] * v.x - 1;
        if (incr) r[i]++;
        cx.push_back(l[i]);
        cx.push_back(r[i]);
    }
    for (int i = 0; i < n; i++) {
        long long y = cross(e, f[i]);
        if (e.x == 0) y = f[i].y;
        events.push_back(event(y, f[i]), 0, i));
        x[i] = f[i].x;
        cx.push_back(x[i]);
    }

    sort(cx.begin(), cx.end());
    cx.erase(unique(cx.begin(), cx.end()), cx.end());
    for (int i = 0; i < q; i++) {
        l[i] = upper_bound(cx.begin(), cx.end(), l[i]) - cx.begin();
        r[i] = upper_bound(cx.begin(), cx.end(), r[i]) - cx.begin();
    }
    for (int i = 0 ; i < n; i++) {
        x[i] = upper_bound(cx.begin(), cx.end(), x[i]) - cx.begin();
    }

    memset(bit,0,sizeof(bit));
    sort(events.begin(), events.end());
    for (auto &ev : events) {
        int i = ev.i;
        if (ev.op == 1) {
            num[i] = get(r[i]) - get(l[i] - 1);
        } else {
            add(x[i], a[i]);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin >> k >> n >> q;
    for (int i = 0; i < k; i++) {
        cin >> v[i].x >> v[i].y;
    }

    for (int i = 0; i < n; i++) {
        cin >> f[i].x >> f[i].y >> a[i];
    }

    for (int i = 0; i < q; i++) {
        cin >> p[i].x >> p[i].y >> t[i];
    }

    minkowski();

    for (int i = 0; i + 1 < uh.size(); i++) {
        go(uh[i], uh[i + 1], i + 2 == uh.size(), 1);
        //cerr << uh[i].x << "," << uh[i].y << " -> " << uh[i+1].x << "," << uh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] += num[j];
            //cerr << num[j] << endl;
        }
    }
    for (int i = 0; i + 1 < lh.size(); i++) {
        go(lh[i], lh[i + 1], i + 2 == lh.size(), 0);
        //cerr << lh[i].x << "," << lh[i].y << " -> " << lh[i+1].x << "," << lh[i+1].y << endl;
        for (int j = 0; j < q; j++) {
            ans[j] -= num[j];
            //cerr << -num[j] << endl;
        }
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i] << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = (int)1e5 + 1, maxs = 21;
int n, m, q, stot, etot, que[maxn], rnk[maxn];
LL bit[maxn], ans[maxn];
struct Point {
	int x, y;
	void read() {
		scanf("%d%d", &x, &y);
	}
	bool operator < (Point const &t) const {
		return x < t.x || (x == t.x && y < t.y);
	}
	Point operator - (Point const &t) const {
		return (Point){x - t.x, y - t.y};
	}
	Point operator + (Point const &t) const {
		return (Point){x + t.x, y + t.y};
	}
	Point operator * (int const &t) const {
		return (Point){x * t, y * t};
	}
	LL det(Point const &t) const {
		return (LL)x * t.y - (LL)y * t.x;
	}
} stk[maxs], vec;
pair<Point, int> a[maxn], b[maxn];
Point p[maxn << 1 | 1];
struct Event {
	int typ, idx;
	LL dis;
	bool operator < (Event const &t) const {
		return dis < t.dis || (dis == t.dis && typ < t.typ);
	}
} eve[maxn << 2 | 1];
int main() {
	scanf("%d%d%d", &m, &n, &q);
	stk[stot++] = (Point){0, 0};
	while(m--) {
		vec.read();
		for(int i = 0; i < stot; ++i) {
			stk[stot + i] = stk[i] + vec;
			stk[i] = stk[i] - vec;
		}
		sort(stk, stk + stot + stot);
		int sz = 0;
		for(int i = 0; i < stot + stot; ++i) {
			for( ; sz > 1 && (stk[sz - 1] - stk[sz - 2]).det(stk[i] - stk[sz - 2]) <= 0; --sz);
			stk[sz++] = stk[i];
		}
		stot = sz;
	}
	stot -= stot > 1 && !(stk[stot - 1] - stk[stot - 2]).x;
	for(int i = 0; i < n; ++i) {
		a[i].first.read();
		scanf("%d", &a[i].second);
		que[i] = a[i].first.x;
	}
	sort(que, que + n);
	m = unique(que, que + n) - que;
	for(int i = 0; i < n; ++i)
		rnk[i] = lower_bound(que, que + m, a[i].first.x) - que + 1;
	for(int i = 0; i < q; ++i) {
		b[i].first.read();
		scanf("%d", &b[i].second);
	}
	for(int i = 0; i + 1 < stot; ++i) {
		vec = stk[i + 1] - stk[i];
		etot = 0;
		for(int j = 0; j < n; ++j)
			eve[etot++] = (Event){0, j, vec.det(a[j].first)};
		for(int j = 0; j < q; ++j) {
			p[j << 1] = b[j].first + stk[i] * b[j].second;
			eve[etot++] = (Event){-1, j << 1, vec.det(p[j << 1])};
			p[j << 1 | 1] = b[j].first - stk[i + 1] * b[j].second;
			eve[etot++] = (Event){1, j << 1 | 1, vec.det(p[j << 1 | 1])};
		}
		sort(eve, eve + etot);
		memset(bit + 1, 0, m * sizeof(LL));
		auto bit_upd = [&](int x, int v) {
			for( ; x <= m; x += x & -x)
				bit[x] += v;
		};
		auto bit_sum = [&](int x) {
			LL ret = 0;
			for( ; x > 0; x -= x & -x)
				ret += bit[x];
			return ret;
		};
		for(int j = 0; j < etot; ++j) {
			int idx = eve[j].idx;
			if(!eve[j].typ) {
				bit_upd(rnk[idx], a[idx].second);
			} else {
				Point lft = p[idx], rht = lft + vec * b[idx >> 1].second;
				lft.x += eve[j].typ < 0 && i > 0 || eve[j].typ > 0 && i + 2 < stot;
				int L = lower_bound(que, que + m, lft.x) - que + 1;
				int R = upper_bound(que, que + m, rht.x) - que;
				LL las = ans[idx >> 1];
				ans[idx >> 1] += eve[j].typ * (bit_sum(R) - bit_sum(L - 1));
				las = ans[idx >> 1] - las;
			}
		}
	}
	for(int i = 0; i < q; ++i)
		printf("%lld\n", ans[i]);
	return 0;
}


#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
#define K 11
#define N 100001
#define int long long
#define LOG(x) cerr<<#x<<" = "<<x<<endl
#define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
char ch;bool fs;void re(int& x)
{
	while(ch=getchar(),ch<33);
	if(ch=='-')fs=1,x=0;else fs=0,x=ch-48;
	while(ch=getchar(),ch>33)x=x*10+ch-48;
	if(fs)x=-x;
}
using namespace std;
struct node
{
	int x,y;
	bool operator < (const node &i)const
	{
		return atan2(y,x)<atan2(i.y,i.x);
	}
}v[K];
struct work
{
	double a;
	int x,y,z;
	bool operator < (const work &i)const
	{
		return fabs(a-i.a)<1e-7?z<i.z:a<i.a;
	}
}w[N+N];
int ans[N],px[N],py[N],t[N];
int k,n,q,sx,sy,hn,fx[N],fy[N],a[N],ha[N];
int s[N];
int ps(int x){return upper_bound(ha+1,ha+hn+1,x)-ha-1;}
void add(int x,int y)
{
	// cerr<<x<<" "<<y<<endl;
	for(;x<N;x+=x&-x)
		s[x]+=y;
}
int ask(int x)
{
	int res=0;
	for(;x;x-=x&-x)
		res+=s[x];
	return res;
}
main()
{
	re(k),re(n),re(q);
	for(int i=1;i<=k;++i)
	{
		re(v[i].x),re(v[i].y);
		if(v[i].x<0)v[i].x*=-1,v[i].y*=-1;
		if(v[i].x==0 && v[i].y<0)v[i].y*=-1;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1);
	for(int i=1;i<=n;++i)
		re(fx[i]),re(fy[i]),re(a[i]),ha[i]=fx[i];
	sort(ha+1,ha+n+1);
	hn=unique(ha+1,ha+n+1)-ha-1;
	for(int i=1;i<=q;++i)
	{
		re(px[i]);re(py[i]);re(t[i]);
		px[i]-=sx*t[i];py[i]-=sy*t[i];t[i]*=2;
	}
	for(int i=1;i<=k;++i)
	{
		if(!v[i].x){for(int j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(int j=1;j<=n;++j)w[j]=(work){fy[j]-(double)fx[j]/v[i].x*v[i].y,fx[j],a[j],0};
		for(int j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]/v[i].x*v[i].y,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1);

		memset(s,0,sizeof s);
		for(int j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=ask(ps(w[j].y))-ask(ps(w[j].x-(i<2)));
			else add(ps(w[j].x),w[j].y);
	}
	for(int i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(int j=1;j<=n;++j)w[j]=(work){fy[j]-(double)fx[j]/v[i].x*v[i].y,fx[j],a[j],0};
		for(int j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]/v[i].x*v[i].y,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1);
		// for(int j=1;j<=n+q;++j)
            // cerr<<w[j].a<<" "<<w[j].x<<" "<<w[j].y<<" "<<w[j].z<<endl;
		memset(s,0,sizeof s);
		for(int j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=ask(ps(w[j].y-(i>1)))-ask(ps(w[j].x-1));
			else add(ps(w[j].x),w[j].y);
	}
	for(int i=1;i<=q;++i)
		printf("%lld\n",ans[i]);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
inline int read()
{
	int x,f=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')f=0;
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return f?x:-x;
}
#define MK 10
#define MN 100000
#define ps(x) (upper_bound(c+1,c+cn+1,x)-c-1)
struct vec{int x,y;}v[MK+5];
struct work{double a;int x,y,z;}w[MN*2+5];
bool cmpv(const vec&a,const vec&b){return atan2(a.y,a.x)<atan2(b.y,b.x);}
bool cmpw(const work&a,const work&b){return abs(a.a-b.a)<1e-7?a.z<b.z:a.a<b.a;}
int x[MN+5],y[MN+5],a[MN+5],c[MN+5],cn,sx,sy,px[MN+5],py[MN+5],t[MN+5];
ll ans[MN+5],s[MN+5];
void add(int k,int x){for(;k<=MN;k+=k&-k)s[k]+=x;}
ll sum(int k){ll res=0;for(;k;k-=k&-k)res+=s[k];return res;}
int main()
{
	int k,n,q,i,j;
	k=read();n=read();q=read();
	for(i=1;i<=k;++i)
	{
		v[i].x=read();v[i].y=read();
		if(v[i].x<0)v[i].x=-v[i].x,v[i].y=-v[i].y;
		if(!v[i].x&&v[i].y<0)v[i].y=-v[i].y;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1,cmpv);
	for(i=1;i<=n;++i)c[i]=x[i]=read(),y[i]=read(),a[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=q;++i)px[i]=read(),py[i]=read(),t[i]=read(),px[i]-=sx*t[i],py[i]-=sy*t[i],t[i]<<=1;
	for(i=1;i<=k;++i)
	{
		if(!v[i].x){for(j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=sum(ps(w[j].y))-sum(ps(w[j].x-(i==1)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=sum(ps(w[j].y))-sum(ps(w[j].x-(i==k)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=q;++i)printf("%I64d\n",ans[i]);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define int long long
#define ll long long
inline int read()
{
	int x,f=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')f=0;
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return f?x:-x;
}
#define MK 10
#define MN 100000
#define ps(x) (upper_bound(c+1,c+cn+1,x)-c-1)
struct vec{int x,y;}v[MK+5];
struct work{double a;int x,y,z;}w[MN*2+5];
bool cmpv(const vec&a,const vec&b){return atan2(a.y,a.x)<atan2(b.y,b.x);}
bool cmpw(const work&a,const work&b){return abs(a.a-b.a)<1e-7?a.z<b.z:a.a<b.a;}
int x[MN+5],y[MN+5],a[MN+5],c[MN+5],cn,sx,sy,px[MN+5],py[MN+5],t[MN+5];
ll ans[MN+5],s[MN+5];
void add(int k,int x){for(;k<=MN;k+=k&-k)s[k]+=x;}
ll sum(int k){ll res=0;for(;k;k-=k&-k)res+=s[k];return res;}
main()
{
	int k,n,q,i,j;
	k=read();n=read();q=read();
	for(i=1;i<=k;++i)
	{
		v[i].x=read();v[i].y=read();
		if(v[i].x<0)v[i].x=-v[i].x,v[i].y=-v[i].y;
		if(!v[i].x&&v[i].y<0)v[i].y=-v[i].y;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1,cmpv);
	for(i=1;i<=n;++i)c[i]=x[i]=read(),y[i]=read(),a[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=q;++i)px[i]=read(),py[i]=read(),t[i]=read(),px[i]-=sx*t[i],py[i]-=sy*t[i],t[i]<<=1;
	for(i=1;i<=k;++i)
	{
		if(!v[i].x){for(j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=sum(ps(w[j].y))-sum(ps(w[j].x-(i==1)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=sum(ps(w[j].y))-sum(ps(w[j].x-(i==k)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=q;++i)printf("%I64d\n",ans[i]);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
inline int read()
{
	int x,f=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')f=0;
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return f?x:-x;
}
#define MK 10
#define MN 100000
#define ps(x) (upper_bound(c+1,c+cn+1,x)-c-1)
struct vec{int x,y;}v[MK+5];
struct work{double a;int x,y,z;}w[MN*2+5];
bool cmpv(const vec&a,const vec&b){return atan2(a.y,a.x)<atan2(b.y,b.x);}
bool cmpw(const work&a,const work&b){return abs(a.a-b.a)<1e-12?a.z<b.z:a.a<b.a;}
int x[MN+5],y[MN+5],a[MN+5],c[MN+5],cn,sx,sy,px[MN+5],py[MN+5],t[MN+5];
ll ans[MN+5],s[MN+5];
void add(int k,int x){for(;k<=MN;k+=k&-k)s[k]+=x;}
ll sum(int k){ll res=0;for(;k;k-=k&-k)res+=s[k];return res;}
int main()
{
	int k,n,q,i,j;
	k=read();n=read();q=read();
	for(i=1;i<=k;++i)
	{
		v[i].x=read();v[i].y=read();
		if(v[i].x<0)v[i].x=-v[i].x,v[i].y=-v[i].y;
		if(!v[i].x&&v[i].y<0)v[i].y=-v[i].y;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1,cmpv);
	for(i=1;i<=n;++i)c[i]=x[i]=read(),y[i]=read(),a[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=q;++i)px[i]=read(),py[i]=read(),t[i]=read(),px[i]-=sx*t[i],py[i]-=sy*t[i],t[i]<<=1;
	for(i=1;i<=k;++i)
	{
		if(!v[i].x){for(j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=sum(ps(w[j].y))-sum(ps(w[j].x-(i==1)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=sum(ps(w[j].y))-sum(ps(w[j].x-(i==k)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=q;++i)printf("%I64d\n",ans[i]);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
inline int read()
{
	int x,f=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')f=0;
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return f?x:-x;
}
#define MK 10
#define MN 100000
#define ps(x) (upper_bound(c+1,c+cn+1,x)-c-1)
struct vec{int x,y;}v[MK+5];
struct work{double a;int x,y,z;}w[MN*2+5];
bool cmpv(const vec&a,const vec&b){return atan2(a.y,a.x)<atan2(b.y,b.x);}
bool cmpw(const work&a,const work&b){return fabs(a.a-b.a)<1e-7?a.z<b.z:a.a<b.a;}
int x[MN+5],y[MN+5],a[MN+5],c[MN+5],cn,sx,sy,px[MN+5],py[MN+5],t[MN+5];
ll ans[MN+5],s[MN+5];
void add(int k,int x){for(;k<=MN;k+=k&-k)s[k]+=x;}
ll sum(int k){ll res=0;for(;k;k-=k&-k)res+=s[k];return res;}
int main()
{
	int k,n,q,i,j;
	k=read();n=read();q=read();
	for(i=1;i<=k;++i)
	{
		v[i].x=read();v[i].y=read();
		if(v[i].x<0)v[i].x=-v[i].x,v[i].y=-v[i].y;
		if(!v[i].x&&v[i].y<0)v[i].y=-v[i].y;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1,cmpv);
	for(i=1;i<=n;++i)c[i]=x[i]=read(),y[i]=read(),a[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=q;++i)px[i]=read(),py[i]=read(),t[i]=read(),px[i]-=sx*t[i],py[i]-=sy*t[i],t[i]<<=1;
	for(i=1;i<=k;++i)
	{
		if(!v[i].x){for(j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=sum(ps(w[j].y))-sum(ps(w[j].x-(i==1)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=sum(ps(w[j].y))-sum(ps(w[j].x-(i==k)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=q;++i)printf("%I64d\n",ans[i]);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
inline int read()
{
	int x,f=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')f=0;
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return f?x:-x;
}
#define MK 10
#define MN 100000
#define ps(x) (upper_bound(c+1,c+cn+1,x)-c-1)
struct vec{int x,y;}v[MK+5];
struct work{double a;int x,y,z;}w[MN*2+5];
bool cmpv(const vec&a,const vec&b){return atan2(a.y,a.x)<atan2(b.y,b.x);}
bool cmpw(const work&a,const work&b){return fabs(a.a-b.a)<1e-7?a.z<b.z:a.a<b.a;}
int x[MN+5],y[MN+5],a[MN+5],c[MN+5],cn,sx,sy,px[MN+5],py[MN+5],t[MN+5];
ll ans[MN+5],s[MN+5];
void add(int k,int x){for(;k<=MN;k+=k&-k)s[k]+=x;}
ll sum(int k){ll res=0;for(;k;k-=k&-k)res+=s[k];return res;}
int main()
{
	int k,n,q,i,j;
	k=read();n=read();q=read();
	for(i=1;i<=k;++i)
	{
		v[i].x=read();v[i].y=read();
		if(v[i].x<0)v[i].x=-v[i].x,v[i].y=-v[i].y;
		if(!v[i].x&&v[i].y<0)v[i].y=-v[i].y;
		sx+=v[i].x;sy+=v[i].y;
	}
	sort(v+1,v+k+1,cmpv);
	for(i=1;i<=n;++i)c[i]=x[i]=read(),y[i]=read(),a[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=q;++i)px[i]=read(),py[i]=read(),t[i]=read(),px[i]-=sx*t[i],py[i]-=sy*t[i],t[i]<<=1;
	for(i=1;i<=k;++i)
	{
		if(!v[i].x){for(j=1;j<=q;++j)py[j]+=v[i].y*t[j];continue;}
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j],px[j]+v[i].x*t[j],-j},
			px[j]+=v[i].x*t[j],py[j]+=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[-w[j].z]-=sum(ps(w[j].y))-sum(ps(w[j].x-(i<2)));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=k;++i)
	{
		if(!v[i].x)break;
		for(j=1;j<=n;++j)w[j]=(work){y[j]-(double)x[j]*v[i].y/v[i].x,x[j],a[j],0};
		for(j=1;j<=q;++j)w[n+j]=(work){py[j]-(double)px[j]*v[i].y/v[i].x,px[j]-v[i].x*t[j],px[j],j},
			px[j]-=v[i].x*t[j],py[j]-=v[i].y*t[j];
		sort(w+1,w+n+q+1,cmpw);
		memset(s,0,sizeof(s));
		for(j=1;j<=n+q;++j)
			if(w[j].z)ans[w[j].z]+=sum(ps(w[j].y-(i>1)))-sum(ps(w[j].x-1));
			else add(ps(w[j].x),w[j].y);
	}
	for(i=1;i<=q;++i)printf("%I64d\n",ans[i]);
}//hello


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXV=10;
const int MAXP=100000;
const int MAXQ=100000;
typedef struct P { int x,y; P() {} P(int x,int y):x(x),y(y) {} } P;
P operator+(const P &a,const P &b) { return P(a.x+b.x,a.y+b.y); }
P operator-(const P &a,const P &b) { return P(a.x-b.x,a.y-b.y); }
ll operator^(const P &a,const P &b) { return (ll)a.x*b.y-(ll)a.y*b.x; }
bool operator<(const P &a,const P &b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; }
ll side(const P &a,const P &b,const P &c) { return (b-a)^(c-a); }
typedef struct R { ll x; ll val; int mlt,idx; R() {} R(ll x,ll val,int mlt,int idx):x(x),val(val),mlt(mlt),idx(idx) {} } R;
bool operator<(const R &a,const R &b) { return a.x<b.x; }

vector<P> calchull(vector<P> p) {
	sort(p.begin(),p.end());
	vector<P> ret; ret.PB(p[0]); int nret;
	nret=SZ(ret); REPSZ(i,p) { while(SZ(ret)-nret>0&&side(ret[SZ(ret)-2],ret[SZ(ret)-1],p[i])<=0) ret.pop_back(); ret.PB(p[i]); }
	nret=SZ(ret); for(int i=SZ(p)-1;i>=0;--i) { while(SZ(ret)-nret>0&&side(ret[SZ(ret)-2],ret[SZ(ret)-1],p[i])<=0) ret.pop_back(); ret.PB(p[i]); }
	ret.pop_back(); return ret;
}

int nv,np,nq;
P v[MAXV];
P p[MAXP]; int pcnt[MAXP];
P q[MAXQ]; int qt[MAXQ];

R a[MAXP]; int na;
R b[2*MAXQ]; int nb;
ll c[MAXP]; int nc;
ll ans[MAXQ];

ll bt[MAXP+1];
void bmod(int idx,int by) { ++idx; while(idx<=nc) { bt[idx]+=by; idx+=idx&-idx; } }
ll bget(int idx) { ll ret=0; ++idx; while(idx>0) { ret+=bt[idx]; idx-=idx&-idx; } return ret; }

void solve(int it) {
	//REP(i,na) printf("a%d: x=%lld val=%lld cnt=%d\n",a[i].idx,a[i].x,a[i].val,a[i].mlt);
	//REP(i,nb) printf("b%d: x=%lld val=%lld mlt=%d\n",b[i].idx,b[i].x,b[i].val,b[i].mlt);

	nc=0; REP(i,na) c[nc++]=a[i].val; sort(c,c+nc); nc=unique(c,c+nc)-c;
	REPE(i,nc) bt[i]=0;
	sort(a,a+na); sort(b,b+nb); int ai=0;
	REP(bi,nb) {
		while(ai<na&&a[ai].x<=b[bi].x) {
			int aidx=lower_bound(c,c+nc,a[ai].val)-c;
			//if(it==3) printf("bmod(%d,%d)\n",aidx,a[ai].mlt);
			bmod(aidx,a[ai].mlt);
			++ai;
		}
		int bidx=lower_bound(c,c+nc,b[bi].val)-c; if(bidx==nc||c[bidx]>b[bi].val) --bidx;
		ll cur=bget(bidx);
		//if(it==3) printf("bget(%d)=%lld\n",bidx,cur);
		ans[b[bi].idx]+=cur*b[bi].mlt;
		//if(cur!=0) printf("\t%d + %lld * %d\n",b[bi].idx,cur,b[bi].mlt);
	}
}

void run() {
	scanf("%d%d%d",&nv,&np,&nq);
	REP(i,nv) scanf("%d%d",&v[i].x,&v[i].y);
	REP(i,np) scanf("%d%d%d",&p[i].x,&p[i].y,&pcnt[i]);
	REP(i,nq) scanf("%d%d%d",&q[i].x,&q[i].y,&qt[i]);

	vector<P> hull; hull.PB(P(0,0));
	REP(i,nv) {
		vector<P> nhull; REPSZ(j,hull) nhull.PB(hull[j]+v[i]),nhull.PB(hull[j]-v[i]);
		hull=calchull(nhull);
	}
	//printf("hull:"); REPSZ(i,hull) printf(" (%d,%d)",hull[i].x,hull[i].y); puts("");

	int xmx=hull[0].x; FORSZ(i,1,hull) xmx=max(xmx,hull[i].x);
	REP(i,nq) ans[i]=0;
	REPSZ(it,hull) {
		P A=hull[it],B=hull[(it+1)%SZ(hull)];
		int lx=A.x,ly=A.y,rx=B.x,ry=B.y; bool swapped=false;
		if(lx==rx) continue; if(lx>rx) swap(lx,rx),swap(ly,ry),swapped=true;
		int dx=rx-lx,dy=ry-ly;
		//printf("(%d,%d)-(%d,%d)%s\n",lx,ly,rx,ry,swapped?"swapped":"");
		// (qx+t*lx,qy+t*ly)-(qx+t*rx,qy+t*ry)    QY=qy+t*ly, QX=qx+t*lx    -> y*dx-x*dy<=QY*dx-QX*dy
		na=nb=0;
		REP(i,np) {
			int x=p[i].x,y=p[i].y;
			a[na++]=R(x,(ll)y*dx-(ll)x*dy,pcnt[i],i);
		}
		REP(i,nq) {
			ll lqx=q[i].x+(ll)qt[i]*lx,lqy=q[i].y+(ll)qt[i]*ly,rqx=q[i].x+(ll)qt[i]*rx;
			b[nb++]=R(rx==xmx?rqx:rqx-1,lqy*dx-lqx*dy+(!swapped?-1:0),!swapped?-1:+1,i);
			b[nb++]=R(lqx-1,lqy*dx-lqx*dy+(!swapped?-1:0),!swapped?+1:-1,i);
		}
		solve(it);
	}
	REP(i,nq) printf("%lld\n",ans[i]);
}

int main() {
	run();
	return 0;
}


#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
typedef vector<int> VI;
int power(int a, int b, int m, int ans=1) {
	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;
	return ans;
}

const int NN = 10011;
INT a[NN], b[NN];
map<INT, INT> sum;
map<INT, int> cnt;
int flag[NN];
int q[NN];
int qf, qb;

int main() {
#ifndef	ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
    int n;
    cin >> n;
    for(int i=0; i<n; i++) cin >> a[i], cnt[a[i]]++;
    for(int i=0; i<n; i++) cin >> b[i];
    INT ans = 0;
    for(int i=0; i<n; i++) {
        if(cnt[a[i]]>1) q[qb++] = i, flag[i] = 1;
    }
    while(qb>qf) {
        int u=q[qf++];
        ans += b[u];
        for(int i=0; i<n; i++) if(flag[i] == 0 and (a[i] & a[u]) == a[i]) q[qb++] = i, flag[i] = 1;
    }
    if(qb<2) ans = 0;
    cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
using DO = double;
using LL = long long;
using VI = vector<int>;
using pii = pair<int, int>;
const int mod = 1e9+7;
const int N = 7e3+10;
LL a[N];
LL b[N];
int c[N];
bool f[N][N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  LL ans=0;
  int n, m;
  cin>>n;
  m=n;
  for(int i=0; i<n; i++) {
    scanf("%I64d", a+i);
    c[i]=i;
  }
  for(int i=0; i<n; i++) {
    scanf("%I64d", b+i);
  }
  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      f[i][j]=(a[i]^(a[i]&a[j]))==0;
    }
  }
  for(int g=n; g>0; g--) {
    for(int i=0; i<m; i++) {
      int j=0;
      for(; j<m; j++) {
        if(i!=j) {
          if(f[c[i]][c[j]]) {
            break;
          }
        }
      }
      if(j == m) {
        swap(c[i], c[m-1]);
        m--;
        break;
      }
    }
    if(m == g) {
      break;
    }
  }
  if(m < 2) {
    puts("0");
    return 0;
  }
  for(int i=0; i<m; i++) {
    ans+=b[c[i]];
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
using DO = double;
using LL = long long;
using VI = vector<int>;
using pii = pair<int, int>;
const int mod = 1e9+7;
const int N = 7e3+10;
const LL inf = 0x3f3f3f3f3f3f3f3fll;
LL a[N];
LL b[N];
bool f[N][N];
int can[N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  LL ans=0;
  int n;
  cin>>n;
  for(int i=0; i<n; i++) {
    scanf("%I64d", a+i);
    can[i]=1;
  }
  for(int i=0; i<n; i++) {
    scanf("%I64d", b+i);
  }
  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      f[i][j]=(a[i]^(a[i]&a[j]))==0;
    }
  }
  int tot=n;
  for(int T=0, ok; T<n; T++) {
    ok=0;
    for(int i=0; i<n; i++) {
      if(can[i]) {
        int j=0;
        for(; j<n; j++) {
          if(i!=j && can[j]) {
            if(f[i][j]) {
              break;
            }
          }
        }
        if(j == n) {
          ok=1;
          can[i]=0;
          tot--;
          break;
        }
      }
    }
    if(ok == 0) {
      break;
    }
  }
  if(tot < 2) {
    puts("0");
    return 0;
  }
  for(int i=0; i<n; i++) {
    if(can[i]) {
      ans+=b[i];
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
using DO = double;
using LL = long long;
using VI = vector<int>;
using pii = pair<int, int>;
const int mod = 1e9+7;
const int N = 7e3+10;
const LL inf = 0x3f3f3f3f3f3f3f3fll;
LL a[N];
LL b[N];
int can[N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  LL ans=0;
  int n;
  cin>>n;
  for(int i=0; i<n; i++) {
    scanf("%I64d", a+i);
    can[i]=1;
  }
  for(int i=0; i<n; i++) {
    scanf("%I64d", b+i);
  }
  int tot=n;
  for(int T=0, ok; T<n; T++) {
    ok=0;
    for(int i=0; i<n; i++) {
      if(can[i]) {
        int j=0;
        for(; j<n; j++) {
          if(i!=j && can[j]) {
            if(!(a[i]^(a[i]&a[j]))) {
//            cerr<<i<<' '<<j<<' '<<(a[i]^(a[i]&a[j]))<<endl;
              break;
            }
          }
        }
        if(j == n) {
          ok=1;
          can[i]=0;
          tot--;
          break;
        }
      }
    }
    if(ok == 0) {
      break;
    }
  }
  if(tot < 2) {
    puts("0");
    return 0;
  }
  for(int i=0; i<n; i++) {
    if(can[i]) {
      ans+=b[i];
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second

using namespace std;
typedef long long INT;
typedef pair<INT,INT> pii;

#define NN 7010
INT a[NN],b[NN];
pii p[NN];
int vst[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%I64d",&b[i]);
	for(int i=1;i<=n;i++) p[i]=pii(a[i],b[i]);
  	INT ans=0;
  	sort(p+1,p+n+1);
  	for(int i=1;i<=n;i++){
		a[i]=p[i].x;
		b[i]=p[i].y;
	}
	for(int i=n;i>=1;i--){
		int st=i;
		while(a[st]==a[i]&&st>=1) st--;
		st++;
		INT x=a[st];
		if(i!=st){
			for(int j=i;j>=st;j--) vst[j]=1;
			for(int j=1;j<st;j++){
				if((a[j]&x)==a[j]) vst[j]=1;
			}
		}
		i=st;
	}
	for(int i=1;i<=n;i++){
		if(vst[i]) ans+=b[i];
	}
	printf("%I64d\n",ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long llint;

const int maxn = 7010;

int n;
llint a[maxn], b[maxn];
map<llint, int> m;

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

    for (int i = 0; i < n; i++)
        scanf("%lld", b+i);

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

    llint sol = 0;
    for (int i = 0; i < n; i++) {
        if (m[a[i]] == 1) continue;

        llint tren = 0;
        for (int j = 0; j < n; j++)
            if ((a[i] | a[j]) == a[i]) tren += b[j];
        sol = max(sol, tren);
    }
    printf("%d", sol);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long llint;

const int maxn = 7010;

int n;
llint a[maxn], b[maxn];
map<llint, int> m;

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

    for (int i = 0; i < n; i++)
        scanf("%lld", b+i);

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

    llint sol = 0;
    for (int i = 0; i < n; i++) {
        if (m[a[i]] == 1) continue;

        llint tren = 0;
        for (int j = 0; j < n; j++)
            if ((a[i] | a[j]) == a[i]) tren += b[i];
        sol = max(sol, tren);
    }
    printf("%d", sol);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long llint;

const int maxn = 7010;

int n;
llint a[maxn], b[maxn];
bool bio[maxn];
map<llint, int> m;

int main() {
    memset(bio, false, sizeof bio);
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%lld", a+i);

    for (int i = 0; i < n; i++)
        scanf("%lld", b+i);

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

    llint sol = 0;
    for (int i = 0; i < n; i++) {
        if (bio[i]) continue;
        if (m[a[i]] == 1) continue;
        for (int j = 0; j < n; j++)
            if (!bio[j] && (a[i] | a[j]) == a[i]) sol += b[j], bio[j] = true, m[a[j]]--;
    }
    printf("%d", sol);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long llint;

const int maxn = 7010;

int n;
llint a[maxn], b[maxn];
bool bio[maxn];
map<llint, int> m;

int main() {
    memset(bio, false, sizeof bio);
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%lld", a+i);

    for (int i = 0; i < n; i++)
        scanf("%lld", b+i);

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

    llint sol = 0;
    for (int i = 0; i < n; i++) {
        if (bio[i]) continue;
        if (m[a[i]] == 1) continue;
        for (int j = 0; j < n; j++)
            if (!bio[j] && (a[i] | a[j]) == a[i]) sol += b[j], bio[j] = true, m[a[j]]--;
    }
    printf("%lld", sol);
    return 0;
}


//#define NDEBUG

#include <bits/stdc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define modadd(a, b) (((a % MOD) + (b % MOD)) % MOD)
#define modtimes(a, b) (((a % MOD) * (b % MOD)) % MOD)

//#define TEST

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const ll MOD = 1000000007;
const ll MAX = 1LL << 60;

int main(){
    StarBurstStream

    int n;
    cin >> n;

    map<ll, ll> cnt;
    vector<bool> choose;
    vector<ll> a(n), b(n);
    vector<bool> c(n);
    vector<ll> v;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cnt[a[i]]++;
    }
    for(int  i = 0; i < n; i++) cin >> b[i];

    for(int i = 0; i < n; i++){
        if(cnt[a[i]] > 1){
            c[i] = true;
            v.pb(a[i]);
        }
    }

    for(int i = 0; i < n; i++){
        if(c[i]) continue;
        for(ll j : v){
            if((a[i] & j) == a[i]){
                c[i] = true;
                v.pb(a[i]);
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < n; i++) if(c[i]) ans += b[i];
    cout << ans << "\n";

    return 0;
}


//#define NDEBUG

#include <bits/stdc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define modadd(a, b) (((a % MOD) + (b % MOD)) % MOD)
#define modtimes(a, b) (((a % MOD) * (b % MOD)) % MOD)

//#define TEST

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const ll MOD = 1000000007;
const ll MAX = 1LL << 60;

int main(){
    StarBurstStream

    int n;
    cin >> n;

    map<ll, ll> cnt;
    vector<bool> choose;
    vector<ll> a(n), b(n);
    vector<bool> c(n);
    vector<ll> v;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cnt[a[i]]++;
    }
    for(int  i = 0; i < n; i++) cin >> b[i];

    for(int i = 0; i < n; i++){
        if(cnt[a[i]] > 1){
            c[i] = true;
            v.pb(a[i]);
        }
    }

    for(int i = 0; i < n; i++){
        for(ll j : v){
            if((a[i] & j) == a[i]){
                c[i] = true;
                v.pb(a[i]);
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < n; i++) if(c[i]) ans += b[i];
    cout << ans << "\n";

    return 0;
}


//#define NDEBUG

#include <bits/stdc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define modadd(a, b) (((a % MOD) + (b % MOD)) % MOD)
#define modtimes(a, b) (((a % MOD) * (b % MOD)) % MOD)

//#define TEST

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const ll MOD = 1000000007;
const ll MAX = 1LL << 60;

int main(){
    StarBurstStream

    int n;
    cin >> n;

    map<ll, ll> cnt;
    vector<bool> choose;
    vector<ll> a(n), b(n);
    vector<bool> c(n);
    vector<ll> v;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cnt[a[i]]++;
    }
    for(int  i = 0; i < n; i++) cin >> b[i];

    for(int i = 0; i < n; i++){
        if(cnt[a[i]] > 1){
            c[i] = true;
            v.pb(a[i]);
        }
    }

    for(int i = 0; i < n; i++){
        if(c[i]) continue;
        for(ll j : v){
            if((a[i] & j) == a[i]){
                c[i] = true;
                v.pb(a[i]);
                break;
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < n; i++) if(c[i]) ans += b[i];
    cout << ans << "\n";

    return 0;
}


#include<bits/stdc++.h>

using namespace std;
bool lesser(int x,int y)
{
     int f=0;
    //to check if y<x
if(y<=x)
return true;

return false;

    }

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

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

   }
 int f=0;

   int sum=0,maxa=0;

   for(int i=0;i<n;i++)
   {
       sum=b[i];
       for(int j=i+1;j<n;j++)
       {
           if(lesser(a[i],a[j]))
           {
               //cout<<b[i]<<" "<<b[j]<<endl;
               sum+=b[j];
              // cout<<sum<<endl;
              f++;

           }
       }
       maxa=max(maxa,sum);

   }
   if(f)
   cout<<maxa;
   else
   cout<<0;

      return 0;
}


#include<bits/stdc++.h>

using namespace std;
bool lesser(int x,int y)
{
     int f=0;
    //to check if y<x
    for(int i=1;i<61;i++)
    {

        if(!((x>>(i-1))&1))
        {
            if((!((y>>(i-1)&1))))
            continue;
            else
            {
            f++;
            break;
            }
        }

        }
                    if(f)
            return false;
            else
             return true;

    }

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

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

   }
 int f=0;

   int sum=0,maxa=0;

   for(int i=0;i<n;i++)
   {
       sum=b[i];
       for(int j=i+1;j<n;j++)
       {
           if(lesser(a[i],a[j]))
           {
               //cout<<b[i]<<" "<<b[j]<<endl;
               sum+=b[j];
              // cout<<sum<<endl;
              f++;

           }
       }
       maxa=max(maxa,sum);

   }
   if(f)
   cout<<maxa;
   else
   cout<<0;

      return 0;
}


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

int main() {
    int n;
    cin>>n;
    ll arr[n+1];
    map<ll,int>s;
    ll brr[n+1];
    for (int i=0;i<n;i++)
    {
        cin>>arr[i];
        if (s.find(arr[i])!=s.end())
        s[arr[i]]++;
        else
        s[arr[i]]=1;
    }
    /*for (auto x:s)
    cout<<x.first<<" "<<x.second<<endl;
    cout<<endl;*/
    for (int i=0;i<n;i++)
    cin>>brr[i];
    bool v[7005]={0};
    ll ans=0;
    for (int i=0;i<n;i++)
    {
        if (s[arr[i]]>1)
        {
            v[arr[i]]=1;
            ans+=brr[i];

        }
    }

   // cout<<ans<<endl<<endl;
    for (int i=0;i<n;i++)

       if (s[arr[i]]==1)
       for (int j=0;j<n;j++)

           if ((arr[j]&arr[i])==arr[i]&&v[arr[j]])
           {
               ans+=brr[i];
               //v[i]=1;
              // cout<<brr[i]<<endl;
               break;
           }

       //cout<<endl;

    cout<<ans<<endl;
}


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

int main() {
    int n;
    cin>>n;
    ll arr[n+1];
    map<ll,int>s;
    ll brr[n+1];
    for (int i=1;i<=n;i++)
    {
        cin>>arr[i];
        if (s.find(arr[i])!=s.end())
        s[arr[i]]++;
        else
        s[arr[i]]=1;
    }
    /*for (auto x:s)
    cout<<x.first<<" "<<x.second<<endl;
    cout<<endl;*/
    for (int i=1;i<=n;i++)
    cin>>brr[i];
    bool v[7005]={0};
    ll ans=0;
    for (int i=1;i<=n;i++)
    {
        if (s[arr[i]]>1)
        {
            v[i]=1;
            ans+=brr[i];

        }
    }

   // cout<<ans<<endl<<endl;
    for (int i=1;i<=n;i++)

       if (s[arr[i]]==1)
       for (int j=1;j<=n;j++)

           if ((arr[j]&arr[i])==arr[i]&&v[j])
           {
               ans+=brr[i];
               //v[i]=1;
              // cout<<brr[i]<<endl;
               break;
           }

       //cout<<endl;

    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define ll int
#define pb push_back
using namespace std;
typedef pair<ll,ll> pll;

ll n;
long long a[7007], b[7007];
ll x[7007];
ll y[7007];
bool done[7007];
map<ll, vector< ll> > mp;
void goz(ll xx, queue<ll> &q, long long &ans)
{
        if(mp[xx].size() > 0) {
                for(auto &i : mp[xx]) {
                        if(x[i] > 0) {
                                x[i]--;
                                if(x[i] == 0) {
                                        ans -= b[i];
                                        q.push(i);
                                }
                        }
                }
        }
        for(ll j=0;j<=59;j++) {
                if(xx&(1LL << j)) {
                        goz((xx^(1LL << j)) , q, ans);
                }
        }
}
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL); cout.tie(NULL);
        cin >> n;
        long long ans = 0;
        for(ll i=1;i<=n;i++) {
                cin >> a[i];
                mp[a[i]].pb(i);
        }
        for(ll i=1;i<=n;i++) {
                cin >> b[i];
                ans += b[i];
        }
        // x is parent, y is child
        for(ll i=1;i<=n;i++) {
                for(ll j=1;j<=n;j++) {
                        if(i==j) continue;
                        if((a[i] | a[j]) == a[i]) {
                                x[j]++;
                                y[i]++;
                        }
                }
        }
        queue<ll> q;
        for(ll i=1;i<=n;i++) {
                if(x[i] == 0) {
                        q.push(i);
                        done[i] = true;
                        ans -= b[i];
                }
        }
        while(q.empty() == false) {

                ll u = q.front();
                q.pop();
                goz(a[u] , q, ans);
        }
        cout << ans << endl;
        return 0;
}


#include <bits/stdc++.h>
#define ll int
#define pb puhs_back
using namespace std;
typedef pair<ll,ll> pll;

ll n;
long long a[7007], b[7007];
set< ll > x[7007], y[7007];
bool done[7007];
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL); cout.tie(NULL);
        cin >> n;
        long long ans = 0;
        for(ll i=1;i<=n;i++) {
                cin >> a[i];
        }
        for(ll i=1;i<=n;i++) {
                cin >> b[i];
                ans += b[i];
        }
        // x is parent, y is child
        for(ll i=1;i<=n;i++) {
                for(ll j=1;j<=n;j++) {
                        if(i==j) continue;
                        if((a[i] | a[j]) == a[i]) {
                                x[j].insert(i);
                                y[i].insert(j);
                        }
                }
        }
        queue<ll> q;
        for(ll i=1;i<=n;i++) {
                if(x[i].size() == 0) {
                        q.push(i);
                        done[i] = true;
                        ans -= b[i];
                }
        }
        while(q.empty() == false) {

                ll u = q.front();
                q.pop();
                for(auto &j : y[u]) {
                        if(x[j].find(u) != x[j].end()) x[j].erase(u);
                        if(x[j].size() == 0 && done[j]==false) {
                                done[j] = true;
                                q.push(j);
                                ans -= b[j];
                        }
                }
        }
        cout << ans << endl;
        return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
#define pb puhs_back
using namespace std;
typedef pair<ll,ll> pll;

ll n;
ll a[7007], b[7007];
set< ll > x[7007], y[7007];
bool done[7007];
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL); cout.tie(NULL);
        cin >> n;
        ll ans = 0;
        for(ll i=1;i<=n;i++) {
                cin >> a[i];
        }
        for(ll i=1;i<=n;i++) {
                cin >> b[i];
                ans += b[i];
        }
        // x is parent, y is child
        for(ll i=1;i<=n;i++) {
                for(ll j=1;j<=n;j++) {
                        if(i==j) continue;
                        if((a[i] | a[j]) == a[i]) {
                                x[j].insert(i);
                                y[i].insert(j);
                        }
                }
        }
        queue<ll> q;
        for(ll i=1;i<=n;i++) {
                if(x[i].size() == 0) {
                        q.push(i);
                        done[i] = true;
                        ans -= b[i];
                }
        }
        while(q.empty() == false) {

                ll u = q.front();
                q.pop();
                for(auto &j : y[u]) {
                        if(x[j].find(u) != x[j].end()) x[j].erase(u);
                        if(x[j].size() == 0 && done[j]==false) {
                                done[j] = true;
                                q.push(j);
                                ans -= b[j];
                        }
                }
        }
        cout << ans << endl;
        return 0;
}


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

typedef long long ll;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector<ll> a(n);
	vector<int> b(n);
	map<ll, vector<int>> m;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	for (int i = 0; i < n; ++i) {
		cin >> b[i];
		m[a[i]].push_back(b[i]);
	}
	ll ans = 0;
	set<ll> masks;
	for (auto x: m) {
		if (x.second.size() > 1) {
			for (auto v: x.second)
				ans += v;
			masks.insert(x.first);
		}
	}
	for (int i = 0; i < n; ++i) {
		if (!masks.count(a[i])) {
			for (auto x: masks) {
				if ((a[i] | x) == x) {
					ans += b[i];
					break;
				}
			}
		}
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll,ll>
#define F first
#define S second
ll max(ll a,ll b){if (a>b) return a; else return b;}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll T=1;
	//cin >> T;
	ll t=0;
	while(t++<T){
		ll n;
		cin>>n;
		vll a(n);
		vll b(n);
		map<ll,ll> m;
		vll v(n,0);
		fo(i,n){cin>>a[i];m[a[i]]++;}
		fo(i,n){if(m[a[i]]>=2)v[i]=1;}
		fo(i,n)cin>>b[i];
		fo(i,n){
			if(v[i]){
				fo(j,n){
					if(v[j])continue;
					ll check=a[i]&a[j];
					if(check==a[j])v[j]=1;
				}
			}
		}
		ll ans=0;
		fo(i,n)if(v[i])ans+=b[i];
		cout<<ans;
	}
return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll,ll>
#define F first
#define S second
ll max(ll a,ll b){if (a>b) return a; else return b;}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll T=1;
	//cin >> T;
	ll t=0;
	while(t++<T){
		ll n;
		cin>>n;
		vvl a(n,vll(2));
		fo(i,n)cin>>a[i][0];
		fo(i,n)cin>>a[i][1];
		sort(a.begin(),a.end());
		vll pre(n,0);
		pre[0]=a[0][1];
		for(ll i=1;i<n;i++)pre[i]=a[i][1]+pre[i-1];
		ll ans=0;
		fo(i,n-1){
			if(a[i][0]==a[i+1][0]){
				ll j=i+1;
				while(j<n && a[j][0]==a[j-1][0])j++;
				ans=max(ans,pre[j-1]);
				i=j;
			}
		}
		cout<<ans;
	}
return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll,ll>
#define F first
#define S second
ll max(ll a,ll b){if (a>b) return a; else return b;}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll T=1;
	//cin >> T;
	ll t=0;
	while(t++<T){
		ll n;
		cin>>n;
		vvl a(n,vll(2));
		fo(i,n)cin>>a[i][0];
		fo(i,n)cin>>a[i][1];
		sort(a.begin(),a.end());
		vll pre(n,0);
		pre[0]=a[0][1];
		for(ll i=1;i<n;i++)pre[i]=a[i][1]+pre[i-1];
		ll ans=0;
		fo(i,n-1){
			if(a[i][0]==a[i+1][0]){
				ll j=i+1;
				while(j<n && a[j][0]==a[j-1][0])j++;
				ans=max(ans,pre[j-1]);
				i=j-1;
			}
		}
		cout<<ans;
	}
return 0;
}


#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>

using namespace std;

#define pb push_back
#define all(X) X.begin(), X.end()
#define ll long long
#define forn(i, n) for (int i = 0; i < (int) n; i++)

int main()
{
#ifdef DEBUG
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<pair<ll, ll>> a(n);
  for (auto &e: a) scanf("%lld", &e.first);
  for (auto &e: a) scanf("%lld", &e.second);

  sort(all(a));
  ll ret = 0;
  set<ll> s;
  forn(i, n) {
    if (i > 0 && a[i].first == a[i - 1].first) s.insert(a[i].first);
  }
  forn(i, n)
    for(auto e: s) {
      if ((a[i].first & e) == a[i].first) {
        ret += a[i].second;
        break;
      }
    }

  printf("%lld\n", ret);

  return 0;
}


#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>

using namespace std;

#define pb push_back
#define all(X) X.begin(), X.end()
#define ll long long
#define forn(i, n) for (int i = 0; i < (int) n; i++)

int main()
{
#ifdef DEBUG
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<pair<ll, ll>> a(n);
  for (auto &e: a) scanf("%lld", &e.first);
  for (auto &e: a) scanf("%lld", &e.second);

  sort(all(a));
  ll ret = 0;
  set<ll> s;
  forn(i, n) {
    if (i > 0 && a[i].first == a[i - 1].first) s.insert(a[i].first);
  }
  forn(i, n)
    for(auto e: s)
      if ((a[i].first & e) == a[i].first) ret += a[i].second;

  printf("%lld\n", ret);

  return 0;
}


#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>

using namespace std;

#define pb push_back
#define all(X) X.begin(), X.end()
#define ll long long
#define forn(i, n) for (int i = 0; i < (int) n; i++)

int main()
{
#ifdef DEBUG
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<pair<ll, ll>> a(n);
  for (auto &e: a) scanf("%lld", &e.first);
  for (auto &e: a) scanf("%lld", &e.second);

  sort(all(a));
  ll ret = 0;
  set<ll> s;
  forn(i, n) {
    if (s.find(a[i].first) != s.end()) {
      ll lret = 0;
      forn(j, i + 1) {
        if ((a[i].first & a[j].first) == a[j].first) {
          lret += a[j].second;
        }
      }
      ret = max(lret, ret);
    }
    s.insert(a[i].first);
  }
  printf("%lld\n", ret);

  return 0;
}


#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>

using namespace std;

#define pb push_back
#define all(X) X.begin(), X.end()
#define ll long long
#define forn(i, n) for (int i = 0; i < (int) n; i++)

int main()
{
#ifdef DEBUG
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<pair<ll, ll>> a(n);
  for (auto &e: a) scanf("%lld", &e.first);
  for (auto &e: a) scanf("%lld", &e.second);

  sort(all(a));
  ll ret = 0;
  set<ll> s;
  forn(i, n) {
    if (i > 0 && a[i].first == a[i - 1].first) s.insert(a[i].first);
  }
  forn(i, n)
    for(auto e: s) {
      if ((a[i].first & e) == a[i].first) ret += a[i].second;
      break;
    }

  printf("%lld\n", ret);

  return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long long ll;

const int max_n = 7022, inf = 1000111222;

int n;
map<ll, ll> mp;
pair<ll, ll> a[max_n];

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].F);
    }
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].S);
        mp[a[i].F] += a[i].S;
    }
    sort(a, a + n);
    ll ans = 0;
    vector<ll> v;
    for (int i = 0; i < n; ++i) {
        if ((i == 0 || a[i].F != a[i - 1].F) && mp[a[i].F] != a[i].S) {
            v.push_back(a[i].F);
        }
    }
    for (int i = 0; i < n; ++i) {
        for (ll bb : v) {
            if ((bb & a[i].F) == a[i].F) {
                ans += a[i].S;
            }
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long long ll;

const int max_n = 100022, inf = 1000111222;

int main() {

    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long long ll;

const int max_n = 7022, inf = 1000111222;

int n;
map<ll, ll> mp;
pair<ll, ll> a[max_n];

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].F);
    }
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].S);
        mp[a[i].F] += a[i].S;
    }
    sort(a, a + n);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        if ((i == 0 || a[i].F != a[i - 1].F) && mp[a[i].F] != a[i].S) {
            ll res = mp[a[i].F];
            for (int q = 0; q < i; ++q) {
                if ((a[q].F & a[i].F) == a[q].F) {
                    res += a[q].S;
                }
            }
            ans = max(ans, res);
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long long ll;

const int max_n = 7022, inf = 1000111222;

int n;
map<ll, ll> mp;
pair<ll, ll> a[max_n];

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].F);
    }
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].S);
        mp[a[i].F] += a[i].S;
    }
    sort(a, a + n);
    ll ans = 0;
    vector<ll> v;
    for (int i = 0; i < n; ++i) {
        if ((i == 0 || a[i].F != a[i - 1].F) && mp[a[i].F] != a[i].S) {
            v.push_back(a[i].F);
        }
    }
    for (int i = 0; i < n; ++i) {
        for (ll bb : v) {
            if ((bb & a[i].F) == a[i].F) {
                ans += a[i].S;
                break;
            }
        }
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long long ll;

const int max_n = 7022, inf = 1000111222;

int n;
map<ll, ll> mp;
pair<ll, ll> a[max_n];

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &a[i].F);
    }
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i].S);
        mp[a[i].F] += a[i].S;
    }
    sort(a, a + n);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        if ((i == 0 || a[i].F != a[i - 1].F) && mp[a[i].F] != a[i].S) {
            ll res = mp[a[i].F];
            for (int q = 0; q < i; ++q) {
                if ((a[q].F & a[i].F) == a[q].F) {
                    res += a[q].S;
                }
            }
            ans = max(ans, res);
        }
    }
    cout << ans << endl;

    return 0;
}


#include<iostream>
#include<cstdio>
#define ll long long
int n;
int vis[100010];
ll a[100010],b[100010];
ll ans=0;
using namespace std;
int main(){
	scanf("%d",&n);
	for(register int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(register int i=1;i<=n;i++)scanf("%lld",&b[i]);
	for(register int i=1;i<=n;i++){
		if(vis[i])continue;
		int res=0;
		for(register int j=1;j<=n;j++)if(a[j]==a[i])res++;
		if(res>=2){
			for(register int j=1;j<=n;j++)if((a[i]&a[j])==a[j])vis[j]=1;
		}
	}
	for(register int i=1;i<=n;i++)if(vis[i])ans+=b[i];
	printf("%lld",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#define ll long long
int n;
int vis[100010];
ll a[100010],b[100010];
ll ans=0;
using namespace std;
int main(){
	scanf("%d",&n);
	for(register int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(register int i=1;i<=n;i++)scanf("%lld",&b[i]);
	for(register int i=1;i<=n;i++){
		if(vis[i])continue;
		int res=0;
		for(register int j=1;j<=n;j++)if(a[j]==a[i])res++;
		if(res>=2){
			for(register int j=1;j<=n;j++)if((a[i]&a[j])==a[j])vis[j]=1;
		}
	}
	for(register int i=1;i<=n;i++)if(vis[i])ans+=b[i];
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#define ll long long
int n;
int vis[100010];
ll a[100010],b[100010];
ll ans=0;
using namespace std;
int main(){
	scanf("%d",&n);
	for(register int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(register int i=1;i<=n;i++)scanf("%lld",&b[i]);
	for(register int i=1;i<=n;i++){
		if(vis[i])continue;
		int res=0;
		for(register int j=1;j<=n;j++)if(a[j]==a[i])res++;
		if(res>=2){
			cout<<a[i]<<endl;
			for(register int j=1;j<=n;j++)if((a[i]&a[j])==a[j])vis[j]=1;
		}
	}
	for(register int i=1;i<=n;i++)if(vis[i])ans+=b[i];
	printf("%d",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n;
map <int,int> times;
long long a[7001], b[7001];
bool ver[7001];
int raspuns;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        times[a[i]]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(times[a[i]]>1)
        {
            raspuns+=b[i];
            ver[i]=1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!ver[i])
        {
            for(int j=1;j<=n;j++)
            {
                if(ver[j] && (a[i]|a[j])==a[j])
                {
                    raspuns+=b[i];
                    break;
                }
            }
        }
    }
    cout<<raspuns;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n;
map <long long,long long> times;
long long a[7001], b[7001];
bool ver[7001];
long long raspuns;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        times[a[i]]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(times[a[i]]>1)
        {
            raspuns+=b[i];
            ver[i]=1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!ver[i])
        {
            for(int j=1;j<=n;j++)
            {
                if(ver[j] && (a[i]|a[j])==a[j])
                {
                    raspuns+=b[i];
                    break;
                }
            }
        }
    }
    cout<<raspuns;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n;
map <int,int> times;
long long a[7001], b[7001];
bool ver[7001];
long long raspuns;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        times[a[i]]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(times[a[i]]>1)
        {
            raspuns+=b[i];
            ver[i]=1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!ver[i])
        {
            for(int j=1;j<=n;j++)
            {
                if(ver[j] && (a[i]|a[j])==a[j])
                {
                    raspuns+=b[i];
                    break;
                }
            }
        }
    }
    cout<<raspuns;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n;
map <long long,long long> times;
long long a[7001], b[7001];
bool ver[7001];
int raspuns;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        times[a[i]]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(times[a[i]]>1)
        {
            raspuns+=b[i];
            ver[i]=1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!ver[i])
        {
            for(int j=1;j<=n;j++)
            {
                if(ver[j] && (a[i]|a[j])==a[j])
                {
                    raspuns+=b[i];
                    break;
                }
            }
        }
    }
    cout<<raspuns;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 7005;

int n;
pair<ll, ll> a[N];
int can[N];
ll dp[N];

int main() {

	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i].first);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i].second);

	sort(a, a + n);

	{
		int k = 1;
		for(int i = 1; i < n; i++) {
			if(a[i].first == a[k - 1].first) {
				can[k-1] = 1;
				a[k-1].second += a[i].second;
			}
			else a[k++] = a[i];
		}
		n = k;
	}

	for(int i = n - 1; i >= 0; i--) {
		for(int j = n - 1; j > i; j--) if(can[j]) {
			if((a[i].first & a[j].first) == a[i].first) {
				a[j].second += a[i].second;
				can[i] = 0;
				break;
			}
		}
	}

	ll ans = 0;

	for(int i = 0; i < n; i++) if(can[i])
		ans += a[i].second;

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 7005;

int n;
pair<ll, ll> a[N];
int can[N];
int id[N];
ll dp[N];

int main() {

	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i].first);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i].second);

	sort(a, a + n);

	{
		int k = 1;
		for(int i = 1; i < n; i++) {
			if(a[i].first == a[k - 1].first) {
				can[k-1] = 1;
				a[k-1].second += a[i].second;
			}
			else a[k++] = a[i];
		}
		n = k;
	}

	for(int i = n - 1; i >= 0; i--) {
		for(int j = i + 1; j < n; j++) {
			if((a[i].first & a[j].first) == a[i].first) {
				a[j].second += a[i].second;
			}
		}
	}

	ll ans = 0;

	for(int i = 0; i < n; i++) if(can[i])
		ans = max(ans, a[i].second);

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 7005;

int n;
ll a[N];
int b[N];
int can[N];
int id[N];
ll dp[N];

int main() {

	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%lld", a + i);
	for(int i = 0; i < n; i++) scanf("%d", b + i);

	{
		map<ll, int> ids;

		for(int i = 0; i < n; i++) {
			if(ids.count(a[i])) {
				int j = ids[ a[i] ];
				can[j] = 1;
				b[j] += b[i];
			}
			else {
				ids[ a[i] ] = i;
			}
		}
		int k = 0;
		for(int i = 0; i < n; i++) {
			if(ids[ a[i] ] == i) {
				a[k] = a[i];
				b[k] = b[i];
				can[k] = can[i];
				k++;
			}
		}
		n = k;
	}

	for(int i = 0; i < n; i++) id[i] = i;

	sort(id, id + n, [](int q, int w){
		return a[q] < a[w];
	});

	{ // change a
		vector<ll> tmp(a, a + n);
		for(int i = 0; i < n; i++)
			a[i] = tmp[ id[i] ];
	}
	{ // change b
		vector<int> tmp(b, b + n);
		for(int i = 0; i < n; i++)
			b[i] = tmp[ id[i] ];
	}
	{ // change can
		vector<int> tmp(can, can + n);
		for(int i = 0; i < n; i++)
			can[i] = tmp[ id[i] ];
	}

	ll ans = 0;
	for(int i = n - 1; i >= 0; i--) {
		ll tmp = 0;
		for(int j = i + 1; j < n; j++) {
			if((a[i] & a[j]) != a[i]) continue;
			if(can[j]) {
				tmp = max(tmp, b[i] + dp[j]);
			}
		}
		ans = max(ans, tmp);
		dp[i] = tmp + b[i];
		if(tmp > 0) can[i] = 1;
	}

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 7005;

int n;
pair<ll, int> a[N];
int can[N];
int id[N];
ll dp[N];

int main() {

	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i].first);
	for(int i = 0; i < n; i++) scanf("%d", &a[i].second);

	sort(a, a + n);

	{
		int k = 1;
		for(int i = 1; i < n; i++) {
			if(a[i].first == a[k - 1].first) {
				can[k-1] = 1;
				a[k-1].second += a[i].second;
			}
			else a[k++] = a[i];
		}
		n = k;
	}

	ll ans = 0;
	for(int i = n - 1; i >= 0; i--) {
		ll tmp = 0;
		for(int j = i + 1; j < n; j++) {
			if((a[i].first & a[j].first) != a[i].first) continue;
			if(can[j]) {
				tmp = max(tmp, a[i].second + dp[j]);
			}
		}
		ans = max(ans, tmp);
		dp[i] = tmp;
		if(tmp > 0) can[i] = 1;
		else if(can[i]) dp[i] = a[i].second;
	}

	printf("%lld\n", ans);
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
bool vis[maxn];
vector<LL>v;
set<int>s;
struct Node
{
    LL v1,v2;
}a[maxn];
bool cmp(const Node a,const Node b)
{
    return a.v1>b.v1;
}
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v1);
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v2);
    sort(a+1,a+N+1,cmp);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=i+1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
                LL x=(a[i].v1&(1ll<<k)),y=(a[j].v1&(1ll<<k));
//                cout<<"x:"<<x<<" y:"<<y<<endl;
                if(x==0&&y!=0)
                {
                    flag1=1;break;
                }
            }
            if(j==i+1&&a[j].v1!=a[i].v1) break;//没有和本身相等的数
            if(a[j].v1==a[i].v1)//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) vis[v[i]]=true;
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=a[i].v2;
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
LL a[maxn],b[maxn],c[maxn];
bool vis[maxn];
vector<LL>v;
set<int>s;
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=N;i++) scanf("%lld",&b[i]);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            if(i==j) continue;
            else if(a[j]>a[i]) continue;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
//                cout<<"**"<<(1ll<<k)<<endl;
//                cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<endl;
//                cout<<(a[i]&(1ll<<k))<<" "<<(a[j]&(1ll<<k))<<endl;
                LL x=a[i]&(1ll<<k),y=a[j]&(1ll<<k);
                if(x==0&&y!=0)
                {
//                    cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<endl;
                    flag1=1;break;
                }
            }
            if(a[j]==a[i])//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
//                cout<<"i:"<<i<<" j:"<<j<<endl;
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) vis[v[i]]=true;
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=b[i];
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/