#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
const int Maxk = 450;
vector<pair<int, int> > co[Maxn];
int p[Maxn];
vector<int> u;
vector<int> bl[Maxn];
set<int> bb[Maxn];
int id[Maxn];
int to[Maxn];
map<pair<int, int>, int> ans;
vector<int> big;
vector<int> cmp[Maxn * 5];
int table[Maxk][Maxn];
int findset(int x) { return p[x] = (p[x] == x) ? x : findset(p[x]); }
void Union(int x, int y) {
  x = findset(x);
  y = findset(y);
  if (x == y) return;
  p[x] = y;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    co[c].push_back(make_pair(a, b));
  }
  for (int i = 0; i < Maxn; i++) {
    p[i] = i;
  }
  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    u.clear();
    for (int j = 0; j < co[i].size(); j++) {
      u.push_back(co[i][j].first);
      u.push_back(co[i][j].second);
      Union(co[i][j].first, co[i][j].second);
    }
    sort(u.begin(), u.end());
    u.resize(unique(u.begin(), u.end()) - u.begin());
    int tot = 0;
    for (int j = 0; j < u.size(); j++) {
      if (!to[findset(u[j])]) to[findset(u[j])] = ++tot;
      cmp[to[findset(u[j])]].push_back(u[j]);
    }
    for (int j = 0; j < u.size(); j++) {
      bl[u[j]].push_back(to[p[u[j]]] + cnt);
    }
    cnt += tot;
    for (int j = 0; j < u.size(); j++) {
      to[p[u[j]]] = 0;
      p[u[j]] = u[j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < bl[i].size(); j++) {
      bb[i].insert(bl[i][j]);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (bb[a].size() > bb[b].size()) swap(a, b);
    if (ans.count(make_pair(a, b))) {
      printf("%d\n", ans[make_pair(a, b)]);
    } else {
      int res = 0;
      for (set<int>::iterator it = bb[a].begin(); it != bb[a].end(); it++) {
        if (bb[b].count(*it)) {
          res++;
        }
      }
      printf("%d\n", ans[make_pair(a, b)] = res);
    }
  }
  return 0;
}
