#include using namespace std; using pii = pair; const int MAXN = 200001, MAXV = 1000, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1}; int n, m, dist[MAXV][MAXV], ans = int(1e9); bool vis[MAXV][MAXV]; int main() { //ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; queue que; for(int i = 1, x, y; i <= n; ++i) { cin >> x >> y; vis[x][y] = 1; dist[x][y] = 0; que.push({x, y}); } for(; !que.empty(); ) { auto [x, y] = que.front(); que.pop(); for(int d : {0, 1, 2, 3}) { int nx = x + dx[d], ny = y + dy[d]; if(nx >= 0 && nx < 1000 && ny >= 0 && ny < 1000 && !vis[nx][ny]) { vis[nx][ny] = 1; dist[nx][ny] = dist[x][y] + 1; que.push({nx, ny}); } } } cin >> m; for(int i = 1, x, y; i <= m; ++i) { cin >> x >> y; ans = min(ans, dist[x][y]); } cout << ans; return 0; }