#include<iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e3 + 10;
int flag[N];
int id[N][N];
pair<int, int> inputs[N];
int n, m, k, s, t;
int d[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int cnt = 20;
void bfs(int u){
//cnt --;
//if(cnt == 0) exit(0);
//printf("u=%d\n", u);
flag[u] = 1;
queue<int> q;
q.push(u);
while(q.size()){
int u = q.front(); q.pop(); flag[u] = 1;
int x = inputs[u].first, y = inputs[u].second;
for(int i = 0; i < 4; ++ i){
int nx = x + d[i][0], ny = y + d[i][1];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && id[nx][ny] && flag[id[nx][ny]] == 0)
q.push(id[nx][ny]);
}
}
}
int main(){
//cout << "hello world";
scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
for(int i = 1; i <= k; ++ i){
int x, y; scanf("%d%d", &x, &y);
id[x][y] = i;
inputs[i] = {x, y};
}
//cout << 1;
bfs(s);
if(flag[t]) return puts("0"), 0; //没有特判开始边界
flag[s] = 1;
int T; cin >> T;
for(int tt = 1; tt <= T; tt ++){
int u, op; scanf("%d%d", &u, &op); op --;
int &x = inputs[u].first, &y = inputs[u].second;
int nx = x + d[op][0], ny = y + d[op][1];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !id[nx][ny])
{
id[x][y] = 0; id[nx][ny] = u;
x = nx; y = ny;
for(int i = 0; i < 4; ++ i){
nx = x + d[i][0], ny = y + d[i][1];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && id[nx][ny])
{
int a = id[nx][ny], b = id[x][y];
if(flag[a] == 1 && flag[b] == 1) continue;
if(flag[a] == 0 && flag[b] == 0) continue;
if(flag[a] == 0 && flag[b] == 1) bfs(a);
if(flag[a] == 1 && flag[b] == 0) bfs(b);
}
}
}
if(flag[t] == 1) return printf("%d\n", tt), 0;
//printf("flag: ");
//for(int i = 1; i <= k; ++ i) printf("%d ", flag[i]);
//puts(" ");
}
puts("-1");
return 0;
}
时间t等于0的时候要特判一下