#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

    template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

struct Func{
    LL left = -LINF;
    LL right = LINF;
    LL offset = 0;

    void update(int t, LL a){
        if(t == 1){
            offset += a;
            if(left != -LINF) left += a;
            if(right != LINF) right += a;

        }
        if(t == 2){
            chmax(left, a);
            chmax(right, a);
        }
        if(t == 3){
            chmin(left, a);
            chmin(right, a);
        }

    }

    LL get(LL x){
        if(left == right){
            return left;
        }
        if(left == -LINF && right == LINF){
            return x + offset;
        }
        if(left == -LINF){
            return min(right, x + offset);
        }
        if(right == LINF){
            return max(left, x + offset);
        }

        LL x1 = left - offset;
        LL x2 = right - offset;
        if(x < x1) return left;
        if(x > x2) return right;

        return x + offset;

    }

};

signed main(int argc, char const *argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);
    int N; cin >> N;
    Func f;
    REP(i,N){
        LL a;
        int t;
        cin >> a >> t;
        f.update(t,a);
    }
    int Q; cin >> Q;
    REP(i,Q){
        LL x; cin >> x;
        cout << f.get(x) << "\n";
    }


}
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

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

#define pb push_back
#define F first
#define S second
#define f(i,a,b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
//#define endl '\n'

using ll = long long;
//using db = long double;
using ii = pair<int, int>;

const int N = 2e5 + 5, LG = 19, MOD = 998244353;
const int SQ =225;
int fast(int b, int c, int m) {
    if(c == 0)
        return 1;
    if(c & 1)
        return b * fast(b,c-1,m) % m;
    return fast(b * b % m, c >> 1,m);
}
int32_t main(){
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif


    string s;
    cin >> s;
    ll ans = 0;
    reverse(s.begin(),s.end());
    int cnt[26] = {};
    cnt[s[0]-'a'] += 1;

    for(int i = 2; i < s.size(); i++) {
        if(s[i] == s[i-1]) {
            ans += i-1-(cnt[s[i]-'a']);
            memset(cnt,0,sizeof cnt);
            cnt[s[i]-'a'] = i;
        }   else {
            cnt[s[i-1]-'a'] += 1;
        }
    }
    cout << ans << '\n';

  return 0;
}
