#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> l_l;
typedef pair<int, int> i_i;

const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = numeric_limits<ll>::max() / 4;
const ll MOD = 1e9+7;
// using mint = atcoder::modint1000000007;
void chmin(ll& a, ll b){ if(a > b) a = b; }
void chmax(ll& a, ll b){ if(a < b) a = b; }

#define rep(i, a, b) for(ll (i) = (a); (i) < (b);++i)

template <typename T>
struct RMQ {
	using Func = function<T(T, T)>;
	const T e;
	int n;
	Func fx;
	// node 0<=i<n-1 n-1<=i 葉
	vector<T> node, lazy;
	RMQ(int n_, T e_, Func func) : n(), e(e_), fx(func), node(n_ * 4, e), lazy(n_ * 4, e) {
		int x = 1;
		while (n_ > x)
			x *= 2;
		n = x;
	}
	void set(int i, T x) { node[i + n - 1] = x; }
	void build() {
		for (int k = n - 2; k >= 0;k--) {
			node[k] = fx(node[2*k+1], node[2*k+2]);
		}
	}
	/* lazy eval */
	void eval(int k) {
		if (lazy[k] == e) return;	 // 更新するものが無ければ終了
		if (k < n - 1) {			 // 葉でなければ子に伝搬
			lazy[k * 2 + 1] = lazy[k];
			lazy[k * 2 + 2] = lazy[k];
		}
		// 自身を更新
		node[k]	= lazy[k];
		lazy[k] = e;
	}
	void update(int a, int b, T x, int k, int l, int r) {
		eval(k);
		if (a <= l && r <= b) {	 // 完全に内側の時
			lazy[k] = x;
			eval(k);
		} else if (a < r && l < b) {  // 一部区間が被る時
			update(a, b, x, k * 2 + 1, l, (l + r) / 2);	 // 左の子
			update(a, b, x, k * 2 + 2, (l + r) / 2, r);	 // 右の子
			node[k] = fx(node[k * 2 + 1], node[k * 2 + 2]);
		}
	}
	void update(int a, int b, T x) { update(a, b, x, 0, 0, n); }
	T query_sub(int a, int b, int k, int l, int r) {
		eval(k);
		if (r <= a || b <= l) {	 // 完全に外側の時
			return e;
		} else if (a <= l && r <= b) {	// 完全に内側の時
			return node[k];
		} else {  // 一部区間が被る時
			T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
			T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
			return fx(vl, vr);
		}
	}
	T query(int a, int b) { return query_sub(a, b, 0, 0, n); }
	/* debug */
	inline T operator[](int a) { return query(a, a + 1); }
	void print() {
		for (int i = 0; i < 2 * n - 1; ++i) {
			cout << (*this)[i];
			if (i != n) cout << ",";
		}
		cout << endl;
	}
};

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

	ll N, Q;
	cin >> N;
	ll a[N], t[N];
	rep(i,0,N) cin >> a[i] >> t[i];
	cin >> Q;
	ll x[Q];
	rep(i,0,Q) cin >> x[i];

	ll low = -INF, high = INF, add = 0;
	for (ll i = 0;i < N;i++) {
		// if (t[i] == 1) {
		// 	high += a[i];
		// 	low += a[i];
		// 	add += a[i];
		// }
		// if (t[i] == 2) {
		// 	chmax(low, a[i]);
		// 	chmax(high, a[i]);
		// }
		// if (t[i] == 3) {
		// 	chmin(high, a[i]);
		// 	chmin(low, a[i]);
		// }
		if(t[i] == 1){
            low += a[i];
            high += a[i];
            add += a[i];
        }
        else if(t[i] == 2){
            chmax(low, a[i]);
            chmax(high, a[i]);
        }
        else{
            chmin(low, a[i]);
            chmin(high, a[i]);
        }
	}
	for (ll i = 0;i < Q;i++) {
		cout << min(max(low, x[i]+add), high) << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define F(i,n) for (int i = 0; i < n; ++i)
#define F1(i,n) for (int i = 1; i <= n; ++i)
#define dbg(x) cerr << #x << " = " << x << endl
#define dbgg(x) cerr << #x << " = " << x << ' '
#define T(x) x[pool]
#define mineq(x,y) { if ((x) > (y)) (x) = (y); }
#define maxeq(x,y) { if ((x) < (y)) (x) = (y); }
#define MEOW cout << "meowwwww" << '\n'; system("pause");
#define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;

template<typename T>
ostream& operator <<(ostream &s, const vector<T> &c)
{
	s << "[ "; for (auto it : c) s << it << " "; s << "\b]\n";
	return s;
}

template<typename T>
ostream& operator <<(ostream &s, const pair<int, T> &c)
{
	s << "[ "; cout << c.fi << " , " << c.se << " ] ";
	return s;
}

const int maxn = 123;
int n;
int a[maxn];
int out[12345][maxn];
string s;

void input()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	++n;
	cin >> s;
	F1 (i, n) cin >> a[i];
}

void solve()
{
	int ans = 12345;
	F1 (i, n - 1) ans = min(ans, abs(a[i] - a[i + 1]));
	int mn = a[1], cur = 1;
	F1 (i, n) if (a[i] < mn) mn = a[i], cur = i;
	for (int i = 1; i <= ans; ++i)
		out[i][cur] = a[cur] / ans;
	for (int i = 1; i <= a[cur] % ans; ++i)
		++out[i][cur]; 
	for (int i = cur - 1; i >= 1; --i)
	{
		if (a[i] < a[i + 1])
		{
			F1 (j, ans) out[j][i] = out[j][i + 1] - 1;	
			priority_queue<pii> p;
			F1 (j, ans) p.push(mp(out[j][i], j));
			F (j, a[i + 1] - a[i] - ans)
			{
				--out[p.top().se][i];
				p.push(mp(out[p.top().se][i], p.top().se));
				p.pop();
			}
		} else
		{
			F1 (j, ans) out[j][i] = out[j][i + 1] + 1;
			priority_queue<pii, vector<pii>, greater<pii>> p;
			F1 (j, ans) p.push(mp(out[j][i], j));
			F (j, a[i] - a[i + 1] - ans)
			{
				++out[p.top().se][i];
				p.push(mp(out[p.top().se][i], p.top().se));
				p.pop();
			}
		}
	}
	for (int i = cur + 1; i <= n; ++i)
	{
		if (a[i] < a[i - 1])
		{
			F1 (j, ans) out[j][i] = out[j][i - 1] - 1;
			priority_queue<pii> p;
			F1 (j, ans) p.push(mp(out[j][i], j));
			//dbg(p.top());
			F (j, a[i - 1] - a[i] - ans)
			{
				--out[p.top().se][i];
				p.push(mp(out[p.top().se][i], p.top().se));
				p.pop();
			}
		} else
		{
			//MEOW
			F1 (j, ans) out[j][i] = out[j][i - 1] + 1;
			priority_queue<pii, vector<pii>, greater<pii>> p;
			//dbg(ans);
			F1 (j, ans) p.push(mp(out[j][i], j));
			//dbg(p.top());
			F (j, a[i] - a[i - 1] - ans)
			{
				++out[p.top().se][i];
				p.push(mp(out[p.top().se][i], p.top().se));
				p.pop();
			}
		}
	}
	cout << ans << '\n';
	F1 (i, ans) 
	{
		F1 (j, n) cout << out[i][j] << ' ';
		cout << '\n';
	}
}

main()
{
	input();
	solve();
}
