#include <bits/stdc++.h>
using namespace std;
int f[100100];
vector<pair<int, int> > E[100100];
vector<int> heavy;
map<pair<int, int>, int> ans;
int comp[290][100100];
vector<int> L[100100];
vector<int> feliz;
int foi[100100];
void dfs(int x, int y, int u, int cmp) {
  if (foi[x] == y) return;
  comp[u][x] = cmp;
  foi[x] = y;
  for (vector<int>::iterator it = L[x].begin(); it < L[x].end(); it++)
    dfs(*it, y, u, cmp);
}
void dfs2(int x, int y) {
  if (foi[x] == y) return;
  ;
  feliz.push_back(x);
  foi[x] = y;
  for (vector<int>::iterator it = L[x].begin(); it < L[x].end(); it++)
    dfs2(*it, 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), a--, b--, c--;
    f[c]++;
    E[c].push_back(pair<int, int>(a, b));
  }
  int sq = 350;
  int cur = 1;
  int conta = 0;
  for (int i = 0; i < m; i++) {
    ;
    if (f[i] == 0) continue;
    if (f[i] > sq) {
      ;
      heavy.push_back(i);
      for (int j = 0; j < n; j++) L[j].clear();
      for (int j = 0; j < f[i]; j++) {
        L[E[i][j].first].push_back(E[i][j].second);
        L[E[i][j].second].push_back(E[i][j].first);
      }
      int k = 0;
      cur++;
      for (int j = 0; j < n; j++)
        if (foi[j] != cur) {
          dfs(j, cur, conta, k);
          k++;
        }
      conta++;
      continue;
    };
    for (int j = 0; j < f[i]; j++) {
      L[E[i][j].first].clear();
      L[E[i][j].second].clear();
    }
    for (int j = 0; j < f[i]; j++) {
      L[E[i][j].first].push_back(E[i][j].second);
      L[E[i][j].second].push_back(E[i][j].first);
      ;
    }
    cur++;
    feliz.clear();
    for (int j = 0; j < f[i]; j++) {
      if (foi[E[i][j].first] != cur) {
        feliz.clear();
        ;
        dfs2(E[i][j].first, cur);
        for (int a = 0; a < feliz.size(); a++)
          for (int aa = a + 1; aa < feliz.size(); aa++) {
            ans[pair<int, int>(min(feliz[a], feliz[aa]),
                               max(feliz[a], feliz[aa]))]++;
            ;
          }
      }
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b), a--, b--;
    if (a > b) swap(a, b);
    int r = ans[pair<int, int>(a, b)];
    for (int i = 0; i < conta; i++)
      if (comp[i][a] == comp[i][b]) r++;
    printf("%d\n", r);
  }
}
