#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstdlib>
using namespace std;
typedef long long int lli;
vector<lli> comp, res;
vector<pair<lli, lli>> Q;
vector<vector<pair<lli, lli>>> graph;
void dfs(lli node, lli v, lli col)
{
comp[node] = v;
for(auto it: graph[node])
{
if(it.second != col && comp[it.first] == -1) dfs(it.first, v, col);
}
}
int main(void)
{
lli n, m, c, q, u, v, a, x, y;
scanf("%lld%lld%lld", &n, &m, &c);
graph.clear(); graph.resize(n);
for(lli i = 0;i < m;i++)
{
scanf("%lld%lld%lld", &u, &v, &a);
u--; v--;
assert(u < n); assert(v < n); assert(a <= c);
graph[u].push_back({v, a});
graph[v].push_back({u, a});
assert(u != v);
}
scanf("%lld", &q);
res.clear(); res.resize(q);
Q.clear(); Q.resize(q);
for(lli i = 0;i < q;i++)
{
scanf("%lld%lld", &x, &y);
x--; y--;
Q[i] = {x, y};
assert(x != y);
}
comp.clear(); comp.resize(n, -1);
int ctr = 0;
for(int i = 0;i < n;i++)
{
if(comp[i] == -1) dfs(i, ctr++, -1);
}
vector<lli> pika = comp;
for(lli i = 1;i <= c;i++)
{
//cout << i << ": ";
lli ctr = 0;
comp.clear(); comp.resize(n, -1);
for(lli j = 0;j < n;j++)
{
if(comp[j] == -1) dfs(j, ctr++, i);
}
for(lli j = 0;j < q;j++)
{
if(comp[Q[j].first] != comp[Q[j].second] || pika[Q[j].first] != pika[Q[j].second])
{
//cout << j << " ";
res[j]++;
}
}
//cout << "\n";
}
for(lli i = 0;i < q;i++) printf("%lld\n", res[i]);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y2Fzc2VydD4KI2luY2x1ZGUgPGNzdGRsaWI+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgbGxpOwoKdmVjdG9yPGxsaT4gY29tcCwgcmVzOw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