#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int B = 555;
const int LG = 20;
int n, m, q;
vector <int> g[maxn];
int c[maxn];
vector <int> nList, hList;
int tin[maxn], tout[maxn], timeDFS, lg[4 * maxn];
int h[maxn], arr[maxn];
void dfs(int u,int p){
    nList.push_back(u); hList.push_back(h[u]);
    tin[u] = ++ timeDFS;
    arr[timeDFS] = u;
    for(int &v : g[u]){
        if(v==p) continue;
        h[v] = h[u] + 1;
        dfs(v,u);
        nList.push_back(u); hList.push_back(h[u]);
    }
    tout[u] = ++timeDFS;
}
int rmq[4 * maxn][LG + 2];
int merge(int i,int j){
    return (hList[i] < hList[j] ? i : j);
}
void init(){
    nList.push_back(0); hList.push_back(0);
    dfs(1,1);
    int m = nList.size()-1;
    for(int i = 1; i <= m; i++) rmq[i][0] = i;   
    for(int i = 2; i <= m; i++) lg[i] = lg[i >> 1] + 1;
    for(int j =1;(1<<j)<=m;j++) for(int i = 1; i + (1 << (j)) - 1 <= m; i++){
        rmq[i][j] =merge(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
    }
}
int LCA(int u,int v){
    if(tin[u] > tin[v]) swap(u, v);
    int k = lg[tin[v]-tin[u]+1];
    return nList[merge(rmq[tin[u]][k], rmq[tin[v]-(1<<k)+1][k])]; 
}
int dist(int u,int v){
    return h[u] + h[v] - 2 * h[LCA(u, v)];
}
namespace sub2{
    int ans[2];
    set <int> Col[2];
    struct Queries{
        int L, R, id;
        bool operator <(const Queries &v)const{
            if(L / B != v.L / B) return L < v.L;
            return ((L / B) & 1 ? R > v.R : R < v.R);
        }
    } Q[maxn];
    int res[maxn];
    int cnt[maxn];
    void add(int u, int c){
        cnt[tin[u]]++;
        if(cnt[tin[u]] >= 2){
            return;
        }
        auto it = Col[c].lower_bound(tin[u]);
        if(Col[c].empty()){
            Col[c].insert(tin[u]);
            return;
        }
        if(it == Col[c].begin()){
            int v2 = arr[*it];
            int vk = arr[*Col[c].rbegin()];
            ans[c]-= dist(v2, vk);
            ans[c]+= dist(u, vk);
            ans[c]+= dist(u, v2);
        } else if(it == Col[c].end()){
            int vk = arr[*prev(it)];
            int v1 = arr[*Col[c].begin()];
            ans[c]-= dist(v1, vk);
            ans[c]+= dist(u, vk);
            ans[c]+= dist(u, v1);
        } else{
            auto itR = it; auto itL = itR; itL--;
            int uL = arr[*itL], uR = arr[*itR];
            ans[c]-= (h[uL] + h[uR] - 2 * h[LCA(uL, uR)]);
            ans[c]+= (h[uL] + h[u] - 2 * h[LCA(u, uL)]);
            ans[c]+= (h[uR] + h[u] - 2 * h[LCA(u, uR)]);
        }
        Col[c].insert(tin[u]);
    }
    void del(int u, int c){
        cnt[tin[u]]--;
        if(cnt[tin[u]] > 0 || cnt[tin[u]] < 0){
            return;
        }
        auto it = Col[c].lower_bound(tin[u]);
        if(it == Col[c].begin()){
            int v2 = arr[*next(it)];
            int vk = arr[*Col[c].rbegin()];
            ans[c]-= (h[u] + h[v2] - 2 * h[LCA(u, v2)]);
            ans[c]-= (h[u] + h[vk] - 2 * h[LCA(u, vk)]);
            ans[c]+= (h[v2] + h[vk] - 2 * h[LCA(v2, vk)]);
        } else if(next(it) == Col[c].end()){
            int vk = arr[*prev(it)];
            int v1 = arr[*Col[c].begin()];
            ans[c]-= (h[u] + h[vk] - 2 * h[LCA(u, vk)]);
            ans[c]-= (h[u] + h[v1] - 2 * h[LCA(u, v1)]);
            ans[c]+= (h[v1] + h[vk] - 2 * h[LCA(v1, vk)]);
        } else{
            auto itR = it, itL = it;
            itR++; itL--;
            int uL = arr[*itL], uR = arr[*itR];
            ans[c]+= (h[uL] + h[uR] - 2 * h[LCA(uL, uR)]);
            ans[c]-= (h[uL] + h[u] - 2 * h[LCA(u, uL)]);
            ans[c]-= (h[uR] + h[u] - 2 * h[LCA(u, uR)]);
        }
        Col[c].erase(tin[u]);
    }
    int l = 1, r = 0;
    void MO(int id){
        while(l > Q[id].L){
            add(c[--l], 0);
        }
        while(r < Q[id].R){
            add(c[++r], 0);
        }
        while(l < Q[id].L){
            del(c[l++], 0);
        }
        while(r > Q[id].R){
            del(c[r--], 0);
        }
        res[Q[id].id] = ans[0] / 2 + 1;
    }
    void solve(){   
        for(int i = 1; i <= q; i++){
            int l, r; cin >> l >> r;
            Q[i] = {l, r, i};
        }
        sort(Q + 1, Q + 1 + q);
        for(int i = 1; i <= q; i++){
            MO(i);
        }
        for(int i = 1; i <= q; i++){
            cout << res[i] << "\n";
        }
    }
}
int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0); 
    #define koa "joi23_tourism"
    if(fopen(koa".inp", "r")){
        freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
    }
    cin >> n >> m >> q;
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        g[u].push_back(v); g[v].push_back(u);
    }
    for(int i = 1; i <= m; i++){
        cin >> c[i];
    }
    init();
    sub2::solve();
    return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
tourism.cpp: In function 'int32_t main()':
tourism.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |         freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:160:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |         freopen(koa".inp", "r", stdin); freopen(koa".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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |