#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int l, n, m, q, i, x, y, c;
vector<pair<int, int> > e[MAXN];
vector<int> act[MAXN], parents[MAXN], sizes[MAXN], col[MAXN];
vector<pair<pair<int, int>, int> > associations_bucket[5000000],
    cache_bucket[5000000];
inline int rint() {
  int x = 0;
  char c;
  do {
    c = getchar();
  } while (c <= 32);
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x;
}
inline void push_association(int col, int key, int association) {
  long long hashed_id = (231178ll * col + 38ll * key) % 5000000;
  associations_bucket[hashed_id].push_back(
      make_pair(make_pair(col, key), association));
}
inline int request_association(int col, int key) {
  long long hashed_id = (231178ll * col + 38ll * key) % 5000000;
  for (__typeof((associations_bucket[hashed_id]).begin()) entry =
           (associations_bucket[hashed_id]).begin();
       entry != (associations_bucket[hashed_id]).end(); entry++) {
    if (entry->first == make_pair(col, key)) return entry->second;
  }
  return -1;
}
int findset(int col, int e) {
  if (e == parents[col][e]) return e;
  parents[col][e] = findset(col, parents[col][e]);
  return parents[col][e];
}
void merge(int col, int x, int y) {
  x = findset(col, x);
  y = findset(col, y);
  if (x != y) {
    if (sizes[col][x] < sizes[col][y]) swap(x, y);
    parents[col][y] = x;
    sizes[col][x] += sizes[col][y];
  }
}
inline void push_cache(int x, int y, int res) {
  long long hashed_id = (231178ll * x + 38ll * y) % 5000000;
  cache_bucket[hashed_id].push_back(make_pair(make_pair(x, y), res));
}
inline int request_cache(int x, int y) {
  long long hashed_id = (231178ll * x + 38ll * y) % 5000000;
  for (__typeof((cache_bucket[hashed_id]).begin()) entry =
           (cache_bucket[hashed_id]).begin();
       entry != (cache_bucket[hashed_id]).end(); entry++)
    if (entry->first == make_pair(x, y)) return entry->second;
  return -1;
}
int main() {
  n = rint(), m = rint();
  for (int i = (int)(1); i <= (int)(m); i++) {
    x = rint(), y = rint(), c = rint();
    col[x].push_back(c);
    col[y].push_back(c);
    e[c].push_back(make_pair(x, y));
    act[c].push_back(x);
    act[c].push_back(y);
  }
  for (int i = (int)(1); i <= (int)(n); i++) {
    if (col[i].empty()) continue;
    sort((col[i]).begin(), (col[i]).end());
    col[i].resize(unique((col[i]).begin(), (col[i]).end()) - col[i].begin());
  }
  for (int i = (int)(1); i <= (int)(100000); i++) {
    if (e[i].empty()) continue;
    sort((act[i]).begin(), (act[i]).end());
    act[i].resize(unique((act[i]).begin(), (act[i]).end()) - act[i].begin());
    for (int j = (int)(0); j <= (int)(((int)(act[i]).size()) - 1); j++) {
      push_association(i, act[i][j], j);
      parents[i].push_back(j);
      sizes[i].push_back(1);
    }
    for (__typeof((e[i]).begin()) j = (e[i]).begin(); j != (e[i]).end(); j++) {
      merge(i, request_association(i, j->first),
            request_association(i, j->second));
    }
  }
  q = rint();
  for (int i = (int)(1); i <= (int)(q); i++) {
    x = rint(), y = rint();
    if (((int)(col[x]).size()) > ((int)(col[y]).size())) swap(x, y);
    l = request_cache(x, y);
    if (l != -1)
      printf("%d\n", l);
    else {
      l = 0;
      for (__typeof((col[x]).begin()) j = (col[x]).begin(); j != (col[x]).end();
           j++) {
        int X = request_association(*j, x);
        int Y = request_association(*j, y);
        if (X != -1 && Y != -1) l += findset(*j, X) == findset(*j, Y);
      }
      push_cache(x, y, l);
      printf("%d\n", l);
    }
  }
  return 0;
}
