/*ㅤ∧_∧
 ( ・∀・)
 ( つ┳⊃
ε (_)へ⌒ヽフ
 (  ( ・ω・)
 ◎―◎   ⊃  ⊃
BePhuongSuperSuwi
From TK4 - CHT
ㅤㅤ/ ⌒\____
  /・   )  \
 /ノへ ノ    /|
ノ    \\ |/_/_/*/
#include<bits/stdc++.h>
#define task "main"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 2500 + 5;
int n, l[maxn], r[maxn], dist[maxn][maxn];
void bfs(int st) {
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            dist[i][j] = oo;
        }
    }
    queue <ii> q;
    dist[l[st]][r[st]] = 1;
    q.push({l[st], r[st]});
    while(!q.empty()) {
        int lt = q.front().fi, rt = q.front().se;
        q.pop();
        for (int i = lt; i <= rt; i++) {
            int nl = min(l[i], lt), rn = max(r[i], rt);
            if (dist[nl][rn] > dist[lt][rt] + 1) {
                dist[nl][rn] = dist[lt][rt] + 1;
                q.push({nl, rn});
            }
        }
    }
    if (dist[1][n] == oo) cout << "-1" << endl;
    else cout << dist[1][n] << endl;
}
main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen(task".inp","r")) {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> l[i] >> r[i];
    }
    int t = 1; cin >>t;
    while(t--) {
        int st; cin >> st;
        bfs(st);
    }
}
컴파일 시 표준 에러 (stderr) 메시지
passport.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   67 | main() {
      | ^~~~
passport.cpp: In function 'int main()':
passport.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
passport.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |