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

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define inf INT_MAX
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>
#define mxn 2002

ll n,m,p,d,t;
vector < vector<parll> > G;
vector <ll> V(mxn,0),D(mxn,llinf);
priority_queue <parll> Q;

ll Dijkstra( ll root )
{
    D[root] = 0;
    Q.push({0,root});

    ll i,novi,w,rez=llinf;
    while ( Q.size() )
    {
        i = Q.top().second; Q.pop();
        if ( V[i] )
        {
            continue;
        }
        V[i] = 1;
        for ( ll j = 0; j < G[i].size(); j++ )
        {
            novi = G[i][j].first; w = G[i][j].second;
            if ( novi == root )
            {
                rez = min( rez, D[i]+w );
                continue;
            }
            if ( D[i]+w < D[novi] )
            {
                D[novi] = D[i]+w;
                Q.push({-D[novi],novi});
            }
        }
    }
    return (rez==llinf)?-1:rez;
}

int main()
{
    ios;

    cin >> n >> m;
    G.resize(n+1);
    for ( int i = 1; i <= m; i++ )
    {
        cin >> p >> d >> t;
        G[p].push_back({d,t});
    }

    for ( int i = 1; i <= n; i++ )
    {
        fill( V.begin(), V.end(), 0 ); fill( D.begin(), D.end(), llinf );
        cout << Dijkstra(i) << "\n";
    }

    return 0;
}

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

int main() {
  int N,M,Q;
  cin >>N;
  long long X[N+1],Y[N+1];
  //input
  for(int i=1;i<=N;i++){
    cin >> X[i] >> Y[i];
  }
  cin >> M;
  vector<pair<int,int>> op;
  int a,b;
  for(int i=1;i<=M;i++){
    cin >> a;
    if(a==3||a==4){
      cin >>b;
      op.push_back(make_pair(a,b));
    }
    else op.push_back(make_pair(a,0));
  }
  cin >> Q;
  vector<tuple<int,int,int>> q;
  for(int i=0;i<Q;i++){
    cin >> a >> b;
    q.push_back(make_tuple(a,b,i));
  }
  sort(q.begin(),q.end());
  long long ans1[Q],ans2[Q];
  int c;
  int cnt = 0;
  long long Xm[3],Ym[3];
  Xm[0] = -1000; Ym[0] = 1000;
  Xm[1] = 1000; Ym[1] = 1000;
  Xm[2] = -1000; Ym[2] = -1000;
  long long cX,cY;
  
  for(int i=0;i<Q;i++){
    tuple<int,int,int> qn = q.at(i);
    a = get<0>(qn);
    b = get<1>(qn);
    c = get<2>(qn);
    for(int j=cnt;j<a;j++){
      auto p = op.at(j);
      int n = p.first; long long m=p.second;
      for(int k=0;k<3;k++){
        if(n==1){
          long long z = Ym[k];
          Ym[k]=(-1)*Xm[k];
          Xm[k]=z;
        }
        else if(n==2){
          long long z = Ym[k];
          Ym[k]=Xm[k];
          Xm[k]=(-1)*z;
        }
        else if(n==3) Xm[k]=2*m-Xm[k];
        else if(n==4) Ym[k]=2*m-Ym[k];
       // cout << "change" << endl;
      }
    }
    cnt = a;
    if(Xm[0]==Xm[1]){
       cX = Xm[0] + (1000-Y[b])*((abs(Xm[2]-Xm[0]))/(Xm[2]-Xm[0]));
       cY = Ym[0] + (X[b]+1000)*((abs(Ym[1]-Ym[0]))/(Ym[1]-Ym[0]));
    }
    else{
       cX = Xm[0] + (X[b]+1000)*((abs(Xm[1]-Xm[0]))/(Xm[1]-Xm[0]));
       cY = Ym[0] + (1000-Y[b])*((abs(Ym[2]-Ym[0]))/(Ym[2]-Ym[0]));
     // cout << 1000-Y[b] << (Ym[2]-Xm[0]) << endl;
    }
    ans1[c] = cX; ans2[c] = cY; 
  }
  
      for(int i=0;i<Q;i++){
        cout << ans1[i] << " " << ans2[i] << endl;
      }
}
