#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         cout<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define endl            "\n"
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());


void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new  ( std::size_t count ) {
    sz += count;
    assert_tle(sz <= 200 * 1024 * 1024);
    return myHeap + sz - count;
}
 
void operator delete (void *ptr) { }
 
 
void fastIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
vector<int> ans;

void dfs(vector<pii> graph[],int cur)
{
    for(pii i: graph[cur])
    {
        if(ans[i.ff]!=-1)
        {
            continue;
        }
        else
        {
            if(ans[cur]!=i.ss)
            {
                ans[i.ff]=i.ss;
            }
            else
            {
                ans[i.ff]=ans[cur]==0?1:(ans[cur]-1);
            }

            dfs(graph,i.ff);
        }
    }
}

void solve()
{
    int n,m;
    cin>>n>>m;
    ans.assign(n,-1);
    vector<pii> graph[n];
    for(int i=0;i<m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        c--;
        graph[a].pb({b,c});
        graph[b].pb({a,c});
    }

    ans[0]=0;
    dfs(graph,0);
    for(int i: ans)
    {
        cout<<i+1<<endl;
    }
}
 
int32_t main()
{
    fastIO();
   // w(t)
    {
        solve();
        cout<<endl;
    }
    return 0;
}
#include<iostream>
#define val first
#define type second
using namespace std;
typedef pair<int, int> Filter;
const int FILTER = 200005;
const int QUERY = 200005;
const long long INF = 1LL << 60;


int nFilter;
Filter filter[FILTER];
int nQuery;
int query[QUERY];


void read() {
    cin >> nFilter;
    for (int i = 0; i < nFilter; ++i) {
        cin >> filter[i].val >> filter[i].type;
    }
    cin >> nQuery;
    for (int i = 0; i < nQuery; ++i) {
        cin >> query[i];
    }
}


void work() {
    long long maxMaxV = -INF;
    long long minMinV = INF;
    long long sum = 0;
    
    for (int i = 0; i < nFilter; ++i) {
        Filter &f = filter[i];
        if (f.type == 1) {
            maxMaxV += f.val;
            minMinV += f.val;
            sum += f.val;
        } else if (f.type == 2) {
            maxMaxV = max(maxMaxV, 1LL * f.val);
            minMinV = max(minMinV, 1LL * f.val);
        } else if (f.type == 3) {
            minMinV = min(minMinV, 1LL * f.val);
            maxMaxV = min(maxMaxV, 1LL * f.val);
        }
    }

    for (int i = 0; i < nQuery; ++i) {
        cout << max(maxMaxV, min(minMinV, 1LL * query[i] + sum)) << endl;
    }
}


int main() {
    read();
    work();
    return 0;
}
