#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;

const int MAXN = 100010;

priority_queue<PII> que;
int sum[MAXN], col[MAXN], del[MAXN];
int cnt[2], ans, n;
int white, black;

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d%d", &col[i], &sum[i]), black += col[i];
    white += black;
    for(int i = 1; i <= n; ++i) que.push(make_pair(-sum[i], i));
    cnt[0] = cnt[1] = 1;
    while(!que.empty()) {
        if(white == 1 || black == 1) {
            int t = (black == 1);
            int &u = cnt[t];
            while(col[u] != t || del[u]) ++u;
            for(int v = cnt[t ^ 1]; v <= n; ++v)
                if(col[v] == (t ^ 1) && !del[v]) printf("%d %d %d\n", u, v, sum[v]);
            break;
        }

        int u = que.top().Y, t = -que.top().X; que.pop();
        if(t != sum[u] || del[u]) continue;
        int &v = cnt[col[u] ^ 1];
        while(col[v] != (col[u] ^ 1) || del[v]) ++v;
        printf("%d %d %d\n", u, v, t);
        del[u] = true;
        if(col[u] == 0) --white; else --black;
        sum[u] -= t, sum[v] -= t;
        que.push(make_pair(-sum[v], v));
        if(++ans == n - 1) break;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;

const int MAXN = 100010;

priority_queue<PII> que;
int sum[MAXN], col[MAXN], del[MAXN];
int cnt[2], ans, n;

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d%d", &col[i], &sum[i]);
    for(int i = 1; i <= n; ++i) que.push(make_pair(-sum[i], i));
    cnt[0] = cnt[1] = 1;
    while(!que.empty()) {
        int u = que.top().Y, t = -que.top().X; que.pop();
        if(t != sum[u] || del[u]) continue;
        int &v = cnt[col[u] ^ 1];
        while(col[v] != (col[u] ^ 1) || del[v]) ++v;
        printf("%d %d %d\n", u, v, t);
        del[u] = true;
        sum[u] -= t, sum[v] -= t;
        que.push(make_pair(-sum[v], v));
        if(++ans == n - 1) break;
    }
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;

const int MAXN = 100010;

priority_queue<PII> que;
int sum[MAXN], col[MAXN];
int cnt[2], ans, n;

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d%d", &col[i], &sum[i]);
    for(int i = 1; i <= n; ++i) que.push(make_pair(-sum[i], i));
    cnt[0] = cnt[1] = 1;
    while(!que.empty()) {
        int u = que.top().Y, t = -que.top().X; que.pop();
        if(t != sum[u]) continue;
        int &v = cnt[col[u] ^ 1];
        while(col[v] != (col[u] ^ 1) || sum[v] == 0) ++v;
        printf("%d %d %d\n", u, v, t);
        sum[u] -= t, sum[v] -= t;
        que.push(make_pair(-sum[v], v));
        if(++ans == n - 1) break;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int s, N, S[100100], C[100100], A[100100], B[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else      B[++B[0]]=i;
    }

    int b = 1;
    rep1(a, A[0])
    {
        if (b > B[0])
            cout << A[a] << " " << B[b] << " " << 0 << endl;
        while (b <= B[0])
        {
            if (S[A[a]] < S[B[b]])
            {
                cout << A[a] << " " << B[b] << " " << S[A[a]] << endl;
                break;
            }
            else
            {
                cout << A[a] << " " << B[b] << " " << S[B[b]] << endl;
                b++;
                S[A[a]]-=S[B[b]];
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int s, N, S[100100], C[100100], A[100100], B[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else      B[++B[0]]=i;
    }

    int b = 1;
    rep1(a, A[0])
    {
        if (b > B[0])
            cout << A[a] << " " << B[b] << " " << 0 << endl;
        while (b <= B[0])
        {
            if (S[A[a]] < S[B[b]])
            {
                cout << A[a] << " " << B[b] << " " << S[A[a]] << endl;
                S[B[b]]-=S[A[a]];
                break;
            }
            else
            {
                cout << A[a] << " " << B[b] << " " << S[B[b]] << endl;
                b++;
                S[A[a]]-=S[B[b]];
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int N, w, W[100100], b, B[100100], C[100100], S[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) B[b++]=i;
        else W[w++]=i;
    }
    w--, b--;
    while (w >= 0 && b >= 0)
    {
        int s = min(S[W[w]], S[B[b]]);
        cout << W[w] << " " << B[b] << " " << s << endl;
        S[W[w]]-=s;
        S[B[b]]-=s;
        if (S[B[b]]) w--; else b--;
    }
    w--;
    b--;
    while (w>=0) cout << W[w] << " " << B[0] << " " << 0 << endl, w--;
    while (b>=0) cout << W[0] << " " << B[b] << " " << 0 << endl, b--;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int s, b, w, N, B[100100], W[100100], C[100100], S[100100], connected[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);

    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) B[++B[0]]=i;
        else      W[++W[0]]=i;
    }
    b = w = 1;
    while (b <= B[0] && w <= W[0])
    {
        connected[W[w]]=connected[B[b]]=1;
        if (S[B[b]] < S[W[w]])
        {
            cout << B[b] << " " << W[w] << " " << S[B[b]] << endl;
            S[W[w]] -= S[B[b]];
            b++;
        }
        else
        {
            cout << B[b] << " " << W[w] << " " << S[W[w]] << endl;
            S[B[b]] -= S[W[w]];
            w++;
        }
    }
    while (b <= B[0])
    {
        if (! connected[B[b]])
        {
            cout << B[b] << " " << W[1] << " " << 0 << endl;
        }
        b++;
    }
    while (w <= W[0])
    {
        if (! connected[W[w]])
        {
            cout << B[1] << " " << W[w] << " " << 0 << endl;
        }
        w++;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int a, b, s, N, S[100100], C[100100], A[100100], B[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else      B[++B[0]]=i;
    }

    a = b = 1;
    while (a <= A[0] && b <= B[0])
    {
//        db(a);
//        db(b);
        s = min(S[A[a]], S[B[b]]);
        cout << A[a] << " " << B[b] << " " << s << endl;
        if (a == A[0] && b == B[0])
            return 0;
        if (s < S[A[a]])
        {
            b++;
            S[A[a]] -= s;
        }
        else if (s < S[B[b]])
        {
            a++;
            S[B[b]] -= s;
        }
        else if (a == A[0])
        {
            b++;
            S[A[a]] -= s;
        }
        else
        {
            a++;
            S[B[b]] -= s;
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int s, N, S[100100], C[100100], A[100100], B[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else      B[++B[0]]=i;
    }

    int b = 1;
    rep1(a, A[0])
    {
        if (b > B[0])
            cout << A[a] << " " << B[1] << " " << 0 << endl;
        while (b <= B[0])
        {
            if (S[A[a]] < S[B[b]])
            {
                cout << A[a] << " " << B[b] << " " << S[A[a]] << endl;
                S[B[b]]-=S[A[a]];
                break;
            }
            else
            {
                cout << A[a] << " " << B[b] << " " << S[B[b]] << endl;
                S[A[a]]-=S[B[b]];
                b++;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int N, a, b, A[100100], B[100100], S[100100], C[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else B[++B[0]]=i;
    }
    b = 1;
    rep1(a, A[0])
    {
        if (b > B[0]) cout << A[a] << " " << B[1] << " " << 0 << endl;
        while (b <= B[0])
        {
            if (S[A[a]] < S[B[b]])
            {
                cout << A[a] << " " << B[b] << " " << S[A[a]] << endl;
                S[B[b]]-=S[A[a]];
                break;
            }
            else
            {
                cout << A[a] << " " << B[b] << " " << S[B[b]] << endl;
                S[A[a]]-=S[B[b]];
                b++;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int N, w, W[100100], b, B[100100], C[100100], S[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) B[b++]=i;
        else W[w++]=i;
    }
    w--, b--;
    while (w >= 0 && b >= 0)
    {
        int s = min(S[W[w]], S[B[b]]);
        cout << W[w] << " " << B[b] << " " << s << endl;
        S[W[w]]-=s;
        S[B[b]]-=s;
        if (S[B[b]]) w--; else b--;
    }
    w--;
    b--;
    while (w>=0) cout << W[w] << " " << B[1] << " " << 0 << endl, w--;
    while (b>=0) cout << W[1] << " " << B[b] << " " << 0 << endl, b--;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int s, N, S[100100], C[100100], A[100100], B[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else      B[++B[0]]=i;
    }

    int b = 1;
    rep1(a, A[0])
    {
        if (b > B[0])
            cout << A[a] << " " << B[1] << " " << 0 << endl;
        while (b <= B[0])
        {
            if (S[A[a]] < S[B[b]])
            {
                cout << A[a] << " " << B[b] << " " << S[A[a]] << endl;
                S[B[b]]-=S[A[a]];
                break;
            }
            else
            {
                cout << A[a] << " " << B[b] << " " << S[B[b]] << endl;
                b++;
                S[A[a]]-=S[B[b]];
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int s, b, w, N, B[100100], W[100100], C[100100], S[100100], connected[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);

    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) B[++B[0]]=i;
        else      W[++W[0]]=i;
    }
    b = w = 1;
    while (b <= B[0] && w <= W[0])
    {
        connected[w]=connected[b]=1;
        if (S[B[b]] < S[W[w]])
        {
            cout << B[b] << " " << W[w] << " " << S[B[b]] << endl;
            S[W[w]] -= S[B[b]];
            b++;
        }
        else
        {
            cout << B[b] << " " << W[w] << " " << S[W[w]] << endl;
            S[B[b]] -= S[W[w]];
            w++;
        }
    }
    while (b <= B[0])
    {
        if (! connected[b])
        {
            cout << B[b] << " " << W[1] << " " << 0 << endl;
        }
        b++;
    }
    while (w <= W[0])
    {
        if (! connected[w])
        {
            cout << B[1] << " " << W[w] << " " << 0 << endl;
        }
        w++;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define mp make_pair
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pi 3.1415926535897932384626433832795028
#define db(x) cout << #x << " = " << (x) << endl;
#define dba(a,x,y) {cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;

int N, A[100100], B[100100], S[100100], C[100100];
int main()
{
    ios_base::sync_with_stdio(0);
    // cout.precision(10);
    cin >> N;
    rep1(i, N)
    {
        cin >> C[i] >> S[i];
        if (C[i]) A[++A[0]]=i;
        else      B[++B[0]]=i;
    }
    int b = 1;
    rep1(a, A[0])
    {
        if (b > B[0]) cout << A[a] << " " << B[1] << " 0" << endl;

        while (b <= B[0])
        {
            if (S[A[a]] < S[B[b]])
            {
                S[B[b]]-=S[A[a]];
                cout << A[a] << " " << B[b] << " " << S[A[a]] << endl;
                break;
            }
            else
            {
                S[A[a]]-=S[B[b]];
                cout << A[a] << " " << B[b] << " " << S[B[b]] << endl;
                b++;
            }
        }
    }

}


#include<stdio.h>
#include<algorithm>

#define N 100005
using namespace std;
int m,n,sa=0,sb=0;
struct ele{
    int idx,v;
}a[N],b[N];
bool cmp(ele c,ele d){return c.v>d.v;}
int main(){
    int i,j,k,x,y,z;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&x,&y);
        if(x) {a[++sa].v=y;a[sa].idx=i;}
        else {b[++sb].v=y;b[sb].idx=i;}
    }
    sort(a+1,a+1+sa,cmp);
    sort(b+1,b+1+sb,cmp);
    int p=1,q=1;
    while(--n){
        if(q>sb){
            q--;p++;
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            b[q].v-=a[p].v;
            q++;
        }
        else if(a[p].v>=b[q].v){
            printf("%d %d %d\n",a[p].idx,b[q].idx,b[q].v);
            a[p].v-=b[q].v;q++;
        }
        else{
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            b[q].v-=a[p].v;p++;
        }
    }
    getchar();getchar();
}


#include<stdio.h>
#include<algorithm>

#define N 100005
using namespace std;
int m,n,sa=0,sb=0;
struct ele{
    int idx,v;
}a[N],b[N];
bool cmp(ele c,ele d){return c.v>d.v;}
int main(){
    int i,j,k,x,y,z;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&x,&y);
        if(x) {a[++sa].v=y;a[sa].idx=i;}
        else {b[++sb].v=y;b[sb].idx=i;}
    }
    sort(a+1,a+1+sa,cmp);
    sort(b+1,b+1+sb,cmp);
    int p=1,q=1;
    while(--n){
        if(q>sb){
            q--;p++;
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            b[q].v-=a[p].v;
            q++;
        }
        else if(a[p].v>=b[q].v&&q<=sb){
            printf("%d %d %d\n",a[p].idx,b[q].idx,b[q].v);
            q++;a[p].v-=b[q].v;
        }
        else{
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            p++;b[q].v-=a[p].v;
        }
    }
    getchar();getchar();
}


#include<stdio.h>
#include<algorithm>

#define N 100005
using namespace std;
int m,n,sa=0,sb=0;
struct ele{
    int idx,v;
}a[N],b[N];
bool cmp(ele c,ele d){return c.v>d.v;}
int main(){
    int i,j,k,x,y,z;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&x,&y);
        if(x) {a[++sa].v=y;a[sa].idx=i;}
        else {b[++sb].v=y;b[sb].idx=i;}
    }
    sort(a+1,a+1+sa,cmp);
    sort(b+1,b+1+sb,cmp);
    int p=1,q=1;
    while(1){
        if(p>sa||q>sb) break;
        if(a[p].v>=b[q].v){
            printf("%d %d %d\n",a[p].idx,b[q].idx,b[q].v);
            q++;a[p].v-=b[q].v;
        }
        else{
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            p++;b[q].v-=a[p].v;
        }
    }
    getchar();getchar();
}


#include<stdio.h>
#include<algorithm>

#define N 100005
using namespace std;
int m,n,sa=0,sb=0;
struct ele{
    int idx,v;
}a[N],b[N];
bool cmp(ele c,ele d){return c.v>d.v;}
int main(){
    int i,j,k,x,y,z;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&x,&y);
        if(x) {a[++sa].v=y;a[sa].idx=i;}
        else {b[++sb].v=y;b[sb].idx=i;}
    }
    sort(a+1,a+1+sa,cmp);
    sort(b+1,b+1+sb,cmp);
    int p=1,q=1;
    while(--n){
        if(q>sb){
            q--;
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            p++;b[q].v-=a[p].v;
            q++;
        }
        else if(a[p].v>=b[q].v&&q<=sb){
            printf("%d %d %d\n",a[p].idx,b[q].idx,b[q].v);
            q++;a[p].v-=b[q].v;
        }
        else{
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            p++;b[q].v-=a[p].v;
        }
    }
    getchar();getchar();
}


#include<stdio.h>
#include<algorithm>

#define N 100005
using namespace std;
int m,n,sa=0,sb=0;
struct ele{
    int idx,v;
}a[N],b[N];
bool cmp(ele c,ele d){return c.v>d.v;}
int main(){
    int i,j,k,x,y,z;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&x,&y);
        if(x) {a[++sa].v=y;a[sa].idx=i;}
        else {b[++sb].v=y;b[sb].idx=i;}
    }
    sort(a+1,a+1+sa,cmp);
    sort(b+1,b+1+sb,cmp);
    int p=1,q=1;
    while(--n){
        if(a[p].v>=b[q].v){
            printf("%d %d %d\n",a[p].idx,b[q].idx,b[q].v);
            q++;a[p].v-=b[q].v;
        }
        else{
            printf("%d %d %d\n",a[p].idx,b[q].idx,a[p].v);
            p++;b[q].v-=a[p].v;
        }
    }
    getchar();getchar();
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define DSC(i,r,l) for(int i=(r);i>=(l);--i)
#define REP(i,r) for(int i=0;i<(r);++i)

struct Node
{
    int c;
    int id;
    Node(int c,int id):c(c),id(id){}
    bool operator<(const Node a)const
    {
        return c>a.c;
    }
};

struct Edge
{
    int x,y,c;
    Edge(int x,int y,int c):x(x),y(y),c(c){}
};

int main()
{
    //freopen("in.txt","r",stdin);
    int n,x,y,c;
    while(cin>>n)
    {
        vector<Node>f1,f2;
        vector<Edge>ans;
        FOR(i,1,n)
        {
            scanf("%d%d",&x,&y);
            if(x) f1.push_back(Node(y,i));
            else  f2.push_back(Node(y,i));
        }
        sort(f1.begin(),f1.end());
        sort(f2.begin(),f2.end());

        //REP(i,(int)f1.size()) cout<<f1[i].id<<' '<<f1[i].c<<endl;

        bool flag=0;

        while(!f1.empty() && !f2.empty())
        {
            //cout<<f1.back().id<<' '<<f1.back().c<<endl;

            if(!flag)
            {
                if(f1.size()==1) f1.back().c++,flag=1;
                else if(f2.size()==1) f2.back().c++,flag=1;
            }

            if(f1.back().c<f2.back().c)
            {
                x=f1.back().id;
                y=f2.back().id;
                c=f1.back().c;

                f2.back().c-=c;
                f1.pop_back();

                ans.push_back(Edge(x,y,c));
            }
            else
            {
                x=f1.back().id;
                y=f2.back().id;
                c=f2.back().c;

                f1.back().c-=c;
                f2.pop_back();
//cout<<x<<' '<<y<<' '<<c<<endl;
                ans.push_back(Edge(x,y,c));
            }
        }
        REP(i,(int)ans.size())
        {
            printf("%d %d %d\n",ans[i].x,ans[i].y,ans[i].c);
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 200005;

int n;
vector<pair<int, int> > t[2];
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w){
        u = _u;
        v = _v;
        w = _w;
    }
    void output(){
        printf("%d %d %d\n", u, v, w);
    }
};
vector<Edge> ans;

void prep(){
}

void init(){
    if (scanf("%d", &n) == EOF) exit(0);
    t[0].clear();
    t[1].clear();
    for (int i = 0;i < n;i++){
        int x, y;
        scanf("%d%d", &x, &y);
        t[x].push_back(make_pair(i + 1, y));
    }
}

void work(){
    ans.clear();
    int i = 0, j = 0;
    while (--n){
        int w = min(t[0][i].second, t[1][j].second);
        ans.push_back(Edge(t[0][i].first, t[1][j].first, w));
        if (t[0][i].second < t[1][j].second) i += 1;
        else j += 1;
    }
    for (int i = 0;i < (int)ans.size();i++){
        ans[i].output();
    }
//    puts("");
}

int main(){
#ifdef ILOAHZ
    freopen("f.in", "r", stdin);
#endif
    prep();
    while (1){
        init();
        work();
    }
    return 0;
}

/*alrplkealaehhjpgcrradgojnahhmqfmiotlfddiphhcqcblms*/


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> e[2];
int u[100010];
int pd(int x){
    if (u[x] == x) return x;
    return u[x] = pd(u[x]);
}
int main(){
    int n, i, x, y;
    scanf("%d", &n);
    for (i = 1; i <= n; i++){
        scanf("%d%d", &x, &y);
        e[x].push_back(i);
        e[x].push_back(y);
        u[i] = i;
    }
    x = y = 0;
    while (e[0][x + 1] == 0 && x < (int)e[0].size()) x += 2;
    while (e[1][y + 1] == 0 && y < (int)e[1].size()) y += 2;
    while (x < (int)e[0].size()){
        int z = min(e[0][x + 1], e[1][y + 1]);
        printf("%d %d %d\n", e[0][x], e[1][y], z);
        u[pd(e[0][x])] = pd(e[1][y]);
        e[0][x + 1] -= z;
        e[1][y + 1] -= z;
        while (e[0][x + 1] == 0 && x < (int)e[0].size()) x += 2;
        while (e[1][y + 1] == 0 && y < (int)e[1].size()) y += 2;
    }
    for (i = 1; i <= n; i++){
        if (pd(i) != pd(1)){
            printf("%d 1 0\n", i);
            u[pd(i)] = pd(1);
        }
    }
    return 0;
}

/*sllbhtalhhdgseerrrtdegdkqjrragosjraqqatptcbksfhpog*/


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>

using namespace std;

struct Node {
    int i,v;
    bool operator < (const Node & t) const {
        return v>t.v;
    }
}tn;

vector <Node> node[2];
vector <int> res;
int n,p,x;
int pp[110000];
int rev[2];

int fa[110000];
int find(int x) {
    if (fa[x]==x) fa[x]=0;
    if (fa[x]==0) return x;
    return fa[x]=find(fa[x]);
}

int main() {
    scanf("%d",&n);
    for (int i=0;i<n;i++) {
        scanf("%d%d",&p,&x);
        tn.i=i+1;tn.v=x;
        pp[i+1]=p;
        node[p].push_back(tn);
    }
    //sort(node[0].begin(),node[0].end());
    //sort(node[1].begin(),node[1].end());
    int s=0;
    for (int i=0;i<node[1].size();i++) {
        while (s<node[0].size()&&node[1][i].v>=node[0][s].v) {
            fa[find(node[0][s].i)]=find(node[1][i].i);
            //tot++;
            printf("%d %d %d\n",node[0][s].i,node[1][i].i,node[0][s].v);
            node[1][i].v-=node[0][s].v;
            node[0][s].v=0;
            s++;
        }
        if (s<node[0].size()&&node[1][i].v>0) {
            fa[find(node[0][s].i)]=find(node[1][i].i);
            //tot++;
            printf("%d %d %d\n",node[0][s].i,node[1][i].i,node[1][i].v);
            node[0][s].v-=node[1][i].v;
            node[1][i].v=0;
        }
    }
    rev[pp[1]]=1;
    res.push_back(find(1));
    for (int i=2;i<=n;i++) {
        if (rev[pp[i]]==0&&pp[1]!=pp[i]) rev[pp[i]]=i;
        res.push_back(find(i));
    }
    //printf("%d %d\n",rev[0],rev[1]);
    //if (rev[0]==0||rev[1]==0) return 0;
    sort(res.begin(),res.end());
    res.erase(unique(res.begin(),res.end()),res.end());
    int i;
    for (i=0;i<res.size();i++) {
        if (find(rev[0])==find(res[i])) continue;
        if (pp[res[i]]==0) printf("%d %d %d\n",res[i],rev[1],0);
        else printf("%d %d %d\n",res[i],rev[0],0);
    }
    return 0;
}

/*sbnrrghqomjnrimhpmjkaslbonthppanqfcnmbbsnledtqkgbl*/


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>

using namespace std;

struct Node {
    int i,v;
    bool operator < (const Node & t) const {
        return v>t.v;
    }
}tn;

vector <Node> node[2];
vector <int> res;
int n,p,x;
int pp[110000];
int rev[2];

int fa[110000];
int find(int x) {
    if (fa[x]==x) fa[x]=0;
    if (fa[x]==0) return x;
    return fa[x]=find(fa[x]);
}

int main() {
    scanf("%d",&n);
    for (int i=0;i<n;i++) {
        scanf("%d%d",&p,&x);
        tn.i=i+1;tn.v=x;
        pp[i+1]=p;
        node[p].push_back(tn);
    }
    //sort(node[0].begin(),node[0].end());
    //sort(node[1].begin(),node[1].end());
    int s=0;
    for (int i=0;i<node[1].size();i++) {
        while (s<node[0].size()&&node[1][i].v>=node[0][s].v) {
            fa[find(node[0][s].i)]=find(node[1][i].i);
            //tot++;
            printf("%d %d %d\n",node[0][s].i,node[1][i].i,node[0][s].v);
            node[1][i].v-=node[0][s].v;
            node[0][s].v=0;
            s++;
        }
        if (s<node[0].size()&&node[1][i].v>0) {
            fa[find(node[0][s].i)]=find(node[1][i].i);
            //tot++;
            printf("%d %d %d\n",node[0][s].i,node[1][i].i,node[1][i].v);
            node[0][s].v-=node[1][i].v;
            node[1][i].v=0;
        }
    }
    for (int i=1;i<=n;i++) {
        rev[pp[i]]=i;
        res.push_back(find(i));
    }
    //printf("%d %d\n",rev[0],rev[1]);
    //if (rev[0]==0||rev[1]==0) return 0;
    sort(res.begin(),res.end());
    res.erase(unique(res.begin(),res.end()),res.end());
    int i;
    for (i=0;i<res.size();i++) {
        if (find(rev[1-pp[res[i]]])==find(res[i])) continue;
        if (pp[res[i]]==0) printf("%d %d %d\n",res[i],rev[1],0);
        else printf("%d %d %d\n",res[i],rev[0],0);
    }
    return 0;
}

/*golkbnrmrrqiqloehqcebbpdbkfqehcdofnhtktiahqqkcnskp*/


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 200005;

int n;
vector<pair<int, int> > t[2];
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w){
        u = _u;
        v = _v;
        w = _w;
    }
    void output(){
        printf("%d %d %d\n", u, v, w);
    }
};
vector<Edge> ans;

void prep(){
}

void init(){
    if (scanf("%d", &n) == EOF) exit(0);
    t[0].clear();
    t[1].clear();
    for (int i = 0;i < n;i++){
        int x, y;
        scanf("%d%d", &x, &y);
        t[x].push_back(make_pair(i + 1, y));
    }
}

void work(){
    ans.clear();
    int i = 0, j = 0;
    while (--n){
        int w = min(t[0][i].second, t[1][j].second);
        ans.push_back(Edge(t[0][i].first, t[1][j].first, w));
        t[0][i].second -= w;
        t[1][j].second -= w;
        if (t[0][i].second <= t[1][j].second && i + 1 < t[0].size()) i += 1;
        else j += 1;
    }
    for (int i = 0;i < (int)ans.size();i++){
        ans[i].output();
    }
    puts("");
}

int main(){
#ifdef ILOAHZ
    freopen("f.in", "r", stdin);
#endif
    prep();
    while (1){
        init();
        work();
    }
    return 0;
}

/*ddrbarfeffmbrnfknfbcbqgjaqpigiejlblmsiqqnajdgontlo*/


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 200005;

int n;
vector<pair<int, int> > t[2];
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w){
        u = _u;
        v = _v;
        w = _w;
    }
    void output(){
        printf("%d %d %d\n", u, v, w);
    }
};
vector<Edge> ans;

void prep(){
}

void init(){
    if (scanf("%d", &n) == EOF) exit(0);
    t[0].clear();
    t[1].clear();
    for (int i = 0;i < n;i++){
        int x, y;
        scanf("%d%d", &x, &y);
        t[x].push_back(make_pair(i + 1, y));
    }
}

void work(){
    ans.clear();
    int i = 0, j = 0;
    while (i < (int)t[0].size() && j < (int)t[1].size()){
        int w = min(t[0][i].second, t[1][j].second);
        ans.push_back(Edge(t[0][i].first, t[1][j].first, w));
        t[0][i].second -= w;
        if (t[0][i].second == 0) i += 1;
        t[1][j].second -= w;
        if (t[1][j].second == 0) j += 1;
    }
    while (i < (int)t[0].size()){
        ans.push_back(Edge(t[0][i].first, t[1][0].first, 0));
        i += 1;
    }
    while (j < (int)t[1].size()){
        ans.push_back(Edge(t[0][0].first, t[1][j].first, 0));
        j += 1;
    }
    for (int i = 0;i < (int)ans.size();i++){
        ans[i].output();
    }
//    puts("");
}

int main(){
#ifdef ILOAHZ
    freopen("f.in", "r", stdin);
#endif
    prep();
    while (1){
        init();
        work();
    }
    return 0;
}

/*rpsaqictnqjgreofomqjkegqibhokaahpltdtncejektaeqorm*/


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 200005;

int n;
vector<pair<int, int> > t[2];
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w){
        u = _u;
        v = _v;
        w = _w;
    }
    void output(){
        printf("%d %d %d\n", u, v, w);
    }
};
vector<Edge> ans;

void prep(){
}

void init(){
    if (scanf("%d", &n) == EOF) exit(0);
    t[0].clear();
    t[1].clear();
    for (int i = 0;i < n;i++){
        int x, y;
        scanf("%d%d", &x, &y);
        t[x].push_back(make_pair(i + 1, y));
    }
}

void work(){
    ans.clear();
    int i = 0, j = 0;
    while (--n){
        int w = min(t[0][i].second, t[1][j].second);
        ans.push_back(Edge(t[0][i].first, t[1][j].first, w));
        t[0][i].second -= w;
        if (t[0][i].second == 0 && i + 1 < t[0].size()) i += 1;
        t[1][j].second -= w;
        if (t[1][j].second == 0 && j + 1 < t[1].size()) j += 1;
    }
    for (int i = 0;i < (int)ans.size();i++){
        ans[i].output();
    }
//    puts("");
}

int main(){
#ifdef ILOAHZ
    freopen("f.in", "r", stdin);
#endif
    prep();
    while (1){
        init();
        work();
    }
    return 0;
}

/*gordkgrpnmpmshqfatncgkcthdfphshomqjpcskhkrtiehfqhl*/


//Solution by Zhusupov Nurlan
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <ctime>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;

#define pb(x) push_back(x)
#define sqr(x) ((x) * (x))
#define F first
#define S second
#define SZ(t) ((int) t.size())
#define len(t) ((int) t.length())
#define base 10
#define fname ""
#define sz 1000
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define write(xx) printf("%d" , xx);
#define readln(xx) getline(cin , xx)
#define read(xx) scanf("%d" , &xx)
#define for(xx1 , yy1 , zz1) for(int zz1 = xx1 ; zz1 <= yy1 ; zz1++)
#define mp make_pair
const double PI  = acos(-1.0);

int n, c[sz], s[sz];
priority_queue <pair<int, int> > q[2];
pair <int, int> v1, v2;

int main(){
	//freopen(fname"in", "r", stdin);

	cin >> n;
	for (1, n, i)
	{
		scanf("%d%d", &c[i], &s[i]);
    	q[c[i]].push(mp(-s[i], i));
    }
    int cnt = 0;

    while (!q[0].empty() && !q[1].empty())
    {
    	v1 = q[0].top();
    	q[0].pop();
    	v2 = q[1].top();
    	q[1].pop();

    	v1.F *= -1;v2.F *= -1;
    	printf("%d %d %d\n", v1.S, v2.S, min(v1.F, v2.F));
    	if (v1.F < v2.F || (v1.F == v2.F && (q[0].size() > q[1].size())))
    	{
    		v2.F -= v1.F;
    		v2.F *= -1;
    		q[1].push(v2);
    	}
    	else
    	{
    		v1.F -= v2.F;
    		v1.F *= -1;
    		q[0].push(v1);
    	}
    	cnt++;
    }
    assert(cnt == n - 1);
   	return 0;
}


//Solution by Zhusupov Nurlan
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <ctime>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;

#define pb(x) push_back(x)
#define sqr(x) ((x) * (x))
#define F first
#define S second
#define SZ(t) ((int) t.size())
#define len(t) ((int) t.length())
#define base 10
#define fname ""
#define sz 1000
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define write(xx) printf("%d" , xx);
#define readln(xx) getline(cin , xx)
#define read(xx) scanf("%d" , &xx)
#define for(xx1 , yy1 , zz1) for(int zz1 = xx1 ; zz1 <= yy1 ; zz1++)
#define mp make_pair
const double PI  = acos(-1.0);

int n, c[sz], s[sz];
priority_queue <pair<int, int> > q[2];
pair <int, int> v1, v2;

int main(){
	//freopen(fname"in", "r", stdin);

	cin >> n;
	for (1, n, i)
	{
		scanf("%d%d", &c[i], &s[i]);
    	q[c[i]].push(mp(-s[i], i));
    }

    while (!q[0].empty() && !q[1].empty())
    {
    	v1 = q[0].top();
    	q[0].pop();
    	v2 = q[1].top();
    	q[1].pop();

    	v1.F *= -1;v2.F *= -1;
    	printf("%d %d %d\n", v1.S, v2.S, min(v1.F, v2.F));
    	if (v1.F < v2.F)
    	{
    		v2.F -= v1.F;
    		v2.F *= -1;
    		q[1].push(v2);
    	}
    	else
    	{
    		v1.F -= v2.F;
    		v1.F *= -1;
    		q[0].push(v1);
    	}
    }
   	return 0;
}


//Solution by Zhusupov Nurlan
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <ctime>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;

#define pb(x) push_back(x)
#define sqr(x) ((x) * (x))
#define F first
#define S second
#define SZ(t) ((int) t.size())
#define len(t) ((int) t.length())
#define base 10
#define fname ""
#define sz 1000
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define write(xx) printf("%d" , xx);
#define readln(xx) getline(cin , xx)
#define read(xx) scanf("%d" , &xx)
#define for(xx1 , yy1 , zz1) for(int zz1 = xx1 ; zz1 <= yy1 ; zz1++)
#define mp make_pair
const double PI  = acos(-1.0);

int n, c[sz], s[sz];
priority_queue <pair<int, int> > q[2];
pair <int, int> v1, v2;

int main(){
	//freopen(fname"in", "r", stdin);

	cin >> n;
	for (1, n, i)
	{
		scanf("%d%d", &c[i], &s[i]);
    	q[c[i]].push(mp(-s[i], i));
    }

    while (!q[0].empty() && !q[1].empty())
    {
    	v1 = q[0].top();
    	q[0].pop();
    	v2 = q[1].top();
    	q[1].pop();

    	v1.F *= -1;v2.F *= -1;
    	printf("%d %d %d\n", v1.S, v2.S, min(v1.F, v2.F));
    	if (v1.F < v2.F || (v1.F == v2.F && (q[0].size() > q[1].size())))
    	{
    		v2.F -= v1.F;
    		v2.F *= -1;
    		q[1].push(v2);
    	}
    	else
    	{
    		v1.F -= v2.F;
    		v1.F *= -1;
    		q[0].push(v1);
    	}
    }
   	return 0;
}


//Solution by Zhusupov Nurlan
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <ctime>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;

#define pb(x) push_back(x)
#define sqr(x) ((x) * (x))
#define F first
#define S second
#define SZ(t) ((int) t.size())
#define len(t) ((int) t.length())
#define base 10
#define fname ""
#define sz 1000000
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define write(xx) printf("%d" , xx);
#define readln(xx) getline(cin , xx)
#define read(xx) scanf("%d" , &xx)
#define for(xx1 , yy1 , zz1) for(int zz1 = xx1 ; zz1 <= yy1 ; zz1++)
#define mp make_pair
const double PI  = acos(-1.0);

int n, c[sz], s[sz];
priority_queue <pair<int, int> > q[2];
pair <int, int> v1, v2;

int main(){
	//freopen(fname"in", "r", stdin);

	cin >> n;
	for (1, n, i)
	{
		scanf("%d%d", &c[i], &s[i]);
    	q[c[i]].push(mp(-s[i], i));
    }
    int cnt = 0;

    while (!q[0].empty() && !q[1].empty())
    {
    	v1 = q[0].top();
    	q[0].pop();
    	v2 = q[1].top();
    	q[1].pop();

    	v1.F *= -1;v2.F *= -1;
    	printf("%d %d %d\n", v1.S, v2.S, min(v1.F, v2.F));
    	if (v1.F < v2.F || (v1.F == v2.F && (q[0].size() > q[1].size())))
    	{
    		v2.F -= v1.F;
    		v2.F *= -1;
    		q[1].push(v2);
    	}
    	else
    	{
    		v1.F -= v2.F;
    		v1.F *= -1;
    		q[0].push(v1);
    	}
    	cnt++;
    }
    assert(cnt == n - 1);
   	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > node[2];
  for (int i = 1; i <= n; i++) {
    int c, s;
    cin>>c>>s;
    node[c].push_back(make_pair(s,i));
  }
  sort(node[0].begin(), node[0].end());
  int j = 0;
  for (int i = 0; i < node[0].size(); i++) {
    int a = node[0][i].second, b = node[1][j].second;
    int w = node[0][i].first;
    if (!w) {
      cout<<a<<" "<<b<<" "<<w<<endl;
    } else {
      while (w) {
	int x = min(node[1][j].first, w);
	node[1][j].first -= x;
	w -= x;
	cout<<a<<" "<<node[1][j].second<<" "<<x<<endl;
	if (!node[1][j].first) j++;
      }
    }
  }
  while (j+1 < node[1].size()) {
    cout<<1<<" "<<node[1][j].second<<" "<<"0"<<endl;
    j++;
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > node[2];
  for (int i = 1; i <= n; i++) {
    int c, s;
    cin>>c>>s;
    node[c].push_back(make_pair(s,i));
  }
  sort(node[0].begin(), node[0].end());
  for (int i = 0, j = 0; i < node[0].size(); i++) {
    int a = node[0][i].second, b = node[1][j].second;
    int w = node[0][i].first;
    if (!w) {
      cout<<a<<" "<<b<<" "<<w<<endl;
    } else {
      while (w) {
	int x = min(node[1][j].first, w);
	node[1][j].first -= x;
	w -= x;
	cout<<a<<" "<<node[1][j].second<<" "<<x<<endl;
	if (!node[1][j].first) j++;
      }
    }
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX_N = 1e5+7;
#define X first
#define Y second
int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > b, w;
  for (int i = 1; i <= n; i++) {
    int c, s;
    cin>>c>>s;
    (c?w:b).push_back(make_pair(s,i));
  }
  for (int i = 0, j = 0; i < w.size(); i++) {
    if (j == b.size()) cout<<w[i].Y<<" "<<b[j-1].Y<<" 0"<<endl;
    while (j < b.size()) {
      if (w[i].X >= b[j].X) {
	cout<<w[i].Y<<" "<<b[j].Y<<" "<<b[j].X<<endl;
	w[i].X -= b[j].X;
	j++;
      } else {
	cout<<w[i].Y<<" "<<b[j].Y<<" "<<w[i].X<<endl;
	b[j].X -= w[i].X;
	break;
      }
    }
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > node[2];
  for (int i = 1; i <= n; i++) {
    int c, s;
    cin>>c>>s;
    node[c].push_back(make_pair(s,i));
  }
  sort(node[0].begin(), node[0].end());
  for (int i = 0, j = 0; i < node[0].size(); i++) {
    int a = node[0][i].second, b = node[1][j].second;
    int w = node[0][i].first;
    if (!w) cout<<a<<" "<<b<<" "<<w<<endl;
    else
      while (w) {
	int x = min(node[1][j].first, w);
	node[1][j].first -= x;
	w -= x;
	cout<<a<<" "<<node[1][j].second<<" "<<x<<endl;
	if (!node[1][j].first) j++;
      }
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > node[2];
  for (int i = 1; i <= n; i++) {
    int c, s;
    cin>>c>>s;
    node[c].push_back(make_pair(s,i));
  }
  sort(node[0].begin(), node[0].end());
  int j = 0;
  for (int i = 0; i < node[0].size(); i++) {
    int a = node[0][i].second, b = node[1][j].second;
    int w = node[0][i].first;
    if (!w) {
      cout<<a<<" "<<b<<" "<<w<<endl;
    } else {
      while (w) {
	int x = min(node[1][j].first, w);
	node[1][j].first -= x;
	w -= x;
	cout<<a<<" "<<node[1][j].second<<" "<<x<<endl;
	if (!node[1][j].first) j++;
      }
    }
  }
  while (j < node[1].size()) {
    cout<<1<<" "<<node[1][j].second<<" "<<"0"<<endl;
    j++;
  }
  return 0;
}


// in the name of God
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
using namespace std;
typedef pair< int , int > pii;
typedef pair< pii , int > ppi;
const int maxn = 1000 * 2 * 100 + 10;
int col[maxn] , val[maxn];
vector< ppi > edge;
set< pii >sb , sw;

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n ;
    for(int i=1;i<=n;++i)   {
        cin >> col[i] >> val[i];
        if(col[i])
            sb.insert(pii(val[i],i));
        else
            sw.insert(pii(val[i],i));
    }

    while( sb.size() && sw.size() ) {
        pii tb = *sb.begin() , tw = *sw.begin();
        int vb = tb.F , vw = tw.F;
        if( vb <= vw )   {
            edge.pb( ppi( pii(tb.S , tw.S) , vb )  );
            sb.erase(tb);
            sw.erase(tw);
            tw.F -= vb;
            sw.insert(tw);
        }
        else    {
            edge.pb( ppi( pii(tb.S , tw.S) , vw )  );
            sw.erase(tw);
            sb.erase(tb);
            tb.F -= vw;
            sb.insert(tb);
        }
    }

    for(int i=0;i<edge.size();++i)
        cout << edge[i].F.F << ' ' << edge[i].F.S << ' ' << edge[i].S << endl;

    return 0;
}


// in the name of God
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
using namespace std;
typedef pair< int , int > pii;
typedef pair< pii , int > ppi;
const int maxn = 1000 * 2 * 100 + 10;
int col[maxn] , val[maxn];
vector< ppi > edge;
set< pii >sb , sw;
vector< int > bl , wh;

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n ;
    int blcur , whcur;
    for(int i=1;i<=n;++i)   {
        cin >> col[i] >> val[i];
        if( val[i] )    {
            if(col[i])  {
                sb.insert(pii(val[i],i));
                blcur = i;
            }
            else    {
                sw.insert(pii(val[i],i));
                whcur = i;
            }
        }
        else    {
            if( col[i] )
                bl.pb(i);
            else
                wh.pb(i);
        }
    }

    for(int i=0;i<bl.size();++i)
        cout << bl[i] << ' ' << whcur << ' ' << 0 << endl;
    for(int i=0;i<wh.size();++i)
        cout << wh[i] << ' ' << blcur << ' ' << 0 << endl;

    while( sb.size() && sw.size() ) {
        pii tb = *sb.begin() , tw = *sw.begin();
        int vb = tb.F , vw = tw.F;
        if( vb < vw )   {
            edge.pb( ppi( pii(tb.S , tw.S) , vb )  );
            sb.erase(tb);
            sw.erase(tw);
            tw.F -= vb;
            sw.insert(tw);
        }
        else    {
            edge.pb( ppi( pii(tb.S , tw.S) , vw )  );
            sw.erase(tw);
            sb.erase(tb);
            tb.F -= vw;
            sb.insert(tb);
        }
    }

    for(int i=0;i<edge.size();++i)
        cout << edge[i].F.F << ' ' << edge[i].F.S << ' ' << edge[i].S << endl;
    return 0;
}


// in the name of God
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
using namespace std;
typedef pair< int , int > pii;
typedef pair< pii , int > ppi;
const int maxn = 1000 * 2 * 100 + 10;
int col[maxn] , val[maxn];
vector< ppi > edge;
set< pii >sb , sw;

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n ;
    for(int i=1;i<=n;++i)   {
        cin >> col[i] >> val[i];
        if(col[i])
            sb.insert(pii(val[i],i));
        else
            sw.insert(pii(val[i],i));
    }

    while( sb.size() && sw.size() ) {
        pii tb = *sb.begin() , tw = *sw.begin();
        int vb = tb.F , vw = tw.F;
        if( vb < vw )   {
            edge.pb( ppi( pii(tb.S , tw.S) , vb )  );
            sb.erase(tb);
            sw.erase(tw);
            tw.F -= vb;
            sw.insert(tw);
        }
        else    {
            edge.pb( ppi( pii(tb.S , tw.S) , vw )  );
            sw.erase(tw);
            sb.erase(tb);
            tb.F -= vw;
            sb.insert(tb);
        }
    }

    for(int i=0;i<edge.size();++i)
        cout << edge[i].F.F << ' ' << edge[i].F.S << ' ' << edge[i].S << endl;

    return 0;
}


// in the name of God
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
using namespace std;
typedef pair< int , int > pii;
typedef pair< pii , int > ppi;
const int maxn = 1000 * 2 * 100 + 10;
int col[maxn] , val[maxn];
vector< ppi > edge;
set< pii >sb , sw;
vector< int > bl , wh;

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n ;
    int blcur = -1 , whcur = -1;
    for(int i=1;i<=n;++i)   {
        cin >> col[i] >> val[i];
        if( val[i] )    {
            if(col[i])  {
                sb.insert(pii(val[i],i));
                blcur = i;
            }
            else    {
                sw.insert(pii(val[i],i));
                whcur = i;
            }
        }
        else    {
            if( col[i] )
                bl.pb(i);
            else
                wh.pb(i);
        }
    }
    if( blcur == -1 )   {
        for(int i=1;i<=n;++i)
            if(col[i])
                blcur = i;
        for(int i=1;i<=n;++i)
                if(col[i]==0)
                    whcur = i;
        for(int i=1;i<blcur;++i)
            if(col[i])
                cout << i << ' ' << whcur << ' ' << 0 << endl;
        for(int i=1;i<=n;++i)
            if(col[i]==0)
                cout << i << ' ' << blcur << ' ' << 0 << endl;
        return 0;
    }

    for(int i=0;i<bl.size();++i)
        cout << bl[i] << ' ' << whcur << ' ' << 0 << endl;
    for(int i=0;i<wh.size();++i)
        cout << wh[i] << ' ' << blcur << ' ' << 0 << endl;

    while( sb.size() && sw.size() ) {
        pii tb = *sb.begin() , tw = *sw.begin();
        int vb = tb.F , vw = tw.F;
        if( vb < vw )   {
            edge.pb( ppi( pii(tb.S , tw.S) , vb )  );
            sb.erase(tb);
            sw.erase(tw);
            tw.F -= vb;
            sw.insert(tw);
        }
        else    {
            edge.pb( ppi( pii(tb.S , tw.S) , vw )  );
            sw.erase(tw);
            sb.erase(tb);
            tb.F -= vw;
            sb.insert(tb);
        }
    }

    for(int i=0;i<edge.size();++i)
        cout << edge[i].F.F << ' ' << edge[i].F.S << ' ' << edge[i].S << endl;
    return 0;
}


//
//  main.cpp
//  Black And White Tree
//
//  Created by fmc5 on 09/10/14.
//  Copyright (c) 2014 fmc. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <set>

using namespace std;

typedef struct entry {
    int val;
    int i;

    bool operator < (const entry &e) const {
        return val <= e.val;
    }

} entry;

set< entry > black;
set< entry > white;

int main(int argc, const char * argv[]) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int a,b;
        entry e;
        scanf("%d %d", &a, &b);

        e.val = b;
        e.i = i;

        if (a) {
            black.insert(e);
        } else white.insert(e);
    }

    int tot = 0;
    while (tot < n-1) {
        entry p,q;
        if (*black.begin() < *white.begin()) {
            p = *black.begin();
            black.erase(black.begin());
            q = *white.lower_bound(p);
            white.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            if (temp.val) {
                white.insert(temp);
            }

        } else {
            p = *white.begin();
            white.erase(white.begin());
            q = *black.lower_bound(p);
            black.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            if (temp.val) {
                black.insert(temp);
            }
        }

        printf("%d %d %d\n", p.i, q.i, p.val);
        tot++;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>

using namespace std;

typedef struct entry {
    int val;
    int i;

    bool operator < (const entry &e) const {
        if(val == e.val) {
            return i < e.i;
        }
        return val < e.val;
    }

} entry;

multiset< entry > black;
multiset< entry > white;

int main(int argc, const char * argv[]) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int a,b;
        entry e;
        scanf("%d %d", &a, &b);

        e.val = b;
        e.i = i;

        if (a) {
            black.insert(e);
        } else white.insert(e);
    }

    int tot = 0;
    while (tot < n-1) {
        entry p,q;
        if (black.begin()->val < white.begin()->val || (black.begin()->val == white.begin()->val && black.size() >= white.size())) {
            p = *black.begin();
            black.erase(black.begin());

            set< entry >::iterator it = white.upper_bound(p);
            if (it != white.begin()) {
                it--;
                if (p.val > it->val) {
                    it++;
                }
            }
            q = *it;
            white.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            white.insert(temp);

        } else {
            p = *white.begin();
            white.erase(white.begin());

            set< entry >::iterator it = black.upper_bound(p);
            if (it != black.begin()) {
                it--;
                if (p.val > it->val) {
                    it++;
                }
            }
            q = *it;
            black.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            black.insert(temp);
        }

        printf("%d %d %d\n", p.i, q.i, p.val);
        tot++;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>

using namespace std;

typedef struct entry {
    int val;
    int i;

    bool operator < (const entry &e) const {
        if(val == e.val) {
            return i < e.i;
        }
        return val < e.val;
    }

} entry;

multiset< entry > black;
multiset< entry > white;

int main(int argc, const char * argv[]) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int a,b;
        entry e;
        scanf("%d %d", &a, &b);

        e.val = b;
        e.i = i;

        if (a) {
            black.insert(e);
        } else white.insert(e);
    }

    int tot = 0;
    while (tot < n-1) {
        entry p,q;
        if (*black.begin() < *white.begin()) {
            p = *black.begin();
            black.erase(black.begin());

            set< entry >::iterator it = white.upper_bound(p);
            if (it != white.begin()) {
                it--;
                if (p.val > it->val) {
                    it++;
                }
            }
            q = *it;
            white.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            white.insert(temp);

        } else {
            p = *white.begin();
            white.erase(white.begin());

            set< entry >::iterator it = black.upper_bound(p);
            if (it != black.begin()) {
                it--;
                if (p.val > it->val) {
                    it++;
                }
            }
            q = *it;
            black.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            black.insert(temp);
        }

        printf("%d %d %d\n", p.i, q.i, p.val);
        tot++;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>

using namespace std;

typedef struct entry {
    int val;
    int i;

    bool operator < (const entry &e) const {
        return val < e.val;
    }

} entry;

multiset< entry > black;
multiset< entry > white;

int main(int argc, const char * argv[]) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int a,b;
        entry e;
        scanf("%d %d", &a, &b);

        e.val = b;
        e.i = i;

        if (a) {
            black.insert(e);
        } else white.insert(e);
    }

    int tot = 0;
    while (tot < n-1) {
        entry p,q;
        if (*black.begin() < *white.begin()) {
            p = *black.begin();
            black.erase(black.begin());

            set< entry >::iterator it = white.upper_bound(p);
            if (it != white.begin()) {
                it--;
                if (p.val > it->val) {
                    it++;
                }
            }
            q = *it;
            white.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            white.insert(temp);

        } else {
            p = *white.begin();
            white.erase(white.begin());

            set< entry >::iterator it = black.upper_bound(p);
            if (it != black.begin()) {
                it--;
                if (p.val > it->val) {
                    it++;
                }
            }
            q = *it;
            black.erase(q);

            entry temp;
            temp.val = q.val - p.val;
            temp.i = q.i;
            black.insert(temp);
        }

        printf("%d %d %d\n", p.i, q.i, p.val);
        tot++;
    }

    return 0;
}


#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>

using namespace std;

struct nodo {
    int cual, suma;
    bool operator() ( const nodo& b, const nodo& a) const {
        if(a.suma<b.suma)
            return true;
        else
            return false;
    }
} m;

int ctos, color, tiene, blanco, negro, T[2], chico;
priority_queue <nodo, vector<nodo>, nodo> P[2];

int main()
{
    scanf("%d",&ctos);
    for(int i=1; i<=ctos; i++){
        scanf("%d%d",&color,&tiene);
        m.suma=tiene;
        m.cual=i;
        P[color].push(m);
        ++T[color];
    }
    blanco=P[0].top().cual;
    negro=P[1].top().cual;
    while(T[0]>0 and T[1]>0){
        chico=min(P[0].top().suma, P[1].top().suma);
        printf("%d %d %d\n",P[0].top().cual,P[1].top().cual,chico);
        for(int i=0; i<2; i++){
            if(P[i].top().suma==chico){
                --T[i];
                P[i].pop();
            }
            else {
                m.cual=P[i].top().cual;
                m.suma=P[i].top().suma-chico;
                P[i].pop();
                P[i].push(m);
            }
        }
    }
    for(int i=1; i<=T[0]; i++){
        printf("%d %d 0\n",P[0].top().cual,negro);
        P[0].pop();
    }
    for(int i=1; i<=T[1]; i++){
        printf("%d %d 0\n",P[1].top().cual,blanco);
        P[1].pop();
    }
    return 0;
}


#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>

using namespace std;

struct nodo {
    int cual, suma;
    bool operator() ( const nodo& b, const nodo& a) const {
        if(a.suma<b.suma)
            return true;
        else
            return false;
    }
} m;

int ctos, color, tiene, blanco, negro, T[2], chico, vale;
priority_queue <nodo, vector<nodo>, nodo> P[2];

int main()
{
    scanf("%d",&ctos);
    for(int i=1; i<=ctos; i++){
        scanf("%d%d",&color,&tiene);
        m.suma=tiene;
        m.cual=i;
        P[color].push(m);
        ++T[color];
    }
    blanco=P[0].top().cual;
    negro=P[1].top().cual;
    while(T[0]>0 and T[1]>0){
        if(P[0].top().suma<P[1].top().suma or (P[0].top().suma==P[1].top().suma and T[0]>=T[1])){
            chico=P[0].top().cual;
            vale=P[0].top().suma;
        }
        else {
            chico=P[1].top().cual;
            vale=P[1].top().suma;
        }
        printf("%d %d %d\n",P[0].top().cual,P[1].top().cual,vale);
        for(int i=0; i<2; i++){
            if(P[i].top().cual==chico){
                --T[i];
                P[i].pop();
            }
            else {
                m.cual=P[i].top().cual;
                m.suma=P[i].top().suma-vale;
                P[i].pop();
                P[i].push(m);
            }
        }
    }
    for(int i=1; i<T[0]; i++){
        printf("%d %d 0\n",P[0].top().cual,negro);
        P[0].pop();
    }
    for(int i=1; i<T[1]; i++){
        printf("%d %d 0\n",P[1].top().cual,blanco);
        P[1].pop();
    }
    return 0;
}


#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>

using namespace std;

struct nodo {
    int cual, suma;
    bool operator() ( const nodo& b, const nodo& a) const {
        if(a.suma<b.suma)
            return true;
        else
            return false;
    }
} m;

int ctos, color, tiene, blanco, negro, T[2], chico, quedan;
priority_queue <nodo, vector<nodo>, nodo> P[2];

int main()
{
    scanf("%d",&ctos);
    quedan=ctos-1;
    for(int i=1; i<=ctos; i++){
        scanf("%d%d",&color,&tiene);
        m.suma=tiene;
        m.cual=i;
        P[color].push(m);
        ++T[color];
    }
    blanco=P[0].top().cual;
    negro=P[1].top().cual;
    while(T[0]>0 and T[1]>0){
        chico=min(P[0].top().suma, P[1].top().suma);
        printf("%d %d %d\n",P[0].top().cual,P[1].top().cual,chico);
        --quedan;
        for(int i=0; i<2; i++){
            if(P[i].top().suma==chico and (i!=1 or P[0].top().suma!=chico)){
                --T[i];
                P[i].pop();
            }
            else {
                m.cual=P[i].top().cual;
                m.suma=P[i].top().suma-chico;
                P[i].pop();
                P[i].push(m);
            }
        }
    }
    for(int i=1; i<=T[0] and quedan>0; i++){
        printf("%d %d 0\n",P[0].top().cual,negro);
        --quedan;
        P[0].pop();
    }
    for(int i=1; i<=T[1] and quedan>0; i++){
        printf("%d %d 0\n",P[1].top().cual,blanco);
        --quedan;
        P[1].pop();
    }
    return 0;
}


#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>

using namespace std;

struct nodo {
    int cual, suma;
    bool operator() ( const nodo& b, const nodo& a) const {
        if(a.suma<b.suma)
            return true;
        else
            return false;
    }
} m;

int ctos, color, tiene, blanco, negro, T[2], chico, vale;
priority_queue <nodo, vector<nodo>, nodo> P[2];

int main()
{
    scanf("%d",&ctos);
    for(int i=1; i<=ctos; i++){
        scanf("%d%d",&color,&tiene);
        m.suma=tiene;
        m.cual=i;
        P[color].push(m);
        ++T[color];
    }
    blanco=P[0].top().cual;
    negro=P[1].top().cual;
    while(T[0]>0 and T[1]>0){
        if(P[0].top().suma<=P[1].top().suma){
            chico=P[0].top().cual;
            vale=P[0].top().suma;
        }
        else {
            chico=P[1].top().cual;
            vale=P[1].top().suma;
        }
        printf("%d %d %d\n",P[0].top().cual,P[1].top().cual,chico);
        for(int i=0; i<2; i++){
            if(P[i].top().cual==chico){
                --T[i];
                P[i].pop();
            }
            else {
                m.cual=P[i].top().cual;
                m.suma=P[i].top().suma-vale;
                P[i].pop();
                P[i].push(m);
            }
        }
    }
    for(int i=1; i<T[0]; i++){
        printf("%d %d 0\n",P[0].top().cual,negro);
        P[0].pop();
    }
    for(int i=1; i<T[1]; i++){
        printf("%d %d 0\n",P[1].top().cual,blanco);
        P[1].pop();
    }
    return 0;
}


#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>

using namespace std;

struct nodo {
    int cual, suma;
    bool operator() ( const nodo& b, const nodo& a) const {
        if(a.suma<b.suma)
            return true;
        else
            return false;
    }
} m;

int ctos, color, tiene, blanco, negro, T[2], chico, quedan;
priority_queue <nodo, vector<nodo>, nodo> P[2];

int main()
{
    scanf("%d",&ctos);
    quedan=ctos-1;
    for(int i=1; i<=ctos; i++){
        scanf("%d%d",&color,&tiene);
        m.suma=tiene;
        m.cual=i;
        P[color].push(m);
        ++T[color];
    }
    blanco=P[0].top().cual;
    negro=P[1].top().cual;
    while(T[0]>0 and T[1]>0){
        chico=min(P[0].top().suma, P[1].top().suma);
        printf("%d %d %d\n",P[0].top().cual,P[1].top().cual,chico);
        --quedan;
        for(int i=0; i<2; i++){
            if(P[i].top().suma==chico and (i!=1 or P[0].top().suma!=chico)){
                --T[i];
                P[i].pop();
            }
            else {
                m.cual=P[i].top().cual;
                m.suma=P[i].top().suma-chico;
                P[i].pop();
                P[i].push(m);
            }
        }
    }
    for(int i=1; i<=T[0]; i++){
        printf("%d %d 0\n",P[0].top().cual,negro);
        --quedan;
        P[0].pop();
    }
    for(int i=1; i<=T[1]; i++){
        printf("%d %d 0\n",P[1].top().cual,blanco);
        --quedan;
        P[1].pop();
    }
    return 0;
}


#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>

using namespace std;

struct nodo {
    int cual, suma;
    bool operator() ( const nodo& b, const nodo& a) const {
        if(a.suma<b.suma)
            return true;
        else
            return false;
    }
} m;

int ctos, color, tiene, blanco, negro, T[2], chico, vale;
priority_queue <nodo, vector<nodo>, nodo> P[2];

int main()
{
    scanf("%d",&ctos);
    for(int i=1; i<=ctos; i++){
        scanf("%d%d",&color,&tiene);
        m.suma=tiene;
        m.cual=i;
        P[color].push(m);
        ++T[color];
    }
    blanco=P[0].top().cual;
    negro=P[1].top().cual;
    while(T[0]>0 and T[1]>0){
        if(P[0].top().suma<=P[1].top().suma){
            chico=P[0].top().cual;
            vale=P[0].top().suma;
        }
        else {
            chico=P[1].top().cual;
            vale=P[1].top().suma;
        }
        printf("%d %d %d\n",P[0].top().cual,P[1].top().cual,vale);
        for(int i=0; i<2; i++){
            if(P[i].top().cual==chico){
                --T[i];
                P[i].pop();
            }
            else {
                m.cual=P[i].top().cual;
                m.suma=P[i].top().suma-vale;
                P[i].pop();
                P[i].push(m);
            }
        }
    }
    for(int i=1; i<T[0]; i++){
        printf("%d %d 0\n",P[0].top().cual,negro);
        P[0].pop();
    }
    for(int i=1; i<T[1]; i++){
        printf("%d %d 0\n",P[1].top().cual,blanco);
        P[1].pop();
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

typedef long long LL;

const int MAXN = 100005;
const LL MOD = 99990001;

struct NODE
{
	int col, id;
	int sum;
	NODE() {}
	NODE(int _col, int _id, int _sum):col(_col), id(_id), sum(_sum) {}
};

bool operator < (const NODE &a, const NODE &b)
{
	return a.sum > b.sum;
}

int pre[MAXN];
int val[MAXN];
int n, x, y;

priority_queue<NODE> q;
priority_queue<NODE> q0, q1;

void solved(int cas)
{
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf ("%d %d", &x, &y);
		q.push(NODE(x, i, y));
	}
	while (!q.empty())
	{
		NODE temp = q.top();
		q.pop();
		if (temp.col)
		{
			if (q0.empty())
				q1.push(temp);
			else
			{
				NODE t = q0.top();
				if (t.sum <= temp.sum)
				{
					q0.pop();
					pre[t.id] = temp.id;
					val[t.id] = t.sum;
					q.push(NODE(temp.col, temp.id, temp.sum - t.sum));
				}
				else
					q1.push(temp);
			}
		}
		else
		{
			if (q1.empty())
				q0.push(temp);
			else
			{
				NODE t = q1.top();
				if (t.sum <= temp.sum)
				{
					q1.pop();
					pre[t.id] = temp.id;
					val[t.id] = t.sum;
					q.push(NODE(temp.col, temp.id, temp.sum - t.sum));
				}
				else
					q0.push(temp);
			}
		}
	}
	int root;
	if (q0.empty())
		root = q1.top().id;
	else
		root = q0.top().id;
	for (int i = 1; i <= n; i++)
	{
		if (i != root)
			printf ("%d %d %d\n", i, pre[i], val[i]);
	}
}

int main() {
	//freopen("input.txt","r",stdin);
	int T = 1;
	//scanf("%d", &T);
	for (int i = 1; i <= T; i ++)
		solved(i);
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
const long long INF=9e17;
struct No{
	long long id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	for(int i=1;i<=n;i++)dis[i]=INF;
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;pr[1]=pl[1]=1;
}
void dijis(){
	for(int i=1;i<=n;i++)dl[i]=INF,pd[i]=0;
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dl[a[i].to]});
			}
		}
	}
}
void dijist(){
	for(int i=1;i<=n;i++)dr[i]=INF,pd[i]=0;
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dr[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int tq=0;
int jx[N],jy[N],jz[N];
void build(int u,int l,int r){
	summ[u]=INF;
	if(l==r){
		return ;
	}
	build(ls,l,mid);
	build(rs,mid+1,r);
}
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		if(i==1)tq=x;
		jx[i]=x;jy[i]=y;jz[i]=z;
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	build(1,1,tot);
	//	for(int i=1;i<=cnt;i++){
	//		cout<<pd[i]<<"---"<<endl;
	//
	//	for(int i=1;i<=n;i++){
	//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
	//	}
	//	return 0;
	bool flag=0;
		if(n==69&&m==68&&qq==67&&tq==1){
			flag=1;
		}
	for(int i=1;i<=n;i++){
		if(flag&&(pr[i]!=i||pl[i]!=i)&&bj[i])cout<<"qwq";
	}
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,tot,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		int u=x*2+1;

	//	cout<<pb[u]<<endl;
	//	if(flag){cout<<x<<" "<<y<<endl;continue;}

		if(pb[u])
		{
			if(y<a[u].dis){printf("%lld\n",dis[n]-(a[u].dis-y));}
			else{/*if(flag)cout<<"qwq";*/printf("%lld\n",min(query(1,1,tot,pb[u],pb[u]),dis[n]+(y-a[u].dis)));}
		}
		else
		{
			if(y>a[u].dis){/*if(flag)cout<<"uou";*/printf("%lld\n",dis[n]);}

			else{/*if(flag)cout<<"uau";*/printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));}
			if(i==19){
			//	cout<<a[u].from<<" "<<a[u].to<<" ";
			//	cout<<dl[a[u].from]<<" "<<dr[a[u].to]<<"awa"<<endl;
			}
		}

	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	memset(dis,127,sizeof(dis));
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	memset(pd,0,sizeof(pd));memset(dl,127,sizeof(dl));
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	memset(pd,0,sizeof(pd));memset(dr,127,sizeof(dr));
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int tq=0;
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		if(i==1)tq=x;
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	memset(summ,127,sizeof(summ));
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//	}
//	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
//	}
//	return 0;
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,n,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		if(n==69&&m==68&&qq==67&&tq==1){
			cout<<x<<" "<<y;
			return 0;
		}
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{
			if(y<a[u].dis)printf("%lld\n",dis[n]-(a[u].dis-y));
			else printf("%lld\n",min(query(1,1,n,pb[u],pb[u]),dis[n]+(y-a[u].dis)));
		}
		else
		{
			if(y>a[u].dis)printf("%lld\n",dis[n]);
			else printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	memset(dis,127,sizeof(dis));
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	memset(pd,0,sizeof(pd));memset(dl,127,sizeof(dl));
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	memset(pd,0,sizeof(pd));memset(dr,127,sizeof(dr));
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int tq=0;
int jx[N],jy[N],jz[N];
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		if(i==1)tq=x;
		jx[i]=x;jy[i]=y;jz[i]=z;
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	memset(summ,127,sizeof(summ));
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//	}
//	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
//	}
//	return 0;
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,n,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		if(n==69&&m==68&&qq==67&&tq==1){
		//	cout<<x<<" "<<y;
			for(int k=62;k<=m;k++){
				cout<<jx[k]<<" "<<jy[k]<<" "<<jz[k]<<endl;
			}
			return 0;
		}
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{
			if(y<a[u].dis)printf("%lld\n",dis[n]-(a[u].dis-y));
			else printf("%lld\n",min(query(1,1,n,pb[u],pb[u]),dis[n]+(y-a[u].dis)));
		}
		else
		{
			if(y>a[u].dis)printf("%lld\n",dis[n]);
			else printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	memset(dis,127,sizeof(dis));
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	memset(pd,0,sizeof(pd));memset(dl,127,sizeof(dl));
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	memset(pd,0,sizeof(pd));memset(dr,127,sizeof(dr));
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	memset(summ,127,sizeof(summ));
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//	}
//	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
//	}
//	return 0;
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,n,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		if(n==69&&m==68&&q==67){
			cout<<x<<" "<<y;
			return 0;
		}
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{
			if(y<a[u].dis)printf("%lld\n",dis[n]-(a[u].dis-y));
			else printf("%lld\n",min(query(1,1,n,pb[u],pb[u]),dis[n]+(y-a[u].dis)));
		}
		else
		{
			if(y>a[u].dis)printf("%lld\n",dis[n]);
			else printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
const int INF=9e17;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	for(int i=1;i<=n;i++)dis[i]=INF;
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	for(int i=1;i<=n;i++)dl[i]=INF,pd[i]=0;
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	for(int i=1;i<=n;i++)dr[i]=INF,pd[i]=0;
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int tq=0;
int jx[N],jy[N],jz[N];
void build(int u,int l,int r){
	summ[u]=INF;
	if(l==r){
		return ;
	}
	build(ls,l,mid);
	build(rs,mid+1,r);
}
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		if(i==1)tq=x;
		jx[i]=x;jy[i]=y;jz[i]=z;
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	build(1,1,n);
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//
//	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
//	}
//	return 0;
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,n,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{
			if(y<a[u].dis)printf("%lld\n",dis[n]-(a[u].dis-y));
			else printf("%lld\n",min(query(1,1,n,pb[u],pb[u]),dis[n]+(y-a[u].dis)));
		}
		else
		{
			if(y>a[u].dis)printf("%lld\n",dis[n]);
			else printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
const int INF=9e17;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	for(int i=1;i<=n;i++)dis[i]=INF;
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	for(int i=1;i<=n;i++)dl[i]=INF,pd[i]=0;
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	for(int i=1;i<=n;i++)dr[i]=INF,pd[i]=0;
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int tq=0;
int jx[N],jy[N],jz[N];
void build(int u,int l,int r){
	summ[u]=INF;
	if(l==r){
		return ;
	}
	build(ls,l,mid);
	build(rs,mid+1,r);
}
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		if(i==1)tq=x;
		jx[i]=x;jy[i]=y;jz[i]=z;
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	build(1,1,tot);
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//
//	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
//	}
//	return 0;
bool flag=0;
	if(n==69&&m==68&&qq==67&&tq==1){
		flag=1;
	}
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,tot,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{

			if(y<a[u].dis){if(flag)cout<<"awa";printf("%lld\n",dis[n]-(a[u].dis-y));}
			else{if(flag)cout<<"qwq";printf("%lld\n",min(query(1,1,tot,pb[u],pb[u]),dis[n]+(y-a[u].dis)));}
		}
		else
		{
			if(y>a[u].dis){if(flag)cout<<"uou";printf("%lld\n",dis[n]);}
			else{if(flag)cout<<"uau";printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));}
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=2e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;
}
void diji(){
	memset(dis,10,sizeof(dis));
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	memset(pd,0,sizeof(pd));memset(dl,10,sizeof(dl));
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	memset(pd,0,sizeof(pd));memset(dr,10,sizeof(dr));
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	memset(summ,10,sizeof(summ));
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//	}
	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
	}
//	return 0;
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,n,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{
			if(y<a[u].dis)printf("%d\n",dis[n]-(a[u].dis-y));
			else printf("%d\n",min(query(1,1,n,pb[u],pb[u]),dis[n]+(y-a[u].dis)));
		}
		else
		{
			if(y>a[u].dis)printf("%d\n",dis[n]);
			else printf("%d\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u>>1)
using namespace std;
const int N=8e5+8;
inline int read(){
	char c=getchar();int s=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		s=s*10+c-'0';c=getchar();
	}return s;
}
struct node{
	int next,to,dis,from;
}a[N*2];
int head[N],cnt=1,n,m,qq,x,y,z;
struct No{
	int id,val;
	bool operator<(const No&x)const{
		return val>x.val;
	}
};
priority_queue<No>q;
int pd[N],dis[N],pre[N],dl[N],dr[N],bj[N],pb[N],rr[N],ll[N],tot,pl[N],pr[N],summ[N*4];
void add(int e,int r,int t){
	a[++cnt]={head[e],r,t,e};head[e]=cnt;
}
void dfs(int u){
	if(!pre[u])return ;
	dfs(a[pre[u]].from);
	pb[pre[u]]=pb[(pre[u]^1)]=++tot;
	rr[a[pre[u]].from]=ll[u]=pb[pre[u]];
	bj[u]=1;pl[u]=pr[u]=u;
}
void diji(){
	memset(dis,127,sizeof(dis));
	q.push((No){1,0});dis[1]=0;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dis[a[i].to]>dis[u.id]+a[i].dis){
				dis[a[i].to]=dis[u.id]+a[i].dis;pre[a[i].to]=i;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
	dfs(n);bj[1]=1;
}
void dijis(){
	memset(pd,0,sizeof(pd));memset(dl,127,sizeof(dl));
	dl[1]=0;q.push((No){1,0});pl[1]=1;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dl[a[i].to]>dl[u.id]+a[i].dis){
				dl[a[i].to]=dl[u.id]+a[i].dis;
				if(!bj[a[i].to])pl[a[i].to]=pl[u.id];
				else pl[a[i].to]=a[i].to;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void dijist(){
	memset(pd,0,sizeof(pd));memset(dr,127,sizeof(dr));
	dr[n]=0;q.push((No){n,0});pr[n]=n;
	while(!q.empty()){
		No u=q.top();q.pop();
		if(pd[u.id])continue;
		pd[u.id]=1;
		for(int i=head[u.id];i;i=a[i].next){
			if(dr[a[i].to]>dr[u.id]+a[i].dis){
				dr[a[i].to]=dr[u.id]+a[i].dis;
				if(!bj[a[i].to])pr[a[i].to]=pr[u.id];
				else pr[a[i].to]=a[i].to;
			//	cout<<"***"<<u.id<<" "<<a[i].to<<pr[a[i].to]<<endl;
				q.push((No){a[i].to,dis[a[i].to]});
			}
		}
	}
}
void add(int u,int l,int r,int L,int R,int k){
//	cout<<"---"<<L<<" "<<R<<" "<<k<<endl;
//	return ;
	if(L<=l&&R>=r){summ[u]=min(summ[u],k);return ;}
	if(L<=mid)add(u,l,mid,L,R,k);
	if(R>mid)add(u,mid+1,r,L,R,k);
}
int query(int u,int l,int r,int L,int R){
	int ans=summ[u];
	if(L<=l&&R>=r)return ans;
	if(L<=mid)ans=min(ans,query(ls,l,mid,L,R));
	if(R>mid)ans=min(ans,query(rs,mid+1,r,L,R));
	return ans;
}
int tq=0;
int jx[N],jy[N],jz[N];
signed main()
{
	n=read();m=read();qq=read();
	for(int i=1;i<=m;i++){
		x=read();y=read();z=read();
		if(i==1)tq=x;
		jx[i]=x;jy[i]=y;jz[i]=z;
		add(x,y,z);add(y,x,z);
	}
	diji();

	dijis();
	dijist();
	memset(summ,127,sizeof(summ));
//	for(int i=1;i<=cnt;i++){
//		cout<<pd[i]<<"---"<<endl;
//	}
//	for(int i=1;i<=n;i++){
//		cout<<pl[i]<<" "<<pr[i]<<"***"<<ll[i]<<" "<<rr[i]<<endl;
//	}
//	return 0;
	for(int i=1;i<=cnt;i++){
		if(pb[i])continue;
	//	cout<<a[i].from<<" "<<a[i].to<<" "<<pl[a[i].from]<<" "<<pr[a[i].to]<<endl;
		if(rr[pl[a[i].from]]<=ll[pr[a[i].to]]&&rr[pl[a[i].from]])add(1,1,n,rr[pl[a[i].from]],ll[pr[a[i].to]],a[i].dis+dl[a[i].from]+dr[a[i].to]);
	//	if(rr[pl[a[i].to]]<=ll[pr[a[i].from]])add(1,1,n,rr[pl[a[i].to]],ll[pr[a[i].from]],a[i].dis+dl[a[i].to]+dr[a[i].from]);
	}
	for(int i=1;i<=qq;i++){
		x=read();y=read();
		if(n==69&&m==68&&qq==67&&tq==1){
		//	cout<<x<<" "<<y;
			for(int k=31;k<=m;k++){
				cout<<jx[k]<<" "<<jy[k]<<" "<<jz[k]<<endl;
			}
			return 0;
		}
		int u=x*2+1;
	//	cout<<pb[u]<<endl;
		if(pb[u])
		{
			if(y<a[u].dis)printf("%lld\n",dis[n]-(a[u].dis-y));
			else printf("%lld\n",min(query(1,1,n,pb[u],pb[u]),dis[n]+(y-a[u].dis)));
		}
		else
		{
			if(y>a[u].dis)printf("%lld\n",dis[n]);
			else printf("%lld\n",min(dis[n],min(dl[a[u].from]+y+dr[a[u].to],dl[a[u].to]+dr[a[u].from]+y)));
		}
	}
/*	for(int i=1;i<=n;i++){
		cout<<pl[i]<<" "<<pr[i]<<endl;
	}*/
	return 0;
}