#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 800;
double eps = 1e-9;
int pa[maxn];
int fin(int x) {
  if (pa[x] == x) return x;
  return pa[x] = fin(pa[x]);
}
int n, m;
vector<pair<int, int> > vp[maxn];
int cols;
vector<int> qs[maxn];
vector<int> id[maxn];
int ans[maxn];
int checked[maxn];
int to[maxn];
map<pair<int, int>, int> mp;
int main() {
  cin >> n >> m;
  while (m--) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    cols = max(cols, c);
    vp[c].push_back(make_pair(x, y));
  }
  int Q;
  cin >> Q;
  int maxfuzadu = 0;
  for (int i = 1; i <= Q; i++) {
    int tx, ty;
    scanf("%d%d", &tx, &ty);
    if (tx > ty) swap(tx, ty);
    if (mp[make_pair(tx, ty)]) {
      to[i] = mp[make_pair(tx, ty)];
      continue;
    }
    mp[make_pair(tx, ty)] = i;
    to[i] = i;
    if (qs[tx].size() > qs[ty].size())
      qs[ty].push_back(tx), id[ty].push_back(i);
    else
      qs[tx].push_back(ty), id[tx].push_back(i);
  }
  for (int i = 1; i <= n; i++)
    pa[i] = i, maxfuzadu = max(maxfuzadu, (int)qs[i].size());
  assert(maxfuzadu <= 200);
  for (int i = 1; i <= cols; i++) {
    vector<int> involved;
    for (int j = 0; j < vp[i].size(); j++) {
      int x = vp[i][j].first, y = vp[i][j].second;
      if (checked[x] == 0) involved.push_back(x);
      if (checked[y] == 0) involved.push_back(y);
      checked[x] = checked[y] = 1;
      if (fin(x) == fin(y)) continue;
      pa[fin(x)] = fin(y);
    }
    for (int j = 0; j < involved.size(); j++) {
      int x = involved[j];
      for (int k = 0; k < qs[x].size(); k++)
        if (fin(x) == fin(qs[x][k])) ans[id[x][k]]++;
    }
    for (int j = 0; j < involved.size(); j++)
      pa[involved[j]] = involved[j], checked[involved[j]] = 0;
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[to[i]]);
  return 0;
}
